1+ import { fileURLToPath } from "node:url" ;
2+ import path from "node:path" ;
3+ import fs from "node:fs" ;
4+ import webpack from "webpack" ;
15import TerserPlugin from "terser-webpack-plugin" ;
2- import { fileURLToPath } from "url" ;
3- import path from "path" ;
4- import fs from "fs" ;
56
67const __dirname = path . dirname ( fileURLToPath ( import . meta. url ) ) ;
78
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+ / ^ n o d e : ( .+ ) $ / ,
26+ resource => {
27+ resource . request = resource . request . replace ( / ^ n o d e : / , '' ) ;
28+ }
29+ ) ;
30+ }
31+ }
32+
833/**
934 * Plugin to post-process build files. Required to solve certain issues with ESM module output.
1035 * See https://github.com/webpack/webpack/issues/17121 for more information.
@@ -55,7 +80,6 @@ function buildConfig({
5580 plugins = [ ] ,
5681} = { } ) {
5782 const outputModule = type === "module" ;
58-
5983 const alias = Object . fromEntries (
6084 ignoreModules . map ( ( module ) => [ module , false ] ) ,
6185 ) ;
@@ -137,13 +161,15 @@ const NODE_EXTERNAL_MODULES = [
137161 "onnxruntime-common" ,
138162 "onnxruntime-node" ,
139163 "sharp" ,
140- "fs" ,
141- "path" ,
142- "url" ,
164+ "node: fs" ,
165+ "node: path" ,
166+ "node: url" ,
143167] ;
144168
145- // Do not bundle onnxruntime-node when packaging for the web.
146- const WEB_IGNORE_MODULES = [ "onnxruntime-node" ] ;
169+ // Do not bundle node-only packages when bundling for the web.
170+ // NOTE: We can exclude the "node:" prefix for built-in modules here,
171+ // since we apply the `StripNodePrefixPlugin` to strip it.
172+ const WEB_IGNORE_MODULES = [ "onnxruntime-node" , "sharp" , "fs" , "path" , "url" ] ;
147173
148174// Do not bundle the following modules with webpack (mark as external)
149175const WEB_EXTERNAL_MODULES = [
@@ -157,12 +183,19 @@ const WEB_BUILD = buildConfig({
157183 type : "module" ,
158184 ignoreModules : WEB_IGNORE_MODULES ,
159185 externalModules : WEB_EXTERNAL_MODULES ,
186+ plugins : [
187+ new StripNodePrefixPlugin ( )
188+ ]
160189} ) ;
161190
162191// Web-only build, bundled with onnxruntime-web
163192const BUNDLE_BUILD = buildConfig ( {
164193 type : "module" ,
165- plugins : [ new PostBuildPlugin ( ) ] ,
194+ ignoreModules : WEB_IGNORE_MODULES ,
195+ plugins : [
196+ new StripNodePrefixPlugin ( ) ,
197+ new PostBuildPlugin ( ) ,
198+ ] ,
166199} ) ;
167200
168201// Node-compatible builds
0 commit comments