forked from sheepzh/time-tracker-4-browser
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrspack.common.ts
More file actions
186 lines (174 loc) · 5.49 KB
/
rspack.common.ts
File metadata and controls
186 lines (174 loc) · 5.49 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
import {
CopyRspackPlugin, CssExtractRspackPlugin, DefinePlugin, HtmlRspackPlugin,
type Chunk, type Configuration,
type RspackPluginInstance,
type RuleSetRule
} from "@rspack/core"
import path, { join } from "path"
import postcssRTLCSS from 'postcss-rtlcss'
import i18nChrome from "../src/i18n/chrome"
import { GenerateJsonPlugin } from "./plugins/generate-json"
export const MANIFEST_JSON_NAME = "manifest.json"
const generateJsonPlugins: RspackPluginInstance[] = []
const localeJsonFiles = Object.entries(i18nChrome)
.map(([locale, message]) => new GenerateJsonPlugin(`_locales/${locale}/messages.json`, message))
.map(plugin => plugin as unknown as RspackPluginInstance)
generateJsonPlugins.push(...localeJsonFiles)
type EntryConfig = {
name: string
path: string
}
const BACKGROUND = 'background'
const CONTENT_SCRIPT = 'content_scripts'
const CONTENT_SCRIPT_SKELETON = 'content_scripts_skeleton'
const POPUP = 'popup'
const entryConfigs: EntryConfig[] = [{
name: BACKGROUND,
path: './src/background',
}, {
name: CONTENT_SCRIPT,
path: './src/content-script',
}, {
name: CONTENT_SCRIPT_SKELETON,
path: './src/content-script/skeleton',
}, {
name: POPUP,
path: './src/pages/popup',
}, {
name: 'popup_skeleton',
path: './src/pages/popup/skeleton',
}, {
name: 'app',
path: './src/pages/app',
}, {
name: 'side',
path: './src/pages/side'
}]
const POSTCSS_LOADER_CONF: RuleSetRule['use'] = {
loader: 'postcss-loader',
options: {
postcssOptions: {
plugins: [postcssRTLCSS({ mode: 'combined' })],
},
},
}
const chunkFilter = ({ name }: Chunk) => {
return !name || ![BACKGROUND, CONTENT_SCRIPT, CONTENT_SCRIPT_SKELETON].includes(name)
}
const staticOptions: Configuration = {
entry() {
const entry: Record<string, string> = {}
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,
},
plugins: [
"@vue/babel-plugin-jsx",
"@emotion/babel-plugin",
],
},
}, 'ts-loader'],
}, {
test: /\.css$/,
use: [CssExtractRspackPlugin.loader, 'css-loader', POSTCSS_LOADER_CONF],
}, {
test: /\.(jpg|jpeg|png|woff|woff2|eot|ttf|svg)$/,
type: 'asset/resource'
}
]
},
resolve: {
extensions: ['.ts', '.tsx', '.js', '.css'],
tsConfig: join(__dirname, '..', 'tsconfig.json'),
},
optimization: {
splitChunks: {
chunks: chunkFilter,
cacheGroups: {
elementPlus: {
name: 'element-plus',
test: /[\\/]node_modules[\\/]element-plus[\\/]/,
},
defaultVendors: {
filename: 'vendor/[name].js'
}
}
},
},
}
type Option = {
outputPath: string
manifest: chrome.runtime.ManifestV3 | chrome.runtime.ManifestFirefox
mode: Configuration["mode"]
}
const generateOption = ({ outputPath, manifest, mode }: Option) => {
const plugins = [
...generateJsonPlugins,
new GenerateJsonPlugin(MANIFEST_JSON_NAME, manifest),
// copy static resources
new CopyRspackPlugin({
patterns: [
{
from: path.join(__dirname, '..', 'public', 'images'),
to: path.join(outputPath, 'static', 'images'),
}
]
}),
new CssExtractRspackPlugin(),
new HtmlRspackPlugin({
filename: path.join('static', 'app.html'),
title: 'Loading...',
meta: {
viewport: {
name: "viewport",
content: 'width=device-width',
},
},
chunks: ['app'],
}),
new HtmlRspackPlugin({
filename: path.join('static', 'popup.html'),
chunks: ['popup'],
}),
new HtmlRspackPlugin({
filename: path.join('static', 'popup_skeleton.html'),
chunks: ['popup_skeleton'],
}),
new HtmlRspackPlugin({
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,
'process.env.VUE_TRUSTED_TYPES': false,
}),
]
const config: Configuration = {
...staticOptions,
output: {
...staticOptions.output,
path: outputPath,
filename: '[name].js',
},
plugins, mode,
// no eval with development, but generate *.map.js
devtool: mode === 'development' ? 'cheap-module-source-map' : false,
}
return config
}
export default generateOption