Skip to content

Commit

Permalink
feat: optimizedeps.allowNodeBuiltins
Browse files Browse the repository at this point in the history
  • Loading branch information
yyx990803 committed Jun 24, 2020
1 parent 63c4a42 commit 07af6f4
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 11 deletions.
22 changes: 15 additions & 7 deletions src/node/optimizer/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ export interface DepOptimizationOptions {
* are also included for optimization.
*/
link?: string[]
/**
* A list of depdendencies that imports Node built-ins, but do not actually
* use them in browsers.
*/
allowNodeBuiltins?: string[]
/**
* Automatically run `vite optimize` on server start?
* @default true
Expand Down Expand Up @@ -181,7 +186,9 @@ export async function optimizeDeps(
onwarn: onRollupWarning(spinner),
...config.rollupInputOptions,
plugins: [
createBuiltInBailPlugin(),
createBuiltInBailPlugin(
config.optimizeDeps && config.optimizeDeps.allowNodeBuiltins
),
depAssetExternalPlugin,
...(await createBaseRollupPlugins(root, resolver, config)),
createDepAssetPlugin(resolver)
Expand Down Expand Up @@ -225,12 +232,13 @@ export async function optimizeDeps(
`Tip:\nMake sure your "dependencies" only include packages that you\n` +
`intend to use in the browser. If it's a Node.js package, it\n` +
`should be in "devDependencies".\n\n` +
`You can also configure what deps to include/exclude for\n` +
`optimization using the "optimizeDeps" option in vite.config.js.\n\n` +
`If you do intend to use this dependency in the browser, then\n` +
`unfortunately it is not distributed in a web-friendly way and\n` +
`it is recommended to look for a modern alternative that ships\n` +
`ES module build.\n`
`If you do intend to use this dependency in the browser and the\n` +
`dependency does not actually use these Node built-ins in the\n` +
`browser, you can add the dependency (not the built-in) to the\n` +
`"optimizeDeps.allowNodeBuiltins" option in vite.config.js.\n\n` +
`If that results in a runtime error, then unfortunately the\n` +
`package is not distributed in a web-friendly format. You should\n` +
`open an issue in its repo, or look for a modern alternative.`
)
// TODO link to docs once we have it
)
Expand Down
18 changes: 14 additions & 4 deletions src/node/optimizer/pluginBuiltInBail.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import { Plugin } from 'rollup'
import { lookupFile } from '../utils'
import { DepOptimizationOptions } from '.'
import chalk from 'chalk'

export const createBuiltInBailPlugin = (): Plugin => {
export const createBuiltInBailPlugin = (
allowList: DepOptimizationOptions['allowNodeBuiltins']
): Plugin => {
const isbuiltin = require('isbuiltin')

return {
Expand All @@ -15,12 +19,18 @@ export const createBuiltInBailPlugin = (): Plugin => {
importingDep = pkg.name
}
}
if (importingDep && allowList && allowList.includes(importingDep)) {
return null
}
const dep = importingDep
? `Dependency "${importingDep}"`
? `Dependency ${chalk.yellow(importingDep)}`
: `A dependency`
throw new Error(
`${dep} is attempting to import Node built-in module "${id}". ` +
`This will not work in a browser environment.`
`${dep} is attempting to import Node built-in module ${chalk.yellow(
id
)}.\n` +
`This will not work in a browser environment.\n` +
`Imported by: ${chalk.gray(importer)}`
)
}
return null
Expand Down

0 comments on commit 07af6f4

Please sign in to comment.