diff --git a/packages/vite/src/node/build.ts b/packages/vite/src/node/build.ts index 4b26a958939ffa..ea40914fdb44c8 100644 --- a/packages/vite/src/node/build.ts +++ b/packages/vite/src/node/build.ts @@ -426,6 +426,7 @@ async function doBuild( exports: ssr ? 'named' : 'auto', sourcemap: options.sourcemap, name: libOptions ? libOptions.name : undefined, + generatedCode: 'es2015', entryFileNames: ssr ? `[name].js` : libOptions diff --git a/packages/vite/src/node/plugins/esbuild.ts b/packages/vite/src/node/plugins/esbuild.ts index 5250f4baf7687b..7b02af2152cb70 100644 --- a/packages/vite/src/node/plugins/esbuild.ts +++ b/packages/vite/src/node/plugins/esbuild.ts @@ -27,7 +27,7 @@ import { searchForWorkspaceRoot } from '..' const debug = createDebugger('vite:esbuild') const INJECT_HELPERS_IIFE_RE = - /(.*)(var [^\s]+=function\([^)]*?\){"use strict";)(.*)/ + /(.*)((?:const|var) [^\s]+=function\([^)]*?\){"use strict";)(.*)/ const INJECT_HELPERS_UMD_RE = /(.*)(\(function\([^)]*?\){.+amd.+function\([^)]*?\){"use strict";)(.*)/ diff --git a/playground/lib/__tests__/lib.spec.ts b/playground/lib/__tests__/lib.spec.ts index c64ccfa075d0e4..f8eea2b41847b8 100644 --- a/playground/lib/__tests__/lib.spec.ts +++ b/playground/lib/__tests__/lib.spec.ts @@ -31,7 +31,7 @@ describe.runIf(isBuild)('build', () => { 'utf-8' ) // esbuild helpers are injected inside of the IIFE wrapper - expect(code).toMatch(/^var MyLib=function\(\){"use strict";/) + expect(code).toMatch(/^const MyLib=function\(\){"use strict";/) }) test('Library mode does not include `preload`', async () => {