Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 63 additions & 0 deletions packages/vite/src/node/__tests__/plugins/oxc.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,37 @@ return exports;
`)
})

test('should inject helper for iife with nested name', async () => {
const renderChunk = await createBuildOxcPluginRenderChunk('es2015')
const result = await renderChunk(
`this.nested = this.nested || {};
this.nested.lib = (function(exports) {

Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });

//#region a.ts
const foo = "foo";

//#endregion
exports.foo = foo;
return exports;
})({});`,
'iife',
)
expect(result).toMatchInlineSnapshot(`
"this.nested = this.nested || {};
this.nested.lib = (function(exports) {
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
//#region a.ts
const foo = "foo";
//#endregion
exports.foo = foo;
return exports;
})({});
"
`)
})

test('should inject helper for umd without exports', async () => {
const renderChunk = await createBuildOxcPluginRenderChunk('es2015')
const result = await renderChunk(
Expand Down Expand Up @@ -392,6 +423,38 @@ return index_default;
`)
})

test('should inject helper for umd with nested name', async () => {
const renderChunk = await createBuildOxcPluginRenderChunk('es2015')
const result = await renderChunk(
`(function(global, factory) {
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.nested = global.nested || {},global.nested.lib = {})));
})(this, function(exports) {
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });

//#region a.ts
const foo = "foo";

//#endregion
exports.foo = foo;
});`,
'umd',
)
expect(result).toMatchInlineSnapshot(`
"(function(global, factory) {
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.nested = global.nested || {}, global.nested.lib = {})));
})(this, function(exports) {
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
//#region a.ts
const foo = "foo";
//#endregion
exports.foo = foo;
});
"
`)
})

test('should inject multiple helpers', async () => {
const renderChunk = await createBuildOxcPluginRenderChunk('es2015')
const result = await renderChunk(
Expand Down
4 changes: 2 additions & 2 deletions packages/vite/src/node/plugins/oxc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ import type { Logger } from '../logger'
import type { ESBuildOptions, TSCompilerOptions } from './esbuild'
import { loadTsconfigJsonForFile } from './esbuild'

// IIFE content looks like `var MyLib = (function() {`.
// IIFE content looks like `var MyLib = (function() {` or `this.nested.myLib = (function() {`.
export const IIFE_BEGIN_RE: RegExp =
/(?:(?:const|var)\s+\S+\s*=\s*|^|\n)\(?function\([^()]*\)\s*\{(?:\s*"use strict";)?/
/(?:(?:(?:const|var)\s+[^.\s]+|[^.\s]+\.[^.\s]+\.[^.\s]+)\s*=\s*|^|\n)\(?function\([^()]*\)\s*\{(?:\s*"use strict";)?/
// UMD content looks like `})(this, function(exports, external1, external2) {`.
export const UMD_BEGIN_RE: RegExp =
/\}\)\((?:this,\s*)?function\([^()]*\)\s*\{(?:\s*"use strict";)?/
Expand Down
Loading