-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwebpack.config.js
150 lines (134 loc) · 3.79 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
145
146
147
148
149
150
const path = require('path');
const { CleanWebpackPlugin } = require('clean-webpack-plugin');
const CopyWebpackPlugin = require('copy-webpack-plugin');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const WriteFilePlugin = require('write-file-webpack-plugin');
const ZipPlugin = require('zip-webpack-plugin');
const camelCase = require('camelcase');
const devMode = process.env.NODE_ENV !== 'production';
const releaseVersion = process.env.npm_package_version;
const NUMBER_OF_COMMANDS = 20;
/**
* Generates the manifest file using the package.json informations
* @param content
* @returns {Buffer}
*/
function transformManifest(content) {
const commands = {};
for (let commandId = 1; commandId <= NUMBER_OF_COMMANDS; commandId++) {
// Add a prefix so that the shortcut is sorted in ascending
// order by the number on chrome://extensions/shortcuts page
const commandNamePrefix = commandId < 10 ? 'a' : 'b';
const commandName = `${commandNamePrefix}${commandId}`;
commands[commandName] = {
description: `Command ${commandId.toString().padStart(2, '0')}`,
};
}
return Buffer.from(JSON.stringify({
version: releaseVersion,
commands,
...JSON.parse(content.toString()),
}));
}
const plugins = [
// Clean the dist folder
new CleanWebpackPlugin({
verbose: true,
// Ignore manifest.json otherwise, it will be deleted after rebuilding
cleanAfterEveryBuildPatterns: [
'!manifest.json',
'!**/messages.json',
'!images/*.png',
'!options.html',
],
}),
new CopyWebpackPlugin({
patterns: [{
from: path.resolve(__dirname, 'src/manifest.json'),
transform: transformManifest,
}],
}),
new CopyWebpackPlugin({
patterns: [{
from: path.resolve(__dirname, 'src/_locales/**/messages.json'),
}],
}),
new CopyWebpackPlugin({
patterns: [{
from: path.resolve(__dirname, 'src/images/*.png'),
}],
}),
new MiniCssExtractPlugin(),
new HtmlWebpackPlugin({
template: path.join(__dirname, 'src/options.html'),
filename: 'options.html',
chunks: ['options'],
}),
new WriteFilePlugin(),
];
if (!devMode) {
const extensionName = camelCase(process.env.npm_package_name, { pascalCase: true });
const releaseName = `${extensionName}-${releaseVersion}.chrome`;
plugins.push(
// Create an archive for publication in the Google Web Store
new ZipPlugin({
filename: releaseName,
}),
);
}
module.exports = {
mode: devMode ? 'development' : 'production',
devtool: devMode ? 'inline-source-map' : false,
context: path.resolve(__dirname, 'src'),
entry: {
options: './js/options/index.jsx',
background: './js/background.js',
},
output: {
path: path.resolve(__dirname, 'dist'),
publicPath: '/',
filename: '[name].bundle.js',
},
resolve: {
extensions: ['.jsx', '.js', '.json', '.scss', '.css'],
alias: {
react: 'preact/compat',
'react-dom': 'preact/compat',
styles: path.resolve(__dirname, 'src/styles'),
constants: path.resolve(__dirname, 'src/js/options/constants'),
},
modules: [
path.resolve(__dirname, 'src/js'),
path.resolve(__dirname, 'src/js/options'),
'node_modules',
],
},
module: {
rules: [
{
test: /\.scss$/,
exclude: /node_modules/,
use: [
MiniCssExtractPlugin.loader,
'css-loader',
'sass-loader',
],
},
{
test: /\.jsx?$/,
exclude: /node_modules/,
use: 'babel-loader',
},
],
},
plugins,
devServer: {
hot: true,
contentBase: path.join(__dirname, 'dist'),
headers: { 'Access-Control-Allow-Origin': '*' },
disableHostCheck: true,
watchContentBase: true,
port: 3000,
},
};