Skip to content

Commit 12f8197

Browse files
authored
fix: accept umd with only default export (#305)
1 parent 139d016 commit 12f8197

File tree

2 files changed

+41
-2
lines changed

2 files changed

+41
-2
lines changed

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

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,4 +330,43 @@ exports.foo = foo;
330330
"
331331
`)
332332
})
333+
334+
test('should inject helper for umd with only default export', async () => {
335+
const renderChunk = await createBuildOxcPluginRenderChunk('es2015')
336+
const result = await renderChunk(
337+
`(function(global, factory) {
338+
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
339+
typeof define === 'function' && define.amd ? define([], factory) :
340+
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, (global.lib = factory()));
341+
})(this, function() {
342+
343+
//#region entry.js
344+
(async () => {
345+
await new Promise((resolve) => setTimeout(resolve, 1e3));
346+
console.log("foo");
347+
})();
348+
var index_default = "foo";
349+
350+
//#endregion
351+
return index_default;
352+
});`,
353+
'umd',
354+
)
355+
expect(result).toMatchInlineSnapshot(`
356+
"(function(global, factory) {
357+
typeof exports === "object" && typeof module !== "undefined" ? module.exports = factory() : typeof define === "function" && define.amd ? define([], factory) : (global = typeof globalThis !== "undefined" ? globalThis : global || self, global.lib = factory());
358+
})(this, 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}({});
359+
360+
//#region entry.js
361+
babelHelpers.asyncToGenerator(function* () {
362+
yield new Promise((resolve) => setTimeout(resolve, 1e3));
363+
console.log("foo");
364+
})();
365+
var index_default = "foo";
366+
//#endregion
367+
return index_default;
368+
});
369+
"
370+
`)
371+
})
333372
})

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) {` or `factory(); })(function() {`.
38+
// UMD content looks like `})(this, function(exports, external1, external2) {`.
3939
export const UMD_BEGIN_RE =
40-
/(?:\(this,\s*function\([^()]+\)\s*\{|factory\(\);\s*\}\)\(function\(\)\s*\{)(?:\s*"use strict";)?/
40+
/\}\)\((?:this,\s*)?function\([^()]*\)\s*\{(?:\s*"use strict";)?/
4141

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

0 commit comments

Comments
 (0)