From 7c9eac10f67c3856dae5983e53b66a20d002f803 Mon Sep 17 00:00:00 2001 From: Haoqun Jiang Date: Mon, 12 Jul 2021 20:22:42 +0800 Subject: [PATCH] fix: correctly ignore optional dependencies when bundling vite deps MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fixes #3977 Fixes #3850 😅 I've accidentally committed the actual fix in https://github.com/vitejs/vite/commit/25d86eb4ba6f2c5094932bd86870265f4d2319ce#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. --- packages/vite/rollup.config.js | 28 ++-------------------------- 1 file changed, 2 insertions(+), 26 deletions(-) diff --git a/packages/vite/rollup.config.js b/packages/vite/rollup.config.js index 4be0533f4404b7..8b1bda554695ad 100644 --- a/packages/vite/rollup.config.js +++ b/packages/vite/rollup.config.js @@ -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(), @@ -261,26 +257,6 @@ function shimDepsPlugin(deps) { } } -/** - * @type { (deps: Record) => 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) {