From 03734417cde10807ab2dd0d71b08c26081aac0b7 Mon Sep 17 00:00:00 2001 From: ygj6 <7699524+ygj6@users.noreply.github.com> Date: Mon, 13 Dec 2021 22:47:02 +0800 Subject: [PATCH] fix(ssr): robust regexp to check cjs content (#6053) --- .../ssr-deps/__tests__/ssr-deps.spec.ts | 21 +++++++++++++++++++ .../define-properties-exports/index.js | 8 +++++++ .../define-properties-exports/package.json | 5 +++++ .../ssr-deps/define-property-exports/index.js | 5 +++++ .../define-property-exports/package.json | 5 +++++ .../only-object-assigned-exports/index.js | 5 +++++ .../only-object-assigned-exports/package.json | 5 +++++ packages/playground/ssr-deps/package.json | 3 +++ packages/playground/ssr-deps/src/app.js | 12 +++++++++++ packages/playground/ssr-deps/vite.config.js | 8 ------- packages/vite/src/node/ssr/ssrExternal.ts | 5 ++++- pnpm-lock.yaml | 15 +++++++++++++ 12 files changed, 88 insertions(+), 9 deletions(-) create mode 100644 packages/playground/ssr-deps/define-properties-exports/index.js create mode 100644 packages/playground/ssr-deps/define-properties-exports/package.json create mode 100644 packages/playground/ssr-deps/define-property-exports/index.js create mode 100644 packages/playground/ssr-deps/define-property-exports/package.json create mode 100644 packages/playground/ssr-deps/only-object-assigned-exports/index.js create mode 100644 packages/playground/ssr-deps/only-object-assigned-exports/package.json delete mode 100644 packages/playground/ssr-deps/vite.config.js diff --git a/packages/playground/ssr-deps/__tests__/ssr-deps.spec.ts b/packages/playground/ssr-deps/__tests__/ssr-deps.spec.ts index 4922310a8b0ca8..8a201c9eb87455 100644 --- a/packages/playground/ssr-deps/__tests__/ssr-deps.spec.ts +++ b/packages/playground/ssr-deps/__tests__/ssr-deps.spec.ts @@ -50,3 +50,24 @@ test('msg from forwarded exports', async () => { 'Hello World!' ) }) + +test('msg from define properties exports', async () => { + await page.goto(url) + expect(await page.textContent('.define-properties-exports-msg')).toMatch( + 'Hello World!' + ) +}) + +test('msg from define property exports', async () => { + await page.goto(url) + expect(await page.textContent('.define-property-exports-msg')).toMatch( + 'Hello World!' + ) +}) + +test('msg from only object assigned exports', async () => { + await page.goto(url) + expect(await page.textContent('.only-object-assigned-exports-msg')).toMatch( + 'Hello World!' + ) +}) diff --git a/packages/playground/ssr-deps/define-properties-exports/index.js b/packages/playground/ssr-deps/define-properties-exports/index.js new file mode 100644 index 00000000000000..5bdd02be906469 --- /dev/null +++ b/packages/playground/ssr-deps/define-properties-exports/index.js @@ -0,0 +1,8 @@ +// prettier-ignore +Object.defineProperties ( exports , { + hello: { + value() { + return 'Hello World!' + } + } +}) diff --git a/packages/playground/ssr-deps/define-properties-exports/package.json b/packages/playground/ssr-deps/define-properties-exports/package.json new file mode 100644 index 00000000000000..7555314741d981 --- /dev/null +++ b/packages/playground/ssr-deps/define-properties-exports/package.json @@ -0,0 +1,5 @@ +{ + "name": "define-properties-exports", + "version": "0.0.0", + "private": true +} diff --git a/packages/playground/ssr-deps/define-property-exports/index.js b/packages/playground/ssr-deps/define-property-exports/index.js new file mode 100644 index 00000000000000..4506dd6200051e --- /dev/null +++ b/packages/playground/ssr-deps/define-property-exports/index.js @@ -0,0 +1,5 @@ +Object.defineProperty(exports, 'hello', { + value() { + return 'Hello World!' + } +}) diff --git a/packages/playground/ssr-deps/define-property-exports/package.json b/packages/playground/ssr-deps/define-property-exports/package.json new file mode 100644 index 00000000000000..02373646db832e --- /dev/null +++ b/packages/playground/ssr-deps/define-property-exports/package.json @@ -0,0 +1,5 @@ +{ + "name": "define-property-exports", + "version": "0.0.0", + "private": true +} diff --git a/packages/playground/ssr-deps/only-object-assigned-exports/index.js b/packages/playground/ssr-deps/only-object-assigned-exports/index.js new file mode 100644 index 00000000000000..b6a4ab368b133d --- /dev/null +++ b/packages/playground/ssr-deps/only-object-assigned-exports/index.js @@ -0,0 +1,5 @@ +Object.assign(exports, { + hello() { + return 'Hello World!' + } +}) diff --git a/packages/playground/ssr-deps/only-object-assigned-exports/package.json b/packages/playground/ssr-deps/only-object-assigned-exports/package.json new file mode 100644 index 00000000000000..b74c5215385df3 --- /dev/null +++ b/packages/playground/ssr-deps/only-object-assigned-exports/package.json @@ -0,0 +1,5 @@ +{ + "name": "only-object-assigned-exports", + "version": "0.0.0", + "private": true +} diff --git a/packages/playground/ssr-deps/package.json b/packages/playground/ssr-deps/package.json index 4ba3c45031731b..a4dfb83e6a0783 100644 --- a/packages/playground/ssr-deps/package.json +++ b/packages/playground/ssr-deps/package.json @@ -10,8 +10,11 @@ }, "dependencies": { "bcrypt": "^5.0.1", + "define-properties-exports": "file:./define-properties-exports", + "define-property-exports": "file:./define-property-exports", "forwarded-export": "file:./forwarded-export", "object-assigned-exports": "file:./object-assigned-exports", + "only-object-assigned-exports": "file:./only-object-assigned-exports", "primitive-export": "file:./primitive-export", "read-file-content": "file:./read-file-content", "ts-transpiled-exports": "file:./ts-transpiled-exports" diff --git a/packages/playground/ssr-deps/src/app.js b/packages/playground/ssr-deps/src/app.js index bbe6ec7f396219..da8883c6a9452b 100644 --- a/packages/playground/ssr-deps/src/app.js +++ b/packages/playground/ssr-deps/src/app.js @@ -5,6 +5,9 @@ import tsDefaultExport, { hello as tsNamedExport } from 'ts-transpiled-exports' import objectAssignedExports from 'object-assigned-exports' import forwardedExport from 'forwarded-export' import bcrypt from 'bcrypt' +import definePropertiesExports from 'define-properties-exports' +import definePropertyExports from 'define-property-exports' +import onlyObjectAssignedExports from 'only-object-assigned-exports' export async function render(url, rootDir) { let html = '' @@ -29,5 +32,14 @@ export async function render(url, rootDir) { const forwardedExportMessage = forwardedExport.hello() html += `\n
` + const definePropertiesExportsMsg = definePropertiesExports.hello() + html += `\nmessage from define-properties-exports: ${definePropertiesExportsMsg}
` + + const definePropertyExportsMsg = definePropertyExports.hello() + html += `\nmessage from define-property-exports: ${definePropertyExportsMsg}
` + + const onlyObjectAssignedExportsMessage = onlyObjectAssignedExports.hello() + html += `\nmessage from only-object-assigned-exports: ${onlyObjectAssignedExportsMessage}
` + return html + '\n' } diff --git a/packages/playground/ssr-deps/vite.config.js b/packages/playground/ssr-deps/vite.config.js deleted file mode 100644 index 96a2eb87f4cb2d..00000000000000 --- a/packages/playground/ssr-deps/vite.config.js +++ /dev/null @@ -1,8 +0,0 @@ -/** - * @type {import('vite').UserConfig} - */ -module.exports = { - ssr: { - external: ['object-assigned-exports'] - } -} diff --git a/packages/vite/src/node/ssr/ssrExternal.ts b/packages/vite/src/node/ssr/ssrExternal.ts index 3549d5653dfc95..07cfaa9c238bcf 100644 --- a/packages/vite/src/node/ssr/ssrExternal.ts +++ b/packages/vite/src/node/ssr/ssrExternal.ts @@ -64,6 +64,9 @@ export function resolveSSRExternal( return externals } +const CJS_CONTENT_RE = + /\bmodule\.exports\b|\bexports[.\[]|\brequire\s*\(|\bObject\.(defineProperty|defineProperties|assign)\s*\(\s*exports\b/ + // do we need to do this ahead of time or could we do it lazily? function collectExternals( root: string, @@ -157,7 +160,7 @@ function collectExternals( } // check if the entry is cjs const content = fs.readFileSync(esmEntry, 'utf-8') - if (/\bmodule\.exports\b|\bexports[.\[]|\brequire\s*\(/.test(content)) { + if (CJS_CONTENT_RE.test(content)) { ssrExternals.add(id) continue } diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 2af95d41ebc029..6c16ab43a73020 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -458,16 +458,22 @@ importers: specifiers: bcrypt: ^5.0.1 cross-env: ^7.0.3 + define-properties-exports: file:./define-properties-exports + define-property-exports: file:./define-property-exports express: ^4.17.1 forwarded-export: file:./forwarded-export object-assigned-exports: file:./object-assigned-exports + only-object-assigned-exports: file:./only-object-assigned-exports primitive-export: file:./primitive-export read-file-content: file:./read-file-content ts-transpiled-exports: file:./ts-transpiled-exports dependencies: bcrypt: 5.0.1 + define-properties-exports: link:define-properties-exports + define-property-exports: link:define-property-exports forwarded-export: link:forwarded-export object-assigned-exports: link:object-assigned-exports + only-object-assigned-exports: link:only-object-assigned-exports primitive-export: link:primitive-export read-file-content: link:read-file-content ts-transpiled-exports: link:ts-transpiled-exports @@ -475,12 +481,21 @@ importers: cross-env: 7.0.3 express: 4.17.1 + packages/playground/ssr-deps/define-properties-exports: + specifiers: {} + + packages/playground/ssr-deps/define-property-exports: + specifiers: {} + packages/playground/ssr-deps/forwarded-export: specifiers: {} packages/playground/ssr-deps/object-assigned-exports: specifiers: {} + packages/playground/ssr-deps/only-object-assigned-exports: + specifiers: {} + packages/playground/ssr-deps/primitive-export: specifiers: {}