error on invalid compiler options — closes #2094

pull/2095/head
Richard Harris 6 years ago
parent 89b00c7d30
commit 3953c2dbfc

@ -2,21 +2,21 @@ const fs = require('fs');
const path = require('path'); const path = require('path');
const { compile } = require('./compiler.js'); const { compile } = require('./compiler.js');
let compileOptions = { let extensions = ['.svelte', '.html'];
extensions: ['.svelte', '.html'] let compileOptions = {};
};
function capitalise(name) { function capitalise(name) {
return name[0].toUpperCase() + name.slice(1); return name[0].toUpperCase() + name.slice(1);
} }
function register(options) { function register(options = {}) {
if (options.extensions) { if (options.extensions) {
compileOptions.extensions.forEach(deregisterExtension); extensions.forEach(deregisterExtension);
options.extensions.forEach(registerExtension); options.extensions.forEach(registerExtension);
} }
compileOptions = options; compileOptions = Object.assign({}, options);
delete compileOptions.extensions;
} }
function deregisterExtension(extension) { function deregisterExtension(extension) {

@ -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 = { const options = {
name: opts.name, name: opts.name,
format: opts.format, format: opts.format,
sourceMap: opts.sourcemap, sourceMap: opts.sourcemap,
globals,
amd: opts.amdId
? {
id: opts.amdId,
}
: undefined,
css: opts.css !== false, css: opts.css !== false,
dev: opts.dev, dev: opts.dev,
immutable: opts.immutable, immutable: opts.immutable,

@ -8,11 +8,9 @@ prog
.option('-o, --output', 'Output (if absent, prints to stdout)') .option('-o, --output', 'Output (if absent, prints to stdout)')
.option('-f, --format', 'Type of output (cjs or esm)', 'esm') .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('-n, --name', 'Name for IIFE/UMD export (inferred from filename by default)')
.option('-m, --sourcemap', 'Generate sourcemap (`-m inline` for inline map)') .option('-m, --sourcemap', 'Generate sourcemap (`-m inline` for inline map)')
.option('-d, --dev', 'Add dev mode warnings and errors') .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('--generate', 'Change generate format between `dom` and `ssr`')
.option('--no-css', `Don't include CSS (useful with SSR)`) .option('--no-css', `Don't include CSS (useful with SSR)`)
.option('--immutable', 'Support immutable data structures') .option('--immutable', 'Support immutable data structures')

@ -3,23 +3,42 @@ import Stats from '../Stats';
import parse from '../parse/index'; import parse from '../parse/index';
import renderDOM from './render-dom/index'; import renderDOM from './render-dom/index';
import renderSSR from './render-ssr/index'; import renderSSR from './render-ssr/index';
import { CompileOptions, Warning, Ast } from '../interfaces'; import { CompileOptions, Ast } from '../interfaces';
import Component from './Component'; import Component from './Component';
import fuzzymatch from '../utils/fuzzymatch';
function default_onwarn({ start, message }: Warning) {
if (start) { const valid_options = [
console.warn(`(${start.line}:${start.column}) ${message}`); 'format',
} else { 'name',
console.warn(message); 'filename',
} 'generate',
} 'outputFilename',
'cssOutputFilename',
'sveltePath',
'dev',
'immutable',
'hydratable',
'legacy',
'customElement',
'css',
'preserveComments'
];
function validate_options(options: CompileOptions, stats: Stats) { function validate_options(options: CompileOptions, stats: Stats) {
const { name, filename } = options; 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)) { 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 new Error(`options.name must be a valid identifier (got '${name}')`);
throw error;
} }
if (name && /^[a-z]/.test(name)) { if (name && /^[a-z]/.test(name)) {

@ -43,10 +43,6 @@ export interface CompileOptions {
name?: string; name?: string;
filename?: string; filename?: string;
generate?: string | false; generate?: string | false;
globals?: ((id: string) => string) | object;
amd?: {
id?: string;
};
outputFilename?: string; outputFilename?: string;
cssOutputFilename?: string; cssOutputFilename?: string;

@ -58,12 +58,12 @@ describe('css', () => {
const dom = svelte.compile( const dom = svelte.compile(
input, input,
Object.assign(config, { format: 'cjs' }) Object.assign(config.compileOptions || {}, { format: 'cjs' })
); );
const ssr = svelte.compile( const ssr = svelte.compile(
input, input,
Object.assign(config, { format: 'cjs', generate: 'ssr' }) Object.assign(config.compileOptions || {}, { format: 'cjs', generate: 'ssr' })
); );
assert.equal(dom.css.code, ssr.css.code); assert.equal(dom.css.code, ssr.css.code);

@ -1,3 +1,5 @@
export default { export default {
compileOptions: {
dev: true dev: true
}
}; };

@ -50,9 +50,6 @@ describe('hydration', () => {
const cwd = path.resolve(`test/hydration/samples/${dir}`); const cwd = path.resolve(`test/hydration/samples/${dir}`);
compileOptions = config.compileOptions || {}; compileOptions = config.compileOptions || {};
compileOptions.shared = path.resolve('internal.js');
compileOptions.dev = config.dev;
compileOptions.hydrate = true;
const window = env(); const window = env();
@ -88,14 +85,12 @@ describe('hydration', () => {
} }
} catch (err) { } catch (err) {
showOutput(cwd, { showOutput(cwd, {
shared: 'svelte/internal.js',
hydratable: true hydratable: true
}); });
throw err; throw err;
} }
if (config.show) showOutput(cwd, { if (config.show) showOutput(cwd, {
shared: 'svelte/internal.js',
hydratable: true hydratable: true
}); });
}); });

@ -23,9 +23,7 @@ describe("js", () => {
let actual; let actual;
try { try {
const options = Object.assign(config.options || {}, { const options = Object.assign(config.options || {});
shared: true
});
actual = svelte.compile(input, options).js.code.replace(/generated by Svelte v\d+\.\d+\.\d+(-\w+\.\d+)?/, 'generated by Svelte vX.Y.Z'); 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) { } catch (err) {

@ -1,10 +0,0 @@
{
"code": "binding-disabled",
"message": "Two-way binding is disabled",
"start": {
"line": 1,
"column": 7,
"character": 7
},
"pos": 7
}
Loading…
Cancel
Save