Skip to content

Commit

Permalink
fix: resolve browser field in package.json (#301)
Browse files Browse the repository at this point in the history
Fix: #294
  • Loading branch information
csr632 authored May 29, 2020
1 parent e35cf46 commit 28d9714
Show file tree
Hide file tree
Showing 9 changed files with 59 additions and 3 deletions.
7 changes: 6 additions & 1 deletion playground/TestModuleResolve.vue
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
<div class="dot-resolve" :class="dotResolve">
filename with dot resolve: {{ dotResolve }}
</div>
<div class="browser-field-resolve" :class="browserFieldResolve">
resolve browser field in package.json: {{ browserFieldResolve }}
</div>
</template>

<script>
Expand All @@ -25,6 +28,7 @@ import { add } from 'lodash-es'
import { test } from 'conditional-exports'
import { foo } from './util'
import { bar } from './util/bar.util'
import value from 'resolve-browser-field-test-package'
export default {
setup() {
Expand All @@ -34,7 +38,8 @@ export default {
optResolve: typeof add === 'function' ? 'ok' : 'error',
conditionalExports: test() ? 'ok' : 'error',
indexResolve: foo() ? 'ok' : 'error',
dotResolve: bar() ? 'ok' : 'error'
dotResolve: bar() ? 'ok' : 'error',
browserFieldResolve: value === 'success' ? 'ok' : 'error'
}
}
}
Expand Down
1 change: 1 addition & 0 deletions playground/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"lodash-es": "link:../node_modules/lodash-es",
"moment": "link:../node_modules/moment",
"rewrite-optimized-test-package": "link:./rewrite-optimized/test-package",
"resolve-browser-field-test-package": "link:./resolve-browser-field",
"conditional-exports": "link:./conditional-exports"
}
}
1 change: 1 addition & 0 deletions playground/resolve-browser-field/out/cjs.node.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default 'fail'
1 change: 1 addition & 0 deletions playground/resolve-browser-field/out/esm.browser.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default 'success'
9 changes: 9 additions & 0 deletions playground/resolve-browser-field/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"name": "resolve-browser-field-test-package",
"version": "1.0.0",
"//": "real world example: https://github.com/axios/axios/blob/3f2ef030e001547eb06060499f8a2e3f002b5a14/package.json#L71-L73",
"main": "out/cjs.node.js",
"browser": {
"./out/cjs.node.js": "./out/esm.browser.js"
}
}
4 changes: 4 additions & 0 deletions playground/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@
version "0.0.0"
uid ""

"resolve-browser-field-test-package@link:./resolve-browser-field":
version "0.0.0"
uid ""

"rewrite-optimized-test-package@link:./rewrite-optimized/test-package":
version "0.0.0"
uid ""
3 changes: 2 additions & 1 deletion src/node/build/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,8 @@ export async function createBaseRollupPlugins(
extensions: supportedExts,
preferBuiltins: false,
dedupe: options.rollupDedupe || [],
mainFields
mainFields,
browser: true
}),
require('@rollup/plugin-commonjs')({
extensions: ['.js', '.cjs']
Expand Down
35 changes: 34 additions & 1 deletion src/node/resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export interface InternalResolver {
}

export const supportedExts = ['.mjs', '.js', '.ts', '.jsx', '.tsx', '.json']
export const mainFields = ['module', 'jsnext', 'jsnext:main', 'browser', 'main']
export const mainFields = ['module', 'jsnext', 'jsnext:main', 'main']

const defaultRequestToFile = (publicPath: string, root: string): string => {
if (moduleRE.test(publicPath)) {
Expand Down Expand Up @@ -340,6 +340,19 @@ export function resolveNodeModule(
}
}

// resolve browser field in package.json
// https://github.com/defunctzombie/package-browser-field-spec
const browserField = pkg.browser
if (typeof browserField === 'string') {
entryPoint = browserField
} else if (
entryPoint &&
typeof browserField === 'object' &&
browserField !== null
) {
entryPoint = mapWithBrowserField(entryPoint, browserField)
}

debug(`(node_module entry) ${id} -> ${entryPoint}`)

// save resolved entry file path using the deep import path as key
Expand Down Expand Up @@ -387,3 +400,23 @@ export function resolveNodeModuleFile(
// error will be reported downstream
}
}

/**
* given a relative path in pkg dir,
* return a relative path in pkg dir,
* mapped with the "map" object
*/
function mapWithBrowserField(
relativePathInPkgDir: string,
map: Record<string, string>
) {
const normalized = path.normalize(relativePathInPkgDir)
const foundEntry = Object.entries(map).find(([from]) => {
return path.normalize(from) === normalized
})
if (!foundEntry) {
return normalized
}
const [, to] = foundEntry
return path.normalize(to)
}
1 change: 1 addition & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ describe('vite', () => {
expect(await getText('.module-resolve-conditional')).toMatch('ok')
expect(await getText('.index-resolve')).toMatch('ok')
expect(await getText('.dot-resolve')).toMatch('ok')
expect(await getText('.browser-field-resolve')).toMatch('ok')
})

if (!isBuild) {
Expand Down

0 comments on commit 28d9714

Please sign in to comment.