Skip to content

Commit

Permalink
fix: correctly ignore optional dependencies when bundling vite deps
Browse files Browse the repository at this point in the history
Fixes vitejs#3977
Fixes vitejs#3850

😅 I've accidentally committed the actual fix in vitejs@25d86eb#diff-d17472499351c5bf75d44c67aaa203337dcce321fb578ff05302c61b2b2a3a8aR172

So this PR just removes the erroneous `ingoreDepPlugin`, and moves
the comments to the `ignore` option of the commonjs plugin.

The cause of the issues is this line: https://github.com/websockets/ws/blob/4c1849a61e773fe0ce016f6eb59bc3877f09aeee/lib/buffer-util.js#L105

When we ignore the optional deps when bundling, we expect
`require('bufferutil')` to throw an error.

However, with the previous `ignoreDepPlugin` implementation, the
`require` expression is turned into:
```js
var bufferutil = {
	__proto__: null
};

var require$$1 = /*@__PURE__*/getAugmentedNamespace(bufferutil);

// ...
const bufferUtil = require$$1;
```
No error is throwed, so the code executes into the wrong branch.

After the fix, the `require` expression is left as-is. As `bufferutil`
is not installed in the user project, the error is thrown as expected.
  • Loading branch information
sodatea committed Jul 12, 2021
1 parent 0f45c21 commit 7c9eac1
Showing 1 changed file with 2 additions and 26 deletions.
28 changes: 2 additions & 26 deletions packages/vite/rollup.config.js
Expand Up @@ -161,14 +161,10 @@ const createNodeConfig = (isProduction) => {
replacement: `: eval('require'),`
}
}),
// Optional peer deps of ws. Native deps that are mostly for performance.
// Since ws is not that perf critical for us, just ignore these deps.
ignoreDepPlugin({
bufferutil: 1,
'utf-8-validate': 1
}),
commonjs({
extensions: ['.js'],
// Optional peer deps of ws. Native deps that are mostly for performance.
// Since ws is not that perf critical for us, just ignore these deps.
ignore: ['bufferutil', 'utf-8-validate']
}),
json(),
Expand Down Expand Up @@ -261,26 +257,6 @@ function shimDepsPlugin(deps) {
}
}

/**
* @type { (deps: Record<string, any>) => import('rollup').Plugin }
*/
function ignoreDepPlugin(ignoredDeps) {
return {
name: 'ignore-deps',
resolveId(id) {
if (id in ignoredDeps) {
return id
}
},
load(id) {
if (id in ignoredDeps) {
console.log(`ignored: ${id}`)
return ''
}
}
}
}

function licensePlugin() {
return license({
thirdParty(dependencies) {
Expand Down

0 comments on commit 7c9eac1

Please sign in to comment.