forked from mozilla/addons-frontend
-
Notifications
You must be signed in to change notification settings - Fork 0
/
webpack-common.js
147 lines (133 loc) · 4.02 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
/* 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 'amo/polyfill';
import { getClientConfig } from 'amo/utils';
export function getStyleRules({
bundleStylesWithJs = false,
_config = config,
} = {}) {
let styleRules;
const postCssPlugins = [];
if (_config.get('enablePostCssLoader')) {
postCssPlugins.push(autoprefixer());
}
const cssLoaderOptions = {
importLoaders: 2,
// This is needed to be backward compatible with css-loader v2.
esModule: false,
};
const postcssLoaderOptions = {
postcssOptions: {
plugins: postCssPlugins,
},
};
if (bundleStylesWithJs) {
// In development, we bundle styles with the JS.
styleRules = [
{
test: /\.(sc|c)ss$/,
use: [
{ loader: 'style-loader' },
{ loader: 'css-loader', options: cssLoaderOptions },
{ loader: 'postcss-loader', options: postcssLoaderOptions },
{ loader: 'sass-loader' },
],
},
];
} 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: cssLoaderOptions },
{ loader: 'postcss-loader', options: postcssLoaderOptions },
{ loader: 'sass-loader' },
],
},
];
}
return styleRules;
}
function getAssetRules() {
// Common options for URL loaders (i.e. derivatives of file-loader).
const urlLoaderOptions = {
encoding: 'base64',
// This has been added in url-loader 2.2.0 and file-loader 5.0.0. The
// default value (`true`) is a breaking change, so we have to set it to
// `false`.
esModule: false,
// Disable inlining of assets (It does more harm than good, we don't need
// it with HTTP/2).
limit: false,
// This is the default value.
fallback: 'file-loader',
// We want to use predictable filenames for "subset" fonts.
name(resourcePath) {
if (/-subset-/.test(resourcePath)) {
return '[name].[contenthash].[ext]';
}
return '[contenthash].[ext]';
},
};
return [
{
test: /\.(svg|jpg|png|gif|webm|mp4|otf|woff|woff2)$/,
use: [{ loader: 'url-loader', options: urlLoaderOptions }],
type: 'javascript/auto',
},
];
}
export function getRules({ babelOptions, bundleStylesWithJs = false } = {}) {
return [
{
test: /\.jsx?$/,
exclude: /node_modules/,
loader: 'babel-loader',
options: babelOptions,
},
...getStyleRules({ bundleStylesWithJs }),
...getAssetRules(),
];
}
export function getPlugins({ withBrowserWindow = true } = {}) {
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$/,
'amo/client/config.js',
),
new CircularDependencyPlugin({
exclude: /node_modules/,
failOnError: true,
}),
// This allow us to exclude locales for other apps being built.
new webpack.ContextReplacementPlugin(/locale$/, /^\.\/.*?\/amo\.js$/),
];
if (withBrowserWindow) {
plugins.push(
// This swaps the server side window object with a standard browser
// window.
new webpack.NormalModuleReplacementPlugin(
/amo\/window/,
'amo/browserWindow.js',
),
);
}
return plugins;
}