implement css: false in main compiler (#153)

pull/154/head
Rich Harris 8 years ago
parent 2ee46b47e1
commit 85368b929a

@ -392,7 +392,7 @@ export default function generate ( parsed, source, options, names ) {
topLevelStatements.push( `[✂${parsed.js.content.start}-${parsed.js.content.end}✂]` ); topLevelStatements.push( `[✂${parsed.js.content.start}-${parsed.js.content.end}✂]` );
} }
if ( parsed.css ) { if ( parsed.css && options.css !== false ) {
topLevelStatements.push( deindent` topLevelStatements.push( deindent`
let addedCss = false; let addedCss = false;
function addCss () { function addCss () {
@ -411,7 +411,7 @@ export default function generate ( parsed, source, options, names ) {
const initStatements = []; const initStatements = [];
if ( parsed.css ) { if ( parsed.css && options.css !== false ) {
initStatements.push( `if ( !addedCss ) addCss();` ); initStatements.push( `if ( !addedCss ) addCss();` );
} }

@ -8,7 +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 ( source, { filename }) {
const parsed = parse( source, {} ); const parsed = parse( source, {} );
validate( parsed, source, {} ); validate( parsed, source, {} );

@ -2,6 +2,6 @@ import * as fs from 'fs';
import compile from './compile.js'; import compile from './compile.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 });
return module._compile( code, filename ); return module._compile( code, filename );
}; };

@ -0,0 +1,7 @@
<p>test</p>
<style>
p {
color: red;
}
</style>

@ -0,0 +1,12 @@
export default {
compileOptions: {
css: false
},
test ( assert, component, target, window ) {
const [ control, test ] = target.querySelectorAll( 'p' );
assert.equal( window.getComputedStyle( control ).color, '' );
assert.equal( window.getComputedStyle( test ).color, '' );
}
};

@ -0,0 +1,10 @@
<p>control</p>
<Widget/>
<script>
import Widget from './Widget.html';
export default {
components: { Widget }
};
</script>

@ -23,9 +23,11 @@ const svelte = process.env.COVERAGE ?
const cache = {}; const cache = {};
let showCompiledCode = false; let showCompiledCode = false;
let compileOptions = null;
require.extensions[ '.html' ] = function ( module, filename ) { require.extensions[ '.html' ] = function ( module, filename ) {
const code = cache[ filename ] || ( cache[ filename ] = svelte.compile( fs.readFileSync( filename, 'utf-8' ) ).code ); const options = Object.assign({ filename }, compileOptions );
const code = cache[ filename ] || ( cache[ filename ] = svelte.compile( fs.readFileSync( filename, 'utf-8' ), options ).code );
if ( showCompiledCode ) console.log( addLineNumbers( code ) ); // eslint-disable-line no-console if ( showCompiledCode ) console.log( addLineNumbers( code ) ); // eslint-disable-line no-console
return module._compile( code, filename ); return module._compile( code, filename );
@ -250,6 +252,7 @@ describe( 'svelte', () => {
let compiled; let compiled;
showCompiledCode = config.show; showCompiledCode = config.show;
compileOptions = config.compileOptions || {};
try { try {
const source = fs.readFileSync( `test/generator/${dir}/main.html`, 'utf-8' ); const source = fs.readFileSync( `test/generator/${dir}/main.html`, 'utf-8' );

Loading…
Cancel
Save