This repository was archived by the owner on Apr 29, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathwebpack.common.js
More file actions
executable file
·151 lines (137 loc) · 4.21 KB
/
webpack.common.js
File metadata and controls
executable file
·151 lines (137 loc) · 4.21 KB
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
const path = require("path");
// removes old bundle fileds
const { CleanWebpackPlugin } = require("clean-webpack-plugin");
// creates index html file for entry, useful when scripts have hashed names
const HtmlWebpackPlugin = require("html-webpack-plugin");
// extract css out of js files
const CssExtractPlugin = require("extract-css-chunks-webpack-plugin");
// dev environment identifier
const is_dev = process.env.NODE_ENV === "development";
// regex to test for modules, loaderUtils is part of webpack dependencies
const cssModuleRegex = new RegExp(/\.module\.(less|css)$/);
const loaderUtils = require("loader-utils");
console.log("Node Environment", process.env.NODE_ENV);
module.exports = {
entry: {
app: "./src/index.js",
},
output: {
path: __dirname + "/dist",
publicPath: "",
filename: "[name].js",
},
resolve: {
extensions: [".js", ".jsx", ".ts", ".tsx", ".json"],
alias: {
"@components": path.resolve(__dirname, "./src/components/"),
"@utils": path.resolve(__dirname, "./src/utils/"),
"@images": path.resolve(__dirname, "./src/images/"),
"@store": path.resolve(__dirname, "./src/store/"),
"@api": path.resolve(__dirname, "./src/api/"),
"@mocks": path.resolve(__dirname, "./src/__mocks__/"),
},
},
module: {
rules: [
// support for ES6 and linting
{
test: /\.(js|ts)x?$/,
exclude: /node_modules/,
use: ["babel-loader", "eslint-loader"], // babel for ES6 syntax and eslint for js best practices
},
// parse and extract all less/css files
{
test: /\.(less|css)$/,
use: [
{
loader: CssExtractPlugin.loader,
options: { hot: is_dev, reloadAll: is_dev },
},
{
loader: "css-loader",
options: {
modules: {
localIdentName: "[local]___[hash:base64:5]",
getLocalIdent: getLocalIdent,
},
localsConvention: "camelCaseOnly",
importLoaders: 2,
},
},
"postcss-loader",
{
loader: "less-loader",
options: {
lessOptions: {
javascriptEnabled: true,
paths: [
path.resolve(__dirname, "./src/styles"),
path.resolve(__dirname, "node_modules"),
],
},
},
},
],
},
{
test: /\.(png|jpe?g|gif|webp)$/i,
use: [
{
loader: "file-loader",
},
],
},
{
test: /\.(ttf)$/i,
use: [
{
loader: "file-loader",
options: {
name: "[name].[ext]",
outputPath: "static/fonts/",
mimetype: "application/font-woff",
publicPath: "../fonts/",
},
},
],
},
],
},
plugins: [
new CleanWebpackPlugin(),
new HtmlWebpackPlugin({
template: "./src/index.html",
}),
new CssExtractPlugin({
filename: "static/css/[name].[contenthash:5].css",
chunkFilename: "static/css/vendors.[contenthash:5].css",
}),
],
};
// this is a copy of the default function, modified slightly to achieve our goal
// more here: https://stackoverflow.com/a/58190532/1878459
function getLocalIdent(loaderContext, localIdentName, localName, options) {
// return original name if it's a global file
if (!cssModuleRegex.test(loaderContext.resourcePath)) {
return localName;
}
if (!options.context) {
// eslint-disable-next-line no-param-reassign
options.context = loaderContext.rootContext;
}
const request = path
.relative(options.context, loaderContext.resourcePath)
.replace(/\\/g, "/");
// eslint-disable-next-line no-param-reassign
options.content = `${options.hashPrefix + request}+${localName}`;
// eslint-disable-next-line no-param-reassign
localIdentName = localIdentName.replace(/\[local\]/gi, localName);
const hash = loaderUtils.interpolateName(
loaderContext,
localIdentName,
options
);
return hash
.replace(new RegExp("[^a-zA-Z0-9\\-_\u00A0-\uFFFF]", "g"), "-")
.replace(/^((-?[0-9])|--)/, "_$1");
}