forked from sheepzh/time-tracker-4-browser
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebpack.common.ts
More file actions
220 lines (206 loc) · 6.75 KB
/
webpack.common.ts
File metadata and controls
220 lines (206 loc) · 6.75 KB
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
211
212
213
214
215
216
217
218
219
220
import CopyWebpackPlugin from "copy-webpack-plugin"
import GenerateJsonPlugin from "generate-json-webpack-plugin"
import HtmlWebpackPlugin from "html-webpack-plugin"
import MiniCssExtractPlugin from "mini-css-extract-plugin"
import path from "path"
import postcssRTLCSS from 'postcss-rtlcss'
import webpack, { Chunk, DefinePlugin, RuleSetRule } from "webpack"
import { POLYFILL_SCRIPT_NAME } from "../src/content-script/polyfill/inject"
import i18nChrome from "../src/i18n/chrome"
import tsConfig from '../tsconfig.json'
export const MANIFEST_JSON_NAME = "manifest.json"
const tsPathAlias = tsConfig.compilerOptions.paths
const generateJsonPlugins = []
const localeJsonFiles = Object.entries(i18nChrome)
.map(([locale, message]) => new GenerateJsonPlugin(`_locales/${locale}/messages.json`, message))
.map(plugin => plugin as unknown as webpack.WebpackPluginInstance)
generateJsonPlugins.push(...localeJsonFiles)
// Process the alias of typescript modules
const resolveAlias: { [index: string]: string | false | string[] } = {}
const aliasPattern = /^(@.*)\/\*$/
const sourcePattern = /^(src(\/.*)?)\/\*$/
Object.entries(tsPathAlias).forEach(([alias, sourceArr]) => {
// Only process the alias starts with '@'
if (!aliasPattern.test(alias)) {
return
}
if (!sourceArr.length) {
return
}
const index = alias.match(aliasPattern)[1]
const webpackSourceArr = sourceArr
.filter(source => sourcePattern.test(source))
// Only set alias which is in /src folder
.map(source => source.match(sourcePattern)[1])
.map(folder => path.resolve(__dirname, '..', folder))
resolveAlias[index] = webpackSourceArr
})
console.log("Alias of typescript: ")
console.log(resolveAlias)
type EntryConfig = {
name: string
path: string
}
const BACKGROUND = 'background'
const CONTENT_SCRIPT = 'content_scripts'
const POPUP = 'popup'
const entryConfigs: EntryConfig[] = [{
name: BACKGROUND,
path: './src/background',
}, {
name: CONTENT_SCRIPT,
path: './src/content-script',
}, {
name: POLYFILL_SCRIPT_NAME,
path: './src/content-script/polyfill',
}, {
name: POPUP,
path: './src/popup',
}, {
name: 'app',
path: './src/app',
}, {
name: 'side',
path: './src/side'
}]
const POSTCSS_LOADER_CONF: RuleSetRule['use'] = {
loader: 'postcss-loader',
options: {
postcssOptions: {
plugins: [postcssRTLCSS({ mode: 'combined' })],
},
},
}
const chunkFilter = ({ name }: Chunk) => {
return ![BACKGROUND, CONTENT_SCRIPT, POLYFILL_SCRIPT_NAME].includes(name)
}
const staticOptions: webpack.Configuration = {
entry() {
const entry = {}
entryConfigs.forEach(({ name, path }) => entry[name] = path)
return entry
},
module: {
rules: [
{
test: /\.tsx?$/,
exclude: /^(node_modules|test|script)/,
use: [{
loader: 'babel-loader',
options: {
assumptions: {
// Fix that react transform array proxy to object, and error occurs while destructing
iterableIsArray: true,
},
presets: ["@babel/preset-env"],
plugins: [
"@vue/babel-plugin-jsx",
"@babel/plugin-transform-modules-commonjs",
],
},
}, 'ts-loader'],
}, {
test: /\.css$/,
use: [MiniCssExtractPlugin.loader, 'css-loader', POSTCSS_LOADER_CONF],
}, {
test: /\.sc|ass$/,
use: [MiniCssExtractPlugin.loader, 'css-loader', POSTCSS_LOADER_CONF, 'sass-loader']
}, {
test: /\.(jpg|jpeg|png|woff|woff2|eot|ttf|svg)$/,
exclude: /node_modules/,
use: ['url-loader']
}, {
test: /\.m?js$/,
exclude: /(node_modules)/,
use: ['babel-loader']
}
]
},
resolve: {
extensions: ['.ts', '.tsx', ".js", '.css', '.scss', '.sass'],
alias: resolveAlias,
fallback: {
// fallbacks of axios's dependencies start
stream: require.resolve('stream-browserify'),
zlib: false,
https: false,
http: false,
url: false,
assert: false,
// fallbacks of axios's dependencies end
}
},
optimization: {
splitChunks: {
chunks: chunkFilter,
cacheGroups: {
defaultVendors: {
filename: 'vendor/[name].js'
}
}
},
},
}
type Option = {
outputPath: string
manifest: chrome.runtime.ManifestV3 | chrome.runtime.ManifestFirefox
mode: webpack.Configuration["mode"]
}
const generateOption = ({ outputPath, manifest, mode }: Option) => {
const plugins = [
...generateJsonPlugins,
new GenerateJsonPlugin(MANIFEST_JSON_NAME, manifest) as unknown as webpack.WebpackPluginInstance,
// copy static resources
new CopyWebpackPlugin({
patterns: [
{
from: path.join(__dirname, '..', 'public', 'images'),
to: path.join(outputPath, 'static', 'images'),
}
]
}),
new MiniCssExtractPlugin(),
new HtmlWebpackPlugin({
filename: path.join('static', 'app.html'),
title: 'Loading...',
meta: {
viewport: {
name: "viewport",
content: 'width=device-width',
},
},
chunks: ['app'],
}),
new HtmlWebpackPlugin({
filename: path.join('static', 'popup.html'),
chunks: ['popup'],
}),
new HtmlWebpackPlugin({
filename: path.join('static', 'side.html'),
title: 'Loading...',
chunks: ['side'],
}),
new DefinePlugin({
// https://github.com/vuejs/vue-cli/pull/7443
__VUE_PROD_HYDRATION_MISMATCH_DETAILS__: false,
__VUE_OPTIONS_API__: false,
__VUE_PROD_DEVTOOLS__: false,
}),
]
const config: webpack.Configuration = {
...staticOptions,
output: {
path: outputPath,
filename: '[name].js',
},
plugins, mode,
}
if (mode === "development") {
// no eval with development, but generate *.map.js
config.devtool = 'cheap-module-source-map'
// Use cache with filesystem
config.cache = { type: 'filesystem' }
}
return config
}
export default generateOption