Skip to content

Commit 8c88ec6

Browse files
authored
Make dev builds faster (#227)
* Make SW generation prod-only * Use SpeedMeasurePlugin * Refactor: Symlink directories for dev builds * Lint fix * Remove SpeedMeasurePlugin
1 parent a6e3c88 commit 8c88ec6

File tree

3 files changed

+65
-24
lines changed

3 files changed

+65
-24
lines changed

webpack.common.js

+15-22
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
const webpack = require('webpack')
22
const path = require('path')
3-
const CopyPlugin = require('copy-webpack-plugin')
43
const HtmlWebpackPlugin = require('html-webpack-plugin')
5-
const WorkboxPlugin = require('workbox-webpack-plugin')
64
// https://webpack.js.org/guides/production/
75

86
const config = {
@@ -57,26 +55,21 @@ const config = {
5755
new webpack.NormalModuleReplacementPlugin(
5856
/prismarine-viewer[/|\\]viewer[/|\\]lib[/|\\]utils/,
5957
'./utils.web.js'
60-
),
61-
new WorkboxPlugin.GenerateSW({
62-
// these options encourage the ServiceWorkers to get in there fast
63-
// and not allow any straggling "old" SWs to hang around
64-
clientsClaim: true,
65-
skipWaiting: true,
66-
include: ['index.html', 'manifest.json'] // not caching a lot as anyway this works only online
67-
}),
68-
new CopyPlugin({
69-
patterns: [
70-
{ from: path.join(__dirname, '/styles.css'), to: './styles.css' },
71-
{ from: path.join(__dirname, '/node_modules/prismarine-viewer/public/blocksStates/'), to: './blocksStates/' },
72-
{ from: path.join(__dirname, '/node_modules/prismarine-viewer/public/textures/'), to: './textures/' },
73-
{ from: path.join(__dirname, '/node_modules/prismarine-viewer/public/worker.js'), to: './' },
74-
{ from: path.join(__dirname, '/node_modules/prismarine-viewer/public/supportedVersions.json'), to: './' },
75-
{ from: path.join(__dirname, 'assets/'), to: './' },
76-
{ from: path.join(__dirname, 'extra-textures/'), to: './extra-textures/' },
77-
{ from: path.join(__dirname, 'config.json'), to: './config.json' }
78-
]
79-
})
58+
)
59+
],
60+
// The directories that can be optionally symlinked
61+
[Symbol.for('webpack_directories')]: [
62+
{ from: path.join(__dirname, '/node_modules/prismarine-viewer/public/blocksStates/'), to: './blocksStates/' },
63+
{ from: path.join(__dirname, '/node_modules/prismarine-viewer/public/textures/'), to: './textures/' },
64+
{ from: path.join(__dirname, 'extra-textures/'), to: './extra-textures/' }
65+
],
66+
// The files that will be copied
67+
[Symbol.for('webpack_files')]: [
68+
{ from: path.join(__dirname, '/styles.css'), to: './styles.css' },
69+
{ from: path.join(__dirname, '/node_modules/prismarine-viewer/public/worker.js'), to: './' },
70+
{ from: path.join(__dirname, '/node_modules/prismarine-viewer/public/supportedVersions.json'), to: './' },
71+
{ from: path.join(__dirname, 'assets/'), to: './' },
72+
{ from: path.join(__dirname, 'config.json'), to: './config.json' }
8073
]
8174
}
8275

webpack.dev.js

+34-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,36 @@
11
const { merge } = require('webpack-merge')
22
const common = require('./webpack.common.js')
3+
const CopyPlugin = require('copy-webpack-plugin')
4+
const fs = require('fs')
35
const path = require('path')
46

7+
class SymlinkPlugin {
8+
constructor (options) {
9+
this.directories = options.directories ?? []
10+
}
11+
12+
apply (compiler) {
13+
compiler.hooks.afterEmit.tap(SymlinkPlugin.name, this.afterEmitHook.bind(this))
14+
}
15+
16+
afterEmitHook (compilation) {
17+
const dir = compilation.options.context
18+
const output = compilation.outputOptions.path
19+
for (const { from: _from, to: _to } of this.directories) {
20+
const to = path.resolve(output, _to)
21+
if (fs.existsSync(to)) {
22+
try {
23+
fs.unlinkSync(to)
24+
} catch (e) {
25+
continue
26+
}
27+
}
28+
const from = path.resolve(dir, _from)
29+
fs.symlinkSync(from, to, 'junction')
30+
}
31+
}
32+
}
33+
534
module.exports = merge(common, {
635
mode: 'development',
736
devtool: 'inline-source-map',
@@ -12,5 +41,9 @@ module.exports = merge(common, {
1241
inline: true,
1342
// open: true,
1443
hot: true
15-
}
44+
},
45+
plugins: [
46+
new CopyPlugin({ patterns: common[Symbol.for('webpack_files')] }),
47+
new SymlinkPlugin({ directories: common[Symbol.for('webpack_directories')] })
48+
]
1649
})

webpack.prod.js

+16-1
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,30 @@
11
const { merge } = require('webpack-merge')
22
const common = require('./webpack.common.js')
33

4+
const CopyPlugin = require('copy-webpack-plugin')
45
const LodashModuleReplacementPlugin = require('lodash-webpack-plugin')
56
const { CleanWebpackPlugin } = require('clean-webpack-plugin')
67
const webpack = require('webpack')
8+
const WorkboxPlugin = require('workbox-webpack-plugin')
79

810
module.exports = merge(common, {
911
mode: 'production',
1012
plugins: [
1113
new CleanWebpackPlugin(),
1214
new webpack.optimize.ModuleConcatenationPlugin(),
13-
new LodashModuleReplacementPlugin()
15+
new LodashModuleReplacementPlugin(),
16+
new CopyPlugin({
17+
patterns: [
18+
...common[Symbol.for('webpack_directories')],
19+
...common[Symbol.for('webpack_files')]
20+
]
21+
}),
22+
new WorkboxPlugin.GenerateSW({
23+
// these options encourage the ServiceWorkers to get in there fast
24+
// and not allow any straggling "old" SWs to hang around
25+
clientsClaim: true,
26+
skipWaiting: true,
27+
include: ['index.html', 'manifest.json'] // not caching a lot as anyway this works only online
28+
})
1429
]
1530
})

0 commit comments

Comments
 (0)