Skip to content

Commit 001802b

Browse files
committed
Fix webpack config
1 parent 32e08a1 commit 001802b

File tree

1 file changed

+30
-8
lines changed

1 file changed

+30
-8
lines changed

webpack.config.js

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,30 @@ import webpack from "webpack";
66

77
const __dirname = path.dirname(fileURLToPath(import.meta.url));
88

9+
/**
10+
* Plugin to strip the "node:" prefix from module requests.
11+
*
12+
* This is necessary to ensure both web and node builds work correctly,
13+
* otherwise we would get an error like:
14+
* ```
15+
* Module build failed: UnhandledSchemeError: Reading from "node:path" is not handled by plugins (Unhandled scheme).
16+
* Webpack supports "data:" and "file:" URIs by default.
17+
* You may need an additional plugin to handle "node:" URIs.
18+
* ```
19+
*
20+
* NOTE: We then do not need to use the `node:` prefix in the resolve.alias configuration.
21+
*/
22+
class StripNodePrefixPlugin extends webpack.NormalModuleReplacementPlugin {
23+
constructor() {
24+
super(
25+
/^node:(.+)$/,
26+
resource => {
27+
resource.request = resource.request.replace(/^node:/, '');
28+
}
29+
);
30+
}
31+
}
32+
933
/**
1034
* Plugin to post-process build files. Required to solve certain issues with ESM module output.
1135
* See https://github.com/webpack/webpack/issues/17121 for more information.
@@ -56,7 +80,6 @@ function buildConfig({
5680
plugins = [],
5781
} = {}) {
5882
const outputModule = type === "module";
59-
6083
const alias = Object.fromEntries(
6184
ignoreModules.map((module) => [module, false]),
6285
);
@@ -77,6 +100,9 @@ function buildConfig({
77100
},
78101
assetModuleFilename: "[name][ext]",
79102
chunkFormat: false,
103+
104+
// https://github.com/huggingface/transformers.js/issues/926
105+
publicPath: "",
80106
},
81107
optimization: {
82108
minimize: true,
@@ -144,7 +170,7 @@ const NODE_EXTERNAL_MODULES = [
144170
];
145171

146172
// Do not bundle onnxruntime-node or sharp when packaging for the web.
147-
const WEB_IGNORE_MODULES = ["onnxruntime-node", "sharp"];
173+
const WEB_IGNORE_MODULES = ["onnxruntime-node", "sharp", "fs", "path", "url"];
148174

149175
// Do not bundle the following modules with webpack (mark as external)
150176
const WEB_EXTERNAL_MODULES = [
@@ -159,9 +185,7 @@ const WEB_BUILD = buildConfig({
159185
ignoreModules: WEB_IGNORE_MODULES,
160186
externalModules: WEB_EXTERNAL_MODULES,
161187
plugins: [
162-
new webpack.IgnorePlugin({
163-
resourceRegExp: /^node:/,
164-
}),
188+
new StripNodePrefixPlugin()
165189
]
166190
});
167191

@@ -170,9 +194,7 @@ const BUNDLE_BUILD = buildConfig({
170194
type: "module",
171195
ignoreModules: WEB_IGNORE_MODULES,
172196
plugins: [
173-
new webpack.IgnorePlugin({
174-
resourceRegExp: /^node:/,
175-
}),
197+
new StripNodePrefixPlugin(),
176198
new PostBuildPlugin(),
177199
],
178200
});

0 commit comments

Comments
 (0)