justlog/web/webpack.config.babel.js

80 lines
1.8 KiB
JavaScript
Raw Normal View History

2020-01-25 14:22:27 +01:00
const path = require("path");
2020-01-26 18:30:45 +01:00
const webpack = require("webpack");
2020-08-30 11:42:09 +02:00
const fs = require("fs");
2020-01-31 23:05:56 +01:00
const PnpWebpackPlugin = require(`pnp-webpack-plugin`);
2020-01-25 14:22:27 +01:00
2020-08-30 11:42:09 +02:00
module.exports = (env, options) => {
const plugins = [
2020-01-26 18:30:45 +01:00
new webpack.DefinePlugin({
'process.env': {
'apiBaseUrl': options.mode === 'development' ? '"http://localhost:8025"' : '""',
}
}),
2020-08-30 11:42:09 +02:00
];
if (options.mode === "production") {
plugins.push(new HashApplier());
}
return {
entry: './src/index.jsx',
output: {
2020-08-30 12:04:29 +02:00
path: path.resolve(__dirname, 'public', 'assets'),
2020-08-30 11:42:09 +02:00
filename: options.mode === "production" ? 'bundle.[hash].js' : "bundle.js",
publicPath: "/",
},
module: {
rules: [
{
test: /\.jsx$/,
exclude: /node_modules/,
use: {
loader: "babel-loader"
}
},
{
test: /\.s?css$/,
use: ["style-loader", "css-loader", "sass-loader"]
},
]
},
resolve: {
extensions: ['.js', '.jsx'],
plugins: [
PnpWebpackPlugin,
],
},
plugins: plugins,
resolveLoader: {
plugins: [
PnpWebpackPlugin.moduleLoader(module),
],
},
stats: {
// Config for minimal console.log mess.
assets: false,
colors: true,
version: false,
hash: false,
timings: true,
chunks: false,
chunkModules: false,
entrypoints: false,
modules: false,
}
}
};
class HashApplier {
apply(compiler) {
compiler.hooks.done.tap('hash-applier', data => {
const fileContents = fs.readFileSync(__dirname + "/public/index.html", "utf8");
const newFileContents = fileContents.replace(
/<!-- webpack-bundle-start -->(.*)?<!-- webpack-bundle-end -->/,
2020-08-30 12:04:29 +02:00
`<!-- webpack-bundle-start --><script src="/assets/bundle.${data.hash}.js"></script><!-- webpack-bundle-end -->`
2020-08-30 11:42:09 +02:00
);
fs.writeFileSync(__dirname + "/public/index.html", newFileContents);
});
2020-02-07 21:10:22 +01:00
}
2020-08-30 11:42:09 +02:00
}