@@ -24,36 +24,13 @@ class PostBuildPlugin {
2424 const file = path . join ( dist , ORT_BUNDLE_FILE ) ;
2525 if ( fs . existsSync ( file ) ) fs . unlinkSync ( file ) ;
2626 }
27-
27+
2828 // 2. Copy unbundled JSEP file
2929 {
3030 const src = path . join ( __dirname , 'node_modules/onnxruntime-web/dist' , ORT_JSEP_FILE ) ;
3131 const dest = path . join ( dist , ORT_JSEP_FILE ) ;
3232 fs . copyFileSync ( src , dest ) ;
3333 }
34-
35- // 3. Replace strings in certain files
36- {
37- const files = [ 'transformers.js' , 'transformers.min.js' ] ;
38- for ( const file of files ) {
39- const filePath = path . join ( dist , file ) ;
40- let content = fs . readFileSync ( filePath , 'utf8' ) ;
41- content = content . replace (
42- // Replace all instances of `new URL("./", import.meta.url)` with `new URL(import.meta.url)`,
43- // as it causes several issues with build tools and bundlers.
44- //
45- // See the following issues for more information:
46- // - https://github.com/huggingface/transformers.js/issues/911
47- // - https://github.com/huggingface/transformers.js/issues/984
48- // - https://github.com/huggingface/transformers.js/issues/980
49- // - https://github.com/huggingface/transformers.js/issues/1021
50- // - https://github.com/huggingface/transformers.js/issues/1026
51- new RegExp ( 'new URL\\(["\']\\.\\\/["\'],\\s*import\\.meta\\.url\\)' , 'gm' ) ,
52- "new URL(import.meta.url)" ,
53- ) ;
54- fs . writeFileSync ( filePath , content , 'utf8' ) ;
55- }
56- }
5734 } ) ;
5835 }
5936}
@@ -98,7 +75,7 @@ function buildConfig({
9875 type,
9976 } ,
10077 assetModuleFilename : "[name][ext]" ,
101- chunkFormat : "module" ,
78+ chunkFormat : false ,
10279 } ,
10380 optimization : {
10481 minimize : true ,
@@ -157,28 +134,48 @@ const NODE_IGNORE_MODULES = ["onnxruntime-web"];
157134// NOTE: This is necessary for both type="module" and type="commonjs",
158135// and will be ignored when building for web (only used for node/deno)
159136const NODE_EXTERNAL_MODULES = [
137+ "onnxruntime-common" ,
160138 "onnxruntime-node" ,
161139 "sharp" ,
162140 "fs" ,
163141 "path" ,
164142 "url" ,
165143] ;
166144
145+ // Do not bundle onnxruntime-node when packaging for the web.
146+ const WEB_IGNORE_MODULES = [ "onnxruntime-node" ] ;
147+
148+ // Do not bundle the following modules with webpack (mark as external)
149+ const WEB_EXTERNAL_MODULES = [
150+ "onnxruntime-common" ,
151+ "onnxruntime-web" ,
152+ ] ;
153+
167154// Web-only build
168155const WEB_BUILD = buildConfig ( {
156+ name : ".web" ,
157+ type : "module" ,
158+ ignoreModules : WEB_IGNORE_MODULES ,
159+ externalModules : WEB_EXTERNAL_MODULES ,
160+ } ) ;
161+
162+ // Web-only build, bundled with onnxruntime-web
163+ const BUNDLE_BUILD = buildConfig ( {
169164 type : "module" ,
170165 plugins : [ new PostBuildPlugin ( ) ] ,
171166} ) ;
172167
173168// Node-compatible builds
174169const NODE_BUILDS = [
175170 buildConfig ( {
171+ name : ".node" ,
176172 suffix : ".mjs" ,
177173 type : "module" ,
178174 ignoreModules : NODE_IGNORE_MODULES ,
179175 externalModules : NODE_EXTERNAL_MODULES ,
180176 } ) ,
181177 buildConfig ( {
178+ name : ".node" ,
182179 suffix : ".cjs" ,
183180 type : "commonjs" ,
184181 ignoreModules : NODE_IGNORE_MODULES ,
@@ -188,6 +185,6 @@ const NODE_BUILDS = [
188185
189186// When running with `webpack serve`, only build the web target.
190187const BUILDS = process . env . WEBPACK_SERVE
191- ? [ WEB_BUILD ]
192- : [ WEB_BUILD , ...NODE_BUILDS ] ;
188+ ? [ BUNDLE_BUILD ]
189+ : [ BUNDLE_BUILD , WEB_BUILD , ...NODE_BUILDS ] ;
193190export default BUILDS ;
0 commit comments