@@ -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,44 @@ 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 the following modules with webpack (mark as external)
146
+ const WEB_EXTERNAL_MODULES = [
147
+ "onnxruntime-common" ,
148
+ "onnxruntime-web" ,
149
+ ] ;
150
+
167
151
// Web-only build
168
152
const 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 ( {
169
160
type : "module" ,
170
161
plugins : [ new PostBuildPlugin ( ) ] ,
171
162
} ) ;
172
163
173
164
// Node-compatible builds
174
165
const NODE_BUILDS = [
175
166
buildConfig ( {
167
+ name : ".node" ,
176
168
suffix : ".mjs" ,
177
169
type : "module" ,
178
170
ignoreModules : NODE_IGNORE_MODULES ,
179
171
externalModules : NODE_EXTERNAL_MODULES ,
180
172
} ) ,
181
173
buildConfig ( {
174
+ name : ".node" ,
182
175
suffix : ".cjs" ,
183
176
type : "commonjs" ,
184
177
ignoreModules : NODE_IGNORE_MODULES ,
@@ -189,5 +182,5 @@ const NODE_BUILDS = [
189
182
// When running with `webpack serve`, only build the web target.
190
183
const BUILDS = process . env . WEBPACK_SERVE
191
184
? [ WEB_BUILD ]
192
- : [ WEB_BUILD , ...NODE_BUILDS ] ;
185
+ : [ WEB_BUILD , BUNDLE_BUILD , ...NODE_BUILDS ] ;
193
186
export default BUILDS ;
0 commit comments