From 3953c2dbfcad6554ae486c4ee7032fe529a416d0 Mon Sep 17 00:00:00 2001 From: Richard Harris Date: Sun, 17 Feb 2019 13:10:37 -0500 Subject: [PATCH] =?UTF-8?q?error=20on=20invalid=20compiler=20options=20?= =?UTF-8?q?=E2=80=94=20closes=20#2094?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- register.js | 12 +++--- src/cli/compile.ts | 14 ------- src/cli/index.ts | 2 - src/compile/index.ts | 41 ++++++++++++++----- src/interfaces.ts | 4 -- test/css/index.js | 4 +- test/css/samples/empty-rule-dev/_config.js | 4 +- test/hydration/index.js | 5 --- test/js/index.js | 4 +- .../samples/error-binding-disabled/error.json | 10 ----- .../error-binding-disabled/input.svelte | 1 - .../error-binding-disabled/options.json | 3 -- 12 files changed, 42 insertions(+), 62 deletions(-) delete mode 100644 test/parser/samples/error-binding-disabled/error.json delete mode 100644 test/parser/samples/error-binding-disabled/input.svelte delete mode 100644 test/parser/samples/error-binding-disabled/options.json diff --git a/register.js b/register.js index 4dee2a4b6a..2c0069c241 100644 --- a/register.js +++ b/register.js @@ -2,21 +2,21 @@ const fs = require('fs'); const path = require('path'); const { compile } = require('./compiler.js'); -let compileOptions = { - extensions: ['.svelte', '.html'] -}; +let extensions = ['.svelte', '.html']; +let compileOptions = {}; function capitalise(name) { return name[0].toUpperCase() + name.slice(1); } -function register(options) { +function register(options = {}) { if (options.extensions) { - compileOptions.extensions.forEach(deregisterExtension); + extensions.forEach(deregisterExtension); options.extensions.forEach(registerExtension); } - compileOptions = options; + compileOptions = Object.assign({}, options); + delete compileOptions.extensions; } function deregisterExtension(extension) { diff --git a/src/cli/compile.ts b/src/cli/compile.ts index 474a31e326..6f187cd9c3 100644 --- a/src/cli/compile.ts +++ b/src/cli/compile.ts @@ -31,24 +31,10 @@ export function compile(input, opts) { } } - const globals = {}; - if (opts.globals) { - opts.globals.split(',').forEach(pair => { - const [key, value] = pair.split(':'); - globals[key] = value; - }); - } - const options = { name: opts.name, format: opts.format, sourceMap: opts.sourcemap, - globals, - amd: opts.amdId - ? { - id: opts.amdId, - } - : undefined, css: opts.css !== false, dev: opts.dev, immutable: opts.immutable, diff --git a/src/cli/index.ts b/src/cli/index.ts index ca0ed51caa..309d23acda 100644 --- a/src/cli/index.ts +++ b/src/cli/index.ts @@ -8,11 +8,9 @@ prog .option('-o, --output', 'Output (if absent, prints to stdout)') .option('-f, --format', 'Type of output (cjs or esm)', 'esm') - .option('-g, --globals', 'Comma-separate list of `module ID:Global` pairs') .option('-n, --name', 'Name for IIFE/UMD export (inferred from filename by default)') .option('-m, --sourcemap', 'Generate sourcemap (`-m inline` for inline map)') .option('-d, --dev', 'Add dev mode warnings and errors') - .option('--amdId', 'ID for AMD module (default is anonymous)') .option('--generate', 'Change generate format between `dom` and `ssr`') .option('--no-css', `Don't include CSS (useful with SSR)`) .option('--immutable', 'Support immutable data structures') diff --git a/src/compile/index.ts b/src/compile/index.ts index 80ad0a583f..404d933327 100644 --- a/src/compile/index.ts +++ b/src/compile/index.ts @@ -3,23 +3,42 @@ import Stats from '../Stats'; import parse from '../parse/index'; import renderDOM from './render-dom/index'; import renderSSR from './render-ssr/index'; -import { CompileOptions, Warning, Ast } from '../interfaces'; +import { CompileOptions, Ast } from '../interfaces'; import Component from './Component'; - -function default_onwarn({ start, message }: Warning) { - if (start) { - console.warn(`(${start.line}:${start.column}) – ${message}`); - } else { - console.warn(message); - } -} +import fuzzymatch from '../utils/fuzzymatch'; + +const valid_options = [ + 'format', + 'name', + 'filename', + 'generate', + 'outputFilename', + 'cssOutputFilename', + 'sveltePath', + 'dev', + 'immutable', + 'hydratable', + 'legacy', + 'customElement', + 'css', + 'preserveComments' +]; function validate_options(options: CompileOptions, stats: Stats) { const { name, filename } = options; + Object.keys(options).forEach(key => { + if (valid_options.indexOf(key) === -1) { + const match = fuzzymatch(key, valid_options); + let message = `Unrecognized option '${key}'`; + if (match) message += ` (did you mean '${match}'?)`; + + throw new Error(message); + } + }); + if (name && !/^[a-zA-Z_$][a-zA-Z_$0-9]*$/.test(name)) { - const error = new Error(`options.name must be a valid identifier (got '${name}')`); - throw error; + throw new Error(`options.name must be a valid identifier (got '${name}')`); } if (name && /^[a-z]/.test(name)) { diff --git a/src/interfaces.ts b/src/interfaces.ts index 23b4c42eb4..11228a0d55 100644 --- a/src/interfaces.ts +++ b/src/interfaces.ts @@ -43,10 +43,6 @@ export interface CompileOptions { name?: string; filename?: string; generate?: string | false; - globals?: ((id: string) => string) | object; - amd?: { - id?: string; - }; outputFilename?: string; cssOutputFilename?: string; diff --git a/test/css/index.js b/test/css/index.js index 1079459b78..684c310526 100644 --- a/test/css/index.js +++ b/test/css/index.js @@ -58,12 +58,12 @@ describe('css', () => { const dom = svelte.compile( input, - Object.assign(config, { format: 'cjs' }) + Object.assign(config.compileOptions || {}, { format: 'cjs' }) ); const ssr = svelte.compile( input, - Object.assign(config, { format: 'cjs', generate: 'ssr' }) + Object.assign(config.compileOptions || {}, { format: 'cjs', generate: 'ssr' }) ); assert.equal(dom.css.code, ssr.css.code); diff --git a/test/css/samples/empty-rule-dev/_config.js b/test/css/samples/empty-rule-dev/_config.js index e26996239d..bdadb774d1 100644 --- a/test/css/samples/empty-rule-dev/_config.js +++ b/test/css/samples/empty-rule-dev/_config.js @@ -1,3 +1,5 @@ export default { - dev: true + compileOptions: { + dev: true + } }; \ No newline at end of file diff --git a/test/hydration/index.js b/test/hydration/index.js index 24c61fb56a..30647cace1 100644 --- a/test/hydration/index.js +++ b/test/hydration/index.js @@ -50,9 +50,6 @@ describe('hydration', () => { const cwd = path.resolve(`test/hydration/samples/${dir}`); compileOptions = config.compileOptions || {}; - compileOptions.shared = path.resolve('internal.js'); - compileOptions.dev = config.dev; - compileOptions.hydrate = true; const window = env(); @@ -88,14 +85,12 @@ describe('hydration', () => { } } catch (err) { showOutput(cwd, { - shared: 'svelte/internal.js', hydratable: true }); throw err; } if (config.show) showOutput(cwd, { - shared: 'svelte/internal.js', hydratable: true }); }); diff --git a/test/js/index.js b/test/js/index.js index 77da160591..14d73d6c65 100644 --- a/test/js/index.js +++ b/test/js/index.js @@ -23,9 +23,7 @@ describe("js", () => { let actual; try { - const options = Object.assign(config.options || {}, { - shared: true - }); + const options = Object.assign(config.options || {}); actual = svelte.compile(input, options).js.code.replace(/generated by Svelte v\d+\.\d+\.\d+(-\w+\.\d+)?/, 'generated by Svelte vX.Y.Z'); } catch (err) { diff --git a/test/parser/samples/error-binding-disabled/error.json b/test/parser/samples/error-binding-disabled/error.json deleted file mode 100644 index 63f01e7056..0000000000 --- a/test/parser/samples/error-binding-disabled/error.json +++ /dev/null @@ -1,10 +0,0 @@ -{ - "code": "binding-disabled", - "message": "Two-way binding is disabled", - "start": { - "line": 1, - "column": 7, - "character": 7 - }, - "pos": 7 -} \ No newline at end of file diff --git a/test/parser/samples/error-binding-disabled/input.svelte b/test/parser/samples/error-binding-disabled/input.svelte deleted file mode 100644 index d0a0feab8d..0000000000 --- a/test/parser/samples/error-binding-disabled/input.svelte +++ /dev/null @@ -1 +0,0 @@ - \ No newline at end of file diff --git a/test/parser/samples/error-binding-disabled/options.json b/test/parser/samples/error-binding-disabled/options.json deleted file mode 100644 index 02fc50c2f2..0000000000 --- a/test/parser/samples/error-binding-disabled/options.json +++ /dev/null @@ -1,3 +0,0 @@ -{ - "bind": false -} \ No newline at end of file