This repository has been archived by the owner on Feb 6, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
webpack.common.js
177 lines (162 loc) · 4.72 KB
/
webpack.common.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
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
const paths = require('./paths');
const autoprefixer = require('autoprefixer');
const fontRules = [
{
test: /\.(woff|woff2)(\?v=\d+\.\d+\.\d+)?$/,
loader: require.resolve('url-loader'),
options:{
limit:10000,
mimetype:'application/font-woff'
}
},
{
test: /\.ttf(\?v=\d+\.\d+\.\d+)?$/,
loader: require.resolve('url-loader'),
options:{
limit:10000,
mimetype:'application/octet-stream'
}
},
{
test: /\.eot(\?v=\d+\.\d+\.\d+)?$/,
loader: require.resolve('file-loader')
}
]
// "url" loader works like "file" loader except that it embeds assets
// smaller than specified limit in bytes as data URLs to avoid requests.
// A missing `test` is equivalent to a match.
const imageRule = {
test: [/\.bmp$/, /\.gif$/, /\.jpe?g$/, /\.png$/],
loader: require.resolve('url-loader'),
options: {
limit: 10000,
name: 'static/media/[name].[hash:8].[ext]',
},
};
// Use the SVG Loader to load svg files as react components
const svgReactRule = {
test: /\.svg$/,
use: [
"babel-loader",
{
loader: require.resolve('react-svg-loader'), // 'react-svg'
query: {
svgo: {
pretty: true,
plugins: [{ removeStyleElement: true }]
}
}
}
]
};
// Use the SVG Loader to load svg files as urls
const svgUrlRule = {
test: /\.svg$/,
resourceQuery: /url/,
loader: require.resolve('url-loader'),
options: {
limit: 10000,
name: 'static/media/[name].[hash:8].[ext]',
}
}
const typescriptRule = {
test: /\.(ts|tsx)$/,
include: paths.appSrc,
loader: require.resolve('ts-loader'),
}
const postCssRule = {
loader: require.resolve('postcss-loader'),
options: {
// Necessary for external CSS imports to work
// https://github.com/facebookincubator/create-react-app/issues/2677
ident: 'postcss',
plugins: () => [
require('postcss-flexbugs-fixes'),
autoprefixer({
browsers: [
'>1%',
'last 4 versions',
'Firefox ESR',
'not ie < 9', // React doesn't support IE8 anyway
],
flexbox: 'no-2009',
}),
],
},
}
// "file" loader makes sure those assets get served by WebpackDevServer.
// When you `import` an asset, you get its (virtual) filename.
// In production, they would get copied to the `build` folder.
// This loader don't uses a "test" so it will catch all modules
// that fall through the other loaders.
const fileLoaderRule = {
// Exclude `js` files to keep "css" loader working as it injects
// it's runtime that would otherwise processed through "file" loader.
// Also exclude `html` and `json` extensions so they get processed
// by webpacks internal loaders.
exclude: [/\.js$/, /\.html$/, /\.json$/],
loader: require.resolve('file-loader'),
options: {
name: 'static/media/[name].[hash:8].[ext]'
}
}
const tsLintRule = {
test: /\.(ts|tsx)$/,
loader: require.resolve('tslint-loader'),
enforce: 'pre',
include: paths.appSrc,
}
const sourceMapRule = {
test: /\.js$/,
loader: require.resolve('source-map-loader'),
enforce: 'pre',
include: paths.appSrc,
}
const defaultOneOfRules = [
imageRule,
svgUrlRule,
svgReactRule,
typescriptRule,
...fontRules
]
/**
* function to get the common OneOfRules. Passes the default rules
* to a delegate that allows consumer to re-order insert and mutate
* oneOf rules before being handed back. The fileLoaderRule is automatically
* added to the bottom of the array that is handed back to catch everything
* not handled by a rule.
* @param {*} [rulesDelegate=(r) => r]
* @returns
*/
function getOneOfRules(rulesDelegate = (r) => r) {
const oneOfRules = rulesDelegate([...defaultOneOfRules]);
return {
oneOf:[
...oneOfRules,
fileLoaderRule
]
}
}
const mainRules = [
// TODO: Disable require.ensure as it's not a standard language feature.
// We are waiting for https://github.com/facebookincubator/create-react-app/issues/2176.
// { parser: { requireEnsure: false } },
// First, run the linter.
// It's important to do this before Babel processes the JS.
tsLintRule,
sourceMapRule
]
module.exports = {
mainRules,
getOneOfRules,
rules: {
postCssRule,
imageRule,
svgReactRule,
svgUrlRule,
typescriptRule,
sourceMapRule,
tsLintRule,
fileLoaderRule
}
}