Skip to content

Commit 85f2db0

Browse files
authored
fix: accept umd without exports (#240)
1 parent a4d8319 commit 85f2db0

File tree

2 files changed

+114
-4
lines changed

2 files changed

+114
-4
lines changed

packages/vite/src/node/__tests__/plugins/oxc.spec.ts

Lines changed: 112 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ describe('transformWithOxc', () => {
159159
})
160160

161161
describe('renderChunk', () => {
162-
test('should inject helper for worker iife from esm', async () => {
162+
test('should inject helper for iife without exports from esm', async () => {
163163
const renderChunk = await createBuildOxcPluginRenderChunk('es2015')
164164
const result = await renderChunk(
165165
`(function() {
@@ -191,7 +191,7 @@ describe('renderChunk', () => {
191191
`)
192192
})
193193

194-
test('should inject helper for worker iife from cjs', async () => {
194+
test('should inject helper for iife without exports from cjs', async () => {
195195
const renderChunk = await createBuildOxcPluginRenderChunk('es2015')
196196
const result = await renderChunk(
197197
`(function() {
@@ -220,4 +220,114 @@ describe('renderChunk', () => {
220220
"
221221
`)
222222
})
223+
224+
test('should inject helper for iife with exports', async () => {
225+
const renderChunk = await createBuildOxcPluginRenderChunk('es2015')
226+
const result = await renderChunk(
227+
`var lib = (function(exports) {
228+
229+
230+
//#region entry.js
231+
(async () => {
232+
await new Promise((resolve) => setTimeout(resolve, 1e3));
233+
console.log("foo");
234+
})();
235+
const foo = "foo";
236+
237+
//#endregion
238+
exports.foo = foo;
239+
return exports;
240+
})({});`,
241+
'iife',
242+
)
243+
expect(result).toMatchInlineSnapshot(`
244+
"var lib = function(exports) {var babelHelpers=function(exports){function t(e,t,n,r,i,a,o){try{var s=e[a](o),c=s.value}catch(e){return void n(e)}s.done?t(c):Promise.resolve(c).then(r,i)}function n(e){return function(){var n=this,r=arguments;return new Promise(function(i,a){var o=e.apply(n,r);function s(e){t(o,i,a,s,c,\`next\`,e)}function c(e){t(o,i,a,s,c,\`throw\`,e)}s(void 0)})}}return exports.asyncToGenerator=n,exports}({});
245+
246+
//#region entry.js
247+
babelHelpers.asyncToGenerator(function* () {
248+
yield new Promise((resolve) => setTimeout(resolve, 1e3));
249+
console.log("foo");
250+
})();
251+
const foo = "foo";
252+
//#endregion
253+
exports.foo = foo;
254+
return exports;
255+
}({});
256+
"
257+
`)
258+
})
259+
260+
test('should inject helper for umd without exports', async () => {
261+
const renderChunk = await createBuildOxcPluginRenderChunk('es2015')
262+
const result = await renderChunk(
263+
`(function(factory) {
264+
265+
typeof define === 'function' && define.amd ? define([], factory) :
266+
factory();
267+
})(function() {
268+
269+
//#region entry.js
270+
(async () => {
271+
await new Promise((resolve) => setTimeout(resolve, 1e3));
272+
console.log("foo");
273+
})();
274+
275+
//#endregion
276+
});`,
277+
'umd',
278+
)
279+
expect(result).toMatchInlineSnapshot(`
280+
"(function(factory) {
281+
typeof define === "function" && define.amd ? define([], factory) : factory();
282+
})(function() {var babelHelpers=function(exports){function t(e,t,n,r,i,a,o){try{var s=e[a](o),c=s.value}catch(e){return void n(e)}s.done?t(c):Promise.resolve(c).then(r,i)}function n(e){return function(){var n=this,r=arguments;return new Promise(function(i,a){var o=e.apply(n,r);function s(e){t(o,i,a,s,c,\`next\`,e)}function c(e){t(o,i,a,s,c,\`throw\`,e)}s(void 0)})}}return exports.asyncToGenerator=n,exports}({});
283+
284+
//#region entry.js
285+
babelHelpers.asyncToGenerator(function* () {
286+
yield new Promise((resolve) => setTimeout(resolve, 1e3));
287+
console.log("foo");
288+
})();
289+
//#endregion
290+
});
291+
"
292+
`)
293+
})
294+
295+
test('should inject helper for umd with exports', async () => {
296+
const renderChunk = await createBuildOxcPluginRenderChunk('es2015')
297+
const result = await renderChunk(
298+
`(function(global, factory) {
299+
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
300+
typeof define === 'function' && define.amd ? define(['exports'], factory) :
301+
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory((global.lib = {})));
302+
})(this, function(exports) {
303+
304+
//#region entry.js
305+
(async () => {
306+
await new Promise((resolve) => setTimeout(resolve, 1e3));
307+
console.log("foo");
308+
})();
309+
const foo = "foo";
310+
311+
//#endregion
312+
exports.foo = foo;
313+
});`,
314+
'umd',
315+
)
316+
expect(result).toMatchInlineSnapshot(`
317+
"(function(global, factory) {
318+
typeof exports === "object" && typeof module !== "undefined" ? factory(exports) : typeof define === "function" && define.amd ? define(["exports"], factory) : (global = typeof globalThis !== "undefined" ? globalThis : global || self, factory(global.lib = {}));
319+
})(this, function(exports) {var babelHelpers=function(exports){function t(e,t,n,r,i,a,o){try{var s=e[a](o),c=s.value}catch(e){return void n(e)}s.done?t(c):Promise.resolve(c).then(r,i)}function n(e){return function(){var n=this,r=arguments;return new Promise(function(i,a){var o=e.apply(n,r);function s(e){t(o,i,a,s,c,\`next\`,e)}function c(e){t(o,i,a,s,c,\`throw\`,e)}s(void 0)})}}return exports.asyncToGenerator=n,exports}({});
320+
321+
//#region entry.js
322+
babelHelpers.asyncToGenerator(function* () {
323+
yield new Promise((resolve) => setTimeout(resolve, 1e3));
324+
console.log("foo");
325+
})();
326+
const foo = "foo";
327+
//#endregion
328+
exports.foo = foo;
329+
});
330+
"
331+
`)
332+
})
223333
})

packages/vite/src/node/plugins/oxc.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,9 @@ import { loadTsconfigJsonForFile } from './esbuild'
3535
// IIFE content looks like `var MyLib = (function() {`.
3636
export const IIFE_BEGIN_RE =
3737
/(?:(?:const|var)\s+\S+\s*=\s*|^|\n)\(?function\([^()]*\)\s*\{(?:\s*"use strict";)?/
38-
// UMD content looks like `(this, function(exports) {`.
38+
// UMD content looks like `(this, function(exports) {` or `factory(); })(function() {`.
3939
export const UMD_BEGIN_RE =
40-
/\(this,\s*function\([^()]*\)\s*\{(?:\s*"use strict";)?/
40+
/(?:\(this,\s*function\([^()]+\)\s*\{|factory\(\);\s*\}\)\(function\(\)\s*\{)(?:\s*"use strict";)?/
4141

4242
const jsxExtensionsRE = /\.(?:j|t)sx\b/
4343
const validExtensionRE = /\.\w+$/

0 commit comments

Comments
 (0)