2020-11-23 08:07:07 +00:00
|
|
|
const path = require('path');
|
2021-06-09 19:18:03 +00:00
|
|
|
const { merge } = require('webpack-merge');
|
|
|
|
|
2021-06-16 22:01:43 +00:00
|
|
|
const TerserPlugin = require('terser-webpack-plugin');
|
2020-12-04 16:46:37 +00:00
|
|
|
const CopyWebpackPlugin = require('copy-webpack-plugin');
|
2021-06-09 19:18:03 +00:00
|
|
|
const HTMLWebpackPlugin = require('html-webpack-plugin');
|
|
|
|
const { CleanWebpackPlugin } = require('clean-webpack-plugin');
|
2020-11-23 08:07:07 +00:00
|
|
|
|
2021-06-09 19:18:03 +00:00
|
|
|
// Production CSS assets - separate, minimised file
|
|
|
|
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
|
|
|
|
const OptimizeCSSAssetsPlugin = require('optimize-css-assets-webpack-plugin');
|
2020-11-26 13:53:31 +00:00
|
|
|
|
2021-06-09 19:18:03 +00:00
|
|
|
var MODE = process.env.npm_lifecycle_event === 'prod' ? 'production' : 'development';
|
|
|
|
var withDebug = !process.env['npm_config_nodebug'] && MODE === 'development';
|
|
|
|
// this may help for Yarn users
|
|
|
|
// var withDebug = !npmParams.includes("--nodebug");
|
|
|
|
console.log('\x1b[36m%s\x1b[0m', `** elm-webpack-starter: mode "${MODE}", withDebug: ${withDebug}\n`);
|
|
|
|
|
|
|
|
var common = {
|
|
|
|
mode: MODE,
|
|
|
|
entry: './src/index.js',
|
|
|
|
output: {
|
|
|
|
path: path.join(__dirname, 'dist'),
|
|
|
|
publicPath: '/',
|
|
|
|
// FIXME webpack -p automatically adds hash when building for production
|
|
|
|
filename: MODE === 'production' ? '[name]-[hash].js' : 'index.js',
|
|
|
|
},
|
|
|
|
plugins: [
|
|
|
|
new HTMLWebpackPlugin({
|
|
|
|
// Use this template to get basic responsive meta tags
|
|
|
|
template: 'index.html',
|
|
|
|
// inject details of output file at end of body
|
|
|
|
inject: 'body',
|
|
|
|
}),
|
2021-06-16 22:12:07 +00:00
|
|
|
new CopyWebpackPlugin({
|
|
|
|
patterns: [
|
|
|
|
{
|
|
|
|
from: './images/*.*',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
from: 'favicon.ico',
|
|
|
|
},
|
2021-06-22 10:28:17 +00:00
|
|
|
{
|
|
|
|
from: './env/*.*',
|
|
|
|
},
|
2021-06-16 22:12:07 +00:00
|
|
|
],
|
|
|
|
}),
|
2021-06-09 19:18:03 +00:00
|
|
|
],
|
|
|
|
resolve: {
|
|
|
|
modules: [path.join(__dirname, 'src'), 'node_modules'],
|
|
|
|
extensions: ['.js', '.elm', '.scss', '.png'],
|
|
|
|
},
|
|
|
|
module: {
|
|
|
|
rules: [
|
|
|
|
{
|
|
|
|
test: /\.js$/,
|
|
|
|
enforce: 'pre',
|
|
|
|
use: ['source-map-loader'],
|
|
|
|
},
|
|
|
|
{
|
|
|
|
test: /\.js$/,
|
|
|
|
exclude: /node_modules/,
|
|
|
|
use: {
|
|
|
|
loader: 'babel-loader',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
test: /\.scss$/,
|
|
|
|
exclude: [/elm-stuff/],
|
|
|
|
// see https://github.com/webpack-contrib/css-loader#url
|
|
|
|
use: ['style-loader', 'css-loader?url=false', 'sass-loader'],
|
|
|
|
},
|
|
|
|
{
|
|
|
|
test: /\.css$/,
|
|
|
|
exclude: [/elm-stuff/],
|
|
|
|
use: ['style-loader', 'css-loader?url=false'],
|
|
|
|
},
|
|
|
|
{
|
|
|
|
test: /\.woff(2)?(\?v=[0-9]\.[0-9]\.[0-9])?$/,
|
|
|
|
exclude: [/elm-stuff/, /node_modules/],
|
|
|
|
use: {
|
|
|
|
loader: 'url-loader',
|
|
|
|
options: {
|
|
|
|
limit: 10000,
|
|
|
|
mimetype: 'application/font-woff',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
test: /\.(ttf|eot|svg)(\?v=[0-9]\.[0-9]\.[0-9])?$/,
|
|
|
|
exclude: [/elm-stuff/, /node_modules/],
|
|
|
|
loader: 'file-loader',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
test: /\.(jpe?g|png|gif|svg)$/i,
|
|
|
|
exclude: [/elm-stuff/, /node_modules/],
|
|
|
|
loader: 'file-loader',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
if (MODE === 'development') {
|
|
|
|
module.exports = merge(common, {
|
|
|
|
optimization: {
|
|
|
|
// Prevents compilation errors causing the hot loader to lose state
|
|
|
|
noEmitOnErrors: true,
|
2020-12-04 16:46:37 +00:00
|
|
|
},
|
|
|
|
module: {
|
|
|
|
rules: [
|
|
|
|
{
|
2021-06-09 19:18:03 +00:00
|
|
|
test: /\.elm$/,
|
2020-12-04 16:46:37 +00:00
|
|
|
exclude: [/elm-stuff/, /node_modules/],
|
|
|
|
use: [
|
|
|
|
{ loader: 'elm-hot-webpack-loader' },
|
|
|
|
{
|
|
|
|
loader: 'elm-webpack-loader',
|
|
|
|
options: {
|
2021-06-09 19:18:03 +00:00
|
|
|
// add Elm's debug overlay to output
|
|
|
|
debug: withDebug,
|
|
|
|
//
|
2022-01-26 11:30:29 +00:00
|
|
|
// forceWatch: true,
|
2020-12-04 16:46:37 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
2021-06-09 19:18:03 +00:00
|
|
|
devServer: {
|
|
|
|
inline: true,
|
|
|
|
stats: 'errors-only',
|
2021-12-28 18:15:01 +00:00
|
|
|
contentBase: path.join(__dirname, 'public'),
|
2021-06-09 19:18:03 +00:00
|
|
|
historyApiFallback: true,
|
|
|
|
// feel free to delete this section if you don't need anything like this
|
|
|
|
before(app) {
|
|
|
|
// on port 3000
|
|
|
|
app.get('/test', function (req, res) {
|
|
|
|
res.json({ result: 'OK' });
|
|
|
|
});
|
|
|
|
},
|
2020-12-04 16:46:37 +00:00
|
|
|
},
|
2021-06-09 19:18:03 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (MODE === 'production') {
|
|
|
|
module.exports = merge(common, {
|
2020-12-04 16:46:37 +00:00
|
|
|
optimization: {
|
2021-06-16 22:01:43 +00:00
|
|
|
minimize: true,
|
2021-06-09 19:18:03 +00:00
|
|
|
minimizer: [
|
2021-06-16 22:01:43 +00:00
|
|
|
new TerserPlugin({
|
|
|
|
parallel: true,
|
|
|
|
terserOptions: {
|
|
|
|
// https://github.com/webpack-contrib/terser-webpack-plugin#terseroptions
|
2021-06-09 19:18:03 +00:00
|
|
|
},
|
2021-06-16 22:01:43 +00:00
|
|
|
}),
|
2021-06-09 19:18:03 +00:00
|
|
|
new OptimizeCSSAssetsPlugin({}),
|
|
|
|
],
|
2020-12-04 16:46:37 +00:00
|
|
|
},
|
|
|
|
plugins: [
|
2021-06-09 19:18:03 +00:00
|
|
|
// Delete everything from output-path (/dist) and report to user
|
|
|
|
new CleanWebpackPlugin({
|
|
|
|
root: __dirname,
|
|
|
|
exclude: [],
|
|
|
|
verbose: true,
|
|
|
|
dry: false,
|
2020-12-04 16:46:37 +00:00
|
|
|
}),
|
2021-06-09 19:18:03 +00:00
|
|
|
new MiniCssExtractPlugin({
|
|
|
|
// Options similar to the same options in webpackOptions.output
|
|
|
|
// both options are optional
|
|
|
|
filename: '[name]-[hash].css',
|
2020-12-04 16:46:37 +00:00
|
|
|
}),
|
|
|
|
],
|
2021-06-09 19:18:03 +00:00
|
|
|
module: {
|
|
|
|
rules: [
|
|
|
|
{
|
|
|
|
test: /\.elm$/,
|
|
|
|
exclude: [/elm-stuff/, /node_modules/],
|
|
|
|
use: {
|
|
|
|
loader: 'elm-webpack-loader',
|
|
|
|
options: {
|
|
|
|
optimize: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|