speed webpack builds

This commit is contained in:
Vitaly 2023-08-09 18:36:27 +03:00
parent a31b1701bb
commit a67d05ae99
5 changed files with 54 additions and 27 deletions

20
build.js Normal file
View file

@ -0,0 +1,20 @@
const fsExtra = require('fs-extra')
exports.copyFiles = () => {
fsExtra.copySync('node_modules/prismarine-viewer/public/blocksStates/', 'public/blocksStates/')
fsExtra.copySync('node_modules/prismarine-viewer/public/textures/', 'public/textures/')
fsExtra.copySync('node_modules/prismarine-viewer/public/worker.js', 'public/worker.js')
fsExtra.copySync('node_modules/prismarine-viewer/public/supportedVersions.json', 'public/supportedVersions.json')
fsExtra.copySync('assets/', 'public/')
fsExtra.copySync('extra-textures/', 'public/extra-textures/')
fsExtra.copySync('config.json', 'public/config.json')
}
exports.copyFilesDev = () => {
if (fsExtra.existsSync('public/config.json')) return
exports.copyFiles()
}
const fn = exports[process.argv[2]]
if (fn) fn()

View file

@ -4,8 +4,8 @@
"description": "A minecraft client running in a browser",
"main": "index.js",
"scripts": {
"build": "webpack --config webpack.prod.js",
"build-dev": "webpack --config webpack.dev.js",
"build": "node build.js copyFiles && webpack --config webpack.prod.js",
"build-dev": "node build.js copyFilesDev && webpack serve --config webpack.dev.js --progress",
"start": "node --max-old-space-size=8192 server.js 8080 dev",
"prod-start": "node server.js",
"build-dev-start": "npm run build-dev && npm run prod-start",

View file

@ -2,7 +2,6 @@ const webpack = require('webpack')
const path = require('path')
const CopyPlugin = require('copy-webpack-plugin')
const HtmlWebpackPlugin = require('html-webpack-plugin')
const WorkboxPlugin = require('workbox-webpack-plugin')
// https://webpack.js.org/guides/production/
const config = {
@ -61,25 +60,18 @@ const config = {
/prismarine-viewer[/|\\]viewer[/|\\]lib[/|\\]utils/,
'./utils.web.js'
),
new WorkboxPlugin.GenerateSW({
// these options encourage the ServiceWorkers to get in there fast
// and not allow any straggling "old" SWs to hang around
clientsClaim: true,
skipWaiting: true,
include: ['index.html', 'manifest.json'] // not caching a lot as anyway this works only online
}),
new CopyPlugin({
patterns: [
{ from: path.join(__dirname, '/styles.css'), to: './styles.css' },
{ from: path.join(__dirname, '/node_modules/prismarine-viewer/public/blocksStates/'), to: './blocksStates/' },
{ from: path.join(__dirname, '/node_modules/prismarine-viewer/public/textures/'), to: './textures/' },
{ from: path.join(__dirname, '/node_modules/prismarine-viewer/public/worker.js'), to: './' },
{ from: path.join(__dirname, '/node_modules/prismarine-viewer/public/supportedVersions.json'), to: './' },
{ from: path.join(__dirname, 'assets/'), to: './' },
{ from: path.join(__dirname, 'extra-textures/'), to: './extra-textures/' },
{ from: path.join(__dirname, 'config.json'), to: './config.json' }
]
})
// new CopyPlugin({
// patterns: [
// { from: path.join(__dirname, '/styles.css'), to: './styles.css' },
// { from: path.join(__dirname, '/node_modules/prismarine-viewer/public/blocksStates/'), to: './blocksStates/' },
// { from: path.join(__dirname, '/node_modules/prismarine-viewer/public/textures/'), to: './textures/' },
// { from: path.join(__dirname, '/node_modules/prismarine-viewer/public/worker.js'), to: './' },
// { from: path.join(__dirname, '/node_modules/prismarine-viewer/public/supportedVersions.json'), to: './' },
// { from: path.join(__dirname, 'assets/'), to: './' },
// { from: path.join(__dirname, 'extra-textures/'), to: './extra-textures/' },
// { from: path.join(__dirname, 'config.json'), to: './config.json' }
// ]
// })
]
}

View file

@ -2,15 +2,22 @@ const { merge } = require('webpack-merge')
const common = require('./webpack.common.js')
const path = require('path')
module.exports = merge(common, {
/** @type {import('webpack-dev-server').Configuration['rel']} */
module.exports = merge(common,
/** @type {import('webpack').Configuration} */
{
mode: 'development',
devtool: 'inline-source-map',
cache: true,
devServer: {
contentBase: path.resolve(__dirname, './public'),
// contentBase: path.resolve(__dirname, './public'),
compress: true,
inline: true,
// inline: true,
// open: true,
hot: true
hot: true,
// liveReload: true,
devMiddleware: {
writeToDisk: true,
},
}
})

View file

@ -3,6 +3,7 @@ const common = require('./webpack.common.js')
const LodashModuleReplacementPlugin = require('lodash-webpack-plugin')
const { CleanWebpackPlugin } = require('clean-webpack-plugin')
const WorkboxPlugin = require('workbox-webpack-plugin')
const webpack = require('webpack')
module.exports = merge(common, {
@ -10,6 +11,13 @@ module.exports = merge(common, {
plugins: [
new CleanWebpackPlugin(),
new webpack.optimize.ModuleConcatenationPlugin(),
new LodashModuleReplacementPlugin()
new LodashModuleReplacementPlugin(),
new WorkboxPlugin.GenerateSW({
// these options encourage the ServiceWorkers to get in there fast
// and not allow any straggling "old" SWs to hang around
clientsClaim: true,
skipWaiting: true,
include: ['index.html', 'manifest.json'] // not caching a lot as anyway this works only online
}),
]
})