-
Notifications
You must be signed in to change notification settings - Fork 29
/
webpack.config.ts
executable file
·204 lines (177 loc) · 5.33 KB
/
webpack.config.ts
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
import webpack from 'webpack'
import path from 'path'
import HtmlWebpackPlugin from 'html-webpack-plugin'
import CopyWebpackPlugin from 'copy-webpack-plugin'
import MiniCssExtractPlugin from 'mini-css-extract-plugin'
import { appEntry, serviceWorkerEntry, contentScriptEntry, pdfViewerEntry, isDevelopment } from "./src/common-vars";
const srcPath = path.resolve(__dirname, "src");
const distPath = path.resolve(__dirname, "dist");
const componentsDir = path.resolve(srcPath, "components");
const configEntries: webpack.EntryObject = {
[appEntry]: path.resolve(componentsDir, "app/index.tsx"),
[contentScriptEntry]: path.resolve(srcPath, "user-script/content-script-entry.tsx"),
[serviceWorkerEntry]: path.resolve(srcPath, "background/background.ts"),
[pdfViewerEntry]: path.resolve(srcPath, "pdf-viewer/pdf-viewer.tsx"),
};
function webpackBaseConfig(): webpack.Configuration {
return {
target: "web",
mode: isDevelopment ? "development" : "production",
devtool: isDevelopment ? "source-map" : false, // https://webpack.js.org/configuration/devtool/
cache: isDevelopment ? { type: "filesystem" } : false,
entry: {}, // to be defined on each config
output: {
libraryTarget: "umd", // "umd": provide module as global variable, CommonJS and AMD
globalObject: "globalThis",
publicPath: "auto",
path: distPath,
filename: '[name].js',
chunkFilename: 'chunks/[name].js',
assetModuleFilename: `assets/[name][ext]`
},
experiments: {
topLevelAwait: true,
},
optimization: {
minimize: false,
},
ignoreWarnings: [
// hide css-loader's warnings about empty classes within defined modules
/export '.*?' \(imported as 'styles?'\) was not found in '.*?\.module\.s?css' .*/i
],
resolve: {
extensions: ['.ts', '.tsx', '.js', '.mjs', '.json'],
fallback: {
// ignore browser polyfill warnings
zlib: false,
path: false,
}
},
module: {
rules: [
{
test: /\.tsx?$/,
use: {
loader: 'ts-loader',
options: {
transpileOnly: false
}
}
},
/**
* Import CSS or SASS styles with modules support (*.module.scss)
* @param {string} styleLoader
*/
{
test: /\.s?css$/,
use: [
MiniCssExtractPlugin.loader,
{
loader: "css-loader",
options: {
sourceMap: isDevelopment,
modules: {
auto: /\.module\./i, // https://github.com/webpack-contrib/css-loader#auto
mode: 'local', // :local(.selector) by default
localIdentName: '[name]__[local]--[hash:base64:5]'
}
}
},
{
loader: "sass-loader",
options: {
sourceMap: isDevelopment,
}
}
]
},
/**
* Import icons and image files.
* Read more about asset types: https://webpack.js.org/guides/asset-modules/
*/
{
test: /\.svg$/,
type: "asset/inline" // data:image/svg+xml;base64,...
},
{
test: /\.(jpg|png|ico)$/,
type: "asset/resource" // path to bundled file, e.g. "/assets/*"
},
/**
* Import custom fonts as URL.
*/
{
test: /\.(ttf|eot|woff2?)$/,
type: "asset/resource"
},
/**
* Import raw "plain/text" resources
*/
{
test: /\.(txt|md|ftl)$/,
type: "asset/source"
}
]
},
plugins: [
new MiniCssExtractPlugin({
filename: '[name].css'
}),
]
}
}
export default [
// build main app (options page), pdf-viewer and content-script
function () {
const config = webpackBaseConfig();
config.target = "web"
config.entry = {
[appEntry]: configEntries[appEntry],
[pdfViewerEntry]: configEntries[pdfViewerEntry],
};
config.plugins.push(...[
new HtmlWebpackPlugin({
inject: true,
chunks: [appEntry],
filename: "options.html",
}),
new HtmlWebpackPlugin({
inject: true,
chunks: [pdfViewerEntry],
filename: `${pdfViewerEntry}.html`,
}),
new CopyWebpackPlugin({
patterns: [
{ from: "README.md" },
{ from: "privacy-policy.md" },
{ from: "manifest.json" },
{ from: "_locales", to: "_locales" },
{ from: "assets", to: "assets" },
]
}),
]);
// exclude non-UI dependencies from final bundle
config.externals = ["openai", "mellowtel"];
return config;
},
// user-script.js
function () {
const config = webpackBaseConfig();
config.target = "web"
config.entry = {
[contentScriptEntry]: configEntries[contentScriptEntry],
};
config.externals = ["openai"];
return config;
},
// background.js
// service-worker must be compiled with appropriate `config.target` to load chunks via global `importScripts()` in manifest@v3
function () {
const config = webpackBaseConfig();
config.target = "webworker";
config.entry = {
[serviceWorkerEntry]: configEntries[serviceWorkerEntry]
};
return config;
}
];