diff --git a/playground/lib/__tests__/lib.spec.ts b/playground/lib/__tests__/lib.spec.ts index d4ced107fdea7a..b203535e0154da 100644 --- a/playground/lib/__tests__/lib.spec.ts +++ b/playground/lib/__tests__/lib.spec.ts @@ -39,6 +39,15 @@ describe.runIf(isBuild)('build', () => { ) }) + test('restrisct-helpers-injection', async () => { + const code = readFile( + 'dist/helpers-injection/my-lib-custom-filename.iife.js', + ) + expect(code).toMatch( + `'"use strict"; return (' + expressionSyntax + ").constructor;"`, + ) + }) + test('Library mode does not include `preload`', async () => { await untilUpdated( () => page.textContent('.dynamic-import-message'), diff --git a/playground/lib/__tests__/serve.ts b/playground/lib/__tests__/serve.ts index 0329eb34af56e4..44e1728ffd6086 100644 --- a/playground/lib/__tests__/serve.ts +++ b/playground/lib/__tests__/serve.ts @@ -67,6 +67,15 @@ export async function serve(): Promise<{ close(): Promise }> { configFile: path.resolve(__dirname, '../vite.nominify.config.js'), }) + await build({ + root: rootDir, + logLevel: 'warn', // output esbuild warns + configFile: path.resolve( + __dirname, + '../vite.helpers-injection.config.js', + ), + }) + // start static file server const serve = sirv(path.resolve(rootDir, 'dist')) const httpServer = http.createServer((req, res) => { diff --git a/playground/lib/src/main-helpers-injection.js b/playground/lib/src/main-helpers-injection.js new file mode 100644 index 00000000000000..5a7fce534d2db9 --- /dev/null +++ b/playground/lib/src/main-helpers-injection.js @@ -0,0 +1,11 @@ +// Trigger wrong helpers injection if not properly constrained +;(function () { + var getEvalledConstructor = function (expressionSyntax) { + try { + return $Function( + '"use strict"; return (' + expressionSyntax + ').constructor;', + )() + } catch (e) {} + } + console.log(getEvalledConstructor(0)) +})() diff --git a/playground/lib/vite.helpers-injection.config.js b/playground/lib/vite.helpers-injection.config.js new file mode 100644 index 00000000000000..540134d30d57b5 --- /dev/null +++ b/playground/lib/vite.helpers-injection.config.js @@ -0,0 +1,19 @@ +import path from 'node:path' +import { defineConfig } from 'vite' + +// Check that helpers injection is properly constrained + +export default defineConfig({ + build: { + lib: { + entry: path.resolve(__dirname, 'src/main-helpers-injection.js'), + name: 'MyLib', + formats: ['iife'], + fileName: 'my-lib-custom-filename', + }, + minify: false, + outDir: 'dist/helpers-injection', + }, + plugins: [], + cacheDir: 'node_modules/.vite-helpers-injection', +})