Skip to content

Commit

Permalink
fix: remove incorrect conditional exports handling
Browse files Browse the repository at this point in the history
The conditional exports handling is not actually working because
equivalent logic isn't implemented in @rollup/plugin-node-resolve.
  • Loading branch information
yyx990803 committed May 30, 2020
1 parent ce3ec6c commit 3fdfe8a
Show file tree
Hide file tree
Showing 7 changed files with 9 additions and 41 deletions.
5 changes: 0 additions & 5 deletions playground/TestModuleResolve.vue
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@
<div class="module-resolve-optimize" :class="optResolve">
optimized {{ optResolve }}
</div>
<div class="module-resolve-conditional" :class="conditionalExports">
conditional exports {{ conditionalExports }}
</div>
<div class="index-resolve" :class="indexResolve">
directory index resolve: {{ indexResolve }}
</div>
Expand All @@ -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'
Expand All @@ -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'
}
Expand Down
3 changes: 0 additions & 3 deletions playground/conditional-exports/files/index.js

This file was deleted.

3 changes: 0 additions & 3 deletions playground/conditional-exports/files/index.mjs

This file was deleted.

11 changes: 0 additions & 11 deletions playground/conditional-exports/package.json

This file was deleted.

1 change: 0 additions & 1 deletion playground/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
26 changes: 9 additions & 17 deletions src/node/resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 0 additions & 1 deletion test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
})
Expand Down

0 comments on commit 3fdfe8a

Please sign in to comment.