Skip to content

Commit b2f371a

Browse files
committed
move vars out of stats
1 parent fd39e95 commit b2f371a

File tree

37 files changed

+198
-155
lines changed

37 files changed

+198
-155
lines changed

src/Stats.ts

Lines changed: 2 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import Component from './compile/Component';
2-
31
const now = (typeof process !== 'undefined' && process.hrtime)
42
? () => {
53
const t = process.hrtime();
@@ -63,41 +61,13 @@ export default class Stats {
6361
this.currentChildren = this.currentTiming ? this.currentTiming.children : this.timings;
6462
}
6563

66-
render(component: Component) {
64+
render() {
6765
const timings = Object.assign({
6866
total: now() - this.startTime
6967
}, collapseTimings(this.timings));
7068

71-
// TODO would be good to have this info even
72-
// if options.generate is false
73-
const imports = component && component.imports.map(node => {
74-
return {
75-
source: node.source.value,
76-
specifiers: node.specifiers.map(specifier => {
77-
return {
78-
name: (
79-
specifier.type === 'ImportDefaultSpecifier' ? 'default' :
80-
specifier.type === 'ImportNamespaceSpecifier' ? '*' :
81-
specifier.imported.name
82-
),
83-
as: specifier.local.name
84-
};
85-
})
86-
}
87-
});
88-
8969
return {
90-
timings,
91-
vars: component.vars.filter(variable => !variable.global && !variable.implicit && !variable.internal).map(variable => ({
92-
name: variable.name,
93-
export_name: variable.export_name || null,
94-
injected: variable.injected || false,
95-
module: variable.module || false,
96-
mutated: variable.mutated || false,
97-
reassigned: variable.reassigned || false,
98-
referenced: variable.referenced || false,
99-
writable: variable.writable || false
100-
}))
70+
timings
10171
};
10272
}
10373
}

src/compile/Component.ts

Lines changed: 88 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -217,106 +217,121 @@ export default class Component {
217217
}
218218

219219
generate(result: string) {
220-
const { compileOptions, name } = this;
221-
const { format = 'esm' } = compileOptions;
220+
let js = null;
221+
let css = null;
222222

223-
const banner = `/* ${this.file ? `${this.file} ` : ``}generated by Svelte v${"__VERSION__"} */`;
223+
if (result) {
224+
const { compileOptions, name } = this;
225+
const { format = 'esm' } = compileOptions;
224226

225-
// TODO use same regex for both
226-
result = result.replace(compileOptions.generate === 'ssr' ? /(@+|#+)(\w*(?:-\w*)?)/g : /(@+)(\w*(?:-\w*)?)/g, (match: string, sigil: string, name: string) => {
227-
if (sigil === '@') {
228-
if (internal_exports.has(name)) {
229-
if (compileOptions.dev && internal_exports.has(`${name}Dev`)) name = `${name}Dev`;
230-
this.helpers.add(name);
231-
}
227+
const banner = `/* ${this.file ? `${this.file} ` : ``}generated by Svelte v${"__VERSION__"} */`;
232228

233-
return this.alias(name);
234-
}
229+
// TODO use same regex for both
230+
result = result.replace(compileOptions.generate === 'ssr' ? /(@+|#+)(\w*(?:-\w*)?)/g : /(@+)(\w*(?:-\w*)?)/g, (match: string, sigil: string, name: string) => {
231+
if (sigil === '@') {
232+
if (internal_exports.has(name)) {
233+
if (compileOptions.dev && internal_exports.has(`${name}Dev`)) name = `${name}Dev`;
234+
this.helpers.add(name);
235+
}
235236

236-
return sigil.slice(1) + name;
237-
});
237+
return this.alias(name);
238+
}
238239

239-
const importedHelpers = Array.from(this.helpers)
240-
.sort()
241-
.map(name => {
242-
const alias = this.alias(name);
243-
return { name, alias };
240+
return sigil.slice(1) + name;
244241
});
245242

246-
const module = wrapModule(
247-
result,
248-
format,
249-
name,
250-
compileOptions,
251-
banner,
252-
compileOptions.sveltePath,
253-
importedHelpers,
254-
this.imports,
255-
this.vars.filter(variable => variable.module && variable.export_name).map(variable => ({
256-
name: variable.name,
257-
as: variable.export_name
258-
})),
259-
this.source
260-
);
243+
const importedHelpers = Array.from(this.helpers)
244+
.sort()
245+
.map(name => {
246+
const alias = this.alias(name);
247+
return { name, alias };
248+
});
261249

262-
const parts = module.split('✂]');
263-
const finalChunk = parts.pop();
250+
const module = wrapModule(
251+
result,
252+
format,
253+
name,
254+
compileOptions,
255+
banner,
256+
compileOptions.sveltePath,
257+
importedHelpers,
258+
this.imports,
259+
this.vars.filter(variable => variable.module && variable.export_name).map(variable => ({
260+
name: variable.name,
261+
as: variable.export_name
262+
})),
263+
this.source
264+
);
264265

265-
const compiled = new Bundle({ separator: '' });
266+
const parts = module.split('✂]');
267+
const finalChunk = parts.pop();
266268

267-
function addString(str: string) {
268-
compiled.addSource({
269-
content: new MagicString(str),
270-
});
271-
}
269+
const compiled = new Bundle({ separator: '' });
272270

273-
const { filename } = compileOptions;
271+
function addString(str: string) {
272+
compiled.addSource({
273+
content: new MagicString(str),
274+
});
275+
}
274276

275-
// special case — the source file doesn't actually get used anywhere. we need
276-
// to add an empty file to populate map.sources and map.sourcesContent
277-
if (!parts.length) {
278-
compiled.addSource({
279-
filename,
280-
content: new MagicString(this.source).remove(0, this.source.length),
281-
});
282-
}
277+
const { filename } = compileOptions;
278+
279+
// special case — the source file doesn't actually get used anywhere. we need
280+
// to add an empty file to populate map.sources and map.sourcesContent
281+
if (!parts.length) {
282+
compiled.addSource({
283+
filename,
284+
content: new MagicString(this.source).remove(0, this.source.length),
285+
});
286+
}
283287

284-
const pattern = /\[(\d+)-(\d+)$/;
288+
const pattern = /\[(\d+)-(\d+)$/;
285289

286-
parts.forEach((str: string) => {
287-
const chunk = str.replace(pattern, '');
288-
if (chunk) addString(chunk);
290+
parts.forEach((str: string) => {
291+
const chunk = str.replace(pattern, '');
292+
if (chunk) addString(chunk);
289293

290-
const match = pattern.exec(str);
294+
const match = pattern.exec(str);
291295

292-
const snippet = this.code.snip(+match[1], +match[2]);
296+
const snippet = this.code.snip(+match[1], +match[2]);
293297

294-
compiled.addSource({
295-
filename,
296-
content: snippet,
298+
compiled.addSource({
299+
filename,
300+
content: snippet,
301+
});
297302
});
298-
});
299303

300-
addString(finalChunk);
304+
addString(finalChunk);
301305

302-
const css = compileOptions.customElement ?
303-
{ code: null, map: null } :
304-
this.stylesheet.render(compileOptions.cssOutputFilename, true);
306+
css = compileOptions.customElement ?
307+
{ code: null, map: null } :
308+
this.stylesheet.render(compileOptions.cssOutputFilename, true);
305309

306-
const js = {
307-
code: compiled.toString(),
308-
map: compiled.generateMap({
309-
includeContent: true,
310-
file: compileOptions.outputFilename,
311-
})
312-
};
310+
js = {
311+
code: compiled.toString(),
312+
map: compiled.generateMap({
313+
includeContent: true,
314+
file: compileOptions.outputFilename,
315+
})
316+
};
317+
}
313318

314319
return {
315320
js,
316321
css,
317322
ast: this.ast,
318323
warnings: this.warnings,
319-
stats: this.stats.render(this)
324+
vars: this.vars.filter(v => !v.global && !v.implicit && !v.internal).map(v => ({
325+
name: v.name,
326+
export_name: v.export_name || null,
327+
injected: v.injected || false,
328+
module: v.module || false,
329+
mutated: v.mutated || false,
330+
reassigned: v.reassigned || false,
331+
referenced: v.referenced || false,
332+
writable: v.writable || false
333+
})),
334+
stats: this.stats.render()
320335
};
321336
}
322337

src/compile/index.ts

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -95,13 +95,11 @@ export default function compile(source: string, options: CompileOptions = {}) {
9595
);
9696
stats.stop('create component');
9797

98-
if (options.generate === false) {
99-
return { ast, warnings, stats: stats.render(component), js: null, css: null };
100-
}
101-
102-
const js = options.generate === 'ssr'
103-
? renderSSR(component, options)
104-
: renderDOM(component, options);
98+
const js = options.generate === false
99+
? null
100+
: options.generate === 'ssr'
101+
? renderSSR(component, options)
102+
: renderDOM(component, options);
105103

106104
return component.generate(js);
107105
}

test/stats/samples/duplicate-globals/_config.js

Lines changed: 0 additions & 5 deletions
This file was deleted.

test/stats/samples/implicit-action/_config.js

Lines changed: 0 additions & 5 deletions
This file was deleted.

test/stats/samples/implicit/_config.js

Lines changed: 0 additions & 5 deletions
This file was deleted.

test/stats/samples/template-references/_config.js

Lines changed: 0 additions & 5 deletions
This file was deleted.

test/stats/samples/undeclared/_config.js

Lines changed: 0 additions & 5 deletions
This file was deleted.

test/vars/index.js

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import * as fs from 'fs';
2+
import * as assert from 'assert';
3+
import { svelte, loadConfig, tryToLoadJson } from '../helpers.js';
4+
5+
describe('vars', () => {
6+
fs.readdirSync('test/vars/samples').forEach(dir => {
7+
if (dir[0] === '.') return;
8+
9+
// add .solo to a sample directory name to only run that test
10+
const solo = /\.solo/.test(dir);
11+
const skip = /\.skip/.test(dir);
12+
13+
if (solo && process.env.CI) {
14+
throw new Error('Forgot to remove `solo: true` from test');
15+
}
16+
17+
(solo ? it.only : skip ? it.skip : it)(dir, () => {
18+
const config = loadConfig(`./vars/samples/${dir}/_config.js`);
19+
const filename = `test/vars/samples/${dir}/input.svelte`;
20+
const input = fs.readFileSync(filename, 'utf-8').replace(/\s+$/, '');
21+
22+
const expectedError = tryToLoadJson(
23+
`test/vars/samples/${dir}/error.json`
24+
);
25+
26+
let result;
27+
let error;
28+
29+
try {
30+
result = svelte.compile(input, config.options);
31+
config.test(assert, result.vars);
32+
} catch (e) {
33+
error = e;
34+
}
35+
36+
if (error || expectedError) {
37+
if (error && !expectedError) {
38+
throw error;
39+
}
40+
41+
if (expectedError && !error) {
42+
throw new Error(`Expected an error: ${expectedError.message}`);
43+
}
44+
45+
assert.equal(error.message, expectedError.message);
46+
assert.deepEqual(error.start, expectedError.start);
47+
assert.deepEqual(error.end, expectedError.end);
48+
assert.equal(error.pos, expectedError.pos);
49+
}
50+
});
51+
});
52+
53+
it('returns a vars object when options.generate is false', () => {
54+
const { vars } = svelte.compile('', {
55+
generate: false
56+
});
57+
58+
assert.ok(Array.isArray(vars));
59+
});
60+
});
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export default {
2+
test(assert, vars) {
3+
assert.deepEqual(vars, []);
4+
},
5+
};

test/stats/samples/duplicate-non-hoistable/_config.js renamed to test/vars/samples/duplicate-non-hoistable/_config.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
export default {
2-
test(assert, stats) {
3-
assert.deepEqual(stats.vars, [
2+
test(assert, vars) {
3+
assert.deepEqual(vars, [
44
{
55
name: 'console',
66
injected: false,

test/stats/samples/duplicate-vars/_config.js renamed to test/vars/samples/duplicate-vars/_config.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
export default {
2-
test(assert, stats) {
3-
assert.deepEqual(stats.vars, [
2+
test(assert, vars) {
3+
assert.deepEqual(vars, [
44
{
55
name: 'foo',
66
injected: false,
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export default {
2+
test(assert, vars) {
3+
assert.deepEqual(vars, []);
4+
},
5+
};

test/stats/samples/implicit-reactive/_config.js renamed to test/vars/samples/implicit-reactive/_config.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
export default {
2-
test(assert, stats) {
3-
assert.deepEqual(stats.vars, [
2+
test(assert, vars) {
3+
assert.deepEqual(vars, [
44
{
55
name: 'a',
66
injected: false,

0 commit comments

Comments
 (0)