1 | let StatoscopeWebpackPlugin = require('@statoscope/webpack-plugin').default
|
2 | let { writeFile } = require('fs').promises
|
3 | let escapeRegexp = require('escape-string-regexp')
|
4 | let CssMinimizerPlugin = require('css-minimizer-webpack-plugin')
|
5 | let { join } = require('path')
|
6 | let mkdirp = require('mkdirp')
|
7 |
|
8 | const STATIC =
|
9 | /\.(eot|woff2?|ttf|otf|svg|png|jpe?g|gif|webp|mp4|mp3|ogg|pdf|html|ico|md)$/
|
10 |
|
11 | module.exports = async function getConfig(limitConfig, check, output) {
|
12 | if (check.import) {
|
13 | let loader = ''
|
14 | for (let i in check.import) {
|
15 | let list = check.import[i].replace(/}|{/g, '').trim()
|
16 | loader +=
|
17 | `import ${check.import[i]} from ${JSON.stringify(i)}\n` +
|
18 | `console.log(${list})\n`
|
19 | }
|
20 | await mkdirp(output)
|
21 | let entry = join(output, 'entry.js')
|
22 | await writeFile(entry, loader)
|
23 | check.files = entry
|
24 | }
|
25 |
|
26 | if (check.files.length === 0) {
|
27 | check.missed = true
|
28 | check.webpack = false
|
29 | }
|
30 |
|
31 | let config = {
|
32 | entry: {
|
33 | index: check.files
|
34 | },
|
35 | output: {
|
36 | filename: limitConfig.why && `${limitConfig.project}.js`,
|
37 | path: output
|
38 | },
|
39 | optimization: {
|
40 | concatenateModules: !check.disableModuleConcatenation,
|
41 | minimizer: ['...', new CssMinimizerPlugin()]
|
42 | },
|
43 | module: {
|
44 | rules: [
|
45 | {
|
46 | test: STATIC,
|
47 | type: 'asset/resource'
|
48 | },
|
49 | {
|
50 | test: /\.css$/,
|
51 | exclude: /\.module\.css$/,
|
52 | use: [require.resolve('style-loader'), require.resolve('css-loader')]
|
53 | },
|
54 | {
|
55 | test: /\.module\.css$/,
|
56 | use: [
|
57 | require.resolve('style-loader'),
|
58 | {
|
59 | loader: require.resolve('css-loader'),
|
60 | options: {
|
61 | modules: true
|
62 | }
|
63 | }
|
64 | ]
|
65 | }
|
66 | ]
|
67 | }
|
68 | }
|
69 |
|
70 | if (check.ignore && check.ignore.length > 0) {
|
71 | let escaped = check.ignore.map(i => escapeRegexp(i))
|
72 | let ignorePattern = new RegExp(`^(${escaped.join('|')})($|/)`)
|
73 | config.externals = ({ request }, callback) => {
|
74 | if (ignorePattern.test(request)) {
|
75 | callback(null, 'root a')
|
76 | } else {
|
77 | callback()
|
78 | }
|
79 | }
|
80 | }
|
81 |
|
82 | if (!config.plugins) config.plugins = []
|
83 | if (limitConfig.why) {
|
84 | let shouldOpen = process.env.NODE_ENV !== 'test' && !limitConfig.saveBundle
|
85 | config.plugins.push(
|
86 | new StatoscopeWebpackPlugin({
|
87 | saveReportTo: join(output, 'report.html'),
|
88 | saveStatsTo: limitConfig.saveBundle
|
89 | ? join(limitConfig.saveBundle, 'stats.json')
|
90 | : undefined,
|
91 | additionalStats: [limitConfig.compareWith, check.compareWith].filter(
|
92 | Boolean
|
93 | ),
|
94 | open: shouldOpen ? 'file' : false,
|
95 | name: limitConfig.project,
|
96 | watchMode: limitConfig.watch,
|
97 | reports: check.uiReports || []
|
98 | })
|
99 | )
|
100 | } else if (limitConfig.saveBundle) {
|
101 | config.plugins.push(
|
102 | new StatoscopeWebpackPlugin({
|
103 | saveStatsTo: join(limitConfig.saveBundle, 'stats.json'),
|
104 | saveOnlyStats: true,
|
105 | open: false,
|
106 | watchMode: limitConfig.watch
|
107 | })
|
108 | )
|
109 | }
|
110 |
|
111 | return config
|
112 | }
|