add a compiler option to output SSR code

pull/159/head
Arpad Borsos 8 years ago
parent 07ce8236a4
commit d21336bb79
No known key found for this signature in database
GPG Key ID: 908EDF65263368B4

@ -12,5 +12,8 @@ export default {
commonjs() commonjs()
], ],
external: [ 'magic-string' ], external: [ 'magic-string' ],
globals: {
'magic-string': 'MagicString'
},
sourceMap: true sourceMap: true
}; };

@ -12,7 +12,7 @@ export default {
nodeResolve({ jsnext: true, module: true }), nodeResolve({ jsnext: true, module: true }),
commonjs() commonjs()
], ],
external: [ path.resolve( 'src/index.js' ), 'magic-string' ], external: [ path.resolve( 'src/index.js' ), 'fs', 'magic-string' ],
paths: { paths: {
[ path.resolve( 'src/index.js' ) ]: '../compiler/svelte.js' [ path.resolve( 'src/index.js' ) ]: '../compiler/svelte.js'
}, },

@ -588,8 +588,7 @@ export default function generate ( parsed, source, options, names ) {
const intro = getIntro( format, options, imports ); const intro = getIntro( format, options, imports );
if ( intro ) addString( intro ); if ( intro ) addString( intro );
// a filename is necessary for sourcemap generation const { filename } = options;
const filename = options.filename || 'SvelteComponent.html';
parts.forEach( str => { parts.forEach( str => {
const chunk = str.replace( pattern, '' ); const chunk = str.replace( pattern, '' );

@ -1,23 +1,36 @@
import parse from './parse/index.js'; import parse from './parse/index.js';
import validate from './validate/index.js'; import validate from './validate/index.js';
import generate from './generate/index.js'; import generate from './generate/index.js';
import generateSSR from './server-side-rendering/compile.js';
export function compile ( source, options = {} ) { function normalizeOptions ( options ) {
const parsed = parse( source, options ); return Object.assign( {
generate: 'dom',
// a filename is necessary for sourcemap generation
filename: 'SvelteComponent.html',
if ( !options.onwarn ) { onwarn: warning => {
options.onwarn = warning => {
if ( warning.loc ) { if ( warning.loc ) {
console.warn( `(${warning.loc.line}:${warning.loc.column}) ${warning.message}` ); // eslint-disable-line no-console console.warn( `(${warning.loc.line}:${warning.loc.column}) ${warning.message}` ); // eslint-disable-line no-console
} else { } else {
console.warn( warning.message ); // eslint-disable-line no-console console.warn( warning.message ); // eslint-disable-line no-console
} }
};
} }
}, options );
}
export function compile ( source, _options ) {
const options = normalizeOptions( _options );
const parsed = parse( source, options );
const { names } = validate( parsed, source, options ); const { names } = validate( parsed, source, options );
return generate( parsed, source, options, names ); const compiler = options.generate === 'ssr'
? generateSSR
: generate;
return compiler( parsed, source, options, names );
} }
export { parse, validate }; export { parse, validate };

@ -8,10 +8,7 @@ import processCss from '../generate/css/process.js';
const voidElementNames = /^(?:area|base|br|col|command|doctype|embed|hr|img|input|keygen|link|meta|param|source|track|wbr)$/i; const voidElementNames = /^(?:area|base|br|col|command|doctype|embed|hr|img|input|keygen|link|meta|param|source|track|wbr)$/i;
export default function compile ( source, { filename }) { export default function compile ( parsed, source, { filename }) {
const parsed = parse( source, {} );
validate( parsed, source, {} );
const code = new MagicString( source ); const code = new MagicString( source );
const templateProperties = {}; const templateProperties = {};

@ -1,7 +1,10 @@
import * as fs from 'fs'; import * as fs from 'fs';
import compile from './compile.js'; import { compile } from '../index.js';
require.extensions[ '.html' ] = function ( module, filename ) { require.extensions[ '.html' ] = function ( module, filename ) {
const { code } = compile( fs.readFileSync( filename, 'utf-8' ), { filename }); const { code } = compile( fs.readFileSync( filename, 'utf-8' ), {
filename,
generate: 'ssr'
});
return module._compile( code, filename ); return module._compile( code, filename );
}; };

Loading…
Cancel
Save