-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebpack.config.js
More file actions
177 lines (165 loc) · 4.56 KB
/
webpack.config.js
File metadata and controls
177 lines (165 loc) · 4.56 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
import webpack from 'webpack';
import TerserPlugin from "terser-webpack-plugin";
import { fileURLToPath } from "url";
import path from "path";
const __dirname = path.dirname(fileURLToPath(import.meta.url));
/**
* Helper function to create webpack configurations.
* @param {Object} options Options for creating a webpack target.
* @param {string} options.name Name of output file.
* @param {string} options.suffix Suffix of output file.
* @param {string} options.type Type of library.
* @param {string} options.ignoreModules The list of modules to ignore.
* @param {string} options.externalModules The list of modules to set as external.
* @param {Object[]} options.plugins List of plugins to use.
* @returns {import('webpack').Configuration} One webpack target.
*/
function buildConfig({
name = "",
suffix = ".js",
type = "module", // 'module' | 'commonjs'
ignoreModules = [],
externalModules = [],
plugins = [],
} = {}) {
const outputModule = type === "module";
const alias = Object.fromEntries(
ignoreModules.map((module) => [module, false]),
);
/** @type {import('webpack').Configuration} */
const config = {
mode: "development",
devtool: "source-map",
entry: {
// [`transformers${name}`]: "./src/transformers.js",
[`workfunction`]: "./src/work-function-entry.js",
// [`transformers${name}.min`]: "./src/transformers.js",
},
output: {
filename: `[name]${suffix}`,
path: path.join(__dirname, "dist"),
library: {
type,
},
assetModuleFilename: "[name][ext]",
chunkFormat: false,
iife: true,
},
optimization: {
minimize: true,
minimizer: [
new TerserPlugin({
test: new RegExp(`\\.min\\${suffix}$`),
// Do not bundle with comments.
// See https://webpack.js.org/plugins/terser-webpack-plugin/#remove-comments for more information.
terserOptions: {
// output: {
// comments: false,
// },
compress: false,
mangle: false,
},
extractComments: false,
}),
],
// usedExports: true,
},
experiments: {
outputModule,
},
resolve: {
alias,
fallback: {
"fs": false,
"tls": false,
}
},
externals: externalModules,
// Development server
devServer: {
static: {
directory: __dirname,
},
port: 8080,
},
plugins,
module: {
rules: [
{
test: /distilbert-base-uncased-finetuned-sst-2-english|tiny-random-mistral|SmolLM2-135M-Instruct|SmolLM2-360M-Instruct|Qwen3-0.6B-ONNX|whisper\-.*/,
type: 'asset/inline', // embed as data urls, see https://webpack.js.org/guides/asset-modules/#inlining-assets
generator: {
dataUrl: {
mimetype: 'application/octet-stream',
},
},
}
]
}
};
if (outputModule) {
config.module = {
parser: {
javascript: {
importMeta: false,
},
},
};
} else {
config.externalsType = "commonjs";
}
return config;
}
/**
* Plugin to strip the "node:" prefix from module requests.
*
* This is necessary to ensure both web and node builds work correctly,
* otherwise we would get an error like:
* ```
* Module build failed: UnhandledSchemeError: Reading from "node:path" is not handled by plugins (Unhandled scheme).
* Webpack supports "data:" and "file:" URIs by default.
* You may need an additional plugin to handle "node:" URIs.
* ```
*
* NOTE: We then do not need to use the `node:` prefix in the resolve.alias configuration.
*/
class StripNodePrefixPlugin extends webpack.NormalModuleReplacementPlugin {
constructor() {
super(
/^node:(.+)$/,
resource => {
resource.request = resource.request.replace(/^node:/, '');
}
);
}
}
export default buildConfig({
name: ".custom",
type: "commonjs",
ignoreModules: [
"onnxruntime-node",
"onnxruntime-web",
"sharp",
"fs",
"path",
"url",
],
externalModules: [
"dcp-wasm.js",
"dcp-ort.js",
],
plugins: [
new StripNodePrefixPlugin(),
new webpack.BannerPlugin({
banner: 'function workFunction(...args) {const exports = {};',
raw: true,
stage: webpack.Compilation.PROCESS_ASSETS_STAGE_REPORT,
}),
new webpack.BannerPlugin({
banner: 'return exports.default(...args);}', // making sure we await the promise
raw: true,
footer: true,
stage: webpack.Compilation.PROCESS_ASSETS_STAGE_REPORT,
}),
]
});