@@ -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,44 @@ 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 the following modules with webpack (mark as external)
146+ const WEB_EXTERNAL_MODULES = [
147+ "onnxruntime-common" ,
148+ "onnxruntime-web" ,
149+ ] ;
150+
167151// Web-only build
168152const WEB_BUILD = buildConfig ( {
153+ name : ".web" ,
154+ type : "module" ,
155+ externalModules : WEB_EXTERNAL_MODULES ,
156+ } ) ;
157+
158+ // Web-only build, bundled with onnxruntime-web
159+ const BUNDLE_BUILD = buildConfig ( {
169160 type : "module" ,
170161 plugins : [ new PostBuildPlugin ( ) ] ,
171162} ) ;
172163
173164// Node-compatible builds
174165const NODE_BUILDS = [
175166 buildConfig ( {
167+ name : ".node" ,
176168 suffix : ".mjs" ,
177169 type : "module" ,
178170 ignoreModules : NODE_IGNORE_MODULES ,
179171 externalModules : NODE_EXTERNAL_MODULES ,
180172 } ) ,
181173 buildConfig ( {
174+ name : ".node" ,
182175 suffix : ".cjs" ,
183176 type : "commonjs" ,
184177 ignoreModules : NODE_IGNORE_MODULES ,
@@ -189,5 +182,5 @@ const NODE_BUILDS = [
189182// When running with `webpack serve`, only build the web target.
190183const BUILDS = process . env . WEBPACK_SERVE
191184 ? [ WEB_BUILD ]
192- : [ WEB_BUILD , ...NODE_BUILDS ] ;
185+ : [ WEB_BUILD , BUNDLE_BUILD , ...NODE_BUILDS ] ;
193186export default BUILDS ;
0 commit comments