Skip to content

Commit

Permalink
fix(ssr): robust regexp to check cjs content (#6053)
Browse files Browse the repository at this point in the history
  • Loading branch information
ygj6 authored Dec 13, 2021
1 parent ab06081 commit 0373441
Show file tree
Hide file tree
Showing 12 changed files with 88 additions and 9 deletions.
21 changes: 21 additions & 0 deletions packages/playground/ssr-deps/__tests__/ssr-deps.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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!'
)
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// prettier-ignore
Object.defineProperties ( exports , {
hello: {
value() {
return 'Hello World!'
}
}
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"name": "define-properties-exports",
"version": "0.0.0",
"private": true
}
5 changes: 5 additions & 0 deletions packages/playground/ssr-deps/define-property-exports/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Object.defineProperty(exports, 'hello', {
value() {
return 'Hello World!'
}
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"name": "define-property-exports",
"version": "0.0.0",
"private": true
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Object.assign(exports, {
hello() {
return 'Hello World!'
}
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"name": "only-object-assigned-exports",
"version": "0.0.0",
"private": true
}
3 changes: 3 additions & 0 deletions packages/playground/ssr-deps/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
12 changes: 12 additions & 0 deletions packages/playground/ssr-deps/src/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = ''
Expand All @@ -29,5 +32,14 @@ export async function render(url, rootDir) {
const forwardedExportMessage = forwardedExport.hello()
html += `\n<p class="forwarded-export-message">message from forwarded-export: ${forwardedExportMessage}</p>`

const definePropertiesExportsMsg = definePropertiesExports.hello()
html += `\n<p class="define-properties-exports-msg">message from define-properties-exports: ${definePropertiesExportsMsg}</p>`

const definePropertyExportsMsg = definePropertyExports.hello()
html += `\n<p class="define-property-exports-msg">message from define-property-exports: ${definePropertyExportsMsg}</p>`

const onlyObjectAssignedExportsMessage = onlyObjectAssignedExports.hello()
html += `\n<p class="only-object-assigned-exports-msg">message from only-object-assigned-exports: ${onlyObjectAssignedExportsMessage}</p>`

return html + '\n'
}
8 changes: 0 additions & 8 deletions packages/playground/ssr-deps/vite.config.js

This file was deleted.

5 changes: 4 additions & 1 deletion packages/vite/src/node/ssr/ssrExternal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
}
Expand Down
15 changes: 15 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 0373441

Please sign in to comment.