forked from mozilla/addons-frontend
-
Notifications
You must be signed in to change notification settings - Fork 0
/
webpack-common.js
160 lines (148 loc) · 4.31 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
/* eslint-disable import/no-extraneous-dependencies */
import autoprefixer from 'autoprefixer';
import CircularDependencyPlugin from 'circular-dependency-plugin';
import config from 'config';
import MiniCssExtractPlugin from 'mini-css-extract-plugin';
import webpack from 'webpack';
import LoadablePlugin from '@loadable/webpack-plugin';
import 'core/polyfill';
import { getClientConfig } from 'core/utils';
export function getStyleRules({
bundleStylesWithJs = false,
_config = config,
} = {}) {
let styleRules;
const postCssPlugins = [];
if (_config.get('enablePostCssLoader')) {
postCssPlugins.push(
autoprefixer({
grid: false,
}),
);
}
if (bundleStylesWithJs) {
// In development, we bundle styles with the JS.
styleRules = [
{
test: /\.(sc|c)ss$/,
use: [
{ loader: 'style-loader' },
{ loader: 'css-loader', options: { importLoaders: 2 } },
{
loader: 'postcss-loader',
options: {
plugins: postCssPlugins,
},
},
{ loader: 'sass-loader', options: { outputStyle: 'expanded' } },
],
},
];
} else {
// In production, we create a separate CSS bundle rather than include
// styles with the JS bundle. This lets the style bundle load in parallel.
styleRules = [
{
test: /\.(sc|c)ss$/,
use: [
MiniCssExtractPlugin.loader,
{
loader: 'css-loader',
options: {
importLoaders: 2,
sourceMap: true,
},
},
{
loader: 'postcss-loader',
options: {
plugins: postCssPlugins,
sourceMap: true,
},
},
{
loader: 'sass-loader',
options: {
outputStyle: 'expanded',
sourceMap: true,
sourceMapContents: true,
},
},
],
},
];
}
return styleRules;
}
export function getAssetRules() {
// Common options for URL loaders (i.e. derivatives of file-loader).
const urlLoaderOptions = {
// If a media file is less than this size in bytes, it will be linked as a
// data: URL. Otherwise it will be linked as a separate file URL.
limit: 10000,
};
return [
{
test: /\.svg$/,
use: [{ loader: 'svg-url-loader', options: urlLoaderOptions }],
},
{
test: /\.(jpg|png|gif|webm|mp4|otf|woff|woff2)$/,
use: [{ loader: 'url-loader', options: urlLoaderOptions }],
},
];
}
export function getRules({ babelOptions, bundleStylesWithJs = false } = {}) {
return [
{
test: /\.jsx?$/,
exclude: /node_modules/,
loader: 'babel-loader',
options: babelOptions,
},
...getStyleRules({ bundleStylesWithJs }),
...getAssetRules(),
];
}
export function getPlugins({ excludeOtherAppLocales = true } = {}) {
const appName = config.get('appName');
const clientConfig = getClientConfig(config);
const plugins = [
// We need this file to be written on disk so that our server code can read
// it. In development mode, webpack usually serves the file from memory but
// that's not what we want for this file.
new LoadablePlugin({ writeToDisk: true }),
new webpack.DefinePlugin({
CLIENT_CONFIG: JSON.stringify(clientConfig),
'process.env.NODE_ENV': JSON.stringify('production'),
}),
// Since the NodeJS code does not run from a webpack bundle, here
// are a few replacements that affect only the client side bundle.
//
// This replaces the config with a new module that has sensitive,
// server-only keys removed.
new webpack.NormalModuleReplacementPlugin(
/config$/,
'core/client/config.js',
),
// This swaps the server side window object with a standard browser window.
new webpack.NormalModuleReplacementPlugin(
/core\/window/,
'core/browserWindow.js',
),
new CircularDependencyPlugin({
exclude: /node_modules/,
failOnError: true,
}),
];
if (excludeOtherAppLocales) {
plugins.push(
// This allow us to exclude locales for other apps being built.
new webpack.ContextReplacementPlugin(
/locale$/,
new RegExp(`^\\.\\/.*?\\/${appName}\\.js$`),
),
);
}
return plugins;
}