forked from eramdam/BetterTweetDeck
-
Notifications
You must be signed in to change notification settings - Fork 0
/
webpack.config.babel.js
140 lines (127 loc) · 3.38 KB
/
webpack.config.babel.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
/* eslint global-require: 0 */
import path from 'path';
import fs from 'fs';
import CopyWebpackPlugin from 'copy-webpack-plugin';
import UglifyJSPlugin from 'uglifyjs-webpack-plugin';
import ExtractTextPlugin from 'extract-text-webpack-plugin';
import CleanWebpackPlugin from 'clean-webpack-plugin';
import GenerateJsonPlugin from 'generate-json-webpack-plugin';
import ZipPlugin from 'zip-webpack-plugin';
import config from 'config';
// const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin;
const extractContent = new ExtractTextPlugin('css/index.css');
const extractOptions = new ExtractTextPlugin('options/css/index.css');
const staticFiles = [
{
from: 'revert-dark-theme.css',
},
{
from: 'icons/*.png',
},
{
from: 'fonts/*',
},
{
from: 'emojis/*.png',
},
{
from: 'options/**/*.html',
},
{
from: 'options/ui/*',
},
{
from: 'options/img/*',
},
{
from: '_locales/**/*',
},
];
const DIST_FOLDER = 'dist';
const IS_PRODUCTION = process.env.NODE_ENV !== 'dev';
const POSSIBLE_BROWSERS = ['chrome', 'firefox'];
const cssLoaders = {
fallback: 'style-loader',
use: [
{
loader: 'css-loader',
options: {
importLoaders: 1,
minimize: IS_PRODUCTION,
sourceMap: !IS_PRODUCTION,
},
},
{
loader: 'postcss-loader',
},
],
};
module.exports = (env) => {
if (!env || !env.browser || !POSSIBLE_BROWSERS.includes(env.browser)) {
throw new Error(`Please provide a browser!
webpack --env.browser=BROWSER
Possible values: chrome, firefox`);
}
if (env.browser !== 'firefox') {
staticFiles.push({
from: '../tools/embeds.js',
});
}
const staticFilesPath = staticFiles.map(i => (Object.assign(i, {
context: './src/',
})));
const getManifest = () => require(`./tools/manifests/${env.browser}.js`);
fs.writeFileSync(path.resolve(__dirname, 'config/config.json'), JSON.stringify(config));
return {
entry: {
'js/inject': './src/js/inject.js',
'js/background': './src/js/background.js',
'js/content': './src/js/content.js',
'options/options': './src/options/options.js',
},
output: {
filename: '[name].js',
path: `${__dirname}/${DIST_FOLDER}`,
},
devtool: 'source-map',
module: {
rules: [
{
test: /.js$/,
exclude: /node_modules/,
loader: 'babel-loader',
},
{
test: /\.css$/,
exclude: path.resolve(__dirname, 'src/css/options'),
use: extractContent.extract(cssLoaders),
},
{
test: /\.css$/,
include: path.resolve(__dirname, 'src/css/options'),
use: extractOptions.extract(cssLoaders),
},
],
},
resolve: {
alias: {
config: path.resolve(__dirname, 'config/config.json'),
},
},
plugins: [
// new BundleAnalyzerPlugin(),
extractContent,
extractOptions,
new CleanWebpackPlugin([DIST_FOLDER]),
new CopyWebpackPlugin(staticFilesPath),
new GenerateJsonPlugin('manifest.json', getManifest(), null, 2),
IS_PRODUCTION && env.browser !== 'firefox' ? new UglifyJSPlugin({
parallel: true,
}) : () => null,
IS_PRODUCTION ? new ZipPlugin({
path: path.resolve(__dirname, 'artifacts'),
filename: `dist-${env.browser}`,
}) : () => null,
],
};
};