From 3fdfe8a78c803b712c2064c1defb43845b2a0039 Mon Sep 17 00:00:00 2001 From: Evan You Date: Fri, 29 May 2020 23:05:11 -0400 Subject: [PATCH] fix: remove incorrect conditional exports handling The conditional exports handling is not actually working because equivalent logic isn't implemented in @rollup/plugin-node-resolve. --- playground/TestModuleResolve.vue | 5 ---- playground/conditional-exports/files/index.js | 3 --- .../conditional-exports/files/index.mjs | 3 --- playground/conditional-exports/package.json | 11 -------- playground/package.json | 1 - src/node/resolver.ts | 26 +++++++------------ test/test.js | 1 - 7 files changed, 9 insertions(+), 41 deletions(-) delete mode 100644 playground/conditional-exports/files/index.js delete mode 100644 playground/conditional-exports/files/index.mjs delete mode 100644 playground/conditional-exports/package.json diff --git a/playground/TestModuleResolve.vue b/playground/TestModuleResolve.vue index 281701ea6ce804..3293035c3fcd55 100644 --- a/playground/TestModuleResolve.vue +++ b/playground/TestModuleResolve.vue @@ -7,9 +7,6 @@
optimized {{ optResolve }}
-
- conditional exports {{ conditionalExports }} -
directory index resolve: {{ indexResolve }}
@@ -22,7 +19,6 @@ import { createRouter } from 'vue-router' import { createStore } from 'vuex' import { add } from 'lodash-es' -import { test } from 'conditional-exports' import { foo } from './util' import { bar } from './util/bar.util' @@ -32,7 +28,6 @@ export default { router: typeof createRouter === 'function' ? 'ok' : 'error', store: typeof createStore === 'function' ? 'ok' : 'error', optResolve: typeof add === 'function' ? 'ok' : 'error', - conditionalExports: test() ? 'ok' : 'error', indexResolve: foo() ? 'ok' : 'error', dotResolve: bar() ? 'ok' : 'error' } diff --git a/playground/conditional-exports/files/index.js b/playground/conditional-exports/files/index.js deleted file mode 100644 index 553fdb1c010e39..00000000000000 --- a/playground/conditional-exports/files/index.js +++ /dev/null @@ -1,3 +0,0 @@ -exports.test = () => { - return true -} diff --git a/playground/conditional-exports/files/index.mjs b/playground/conditional-exports/files/index.mjs deleted file mode 100644 index a002a810e77777..00000000000000 --- a/playground/conditional-exports/files/index.mjs +++ /dev/null @@ -1,3 +0,0 @@ -export function test() { - return true -} diff --git a/playground/conditional-exports/package.json b/playground/conditional-exports/package.json deleted file mode 100644 index 0e06302755c5b6..00000000000000 --- a/playground/conditional-exports/package.json +++ /dev/null @@ -1,11 +0,0 @@ -{ - "name": "conditional-exports", - "version": "1.0.0", - "main": "files/index.js", - "exports": { - ".": { - "import": "files/index.mjs", - "require": "files/index.js" - } - } -} diff --git a/playground/package.json b/playground/package.json index 64cb1cc0e8a599..d704e6f35d3bb9 100644 --- a/playground/package.json +++ b/playground/package.json @@ -6,7 +6,6 @@ }, "dependencies": { "bootstrap": "link:../node_modules/bootstrap", - "conditional-exports": "link:./conditional-exports", "lodash-es": "link:../node_modules/lodash-es", "moment": "link:../node_modules/moment", "rewrite-optimized-test-package": "link:./rewrite-optimized/test-package" diff --git a/src/node/resolver.ts b/src/node/resolver.ts index cb0580c5de93f8..f71b4aac8395fb 100644 --- a/src/node/resolver.ts +++ b/src/node/resolver.ts @@ -320,27 +320,19 @@ export function resolveNodeModule( return } let entryPoint: string | null = null - if (pkg.exports) { - if (typeof pkg.exports === 'string') { - entryPoint = pkg.exports - } else if (pkg.exports['.']) { - if (typeof pkg.exports['.'] === 'string') { - entryPoint = pkg.exports['.'] - } else { - entryPoint = pkg.exports['.'].import - } - } - } - if (!entryPoint) { - for (const mainField of mainFields) { - if (pkg[mainField]) { - entryPoint = pkg[mainField] - break - } + for (const mainField of mainFields) { + if (pkg[mainField]) { + entryPoint = pkg[mainField] + break } } + // TODO properly support conditinal exports + // https://nodejs.org/api/esm.html#esm_conditional_exports + // Note: this would require @rollup/plugin-node-resolve to support it too + // or we will have to implement that logic in vite's own resolve plugin. + // save resolved entry file path using the deep import path as key // e.g. foo/dist/foo.js // this is the path raw imports will be rewritten to, and is what will diff --git a/test/test.js b/test/test.js index 4a77124fc90204..f5edb487505df4 100644 --- a/test/test.js +++ b/test/test.js @@ -105,7 +105,6 @@ describe('vite', () => { expect(await getText('.module-resolve-router')).toMatch('ok') expect(await getText('.module-resolve-store')).toMatch('ok') expect(await getText('.module-resolve-optimize')).toMatch('ok') - expect(await getText('.module-resolve-conditional')).toMatch('ok') expect(await getText('.index-resolve')).toMatch('ok') expect(await getText('.dot-resolve')).toMatch('ok') })