Skip to content

Commit

Permalink
fix(resolve): skip module field when the importer is a require ca…
Browse files Browse the repository at this point in the history
…ll (#7438)
  • Loading branch information
sodatea committed Mar 24, 2022
1 parent 90e812a commit fe4c1ed
Show file tree
Hide file tree
Showing 9 changed files with 56 additions and 2 deletions.
6 changes: 6 additions & 0 deletions packages/playground/resolve/__tests__/resolve.spec.ts
Expand Up @@ -58,6 +58,12 @@ test('dont add extension to directory name (./dir-with-ext.js/index.js)', async
expect(await page.textContent('.dir-with-ext')).toMatch('[success]')
})

test('do not resolve to the `module` field if the importer is a `require` call', async () => {
expect(await page.textContent('.require-pkg-with-module-field')).toMatch(
'[success]'
)
})

test('a ts module can import another ts module using its corresponding js file name', async () => {
expect(await page.textContent('.ts-extension')).toMatch('[success]')
})
Expand Down
6 changes: 6 additions & 0 deletions packages/playground/resolve/index.html
Expand Up @@ -58,6 +58,9 @@ <h2>Resolve file name containing dot</h2>
<h2>Browser Field</h2>
<p class="browser">fail</p>

<h2>Don't resolve to the `module` field if the importer is a `require` call</h2>
<p class="require-pkg-with-module-field">fail</p>

<h2>CSS Entry</h2>
<p class="css"></p>

Expand Down Expand Up @@ -181,6 +184,9 @@ <h2>resolve package that contains # in path</h2>
text('.browser', main)
}

import { msg as requireButWithModuleFieldMsg } from 'require-pkg-with-module-field'
text('.require-pkg-with-module-field', requireButWithModuleFieldMsg)

import { msg as customExtMsg } from './custom-ext'
text('.custom-ext', customExtMsg)

Expand Down
1 change: 1 addition & 0 deletions packages/playground/resolve/package.json
Expand Up @@ -12,6 +12,7 @@
"@babel/runtime": "^7.16.0",
"es5-ext": "0.10.53",
"normalize.css": "^8.0.1",
"require-pkg-with-module-field": "link:./require-pkg-with-module-field",
"resolve-browser-field": "link:./browser-field",
"resolve-custom-condition": "link:./custom-condition",
"resolve-custom-main-field": "link:./custom-main-field",
Expand Down
@@ -0,0 +1,5 @@
const BigNumber = require('bignumber.js')

const x = new BigNumber('1111222233334444555566')

module.exports = x.toString()
@@ -0,0 +1,8 @@
const dep = require('./dep.cjs')

const msg =
dep === '1.111222233334444555566e+21'
? '[success] require-pkg-with-module-field'
: '[failed] require-pkg-with-module-field'

exports.msg = msg
@@ -0,0 +1,9 @@
{
"name": "require-pkg-with-module-field",
"private": true,
"version": "1.0.0",
"main": "./index.cjs",
"dependencies": {
"bignumber.js": "9.0.2"
}
}
5 changes: 4 additions & 1 deletion packages/playground/resolve/vite.config.js
Expand Up @@ -40,5 +40,8 @@ module.exports = {
}
}
}
]
],
optimizeDeps: {
include: ['require-pkg-with-module-field']
}
}
6 changes: 5 additions & 1 deletion packages/vite/src/node/plugins/resolve.ts
Expand Up @@ -738,7 +738,11 @@ export function resolvePackageEntry(
: isObject(data.browser) && data.browser['.']
if (browserEntry) {
// check if the package also has a "module" field.
if (typeof data.module === 'string' && data.module !== browserEntry) {
if (
!options.isRequire &&
typeof data.module === 'string' &&
data.module !== browserEntry
) {
// if both are present, we may have a problem: some package points both
// to ESM, with "module" targeting Node.js, while some packages points
// "module" to browser ESM and "browser" to UMD.
Expand Down
12 changes: 12 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit fe4c1ed

Please sign in to comment.