-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
Copy pathbase.js
116 lines (97 loc) · 3.21 KB
/
base.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/* eslint global-require: 0 */
/* eslint import/no-dynamic-require: 0 */
const { basename, dirname, join, relative, resolve } = require('path')
const extname = require('path-complete-extname')
const PnpWebpackPlugin = require('pnp-webpack-plugin')
const { sync: globSync } = require('glob')
const WebpackAssetsManifest = require('webpack-assets-manifest')
const webpack = require('webpack')
const rules = require('../rules')
const { isProduction } = require('../env')
const config = require('../config')
const { moduleExists } = require('../utils/helpers')
const getEntryObject = () => {
const entries = {}
const rootPath = join(config.source_path, config.source_entry_path)
globSync(`${rootPath}/*.*`).forEach((path) => {
const namespace = relative(join(rootPath), dirname(path))
const name = join(namespace, basename(path, extname(path)))
let assetPaths = resolve(path)
// Allows for multiple filetypes per entry (https://webpack.js.org/guides/entry-advanced/)
// Transforms the config object value to an array with all values under the same name
let previousPaths = entries[name]
if (previousPaths) {
previousPaths = Array.isArray(previousPaths)
? previousPaths
: [previousPaths]
previousPaths.push(assetPaths)
assetPaths = previousPaths
}
entries[name] = assetPaths
})
return entries
}
const getModulePaths = () => {
const result = [resolve(config.source_path)]
if (config.additional_paths) {
config.additional_paths.forEach((path) => result.push(resolve(path)))
}
result.push('node_modules')
return result
}
const getPlugins = () => {
const plugins = [
new webpack.EnvironmentPlugin(process.env),
new WebpackAssetsManifest({
entrypoints: true,
writeToDisk: true,
output: 'manifest.json',
entrypointsUseAssets: true,
publicPath: true
})
]
if (moduleExists('css-loader') && moduleExists('mini-css-extract-plugin')) {
const hash = isProduction ? '-[contenthash:8]' : ''
const MiniCssExtractPlugin = require('mini-css-extract-plugin')
plugins.push(
new MiniCssExtractPlugin({
filename: `css/[name]${hash}.css`,
chunkFilename: `css/[id]${hash}.css`
})
)
}
return plugins
}
// Don't use contentHash except for production for performance
// https://webpack.js.org/guides/build-performance/#avoid-production-specific-tooling
const hash = isProduction ? '-[contenthash]' : ''
module.exports = {
mode: 'production',
output: {
filename: `js/[name]${hash}.js`,
chunkFilename: `js/[name]${hash}.chunk.js`,
// https://webpack.js.org/configuration/output/#outputhotupdatechunkfilename
hotUpdateChunkFilename: 'js/[id].[fullhash].hot-update.js',
path: config.outputPath,
publicPath: config.publicPath
},
entry: getEntryObject(),
resolve: {
extensions: ['.js', '.jsx', '.mjs', '.ts', '.tsx', '.coffee'],
modules: getModulePaths(),
plugins: [PnpWebpackPlugin]
},
plugins: getPlugins(),
resolveLoader: {
modules: ['node_modules'],
plugins: [PnpWebpackPlugin.moduleLoader(module)]
},
optimization: {
splitChunks: { chunks: 'all' },
runtimeChunk: 'single'
},
module: {
strictExportPresence: true,
rules
}
}