/
base.js
142 lines (119 loc) · 4.23 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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/* eslint global-require: 0 */
/* eslint import/no-dynamic-require: 0 */
const { existsSync, readdirSync } = require("fs")
const { basename, dirname, join, relative, resolve } = require("path")
const extname = require("path-complete-extname")
const WebpackAssetsManifest = require("webpack-assets-manifest")
const webpack = require("webpack")
const rules = require("../rules")
const config = require("../config")
const { isProduction } = require("../env")
const { moduleExists } = require("../utils/helpers")
const getFilesInDirectory = (dir, includeNested) => {
if (!existsSync(dir)) {
return []
}
return readdirSync(dir, { withFileTypes: true }).flatMap((dirent) => {
const filePath = join(dir, dirent.name)
if (dirent.isDirectory() && includeNested) {
return getFilesInDirectory(filePath, includeNested)
}
if (dirent.isFile()) {
return filePath
}
return []
})
}
const getEntryObject = () => {
const entries = {}
const rootPath = join(config.source_path, config.source_entry_path)
if (config.source_entry_path === "/" && config.nested_entries) {
throw new Error(
"Your shakapacker config specified using a source_entry_path of '/' with 'nested_entries' == " +
"'true'. Doing this would result in packs for every one of your source files"
)
}
getFilesInDirectory(rootPath, config.nested_entries).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: config.manifestPath,
entrypointsUseAssets: true,
publicPath: true
})
]
if (moduleExists("css-loader") && moduleExists("mini-css-extract-plugin")) {
const hash = isProduction || config.useContentHash ? "-[contenthash:8]" : ""
const MiniCssExtractPlugin = require("mini-css-extract-plugin")
plugins.push(
new MiniCssExtractPlugin({
filename: `css/[name]${hash}.css`,
chunkFilename: `css/[id]${hash}.css`,
// For projects where css ordering has been mitigated through consistent use of scoping or naming conventions,
// the css order warnings can be disabled by setting the ignoreOrder flag.
// Read: https://stackoverflow.com/questions/51971857/mini-css-extract-plugin-warning-in-chunk-chunkname-mini-css-extract-plugin-con
ignoreOrder: config.css_extract_ignore_order_warnings
})
)
}
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 || config.useContentHash ? "-[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: getPlugins(),
resolveLoader: {
modules: ["node_modules"]
},
optimization: {
splitChunks: { chunks: "all" },
runtimeChunk: "single"
},
module: {
strictExportPresence: true,
rules
}
}