This repository has been archived by the owner on Sep 22, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
webpack.config.common.js
210 lines (196 loc) · 8 KB
/
webpack.config.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
const path = require('path');
const merge = require('webpack-merge');
const CopyWebpackPlugin = require('copy-webpack-plugin');
const RemovePlugin = require('remove-files-webpack-plugin');
const ExtractTextPlugin = require('extract-text-webpack-plugin');
const HTMLWebpackPlugin = require('html-webpack-plugin');
const {TsConfigPathsPlugin} = require('awesome-typescript-loader');
//#region Custom plugins.
class CreateManifestPlugin {
constructor(options) {
this.pluginName = 'create-manifest-plugin';
this.commonManifestPath = options.commonManifestPath;
this.platformManifestPath = options.platformManifestPath;
this.resultManifestPath = options.resultManifestPath;
}
apply(compiler) {
/**
* webpack 4+ comes with a new plugin system.
* Check for hooks in-order to support old plugin system.
*/
if (compiler.hooks) {
compiler.hooks.emit.tapAsync(this.pluginName, (compilation, callback) => {
this.handleEmit(compilation, callback);
});
} else {
compiler.plugin('emit', (compilation, callback) => {
this.handleEmit(compilation, callback);
});
}
}
handleEmit(compilation, callback) {
const commonManifest = require(this.commonManifestPath);
const platformManifest = require(this.platformManifestPath);
const resultManifest = merge(commonManifest, platformManifest);
compilation.assets[this.resultManifestPath] = {
source: () => {
return JSON.stringify(resultManifest);
},
size: () => {
return resultManifest.length;
}
};
callback();
}
}
//#endregion
module.exports = function(env) {
env = env || {};
const platform = env.platform ? env.platform : 'chromium';
return {
entry: {
'/interface/js/scripts/popup': './src/build/interface/popup.js',
'/interface/js/scripts/settings': './src/build/interface/settings.js',
'/interface/js/scripts/settings-iframe': './src/build/interface/settings-iframe.js',
'/interface/js/scripts/settings-screenshot': './src/build/interface/settings-screenshot.js',
'/interface/js/scripts/settings-download': './src/build/interface/settings-download.js',
'/interface/js/scripts/settings-other': './src/build/interface/settings-other.js',
'/interface/js/scripts/statistics': './src/build/interface/statistics.js',
'/interaction/js/content': './src/build/interaction/content.js',
'/interaction/js/background': './src/build/interaction/background.js',
'/interaction/js/exif': './src/build/interaction/exif.js',
'/interaction/css/custom-thread': './src/build/interaction/custom-thread.js'
},
output: {
path: path.resolve(__dirname, 'dist', platform)
},
module: {
rules: [
{
test: /\.s(c|a)ss$/,
use: ExtractTextPlugin.extract({
use: [
{
loader: 'css-loader',
options: {
minimize: true,
sourceMap: false
}
},
{
loader: 'sass-loader',
options: {
sourceMap: false
}
}
]
})
},
{
test: /\.pug$/,
loader: 'pug-loader'
},
{
test: /\.tsx?$/,
loader: 'awesome-typescript-loader'
}
]
},
plugins: [
env.dontRemove ? {apply: () => {return true}} : new RemovePlugin({
before: {
root: __dirname,
include: [`dist/${platform}`]
},
after: {
root: __dirname,
test: [{
folder: `dist/${platform}/interface/js/scripts`,
method: (filePath) => {
return filePath.includes("settings-iframe.js");
}
}]
}
}),
new CopyWebpackPlugin([
{
from: './src/static/interface',
to: './interface'
},
{
from: './src/static/interaction',
to: './interaction/assets'
}
]),
/*
* Replace with mini-css-extract-plugin, when
* they do a support for filename function type.
* https://github.com/webpack-contrib/mini-css-extract-plugin/issues/67
* https://github.com/webpack-contrib/mini-css-extract-plugin/issues/143
*/
new ExtractTextPlugin({
filename: (getPath) => {
let path = getPath('[name]');
path = path.replace('/interface/js/scripts/', '/interface/css/styles/');
path = new RegExp('(\.css$)').test(path) ? path : path.concat('.css');
return path;
}
}),
new HTMLWebpackPlugin({
template: './src/interface/html/popup.pug',
filename: '/interface/html/popup.html',
inject: false
}),
new HTMLWebpackPlugin({
template: './src/interface/html/settings.pug',
filename: '/interface/html/settings.html',
inject: false
}),
new HTMLWebpackPlugin({
template: './src/interface/html/settings-download.pug',
filename: '/interface/html/settings-download.html',
inject: false
}),
new HTMLWebpackPlugin({
template: './src/interface/html/settings-screenshot.pug',
filename: '/interface/html/settings-screenshot.html',
inject: false
}),
new HTMLWebpackPlugin({
template: './src/interface/html/settings-other.pug',
filename: '/interface/html/settings-other.html',
inject: false
}),
new HTMLWebpackPlugin({
template: './src/interface/html/statistics.pug',
filename: '/interface/html/statistics.html',
inject: false
}),
new CreateManifestPlugin({
commonManifestPath: './src/manifest/common.json',
platformManifestPath: `./src/manifest/${platform}.json`,
resultManifestPath: 'manifest.json'
}),
],
resolve: {
alias: {
'@interface': path.resolve(__dirname, './src/interface'),
'@interaction': path.resolve(__dirname, './src/interaction')
},
plugins: [
/*
* "If you want to use new paths and baseUrl feature of TS 2.0 please include TsConfigPathsPlugin".
* https://github.com/s-panferov/awesome-typescript-loader#advanced-path-resolution-in-typescript-20
*/
new TsConfigPathsPlugin()
],
extensions: [
'.ts', '.tsx', '.d.ts',
'.js', '.jsx',
'.json',
'.scss',
'.pug'
]
}
}
}