diff --git a/src/Stats.ts b/src/Stats.ts index 873ae15b46..c1a4b9b642 100644 --- a/src/Stats.ts +++ b/src/Stats.ts @@ -26,6 +26,8 @@ function collapseTimings(timings) { } export default class Stats { + onwarn: (warning: Warning) => void; + startTime: number; currentTiming: Timing; currentChildren: Timing[]; @@ -33,11 +35,15 @@ export default class Stats { stack: Timing[]; warnings: Warning[]; - constructor() { + constructor({ onwarn }: { + onwarn: (warning: Warning) => void + }) { this.startTime = now(); this.stack = []; this.currentChildren = this.timings = []; + this.onwarn = onwarn; + this.warnings = []; } @@ -99,4 +105,9 @@ export default class Stats { hooks }; } + + warn(warning) { + this.warnings.push(warning); + this.onwarn(warning); + } } \ No newline at end of file diff --git a/src/index.ts b/src/index.ts index d13d568069..1b297e3d29 100644 --- a/src/index.ts +++ b/src/index.ts @@ -106,11 +106,13 @@ export async function preprocess(source: string, options: PreprocessOptions) { }; } -export function compile(source: string, _options: CompileOptions) { +function compile(source: string, _options: CompileOptions) { const options = normalizeOptions(_options); let parsed: Parsed; - const stats = new Stats(); + const stats = new Stats({ + onwarn: options.onwarn + }); try { stats.start('parse'); @@ -126,23 +128,19 @@ export function compile(source: string, _options: CompileOptions) { stats.stop('stylesheet'); stats.start('validate'); - // TODO remove this when we remove svelte.validate from public API — we - // can use the stats object instead - const onwarn = options.onwarn; - options.onwarn = warning => { - stats.warnings.push(warning); - onwarn(warning); - }; - - validate(parsed, source, stylesheet, options); + validate(parsed, source, stylesheet, stats, options); stats.stop('validate'); + if (options.generate === false) { + return { ast: parsed, stats, js: null, css: null }; + } + const compiler = options.generate === 'ssr' ? generateSSR : generate; return compiler(parsed, source, stylesheet, options, stats); }; -export function create(source: string, _options: CompileOptions = {}) { +function create(source: string, _options: CompileOptions = {}) { _options.format = 'eval'; const compiled = compile(source, _options); @@ -163,4 +161,4 @@ export function create(source: string, _options: CompileOptions = {}) { } } -export { parse, validate, Stylesheet, version as VERSION }; +export { parse, create, compile, version as VERSION }; diff --git a/src/interfaces.ts b/src/interfaces.ts index dfce819d5e..8a16b57b54 100644 --- a/src/interfaces.ts +++ b/src/interfaces.ts @@ -44,7 +44,7 @@ export interface CompileOptions { format?: ModuleFormat; name?: string; filename?: string; - generate?: string; + generate?: string | false; globals?: ((id: string) => string) | object; amd?: { id?: string; diff --git a/src/validate/index.ts b/src/validate/index.ts index f5bb2d30ef..3bdbbc231b 100644 --- a/src/validate/index.ts +++ b/src/validate/index.ts @@ -5,15 +5,16 @@ import getCodeFrame from '../utils/getCodeFrame'; import Stats from '../Stats'; import error from '../utils/error'; import Stylesheet from '../css/Stylesheet'; +import Stats from '../Stats'; import { Node, Parsed, CompileOptions, Warning } from '../interfaces'; export class Validator { readonly source: string; readonly filename: string; readonly v2: boolean; + readonly stats: Stats; options: CompileOptions; - onwarn: ({}) => void; locator?: (pos: number) => Location; namespace: string; @@ -34,10 +35,11 @@ export class Validator { actions: Set; }; - constructor(parsed: Parsed, source: string, options: CompileOptions) { + constructor(parsed: Parsed, source: string, stats: Stats, options: CompileOptions) { this.source = source; + this.stats = stats; + this.filename = options.filename; - this.onwarn = options.onwarn; this.options = options; this.v2 = options.parser === 'v2'; @@ -79,7 +81,7 @@ export class Validator { const frame = getCodeFrame(this.source, start.line, start.column); - this.onwarn({ + this.stats.warn({ code, message, frame, @@ -96,9 +98,10 @@ export default function validate( parsed: Parsed, source: string, stylesheet: Stylesheet, + stats: Stats, options: CompileOptions ) { - const { onwarn, onerror, name, filename, store, dev, parser } = options; + const { onerror, name, filename, store, dev, parser } = options; try { if (name && !/^[a-zA-Z_$][a-zA-Z_$0-9]*$/.test(name)) { @@ -108,7 +111,7 @@ export default function validate( if (name && /^[a-z]/.test(name)) { const message = `options.name should be capitalised`; - onwarn({ + stats.warn({ code: `options-lowercase-name`, message, filename, @@ -116,8 +119,7 @@ export default function validate( }); } - const validator = new Validator(parsed, source, { - onwarn, + const validator = new Validator(parsed, source, stats, { name, filename, store, diff --git a/test/validator/index.js b/test/validator/index.js index 211c763f48..16e84e63aa 100644 --- a/test/validator/index.js +++ b/test/validator/index.js @@ -28,7 +28,8 @@ describe("validate", () => { const { code, message, pos, loc, end } = warning; warnings.push({ code, message, pos, loc, end }); }, - dev: config.dev + dev: config.dev, + generate: false }); assert.equal(stats.warnings.length, warnings.length); @@ -87,7 +88,8 @@ describe("validate", () => { it("errors if options.name is illegal", () => { assert.throws(() => { svelte.compile("
", { - name: "not.valid" + name: "not.valid", + generate: false }); }, /options\.name must be a valid identifier/); }); @@ -103,7 +105,8 @@ describe("validate", () => { pos: warning.pos, loc: warning.loc }); - } + }, + generate: false }); assert.deepEqual(warnings, [ { @@ -126,7 +129,8 @@ describe("validate", () => { pos: warning.pos, loc: warning.loc }); - } + }, + generate: false }); assert.deepEqual(warnings, []); });