-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathwebpack.config.js
153 lines (144 loc) · 4.71 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
151
152
153
const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const CopyWebpackPlugin = require('copy-webpack-plugin');
module.exports = {
// Set mode to development by default or use NODE_ENV
mode: process.env.NODE_ENV || 'development',
// Add devtool setting
devtool: 'cheap-module-source-map', // Recommended for development to avoid 'eval'
// Entry point of the application; adjust this path if necessary
entry: {
popup: './src/popup/popup.js',
options: './src/options/options.js',
background: './src/js/background.js',
contentScript: './src/js/contentScript.js', // 添加 content script 入口
storageCache: './src/utils/storage-cache.js', // 添加存储缓存入口
},
// Output configuration
output: {
path: path.resolve(__dirname, 'dist'),
filename: '[name].js',
clean: true, // Clean the output directory before each build
},
// Development server configuration
devServer: {
static: path.join(__dirname, 'dist'),
port: 3000,
open: true,
hot: true,
},
// Module rules to handle different file types
module: {
rules: [
{
// Transpile modern JavaScript to older versions using babel-loader
test: /\.js$/,
exclude: /node_modules/,
use: {
loader: 'babel-loader'
}
},
{
// Process CSS files with appropriate loaders
test: /\.css$/,
use: ['style-loader', 'css-loader'],
},
{
test: /\.(png|jpg|jpeg|gif|svg)$/,
type: 'asset/resource',
generator: {
filename: 'assets/[name][ext]'
}
}
],
},
// Plugins configuration
plugins: [
new HtmlWebpackPlugin({
template: './src/popup/popup.html',
filename: 'popup.html',
chunks: ['popup'],
templateParameters: {
cssPath: './assets/styles/popup.css'
}
}),
new HtmlWebpackPlugin({
template: './src/options/options.html',
filename: 'options.html',
chunks: ['options'],
templateParameters: {
cssPath: './assets/styles/options.css'
}
}),
new CopyWebpackPlugin({
patterns: [
{
from: './manifest.json',
to: 'manifest.json',
transform(content) {
// 修改 manifest.json 中的路径
const manifest = JSON.parse(content.toString());
// 修正 action 中的图标路径
if (manifest.action && manifest.action.default_icon) {
manifest.action.default_icon = {
"16": "assets/icons/icon16.png",
"48": "assets/icons/icon48.png",
"128": "assets/icons/icon128.png"
};
}
// 修正 icons 路径
if (manifest.icons) {
manifest.icons = {
"16": "assets/icons/icon16.png",
"48": "assets/icons/icon48.png",
"128": "assets/icons/icon128.png"
};
}
// 修正 content_scripts 路径
if (manifest.content_scripts && manifest.content_scripts.length > 0) {
manifest.content_scripts[0].js = ["contentScript.js"];
manifest.content_scripts[0].css = ["assets/styles/popupStyles.css", "assets/styles/splitViewStyles.css"];
}
// 修正 web_accessible_resources 路径
if (manifest.web_accessible_resources && manifest.web_accessible_resources.length > 0) {
manifest.web_accessible_resources[0].resources = ["assets/styles/splitViewStyles.css"];
}
// 修正 background 路径
if (manifest.background) {
manifest.background.service_worker = "background.js";
}
// 修正 options_page 路径
if (manifest.options_page) {
manifest.options_page = "options.html";
}
// 修正 action 中的 default_popup 路径
if (manifest.action && manifest.action.default_popup) {
manifest.action.default_popup = "popup.html";
}
return Buffer.from(JSON.stringify(manifest, null, 2));
}
},
// 复制 CSS 文件到 assets 目录
{
from: 'src/styles',
to: 'assets/styles',
noErrorOnMissing: true
},
{
from: 'src/assets',
to: 'assets',
noErrorOnMissing: true
},
// 复制 _locales 目录
{
from: '_locales',
to: '_locales',
noErrorOnMissing: true
}
],
}),
],
optimization: {
moduleIds: 'deterministic'
},
};