Skip to content

Commit

Permalink
test: add a test case for vitejs#7578
Browse files Browse the repository at this point in the history
  • Loading branch information
haoqunjiang committed Apr 2, 2022
1 parent 2e6eaa1 commit 61af204
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 6 deletions.
6 changes: 6 additions & 0 deletions packages/playground/resolve/__tests__/resolve.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,12 @@ test('resolve to the `browser` field instead of `module` when the importer is a
).toMatch('[success]')
})

test('resolve to the `main` field instead of `module` when the importer is a `require` call', async () => {
expect(await page.textContent('.require-pkg-with-esm-entries')).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
16 changes: 11 additions & 5 deletions packages/playground/resolve/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,12 @@ <h2>
</h2>
<p class="require-pkg-with-browser-and-module-field">fail</p>

<h2>
Resolve to the `main` field instead of `module` when the importer is a
`require` call
</h2>
<p class="require-pkg-with-esm-entries">fail</p>

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

Expand Down Expand Up @@ -188,11 +194,11 @@ <h2>resolve package that contains # in path</h2>
text('.browser', main)
}

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

import { msg as requireMainMsg } from 'require-pkg-with-esm-entries'
text('.require-pkg-with-esm-entries', requireMainMsg)

import { msg as customExtMsg } from './custom-ext'
text('.custom-ext', customExtMsg)
Expand Down
1 change: 1 addition & 0 deletions packages/playground/resolve/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"es5-ext": "0.10.53",
"normalize.css": "^8.0.1",
"require-pkg-with-browser-and-module-field": "link:./require-pkg-with-browser-and-module-field",
"require-pkg-with-esm-entries": "link:./require-pkg-with-esm-entries",
"resolve-browser-field": "link:./browser-field",
"resolve-custom-condition": "link:./custom-condition",
"resolve-custom-main-field": "link:./custom-main-field",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const fromEvent = require('callbag-from-event')

const msg =
// should be the exported function instead of the ES Module record (`{ default: ... }`)
typeof fromEvent === 'function'
? '[success] require-pkg-with-esm-entries'
: '[failed] require-pkg-with-esm-entries'

exports.msg = msg
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"name": "require-pkg-with-esm-entries",
"private": true,
"version": "1.0.0",
"main": "./index.cjs",
"dependencies": {
"callbag-from-event": "1.3.0"
}
}
5 changes: 4 additions & 1 deletion packages/playground/resolve/vite.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ module.exports = {
}
],
optimizeDeps: {
include: ['require-pkg-with-browser-and-module-field']
include: [
'require-pkg-with-browser-and-module-field',
'require-pkg-with-esm-entries'
]
}
}

0 comments on commit 61af204

Please sign in to comment.