forked from birchill/10ten-ja-reader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
webpack.config.js
144 lines (133 loc) · 3.25 KB
/
webpack.config.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
const path = require('path');
const CopyWebpackPlugin = require('copy-webpack-plugin');
const WebExtWebpackPlugin = require('web-ext-webpack-plugin');
// Look for a --firefox <path> argument
const firefoxIndex = process.argv.indexOf('--firefox');
const firefox =
firefoxIndex !== -1 && firefoxIndex < process.argv.length - 1
? process.argv[firefoxIndex + 1]
: undefined;
// Likewise for firefoxProfile
const firefoxProfileIndex = process.argv.indexOf('--firefoxProfile');
const firefoxProfile =
firefoxProfileIndex !== -1 && firefoxProfileIndex < process.argv.length - 1
? process.argv[firefoxProfileIndex + 1]
: undefined;
const commonConfig = {
// No need for uglification etc.
mode: 'development',
devtool: 'source-map',
module: {
rules: [
{
test: /\.ts$/,
use: 'ts-loader',
exclude: /node_modules/,
},
{ enforce: 'pre', test: /\.js$/, loader: 'source-map-loader' },
],
},
resolve: {
extensions: ['.ts', '.js'],
},
};
const commonExtConfig = {
...commonConfig,
entry: {
'rikaichamp-content': './src/content.ts',
'rikaichamp-background': './src/background.ts',
'rikaichamp-options': './src/options.ts',
},
};
const getPreprocessorConfig = (...features) => ({
test: /\.src$/,
use: [
{
loader: 'file-loader',
options: {
name: '[name]',
},
},
{
loader:
'webpack-preprocessor?' +
features.map(feature => `definitions[]=${feature}`).join(','),
},
],
});
const extendArray = (array, ...newElems) => {
const result = array.slice();
result.push(...newElems);
return result;
};
const firefoxConfig = {
...commonExtConfig,
module: {
...commonExtConfig.module,
rules: extendArray(
commonExtConfig.module.rules,
getPreprocessorConfig(
'supports_svg_icons',
'supports_browser_style',
'supports_applications_field'
)
),
},
output: {
path: path.resolve(__dirname, 'dist-firefox'),
filename: '[name].js',
},
plugins: [
new CopyWebpackPlugin(['css/*', 'images/*', 'data/*', '_locales/**/*']),
new WebExtWebpackPlugin({
firefox,
firefoxProfile,
browserConsole: true,
startUrl: ['tests/playground.html'],
sourceDir: path.resolve(__dirname, 'dist-firefox'),
}),
],
};
const chromeConfig = {
...commonExtConfig,
module: {
...commonExtConfig.module,
rules: extendArray(
commonExtConfig.module.rules,
getPreprocessorConfig('use_polyfill')
),
},
output: {
path: path.resolve(__dirname, 'dist-chrome'),
filename: '[name].js',
},
plugins: [
new CopyWebpackPlugin([
'css/*',
'images/*',
'data/*',
'_locales/**/*',
{ from: 'lib/browser-polyfill.js*', to: '[name].[ext]' },
]),
],
};
const testConfig = {
...commonConfig,
name: 'tests',
entry: {
'content-loader': './tests/content-loader.ts',
},
output: {
path: path.resolve(__dirname, 'tests'),
filename: '[name].js',
},
};
module.exports = (env, argv) => {
let configs = [testConfig];
if (env && env.target === 'chrome') {
configs.push({ ...chromeConfig, name: 'extension' });
} else {
configs.push({ ...firefoxConfig, name: 'extension' });
}
return configs;
};