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()
],
external: [ 'magic-string' ],
globals: {
'magic-string': 'MagicString'
},
sourceMap: true
};

@ -12,7 +12,7 @@ export default {
nodeResolve({ jsnext: true, module: true }),
commonjs()
],
external: [ path.resolve( 'src/index.js' ), 'magic-string' ],
external: [ path.resolve( 'src/index.js' ), 'fs', 'magic-string' ],
paths: {
[ 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 );
if ( intro ) addString( intro );
// a filename is necessary for sourcemap generation
const filename = options.filename || 'SvelteComponent.html';
const { filename } = options;
parts.forEach( str => {
const chunk = str.replace( pattern, '' );

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

@ -1,7 +1,10 @@
import * as fs from 'fs';
import compile from './compile.js';
import { compile } from '../index.js';
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 );
};

Loading…
Cancel
Save