UNPKG

1.23 kBJavaScriptView Raw
1import { processImport } from 'size-limit'
2
3function escapeRegexp(string) {
4 return string.replace(/[|\\{}()[\]^$+*?.]/g, '\\$&').replace(/-/g, '\\x2d')
5}
6
7const STATIC =
8 /\.(eot|woff2?|ttf|otf|svg|png|jpe?g|gif|webp|mp4|mp3|ogg|pdf|html|ico|md)$/
9
10export async function getConfig(limitConfig, check, output) {
11 await processImport(check, output)
12
13 if (check.files.length === 0) {
14 check.missed = true
15 check.webpack = false
16 }
17
18 let config = {
19 entry: {
20 index: check.files
21 },
22 module: {
23 rules: [
24 {
25 test: STATIC,
26 type: 'asset/resource'
27 }
28 ]
29 },
30 optimization: {
31 concatenateModules: !check.disableModuleConcatenation
32 },
33 output: {
34 filename: limitConfig.why && `${limitConfig.project}.js`,
35 path: output
36 }
37 }
38
39 if (check.ignore && check.ignore.length > 0) {
40 let escaped = check.ignore.map(i => escapeRegexp(i))
41 let ignorePattern = new RegExp(`^(${escaped.join('|')})($|/)`)
42 config.externals = ({ request }, callback) => {
43 if (ignorePattern.test(request)) {
44 callback(null, 'root a')
45 } else {
46 callback()
47 }
48 }
49 }
50
51 if (!config.plugins) config.plugins = []
52
53 return config
54}