Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(resolve): improve browser filed substitutions #2701

Merged
merged 2 commits into from
Mar 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/playground/resolve/__tests__/resolve.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ test('monorepo linked dep', async () => {
expect(await page.textContent('.monorepo')).toMatch('[success]')
})

test('plugin resolved virutal file', async () => {
test('plugin resolved virtual file', async () => {
expect(await page.textContent('.virtual')).toMatch('[success]')
})

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
const fs = require('fs')
console.log('this should not run in the browser')
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import jsdom from 'jsdom' // should be redireted to empty module
export default ''
2 changes: 2 additions & 0 deletions packages/playground/resolve/browser-field/no-ext.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import jsdom from 'jsdom' // should be redireted to empty module
export default ''
5 changes: 5 additions & 0 deletions packages/playground/resolve/browser-field/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,12 @@
"main": "out/cjs.node.js",
"browser": {
"./out/cjs.node.js": "./out/esm.browser.js",
"./no-ext": "./out/esm.browser.js",
"./ext.js": "./out/esm.browser.js",
"./ext-index/index.js": "./out/esm.browser.js",
"./no-ext-index": "./out/esm.browser.js",
"./not-browser.js": false,
"./multiple.dot.path.js": false,
"jsdom": false
}
}
9 changes: 9 additions & 0 deletions packages/playground/resolve/browser-field/relative.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import ra from './no-ext'
import rb from './no-ext.js' // no substitution
import rc from './ext'
import rd from './ext.js'
import re from './ext-index/index.js'
import rf from './ext-index'
import rg from './no-ext-index/index.js' // no substitution

export { ra, rb, rc, rd, re, rf, rg }
24 changes: 22 additions & 2 deletions packages/playground/resolve/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,27 @@ <h2>resolve.conditions</h2>
text('.dot', bar())

// browser field
import value from 'resolve-browser-field'
text('.browser', value)
import main from 'resolve-browser-field'

import a from 'resolve-browser-field/no-ext'
import b from 'resolve-browser-field/no-ext.js' // no substitution
import c from 'resolve-browser-field/ext'
import d from 'resolve-browser-field/ext.js'
import e from 'resolve-browser-field/ext-index/index.js'
import f from 'resolve-browser-field/ext-index'
import g from 'resolve-browser-field/no-ext-index/index.js' // no substitution

import { ra, rb, rc, rd, re, rf, rg } from 'resolve-browser-field/relative'

const success = [main, a, c, d, e, f, ra, rc, rd, re, rf]
const noSuccess = [b, g, rb, rg]

if (
[...success, ...noSuccess].filter((text) => text.includes('[success]'))
.length === success.length
) {
text('.browser', main)
}

import { msg as customExtMsg } from './custom-ext'
text('.custom-ext', customExtMsg)
Expand All @@ -114,6 +133,7 @@ <h2>resolve.conditions</h2>

// should be ok to import a file marked with browser: false
import 'resolve-browser-field/not-browser'
import 'resolve-browser-field/multiple.dot.path'

// css entry
import css from 'normalize.css'
Expand Down
14 changes: 10 additions & 4 deletions packages/vite/src/node/plugins/resolve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -658,14 +658,20 @@ function mapWithBrowserField(
relativePathInPkgDir: string,
map: Record<string, string | false>
): string | false | undefined {
const normalized = normalize(relativePathInPkgDir)
const normalizedPath = path.posix.normalize(relativePathInPkgDir)

for (const key in map) {
if (normalize(key) === normalized) {
const normalizedKey = path.posix.normalize(key)
if (
normalizedPath === normalizedKey ||
equalWithoutSuffix(normalizedPath, normalizedKey, '.js') ||
equalWithoutSuffix(normalizedPath, normalizedKey, '/index.js')
) {
return map[key]
}
}
}

fi3ework marked this conversation as resolved.
Show resolved Hide resolved
function normalize(file: string) {
return path.posix.normalize(path.extname(file) ? file : file + '.js')
function equalWithoutSuffix(path: string, key: string, suffix: string) {
return key.endsWith(suffix) && key.slice(0, -suffix.length) === path
}