-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathwebpack.config.js
135 lines (128 loc) · 4.11 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
const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const { CleanWebpackPlugin } = require('clean-webpack-plugin');
const fse = require('fs-extra');
const chokidar = require('chokidar');
const moment = require('moment');
const chalk = require('chalk');
const editJsonFile = require("edit-json-file");
const ChromeExtensionReloader = require('webpack-chrome-extension-reloader');
const NodePolyfillPlugin = require("node-polyfill-webpack-plugin");
const NodeEnv = process.env.NODE_ENV;
const isDev = () => (NodeEnv === 'development');
const isProd = () => (NodeEnv === 'production');
const TitleName = 'K:Wallet';
module.exports = {
mode: isDev() ? 'development' : 'production',
devtool: isDev() ? 'nosources-source-map' : false,
watch: isDev(),
stats: {
all: false,
modules: true,
errors: true,
warnings: true,
moduleTrace: true,
errorDetails: true
},
entry: {
background: './src/background/index.js',
browserAction: './src/browserAction/index.js',
contentScripts: './src/contentScripts/index.js'
},
plugins: [
new CleanWebpackPlugin(),
new NodePolyfillPlugin(),
new CopyFilesOnWatch([
{from:'./src/icons/', to:'./dist/icons/'},
{from:'./src/manifest.json', to:'./dist/manifest.json'},
{from:'./src/_locales/', to:'./dist/_locales/'},
{from:'./src/images/', to:'./dist/images/'}
]),
new HtmlWebpackPlugin({
title: TitleName,
chunks: ['browserAction'],
filename: 'browserAction/index.html',
template: 'src/browserAction/index.html'
}),
new HtmlWebpackPlugin({
title: TitleName,
chunks: ['contentScripts'],
filename: 'contentScripts/index.html',
template: 'src/browserAction/index.html'
}),
isDev() ? ()=>new ChromeExtensionReloader({
port: 9090,
reloadPage: true,
entries: {
contentScript: 'contentScripts',
background: 'background'
}
}) : ()=>new NothingPlugin()
],
output: {
filename: t => t.chunk.name + '/index.js',
path: path.resolve(__dirname, 'dist')
},
module: {
rules: [
{
enforce: 'pre',
test: /\.(js|jsx)$/,
exclude: /node_modules/,
loader: 'eslint-loader',
},
{
test: /\.(js|jsx)$/,
exclude: /node_modules/,
use: {
loader: "babel-loader"
}
},
{
test: /\.css$/,
use: [
'style-loader',
'css-loader'
]
},
{
test: /\.s[ac]ss$/i,
use: [
'style-loader',
'css-loader',
'sass-loader',
],
},
{
test: /\.(png|svg|jpg|gif|woff|woff2|eot|ttf|otf)$/,
loader: 'file-loader',
options:{
outputPath: 'browserAction',
publicPath: '/browserAction'
}
}
]
}
};
function NothingPlugin() {
this.apply = function(){};
}
function CopyFilesOnWatch(options) {
this.apply = function(compiler){
if(isDev()){
options.forEach((v)=>{
chokidar.watch(v.from).on('change',(path)=>{
console.log( chalk.red("Changed:"), chalk.green(path), chalk.yellow(moment().format('LTS')) );
fse.copy(path, path.replace('src','dist'));
});
});
}
compiler.hooks.emit.tapAsync('CopyFilesOnWatch', (compilation, callback) => {
Promise.all(options.map(v=>fse.copy(v.from,v.to))).then(()=>{
callback();
}).catch((err)=>{
console.error(err);
})
});
};
}