Repository navigation
Expand file tree
/
Copy pathwebpack.config.js
More file actions
252 lines (232 loc) · 9.41 KB
/
Copy pathwebpack.config.js
File metadata and controls
252 lines (232 loc) · 9.41 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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
const path = require('path');
const webpack = require('webpack');
const { modelSource: hfModelSource } = require('./scripts/hf-model/model-source');
const CopyPlugin = require('copy-webpack-plugin');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const sveltePreprocess = require('svelte-preprocess');
const {
LocalNerAssetsPlugin,
getNerAssetCopyPatterns,
} = require('./scripts/extension-packaging');
const { renderTermsHtml } = require('./scripts/terms-html');
const { buildManifestVersion } = require('./scripts/build-number');
const { toFirefoxManifest } = require('./scripts/firefox/firefox-manifest');
class TermsHtmlPlugin {
constructor(options = {}) {
this.rootDir = options.rootDir || __dirname;
}
apply(compiler) {
compiler.hooks.thisCompilation.tap('TermsHtmlPlugin', (compilation) => {
compilation.hooks.processAssets.tap(
{
name: 'TermsHtmlPlugin',
stage: compiler.webpack.Compilation.PROCESS_ASSETS_STAGE_ADDITIONS,
},
() => {
const sourcePath = path.join(this.rootDir, 'TERMS.md');
const markdown = compiler.inputFileSystem.readFileSync(sourcePath, 'utf8');
const html = renderTermsHtml(markdown);
compilation.emitAsset('TERMS.html', new compiler.webpack.sources.RawSource(html));
compilation.fileDependencies.add(sourcePath);
}
);
});
}
}
module.exports = (_env = {}) => {
const requirePreparedModel =
process.env.NER_MODEL_ASSETS_REQUIRED === '1' || _env.requireNerModelAssets === true;
const manifestVersion = buildManifestVersion(
JSON.parse(require('fs').readFileSync(path.join(__dirname, 'manifest.json'), 'utf8')).version,
__dirname,
);
console.log(`[build] manifest version ${manifestVersion}`);
// One code base for every browser; for Firefox (BROWSER=firefox or --env
// browser=firefox) only the manifest and the output folder differ.
const browser = _env.browser || process.env.BROWSER || 'chrome';
if (!['chrome', 'firefox'].includes(browser)) {
throw new Error(`Unknown BROWSER "${browser}"; expected chrome or firefox.`);
}
console.log(`[build] browser ${browser}`);
// The Local AI model is not packaged: every browser downloads it from
// Hugging Face on first use (src/shared/local-ai-model-download.ts), which
// keeps the package ~70 MB instead of ~230 MB. MODEL_SOURCE=bundled packages
// the prepared model instead, for local testing without a network.
const modelSource = _env.modelSource || process.env.MODEL_SOURCE || 'huggingface';
if (!['huggingface', 'bundled', 'host'].includes(modelSource)) {
throw new Error(`Unknown MODEL_SOURCE "${modelSource}"; expected huggingface, bundled or host.`);
}
const { repo: modelHfRepo, revision: modelRevision } = hfModelSource();
if (modelSource === 'bundled') {
console.log('[build] Local AI model packaged with the build');
} else {
console.log(`[build] Local AI model downloaded from Hugging Face ${modelHfRepo} @ ${modelRevision}`);
}
return {
entry: {
'background/service-worker': './src/background/service-worker.ts',
'content/content-script': './src/content/content-script.ts',
'content/search-script': './src/content/search-script.ts',
'content/clipboard-interceptor-page': './src/content/clipboard-interceptor-page.ts',
'offscreen/offscreen': './src/offscreen/offscreen.ts',
'system-check/system-check-offscreen': './src/system-check/system-check-offscreen.ts',
'popup/popup': './src/popup/popup.ts',
'options/options': './src/options/options.ts',
'sidepanel/sidepanel': './src/sidepanel/sidepanel.ts',
},
output: {
path: path.resolve(__dirname, browser === 'firefox' ? 'dist-firefox' : 'dist'),
filename: '[name].js',
clean: true,
},
resolve: {
extensions: ['.svelte', '.ts', '.js', '.wasm'],
conditionNames: ['svelte', 'browser', 'import', 'module', 'default'],
mainFields: ['svelte', 'browser', 'module', 'main'],
},
module: {
rules: [
{
// Transformers.js / ONNX Runtime Web / wasm-bindgen reference their
// .wasm (and pthread worker) files via `new URL(..., import.meta.url)`.
// Webpack would emit each referenced file as a hashed asset at the
// dist root (~56 MB of duplicates). The runtime never uses those:
// ner-provider.ts pins ORT to vendor/onnxruntime-web/* via wasmPaths,
// and wasm-bridge.ts passes an explicit chrome.runtime.getURL for the
// crate wasm. Disable URL parsing for these packages so the copies in
// vendor/ and wasm/ (staged by CopyPlugin) stay the single source.
test: /\.m?js$/,
include: [
path.resolve(__dirname, 'node_modules/@huggingface/transformers'),
path.resolve(__dirname, 'node_modules/onnxruntime-web'),
path.resolve(__dirname, 'crate/pkg'),
],
parser: { url: false },
},
{
test: /\.svelte$/,
use: {
loader: 'svelte-loader',
options: {
emitCss: true,
compilerOptions: { runes: true },
preprocess: sveltePreprocess({ typescript: true }),
},
},
},
{
test: /\.ts$/,
use: 'ts-loader',
exclude: /node_modules/,
},
{
test: /\.css$/,
oneOf: [
{
// Overlay stylesheet is injected as a raw string into the
// closed Shadow Root attached by the review overlay.
include: path.resolve(__dirname, 'src/ui/overlay/overlay-styles.css'),
use: [
{
loader: 'css-loader',
options: { url: false, exportType: 'string' },
},
],
},
{
use: [MiniCssExtractPlugin.loader, { loader: 'css-loader', options: { url: false } }],
},
],
},
],
},
plugins: [
new MiniCssExtractPlugin({
filename: '[name].css',
}),
new LocalNerAssetsPlugin({
rootDir: __dirname,
// A downloaded model is not needed at build time.
requirePreparedModel: requirePreparedModel && modelSource === 'bundled',
}),
new webpack.DefinePlugin({
__REDACTO_MODEL_SOURCE__: JSON.stringify(modelSource),
__REDACTO_MODEL_HF_REPO__: JSON.stringify(modelHfRepo),
__REDACTO_MODEL_REVISION__: JSON.stringify(modelRevision),
}),
new TermsHtmlPlugin({ rootDir: __dirname }),
// Copy static files to dist/
new CopyPlugin({
patterns: [
{
from: 'manifest.json',
to: '.',
transform: (content) => {
let manifest = JSON.parse(content.toString());
if (browser === 'firefox') manifest = toFirefoxManifest(manifest);
manifest.version = manifestVersion;
return `${JSON.stringify(manifest, null, 2)}\n`;
},
},
{ from: 'LICENSE', to: '.' },
{ from: 'NOTICE', to: '.' },
{ from: 'FORK.md', to: '.' },
{ from: 'TERMS.md', to: '.' },
{ from: 'THIRD_PARTY_NOTICES.md', to: '.' },
{ from: 'docs/assets/redacto-logo-black.png', to: 'legal/redacto-logo-black.png' },
{ from: 'src/assets', to: 'assets', globOptions: { ignore: ['**/.DS_Store'] } },
{ from: 'src/assets/fonts', to: 'fonts' },
{ from: 'src/ui/banner/de-anon-banner.css', to: 'ui/banner/' },
// Machine-translated UI languages, fetched when picked (src/shared/i18n).
{ from: 'src/shared/i18n/generated/locales/*.json', to: 'i18n/[name][ext]', noErrorOnMissing: true },
// Copy the generated wasm-bindgen binary asset for runtime loading.
{
from: 'crate/pkg/privacy_guardrail_wasm_bg.wasm',
to: 'wasm/[name][ext]',
noErrorOnMissing: true,
},
...getNerAssetCopyPatterns(__dirname, { includeNerModel: modelSource === 'bundled' }),
],
}),
// Popup HTML
new HtmlWebpackPlugin({
template: 'src/popup/popup.html',
filename: 'popup/popup.html',
chunks: ['popup/popup'],
}),
// Options page HTML
new HtmlWebpackPlugin({
template: 'src/options/options.html',
filename: 'options/options.html',
chunks: ['options/options'],
}),
// Side panel HTML
new HtmlWebpackPlugin({
template: 'src/sidepanel/sidepanel.html',
filename: 'sidepanel/sidepanel.html',
chunks: ['sidepanel/sidepanel'],
}),
// Offscreen HTML
new HtmlWebpackPlugin({
template: 'src/offscreen/offscreen.html',
filename: 'offscreen/offscreen.html',
chunks: ['offscreen/offscreen'],
}),
// Lightweight passive system-check offscreen HTML
new HtmlWebpackPlugin({
template: 'src/system-check/system-check-offscreen.html',
filename: 'system-check/system-check-offscreen.html',
chunks: ['system-check/system-check-offscreen'],
}),
],
// Chrome extensions require specific settings
optimization: {
splitChunks: false, // Don't split — each entry must be self-contained
},
devtool: 'cheap-module-source-map',
experiments: {
asyncWebAssembly: true,
},
};
};