-
Notifications
You must be signed in to change notification settings - Fork 0
/
webpack.config.js
110 lines (102 loc) · 3 KB
/
webpack.config.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
const webpack = require('webpack');
const path = require('path');
const CopyPlugin = require('copy-webpack-plugin');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin;
const { CleanWebpackPlugin } = require('clean-webpack-plugin');
require('dotenv').config();
// const { argv, env } = require('process');
// require("regenerator-runtime");
// console.log(`mode: ${argv.env}`);
// Try the environment variable, otherwise use root
// const ASSET_PATH = process.env.ASSET_PATH || './public';
module.exports = (_env, argv) => {
// console.log('params', [env, argv, process.env]);
const env = process.env;
const config = {
entry: './src/index.js',
output: {
path: path.resolve(__dirname, 'dist'),
// publicPath: ASSET_PATH,
filename: '[name].[contenthash].js',
},
devServer: {
contentBase: './dist',
writeToDisk: true,
},
module: {
rules: [
{
test: /\.js$/,
use: 'babel-loader',
exclude: /node_modules/
},
{
test: /\.scss$/,
use: [
'style-loader',
'css-loader',
'sass-loader'
]
},
{
test: /\.(png|svg|jpg|jpeg|gif)$/i,
type: 'asset/resource',
},
]
},
plugins: [
new CopyPlugin({
patterns: [{ from: 'assets/models', to: 'assets/models' }],
}),
new HtmlWebpackPlugin({
// appMountId: 'app',
title: argv.mode === 'production' ? 'PWTweetAR' : 'PWTweetAR Dev',
template: 'src/index.html',
inject: false,
templateParameters: {
'ga': argv.mode === 'production' && env && env.GA && env.GA.length > 0 ? `
<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=${env.GA}"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', '${env.GA}');
</script>
` : '',
},
}),
new BundleAnalyzerPlugin({
analyzerMode: 'static',
openAnalyzer: false,
}),
new webpack.ContextReplacementPlugin(/moment[\/\\]locale$/, /en/)
]
};
if(argv.mode === 'development') {
config.devtool = 'inline-source-map';
Object.assign(config, {
devtool: 'inline-source-map',
});
}
if(argv.mode === 'production') {
Object.assign(config, {
optimization: {
runtimeChunk: true, // 'single',
splitChunks: {
cacheGroups: {
vendor: {
test: /[\\/]node_modules[\\/]/,
name: 'vendors',
chunks: 'all'
}
}
}
},
});
config.plugins.push(new CleanWebpackPlugin());
}
return config;
};
// module.exports = config;