forked from mozilla/addons-frontend
-
Notifications
You must be signed in to change notification settings - Fork 1
/
webpack-common.js
180 lines (168 loc) · 4.65 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
178
179
180
/* eslint-disable import/no-extraneous-dependencies */
import autoprefixer from 'autoprefixer';
import CircularDependencyPlugin from 'circular-dependency-plugin';
import config from 'config';
import ExtractTextPlugin from 'extract-text-webpack-plugin';
import webpack from 'webpack';
import 'core/polyfill';
import { getClientConfig } from 'core/utils';
// 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,
};
const postCssPlugins = [];
if (config.get('enablePostCssLoader')) {
postCssPlugins.push(
autoprefixer({
browsers: ['last 2 versions'],
grid: false,
}),
);
}
export function getRules({ babelOptions, bundleStylesWithJs = false } = {}) {
let styleRules;
if (bundleStylesWithJs) {
// In development, we bundle styles with the JS.
styleRules = [
{
test: /\.css$/,
use: [
{ loader: 'style-loader' },
{ loader: 'css-loader', options: { importLoaders: 2 } },
{
loader: 'postcss-loader',
options: {
plugins: postCssPlugins,
},
},
],
},
{
test: /\.scss$/,
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.
const cssLoaderOptions = {
importLoaders: 2,
sourceMap: true,
minimize: true,
};
styleRules = [
{
test: /\.css$/,
loader: ExtractTextPlugin.extract({
fallback: 'style-loader',
use: [
{
loader: 'css-loader',
options: cssLoaderOptions,
},
{
loader: 'postcss-loader',
options: {
plugins: postCssPlugins,
sourceMap: true,
},
},
],
}),
},
{
test: /\.scss$/,
loader: ExtractTextPlugin.extract({
fallback: 'style-loader',
use: [
{
loader: 'css-loader',
options: cssLoaderOptions,
},
{
loader: 'postcss-loader',
options: {
plugins: postCssPlugins,
},
},
{
loader: 'sass-loader',
options: {
outputStyle: 'expanded',
sourceMap: true,
sourceMapContents: true,
},
},
],
}),
},
];
}
return [
{
test: /\.jsx?$/,
exclude: /node_modules/,
loader: 'babel-loader',
options: babelOptions,
},
...styleRules,
{
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 getPlugins({ excludeOtherAppLocales = true } = {}) {
const appName = config.get('appName');
const clientConfig = getClientConfig(config);
const plugins = [
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;
}