UNPKG

2.58 kBJavaScriptView Raw
1const MESSAGES = {
2 noPackage: () =>
3 'Size Limit didn’t find *package.json*. ' +
4 'Create npm package and run Size Limit there.',
5 unknownArg: arg =>
6 `Unknown argument *${arg}*. Check command for typo and read docs.`,
7 argWithoutWebpack: arg =>
8 `Argument *--${arg}* works only with *@size-limit/webpack* plugin` +
9 (arg === 'why' ? '. You can add Bundle Analyzer to you own bundler.' : ''),
10 argWithoutAnotherArg: (arg, anotherArg) =>
11 `Argument *--${arg}* works only with *--${anotherArg}* argument`,
12 argWithoutParameter: (arg, parameter) =>
13 `Missing parameter *${parameter}* for *--${arg}* argument`,
14 noConfig: () => 'Create Size Limit config in *package.json*',
15 noArrayConfig: () => 'Size Limit config must contain *an array*',
16 emptyConfig: () => 'Size Limit config must *not be empty*',
17 noObjectCheck: () => 'Size Limit config array should contain *only objects*',
18 pathNotString: () =>
19 'The *path* in Size Limit config ' +
20 'must be *a string* or *an array of strings*',
21 entryNotString: () =>
22 'The *entry* in Size Limit config ' +
23 'must be *a string* or *an array of strings*',
24 pluginlessConfig: (opt, mod) =>
25 `Config option *${opt}* needs *@size-limit/${mod}* plugin`,
26 multiPluginlessConfig: (opt, mod1, mod2) =>
27 `Config option *${opt}* needs *@size-limit/${mod1}* ` +
28 `or *@size-limit/${mod2}* plugin`,
29 timeWithoutPlugin: () => 'Add *@size-limit/time* plugin to use time limit',
30 unknownOption: opt =>
31 `Unknown option *${opt}* in config. Check Size Limit docs and version.`,
32 missedPlugin: mod => `Add *@size-limit/${mod}* plugin to Size Limit`,
33 unknownEntry: entry =>
34 `Size Limit didn’t find *${entry}* entry in custom Webpack config`,
35 brotliUnsupported: () =>
36 'Update your Node.js to version >= v11.7.0 to use Brotli',
37 cmdError: (cmd, error) => (error ? `${cmd} error: ${error}` : `${cmd} error`),
38 bundleDirNotEmpty: dir =>
39 `The directory *${dir}* is not empty. ` +
40 'Pass *--clean-dir* if you want to remove it'
41}
42
43const ADD_CONFIG_EXAMPLE = {
44 noConfig: true,
45 emptyConfig: true,
46 noObjectCheck: true,
47 noArrayConfig: true,
48 pathNotString: true
49}
50
51class SizeLimitError extends Error {
52 constructor(type, ...args) {
53 super(MESSAGES[type](...args))
54 this.name = 'SizeLimitError'
55 if (ADD_CONFIG_EXAMPLE[type]) {
56 this.example =
57 ' "size-limit": [\n' +
58 ' {\n' +
59 ' "path": "dist/bundle.js",\n' +
60 ' "limit": "10 kB"\n' +
61 ' }\n' +
62 ' ]\n'
63 }
64 }
65}
66
67module.exports = SizeLimitError