Skip to content

Commit

Permalink
switch from htmlplugin to dotenv to divide dev environment and prod e…
Browse files Browse the repository at this point in the history
…nvironment
  • Loading branch information
pengyin-shan committed Dec 20, 2023
1 parent ce8d347 commit f4a45e6
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 17 deletions.
58 changes: 58 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
"clean-webpack-plugin": "^3.0.0",
"css-loader": "^5.2.4",
"css-minimizer-webpack-plugin": "^2.0.0",
"dotenv-webpack": "^8.0.1",
"eslint": "^7.26.0",
"eslint-config-airbnb": "^18.2.1",
"eslint-config-prettier": "^8.3.0",
Expand Down
4 changes: 2 additions & 2 deletions src/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<script async src="<%= htmlWebpackPlugin.options.GA_URL %>"></script>
<script async src="<%= process.env.GA_URL %>"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
var url = "<%= htmlWebpackPlugin.options.GA_URL %>";
var url = "<%= process.env.GA_URL %>";
var regex = /id=(.*)/;
var id = url.match(regex)[1];
gtag('config', id);
Expand Down
9 changes: 7 additions & 2 deletions webpack.common.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ const path = require('path');
const Webpack = require('webpack');
const { CleanWebpackPlugin } = require('clean-webpack-plugin');
const FaviconsWebpackPlugin = require('favicons-webpack-plugin');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const Dotenv = require('dotenv-webpack');

module.exports = {
target: 'web',
Expand Down Expand Up @@ -78,7 +80,9 @@ module.exports = {
},

plugins: [

new HtmlWebpackPlugin({
template: 'src/index.html'
}),
new Webpack.DefinePlugin({
"process.env": {
"APP_ENV": JSON.stringify(process.env.APP_ENV)
Expand All @@ -104,6 +108,7 @@ module.exports = {
}
}),
new MiniCssExtractPlugin({ filename: 'css/[name]-[fullhash].css' }),
new CleanWebpackPlugin()
new CleanWebpackPlugin(),
new Dotenv(),
]
};
7 changes: 1 addition & 6 deletions webpack.dev.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
const Webpack = require('webpack');
const { merge } = require('webpack-merge');
const { BundleAnalyzerPlugin } = require('webpack-bundle-analyzer');
const HtmlWebpackPlugin = require('html-webpack-plugin');

const commonConfig = require('./webpack.common');

Expand Down Expand Up @@ -43,11 +42,7 @@ module.exports = merge(commonConfig, {

plugins: [
new Webpack.DefinePlugin({
ENV: JSON.stringify('development'),
}),
new HtmlWebpackPlugin({
template: './src/index.html',
GA_URL: 'https://www.googletagmanager.com/gtag/js?id=G-K4MLHWSVVT',
ENV: JSON.stringify('development')
}),
new BundleAnalyzerPlugin({ openAnalyzer: false, analyzerPort: 8081 })
],
Expand Down
9 changes: 2 additions & 7 deletions webpack.prod.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const Webpack = require('webpack');
const { merge } = require('webpack-merge');
const HtmlWebpackPlugin = require('html-webpack-plugin');

const commonConfig = require('./webpack.common');

module.exports = merge(commonConfig, {
Expand Down Expand Up @@ -33,13 +33,8 @@ module.exports = merge(commonConfig, {
},

plugins: [
new HtmlWebpackPlugin({
template: 'src/index.html',
GA_URL: 'https://www.googletagmanager.com/gtag/js?id=G-GFR8PTXMDM',
}),
new Webpack.DefinePlugin({
ENV: JSON.stringify('production'),

ENV: JSON.stringify('production')
})
]
});

0 comments on commit f4a45e6

Please sign in to comment.