Skip to content

Commit 1308907

Browse files
committedMar 28, 2017
include css in compiler output (#409)
1 parent b9d3c23 commit 1308907

File tree

7 files changed

+45
-8
lines changed

7 files changed

+45
-8
lines changed
 

‎.gitignore

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,4 @@ coverage
1111
coverage.lcov
1212
test/sourcemaps/samples/*/output.js
1313
test/sourcemaps/samples/*/output.js.map
14-
_actual.json
14+
_actual.*

‎src/generators/Generator.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import globalWhitelist from '../utils/globalWhitelist.js';
66
import reservedNames from '../utils/reservedNames.js';
77
import getIntro from './shared/utils/getIntro.js';
88
import getOutro from './shared/utils/getOutro.js';
9+
import processCss from './shared/processCss.js';
910
import annotateWithScopes from './annotateWithScopes.js';
1011

1112
export default class Generator {
@@ -30,6 +31,7 @@ export default class Generator {
3031
this.elementDepth = 0;
3132

3233
this.code = new MagicString( source );
34+
this.css = parsed.css ? processCss( parsed, this.code ) : null;
3335
this.cssId = parsed.css ? `svelte-${parsed.hash}` : '';
3436
this.usesRefs = false;
3537

@@ -233,7 +235,8 @@ export default class Generator {
233235

234236
return {
235237
code: compiled.toString(),
236-
map: compiled.generateMap({ includeContent: true, file: options.outputFilename })
238+
map: compiled.generateMap({ includeContent: true, file: options.outputFilename }),
239+
css: this.css
237240
};
238241
}
239242

‎src/generators/dom/index.js

+2-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import deindent from '../../utils/deindent.js';
22
import getBuilders from './utils/getBuilders.js';
33
import CodeBuilder from '../../utils/CodeBuilder.js';
44
import namespaces from '../../utils/namespaces.js';
5-
import processCss from '../shared/processCss.js';
65
import removeObjectKey from '../../utils/removeObjectKey.js';
76
import visitors from './visitors/index.js';
87
import Generator from '../Generator.js';
@@ -275,12 +274,12 @@ export default function dom ( parsed, source, options ) {
275274
builders.main.addBlock( `[✂${parsed.js.content.start}-${parsed.js.content.end}✂]` );
276275
}
277276

278-
if ( parsed.css && options.css !== false ) {
277+
if ( generator.css && options.css !== false ) {
279278
builders.main.addBlock( deindent`
280279
var ${generator.alias( 'addedCss' )} = false;
281280
function ${generator.alias( 'addCss' )} () {
282281
var style = ${generator.helper( 'createElement' )}( 'style' );
283-
style.textContent = ${JSON.stringify( processCss( parsed, generator.code ) )};
282+
style.textContent = ${JSON.stringify( generator.css )};
284283
${generator.helper( 'appendNode' )}( style, document.head );
285284
286285
${generator.alias( 'addedCss' )} = true;

‎src/generators/server-side-rendering/index.js

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import deindent from '../../utils/deindent.js';
22
import CodeBuilder from '../../utils/CodeBuilder.js';
33
import flattenReference from '../../utils/flattenReference.js';
4-
import processCss from '../shared/processCss.js';
54
import visitors from './visitors/index.js';
65
import Generator from '../Generator.js';
76

@@ -93,11 +92,11 @@ export default function ssr ( parsed, source, options ) {
9392
`var components = [];`
9493
);
9594

96-
if ( parsed.css ) {
95+
if ( generator.css ) {
9796
builders.renderCss.addBlock( deindent`
9897
components.push({
9998
filename: ${name}.filename,
100-
css: ${JSON.stringify( processCss( parsed, generator.code ) )},
99+
css: ${JSON.stringify( generator.css )},
101100
map: null // TODO
102101
});
103102
` );

‎test/css/index.js

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import assert from 'assert';
2+
import * as fs from 'fs';
3+
import { svelte, exists } from '../helpers.js';
4+
5+
describe.only( 'css', () => {
6+
fs.readdirSync( 'test/css/samples' ).forEach( dir => {
7+
if ( dir[0] === '.' ) return;
8+
9+
const solo = exists( `test/css/samples/${dir}/solo` );
10+
11+
if ( solo && process.env.CI ) {
12+
throw new Error( 'Forgot to remove `solo: true` from test' );
13+
}
14+
15+
( solo ? it.only : it )( dir, () => {
16+
const input = fs.readFileSync( `test/css/samples/${dir}/input.html`, 'utf-8' ).replace( /\s+$/, '' );
17+
18+
const actual = svelte.compile( input ).css;
19+
fs.writeFileSync( `test/css/samples/${dir}/_actual.css`, actual );
20+
const expected = fs.readFileSync( `test/css/samples/${dir}/expected.css`, 'utf-8' );
21+
22+
assert.equal( actual.trim(), expected.trim() );
23+
});
24+
});
25+
});

‎test/css/samples/basic/expected.css

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
2+
div[svelte-281576708], [svelte-281576708] div {
3+
color: red;
4+
}

‎test/css/samples/basic/input.html

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<div>red</div>
2+
3+
<style>
4+
div {
5+
color: red;
6+
}
7+
</style>

0 commit comments

Comments
 (0)
Please sign in to comment.