-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebpack.js
194 lines (176 loc) · 7.82 KB
/
webpack.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
import { join, dirname } from 'node:path'
import { tryRequire } from '@dr-js/core/module/env/tryRequire.js'
import { binary, time, padTable } from '@dr-js/core/module/common/format.js'
import { writeJSON } from '@dr-js/core/module/node/fs/File.js'
import { createDirectory } from '@dr-js/core/module/node/fs/Directory.js'
import { addExitListenerAsync } from '@dr-js/core/module/node/system/ExitListener.js'
import { argvFlag } from '@dr-js/core/module/node/kit.js'
import { getWebpackBabelConfig } from './babel.js'
import { createProgressPlugin } from './webpack-progress-plugin.js'
// https://webpack.js.org/api/stats/
const GET_WEBPACK = (log = console.warn) => {
const Webpack = tryRequire('webpack')
if (Webpack) return Webpack
const error = new Error('[Webpack] failed to load package "webpack"')
log(error)
throw error
}
const getStatsCheck = (onError, onStats) => (error, stats) => {
if (error) return onError(error)
const isErrorLog = stats.hasErrors()
const isWarningLog = stats.hasWarnings()
if (isErrorLog || isWarningLog) {
const { errors = [], warnings = [] } = stats.toJson({
all: false, // fallback value for stats options when an option is not defined (has precedence over local webpack defaults)
errors: true, // Add errors
errorDetails: true, // Add details to errors (like resolving log)
warnings: true // Add warnings
})
errors.forEach((message) => console.error(message))
warnings.forEach((message) => console.warn(message))
if (isErrorLog) return onError(new Error('webpack stats Error'))
}
onStats(stats)
}
const getLogStats = (isWatch, kitLogger) => {
const logSingleStats = (statsData, startTime, endTime) => {
startTime && endTime && kitLogger.padLog(`[${isWatch ? 'watch' : 'compile'}] time: ${time(endTime - startTime)}`)
const { assets, chunks } = statsData.toJson({
all: false, // fallback value for stats options when an option is not defined (has precedence over local webpack defaults)
assets: true, // Add asset Information
chunks: kitLogger.isVerbose // Add chunk information (setting this to `false` allows for a less verbose output)
})
const table = []
assets.forEach(({ name, size, emitted }) => table.push([
` Asset ${name}`,
formatSize(size),
formatTag({ emitted })
]))
kitLogger.isVerbose && chunks.forEach(({ id, names, size, entry, initial, rendered }) => table.push([
` Chunk ${id || '-'} ${names.join(',')}`,
formatSize(size),
formatTag({ entry, initial, rendered })
]))
kitLogger.log(`output:\n${padTable({ table, padFuncList: [ 'L', 'R', 'L' ] })}`)
}
return (stats) => {
if (stats.compilation) return logSingleStats(stats) // Stats (for single config)
if (stats.stats) return stats.stats.map(logSingleStats) // MultiStats (for more than one config)
console.warn('[getLogStats] unexpected statData', stats)
throw new Error('[getLogStats] unexpected statData')
}
}
const formatSize = (size) => `${binary(size)}B`
const formatTag = (tagMap) => Object.entries(tagMap).map(([ k, v ]) => v && k).filter(Boolean).join(',')
const compileWithWebpack = async ({
kit, kitLogger = kit,
Webpack = GET_WEBPACK(kitLogger.log),
config, isWatch, profileOutput, namedChunkGroupOutput
}) => {
if (profileOutput) {
isWatch && console.warn('[watch] warning: skipped generate profileOutput')
config.profile = true
}
const compiler = Webpack(config)
const logStats = getLogStats(isWatch, kitLogger)
if (isWatch) {
kitLogger.log('[watch] start')
const watching = compiler.watch({ aggregateTimeout: 512, poll: undefined }, getStatsCheck((error) => kitLogger.log(`error: ${error}`), logStats))
addExitListenerAsync(async (eventPack) => {
await new Promise((resolve) => watching.close(resolve))
kitLogger.log(`[watch] exit with state: ${JSON.stringify(eventPack)}`)
})
} else {
kitLogger.log('[compile] start')
const stats = await new Promise((resolve, reject) => compiler.run(getStatsCheck(reject, resolve)))
await new Promise((resolve, reject) => compiler.close((error, result) => error ? reject(error) : resolve(result))) // close for Webpack5
logStats(stats)
let statsJSON
const getStatsJSON = () => {
if (statsJSON === undefined) statsJSON = stats.toJson('verbose') // all data
return statsJSON
}
if (profileOutput) {
await createDirectory(dirname(profileOutput))
await writeJSON(profileOutput, getStatsJSON())
kitLogger.log(`[compile] generated profileOutput at: ${profileOutput}`)
}
if (namedChunkGroupOutput) {
await createDirectory(dirname(namedChunkGroupOutput))
const { namedChunkGroups, children } = getStatsJSON() // for MultiStats, should check each Stats in statsJSON.children
await writeJSON(namedChunkGroupOutput, (
namedChunkGroups ||
(children && children.map(({ name, namedChunkGroups }) => ({ name, namedChunkGroups }))) ||
{}
))
kitLogger.log(`[compile] generated namedChunkGroupOutput at: ${namedChunkGroupOutput}`)
}
return stats
}
}
const commonFlag = async ({
kit, kitLogger = kit,
fromRoot = kit && kit.fromRoot, // optional if directly set `profileOutput`
Webpack = GET_WEBPACK(kitLogger),
mode = argvFlag('development', 'production') || 'production',
isWatch = Boolean(argvFlag('watch')),
isProduction = mode === 'production',
profileOutput = argvFlag('profile') ? fromRoot('.temp-gitignore/profile-stat.json') : null,
namedChunkGroupOutput = ''
}) => {
kitLogger.log(`compile flag: ${JSON.stringify({ mode, isWatch, isProduction, profileOutput, namedChunkGroupOutput }, null, ' ')}`)
const getCommonWebpackConfig = ({
babelOption = getWebpackBabelConfig({ isProduction }),
packageJSONPickOption = { keys: [ 'name', 'version' ], exportMode: 'export-each' }, // prefer style like: `import { a, b } from './package.json'`
output, // = { path: fromOutput('library'), filename: '[name].js', library: 'LIBRARY', libraryTarget: 'umd' },
entry, // = { index: 'source/index' },
resolve = { alias: { source: fromRoot('source') } },
externals = undefined,
isNodeEnv = false,
isNodeBin = false, // add `#!/usr/bin/env node`
isMinimize = false, // expect later manual run for each file
extraModuleRuleList = [],
extraPluginList = [],
extraDefine = {},
...extraConfig
}) => ({
mode,
bail: isProduction,
target: isNodeEnv ? 'node12' : [ 'web', 'es8' ], // support node main modules like 'fs'
node: isNodeEnv ? false : undefined, // do not polyfill fake node environment when build for node
output,
entry,
resolve,
externals,
module: {
rules: [
babelOption && { test: /\.m?js$/, use: { loader: 'babel-loader', options: babelOption } },
packageJSONPickOption && {
test: /\/package\.json$/,
type: (!packageJSONPickOption.exportMode || packageJSONPickOption.exportMode.startsWith('export-')) ? 'javascript/auto' : undefined,
use: { loader: join(__dirname, 'webpack-json-pick-loader.js'), options: packageJSONPickOption }
},
...extraModuleRuleList
].filter(Boolean)
},
plugins: [
createProgressPlugin({ log: kitLogger.log }),
new Webpack.DefinePlugin({
'process.env.NODE_ENV': JSON.stringify(mode),
'__DEV__': !isProduction,
...extraDefine
}),
isNodeBin && new Webpack.BannerPlugin({ banner: '#!/usr/bin/env node', raw: true }),
...extraPluginList
].filter(Boolean),
optimization: { minimize: isMinimize },
performance: { hints: isMinimize ? 'warning' : false }, // mute: `The following asset(s) exceed the recommended size limit (250 kB).`
...extraConfig
})
return { mode, isWatch, isProduction, profileOutput, namedChunkGroupOutput, getCommonWebpackConfig }
}
export {
GET_WEBPACK,
compileWithWebpack,
commonFlag
}