Skip to content

Instantly share code, notes, and snippets.

@fvilches17
Last active May 14, 2019 09:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fvilches17/181155bcbc1662daaef0c51f9d0a666b to your computer and use it in GitHub Desktop.
Save fvilches17/181155bcbc1662daaef0c51f9d0a666b to your computer and use it in GitHub Desktop.
const path = require('path');
const CleanWebpackPlugin = require('clean-webpack-plugin');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const WasmPackPlugin = require('@wasm-tool/wasm-pack-plugin');
function loadOutput(environment) {
const filename = environment.production ? 'scripts/[name].[hash].min.js' : 'scripts/[name].js';
const chunkFilename = environment.production ? '[name].[hash].min.js' : '[name].js';
return {
path: path.resolve(__dirname, 'dist'),
filename,
chunkFilename,
publicPath: '/'
};
};
function loadPlugins(environment) {
let wasmPackArgs = '--no-typescript';
if (environment.production) wasmPackArgs += ' --release';
return [
new HtmlWebpackPlugin({
template: './src/index.html',
chunks: ['app'],
hash: true
}),
new WasmPackPlugin({
crateDirectory: path.resolve(__dirname, `./src/hello_world`),
extraArgs: wasmPackArgs
}),
new CleanWebpackPlugin()
];
};
module.exports = environment => {
const mode = environment.production ? 'production' : 'development';
const entry = { app: './src/app.js' };
const output = loadOutput(environment);
const plugins = loadPlugins(environment);
//Base Config
const config = { mode, entry, output, plugins };
//Additional Production Config
if (environment.production) {
config.devtool = 'source-map';
}
//Additional Development Config
else {
config.devServer = {
contentBase: path.join(__dirname, 'dist'),
open: true,
overlay: { errors: true, warnings: false },
port: 5000,
watchContentBase: true
};
}
return config;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment