mirror of
https://github.com/sveltejs/svelte.git
synced 2024-11-29 08:32:05 +01:00
Merge pull request #2095 from sveltejs/gh-2094
error on invalid compiler options
This commit is contained in:
commit
516f8c8b8d
12
register.js
12
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) {
|
||||
|
@ -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,
|
||||
|
@ -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')
|
||||
|
@ -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';
|
||||
import fuzzymatch from '../utils/fuzzymatch';
|
||||
|
||||
function default_onwarn({ start, message }: Warning) {
|
||||
if (start) {
|
||||
console.warn(`(${start.line}:${start.column}) – ${message}`);
|
||||
} else {
|
||||
console.warn(message);
|
||||
}
|
||||
}
|
||||
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)) {
|
||||
|
@ -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;
|
||||
|
@ -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);
|
||||
|
@ -1,3 +1,5 @@
|
||||
export default {
|
||||
dev: true
|
||||
compileOptions: {
|
||||
dev: true
|
||||
}
|
||||
};
|
@ -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
|
||||
});
|
||||
});
|
||||
|
@ -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) {
|
||||
|
@ -1,10 +0,0 @@
|
||||
{
|
||||
"code": "binding-disabled",
|
||||
"message": "Two-way binding is disabled",
|
||||
"start": {
|
||||
"line": 1,
|
||||
"column": 7,
|
||||
"character": 7
|
||||
},
|
||||
"pos": 7
|
||||
}
|
@ -1 +0,0 @@
|
||||
<input bind:value={name}>
|
@ -1,3 +0,0 @@
|
||||
{
|
||||
"bind": false
|
||||
}
|
Loading…
Reference in New Issue
Block a user