tidy up, add sourcemaps to add_css function

pull/698/head
Rich Harris 8 years ago
parent 45039e9612
commit f2db3ea828

@ -80,7 +80,6 @@ export default class Generator {
// TODO this is legacy — just to get the tests to pass during the transition // TODO this is legacy — just to get the tests to pass during the transition
this.css = this.stylesheet.render(options.cssOutputFilename).css; this.css = this.stylesheet.render(options.cssOutputFilename).css;
this.cssId = `svelte-${parsed.hash}`;
// allow compiler to deconflict user's `import { get } from 'whatever'` and // allow compiler to deconflict user's `import { get } from 'whatever'` and
// Svelte's builtin `import { get, ... } from 'svelte/shared.ts'`; // Svelte's builtin `import { get, ... } from 'svelte/shared.ts'`;

@ -135,12 +135,18 @@ export default function dom(
builder.addBlock(`[✂${parsed.js.content.start}-${parsed.js.content.end}✂]`); builder.addBlock(`[✂${parsed.js.content.start}-${parsed.js.content.end}✂]`);
} }
if (generator.css && options.css !== false) { if (generator.stylesheet.hasStyles && options.css !== false) {
const { css, cssMap } = generator.stylesheet.render(options.filename);
const textContent = options.dev ?
`${css}\n/*# sourceMappingURL=${cssMap.toUrl()} */` :
css;
builder.addBlock(deindent` builder.addBlock(deindent`
function @add_css () { function @add_css () {
var style = @createElement( 'style' ); var style = @createElement( 'style' );
style.id = '${generator.cssId}-style'; style.id = '${generator.stylesheet.id}-style';
style.textContent = ${stringify(generator.css)}; style.textContent = ${JSON.stringify(textContent)};
@appendNode( style, document.head ); @appendNode( style, document.head );
} }
`); `);
@ -199,9 +205,9 @@ export default function dom(
this._yield = options._yield; this._yield = options._yield;
this._torndown = false; this._torndown = false;
${generator.css && ${generator.stylesheet.hasStyles &&
options.css !== false && options.css !== false &&
`if ( !document.getElementById( '${generator.cssId}-style' ) ) @add_css();`} `if ( !document.getElementById( '${generator.stylesheet.id}-style' ) ) @add_css();`}
${(generator.hasComponents || generator.hasIntroTransitions) && ${(generator.hasComponents || generator.hasIntroTransitions) &&
`this._oncreate = [];`} `this._oncreate = [];`}
${generator.hasComplexBindings && `this._bindings = [];`} ${generator.hasComplexBindings && `this._bindings = [];`}

@ -84,7 +84,7 @@ export default function visitElement(
// TODO add a helper for this, rather than repeating it // TODO add a helper for this, rather than repeating it
if (node._needsCssAttribute) { if (node._needsCssAttribute) {
block.builders.hydrate.addLine( block.builders.hydrate.addLine(
`@setAttribute( ${name}, '${generator.cssId}', '' );` `@setAttribute( ${name}, '${generator.stylesheet.id}', '' );`
); );
} }

@ -86,6 +86,8 @@ export default function ssr(
visit(generator, mainBlock, node); visit(generator, mainBlock, node);
}); });
const { css, cssMap } = generator.stylesheet.render(options.filename);
const result = deindent` const result = deindent`
${hasJs && `[✂${parsed.js.content.start}-${parsed.js.content.end}✂]`} ${hasJs && `[✂${parsed.js.content.start}-${parsed.js.content.end}✂]`}
@ -127,12 +129,12 @@ export default function ssr(
${name}.renderCss = function () { ${name}.renderCss = function () {
var components = []; var components = [];
${generator.css && ${generator.stylesheet.hasStyles &&
deindent` deindent`
components.push({ components.push({
filename: ${name}.filename, filename: ${name}.filename,
css: ${JSON.stringify(generator.css)}, css: ${JSON.stringify(css)},
map: null // TODO map: ${JSON.stringify(cssMap)}
}); });
`} `}

@ -57,7 +57,7 @@ export default function visitElement(
}); });
if (node._needsCssAttribute) { if (node._needsCssAttribute) {
openingTag += ` ${generator.cssId}`; openingTag += ` ${generator.stylesheet.id}`;
} }
openingTag += '>'; openingTag += '>';

Loading…
Cancel
Save