Skip to content

Commit

Permalink
feat: relax dep browser externals as warning (#9837)
Browse files Browse the repository at this point in the history
  • Loading branch information
bluwy committed Aug 25, 2022
1 parent 1ee0364 commit 71cb374
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 11 deletions.
2 changes: 1 addition & 1 deletion packages/vite/src/node/optimizer/esbuildDepPlugin.ts
Expand Up @@ -276,7 +276,7 @@ module.exports = Object.create(new Proxy({}, {
key !== 'constructor' &&
key !== 'splice'
) {
throw new Error(\`Module "${path}" has been externalized for browser compatibility. Cannot access "${path}.\${key}" in client code.\`)
console.warn(\`Module "${path}" has been externalized for browser compatibility. Cannot access "${path}.\${key}" in client code.\`)
}
}
}))`
Expand Down
23 changes: 13 additions & 10 deletions playground/optimize-deps/__tests__/optimize-deps.spec.ts
Expand Up @@ -149,16 +149,23 @@ test('flatten id should generate correctly', async () => {
test.runIf(isServe)('error on builtin modules usage', () => {
expect(browserLogs).toEqual(
expect.arrayContaining([
// from dep-with-builtin-module-esm top-level try-catch
// from dep-with-builtin-module-esm
expect.stringMatching(/dep-with-builtin-module-esm.*is not a function/),
// dep-with-builtin-module-esm warnings
expect.stringContaining(
'dep-with-builtin-module-esm Error: Module "fs" has been externalized for browser compatibility. Cannot access "fs.readFileSync" in client code.'
'Module "fs" has been externalized for browser compatibility. Cannot access "fs.readFileSync" in client code.'
),
expect.stringContaining(
'dep-with-builtin-module-esm Error: Module "path" has been externalized for browser compatibility. Cannot access "path.join" in client code.'
'Module "path" has been externalized for browser compatibility. Cannot access "path.join" in client code.'
),
// from dep-with-builtin-module-cjs top-level try-catch
// from dep-with-builtin-module-cjs
expect.stringMatching(/dep-with-builtin-module-cjs.*is not a function/),
// dep-with-builtin-module-cjs warnings
expect.stringContaining(
'dep-with-builtin-module-cjs Error: Module "path" has been externalized for browser compatibility. Cannot access "path.join" in client code.'
'Module "fs" has been externalized for browser compatibility. Cannot access "fs.readFileSync" in client code.'
),
expect.stringContaining(
'Module "path" has been externalized for browser compatibility. Cannot access "path.join" in client code.'
)
])
)
Expand All @@ -167,11 +174,7 @@ test.runIf(isServe)('error on builtin modules usage', () => {
expect.arrayContaining([
// from user source code
'Module "buffer" has been externalized for browser compatibility. Cannot access "buffer.Buffer" in client code.',
'Module "child_process" has been externalized for browser compatibility. Cannot access "child_process.execSync" in client code.',
// from dep-with-builtin-module-esm read()
'Module "fs" has been externalized for browser compatibility. Cannot access "fs.readFileSync" in client code.',
// from dep-with-builtin-module-esm read()
'Module "fs" has been externalized for browser compatibility. Cannot access "fs.readFileSync" in client code.'
'Module "child_process" has been externalized for browser compatibility. Cannot access "child_process.execSync" in client code.'
])
)
})

0 comments on commit 71cb374

Please sign in to comment.