Skip to content

Commit 00877e8

Browse files
committed
output { js, css, ast } from svelte.compile - fixes sveltejs#795
1 parent ae25641 commit 00877e8

File tree

18 files changed

+58
-19
lines changed

18 files changed

+58
-19
lines changed

src/css/Stylesheet.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -375,7 +375,7 @@ export default class Stylesheet {
375375

376376
render(cssOutputFilename: string, shouldTransformSelectors: boolean) {
377377
if (!this.hasStyles) {
378-
return { css: null, cssMap: null };
378+
return { code: null, map: null };
379379
}
380380

381381
const code = new MagicString(this.source);
@@ -405,8 +405,8 @@ export default class Stylesheet {
405405
code.remove(c, this.source.length);
406406

407407
return {
408-
css: code.toString(),
409-
cssMap: code.generateMap({
408+
code: code.toString(),
409+
map: code.generateMap({
410410
includeContent: true,
411411
source: this.filename,
412412
file: cssOutputFilename

src/generators/Generator.ts

+27-6
Original file line numberDiff line numberDiff line change
@@ -347,19 +347,40 @@ export default class Generator {
347347

348348
addString(finalChunk);
349349

350-
const { css, cssMap } = this.customElement ?
351-
{ css: null, cssMap: null } :
350+
const css = this.customElement ?
351+
{ code: null, map: null } :
352352
this.stylesheet.render(options.cssOutputFilename, true);
353353

354-
return {
355-
ast: this.ast,
354+
const js = {
356355
code: compiled.toString(),
357356
map: compiled.generateMap({
358357
includeContent: true,
359358
file: options.outputFilename,
360-
}),
359+
})
360+
};
361+
362+
const stringMethods = Object.getOwnPropertyDescriptors(String.prototype);
363+
Object.entries(stringMethods).forEach(([name, descriptor]) => {
364+
if (typeof descriptor.value === 'function') {
365+
Object.defineProperty(css, name, {
366+
value: (...args) => {
367+
return css.code === null
368+
? null
369+
: css.code[name].call(css.code, ...args);
370+
}
371+
});
372+
}
373+
});
374+
375+
return {
376+
ast: this.ast,
377+
js,
361378
css,
362-
cssMap
379+
380+
// TODO deprecate
381+
code: js.code,
382+
map: js.map,
383+
cssMap: css.map
363384
};
364385
}
365386

src/generators/dom/index.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -135,10 +135,10 @@ export default function dom(
135135
builder.addBlock(generator.javascript);
136136
}
137137

138-
const { css, cssMap } = generator.stylesheet.render(options.filename, !generator.customElement);
138+
const css = generator.stylesheet.render(options.filename, !generator.customElement);
139139
const styles = generator.stylesheet.hasStyles && stringify(options.dev ?
140-
`${css}\n/*# sourceMappingURL=${cssMap.toUrl()} */` :
141-
css, { onlyEscapeAtSymbol: true });
140+
`${css.code}\n/*# sourceMappingURL=${css.map.toUrl()} */` :
141+
css.code, { onlyEscapeAtSymbol: true });
142142

143143
if (styles && generator.options.css !== false && !generator.customElement) {
144144
builder.addBlock(deindent`
@@ -234,7 +234,7 @@ export default function dom(
234234
${generator.customElement ?
235235
deindent`
236236
this.attachShadow({ mode: 'open' });
237-
${css && `this.shadowRoot.innerHTML = \`<style>${escape(css, { onlyEscapeAtSymbol: true }).replace(/\\/g, '\\\\')}${options.dev ? `\n/*# sourceMappingURL=${cssMap.toUrl()} */` : ''}</style>\`;`}
237+
${css.code && `this.shadowRoot.innerHTML = \`<style>${escape(css.code, { onlyEscapeAtSymbol: true }).replace(/\\/g, '\\\\')}${options.dev ? `\n/*# sourceMappingURL=${css.map.toUrl()} */` : ''}</style>\`;`}
238238
` :
239239
(generator.stylesheet.hasStyles && options.css !== false &&
240240
`if (!document.getElementById("${generator.stylesheet.id}-style")) @add_css();`)

src/generators/server-side-rendering/index.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,8 @@ export default function ssr(
6565
visit(generator, mainBlock, node);
6666
});
6767

68-
const { css, cssMap } = generator.customElement ?
69-
{ css: null, cssMap: null } :
68+
const css = generator.customElement ?
69+
{ code: null, map: null } :
7070
generator.stylesheet.render(options.filename, true);
7171

7272
// generate initial state object
@@ -155,8 +155,8 @@ export default function ssr(
155155
};
156156
157157
${name}.css = {
158-
code: ${css ? stringify(css) : `''`},
159-
map: ${cssMap ? stringify(cssMap.toString()) : 'null'}
158+
code: ${css.code ? stringify(css.code) : `''`},
159+
map: ${css.map ? stringify(css.map.toString()) : 'null'}
160160
};
161161
162162
var warned = false;

test/css/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ describe('css', () => {
8181
checkCodeIsValid(dom.code);
8282
checkCodeIsValid(ssr.code);
8383

84-
assert.equal(dom.css, ssr.css);
84+
assert.equal(dom.css.toString(), ssr.css.toString());
8585

8686
assert.deepEqual(
8787
domWarnings.map(normalizeWarning),

test/sourcemaps/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ describe("sourcemaps", () => {
6767
const locateInGenerated = getLocator(_code);
6868

6969
const smcCss = cssMap && new SourceMapConsumer(cssMap);
70-
const locateInGeneratedCss = getLocator(css || '');
70+
const locateInGeneratedCss = getLocator(css.code || '');
7171

7272
test({ assert, code: _code, map, smc, smcCss, locateInSource, locateInGenerated, locateInGeneratedCss });
7373
});

test/sourcemaps/samples/basic/output.css

+2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/sourcemaps/samples/basic/output.css.map

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/sourcemaps/samples/binding-shorthand/output.css

+2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/sourcemaps/samples/binding-shorthand/output.css.map

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/sourcemaps/samples/binding/output.css

+2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/sourcemaps/samples/binding/output.css.map

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/sourcemaps/samples/each-block/output.css

+2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/sourcemaps/samples/each-block/output.css.map

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/sourcemaps/samples/script/output.css

+2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/sourcemaps/samples/script/output.css.map

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/sourcemaps/samples/static-no-script/output.css

+2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/sourcemaps/samples/static-no-script/output.css.map

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)