@@ -24,36 +24,13 @@ class PostBuildPlugin {
24
24
const file = path . join ( dist , ORT_BUNDLE_FILE ) ;
25
25
if ( fs . existsSync ( file ) ) fs . unlinkSync ( file ) ;
26
26
}
27
-
27
+
28
28
// 2. Copy unbundled JSEP file
29
29
{
30
30
const src = path . join ( __dirname , 'node_modules/onnxruntime-web/dist' , ORT_JSEP_FILE ) ;
31
31
const dest = path . join ( dist , ORT_JSEP_FILE ) ;
32
32
fs . copyFileSync ( src , dest ) ;
33
33
}
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
- }
57
34
} ) ;
58
35
}
59
36
}
@@ -98,7 +75,7 @@ function buildConfig({
98
75
type,
99
76
} ,
100
77
assetModuleFilename : "[name][ext]" ,
101
- chunkFormat : "module" ,
78
+ chunkFormat : false ,
102
79
} ,
103
80
optimization : {
104
81
minimize : true ,
@@ -157,28 +134,48 @@ const NODE_IGNORE_MODULES = ["onnxruntime-web"];
157
134
// NOTE: This is necessary for both type="module" and type="commonjs",
158
135
// and will be ignored when building for web (only used for node/deno)
159
136
const NODE_EXTERNAL_MODULES = [
137
+ "onnxruntime-common" ,
160
138
"onnxruntime-node" ,
161
139
"sharp" ,
162
140
"fs" ,
163
141
"path" ,
164
142
"url" ,
165
143
] ;
166
144
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
+
167
154
// Web-only build
168
155
const 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 ( {
169
164
type : "module" ,
170
165
plugins : [ new PostBuildPlugin ( ) ] ,
171
166
} ) ;
172
167
173
168
// Node-compatible builds
174
169
const NODE_BUILDS = [
175
170
buildConfig ( {
171
+ name : ".node" ,
176
172
suffix : ".mjs" ,
177
173
type : "module" ,
178
174
ignoreModules : NODE_IGNORE_MODULES ,
179
175
externalModules : NODE_EXTERNAL_MODULES ,
180
176
} ) ,
181
177
buildConfig ( {
178
+ name : ".node" ,
182
179
suffix : ".cjs" ,
183
180
type : "commonjs" ,
184
181
ignoreModules : NODE_IGNORE_MODULES ,
@@ -188,6 +185,6 @@ const NODE_BUILDS = [
188
185
189
186
// When running with `webpack serve`, only build the web target.
190
187
const BUILDS = process . env . WEBPACK_SERVE
191
- ? [ WEB_BUILD ]
192
- : [ WEB_BUILD , ...NODE_BUILDS ] ;
188
+ ? [ BUNDLE_BUILD ]
189
+ : [ BUNDLE_BUILD , WEB_BUILD , ...NODE_BUILDS ] ;
193
190
export default BUILDS ;
0 commit comments