Skip to content

Commit

Permalink
chore: add dynamic import test
Browse files Browse the repository at this point in the history
  • Loading branch information
XiSenao committed Apr 18, 2024
1 parent 25908a5 commit aeaf129
Show file tree
Hide file tree
Showing 10 changed files with 31 additions and 19 deletions.
2 changes: 1 addition & 1 deletion packages/vite/src/node/__tests__/plugins/import.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ describe('transformCjsImport', () => {
),
).toBe(
'import __vite__cjsImport0_react from "./node_modules/.vite/deps/react.js"; ' +
`const react = ((m) => m?.__esModule ? m : { ...typeof m === "object" && !Array.isArray(m) || typeof m === "function" ? m : {}, default: m })(__vite__cjsImport0_react)`,
`const react = ((m) => m?.__esModule && Object.prototype.hasOwnProperty.call(m, "default") ? m : { ...typeof m === "object" && !Array.isArray(m) || typeof m === "function" ? m : {}, default: m })(__vite__cjsImport0_react)`,
)
})

Expand Down
9 changes: 9 additions & 0 deletions playground/optimize-deps/cjs-dynamic.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,15 @@
text('.cjs-dynamic-dep-cjs-compiled-from-cjs', 'ok')
}

const cjsWithEsModuleTag = await import(
'@vitejs/test-dep-cjs-with-es-module-flag'
)
console.log('cjsWithEsModuleTag', cjsWithEsModuleTag)
text(
'.cjs-dynamic-dep-cjs-with-es-module-flag',
cjsWithEsModuleTag.default.info,
)

function App() {
const [count, setCount] = useState(0)

Expand Down
5 changes: 3 additions & 2 deletions playground/optimize-deps/cjs.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import React, { useState } from 'react'
import ReactDOM from 'react-dom/client'
import { Socket } from 'phoenix'
import clip from 'clipboard'
import m from '@vitejs/test-dep-cjs-with-es-module'
import m from '@vitejs/test-dep-cjs-with-es-module-flag'
// Test exporting a name that was already imported
export { useState } from 'react'
export { useState as anotherNameForUseState } from 'react'
export { default as React } from 'react'
Expand All @@ -18,7 +19,7 @@ if (typeof Socket === 'function') {
text('.cjs-phoenix', 'ok')
}

text('.cjs-with-es-module', m.info)
text('.cjs-with-es-module-flag', m.info)

function App() {
const [count, setCount] = useState(0)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"name": "@vitejs/test-dep-cjs-with-es-module-flag",
"version": "1.0.0",
"main": "index.js"
}
5 changes: 0 additions & 5 deletions playground/optimize-deps/dep-cjs-with-es-module/package.json

This file was deleted.

6 changes: 4 additions & 2 deletions playground/optimize-deps/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ <h2>CommonJS w/ named imports (phoenix)</h2>
<div class="cjs-phoenix">fail</div>
<h2>CommonJS w/ default export (clipboard)</h2>
<div class="cjs-clipboard">fail</div>
<h2>CommonJS w/ default export (dep-cjs-with-es-module)</h2>
<div class="cjs-with-es-module">fail</div>
<h2>CommonJS w/ default export (dep-cjs-with-es-module-flag)</h2>
<div class="cjs-with-es-module-flag">fail</div>

<script type="module" src="./cjs.js"></script>

Expand All @@ -21,6 +21,8 @@ <h2>CommonJS dynamic import default (dep-cjs-compiled-from-esm)</h2>
<div class="cjs-dynamic-dep-cjs-compiled-from-esm"></div>
<h2>CommonJS dynamic import default (dep-cjs-compiled-from-cjs)</h2>
<div class="cjs-dynamic-dep-cjs-compiled-from-cjs"></div>
<h2>CommonJS dynamic import default (dep-cjs-with-es-module-flag)</h2>
<div class="cjs-dynamic-dep-cjs-with-es-module-flag"></div>

<script type="module" src="./cjs-dynamic.js"></script>

Expand Down
2 changes: 1 addition & 1 deletion playground/optimize-deps/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
"@vitejs/test-dep-with-optional-peer-dep-submodule": "file:./dep-with-optional-peer-dep-submodule",
"@vitejs/test-dep-non-optimized": "file:./dep-non-optimized",
"@vitejs/test-added-in-entries": "file:./added-in-entries",
"@vitejs/test-dep-cjs-with-es-module": "file:./dep-cjs-with-es-module",
"@vitejs/test-dep-cjs-with-es-module-flag": "file:./dep-cjs-with-es-module-flag",
"lodash-es": "^4.17.21",
"@vitejs/test-nested-exclude": "file:./nested-exclude",
"phoenix": "^1.7.11",
Expand Down
2 changes: 1 addition & 1 deletion playground/optimize-deps/vite.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export default defineConfig({
'@vitejs/test-dep-optimize-exports-with-glob/**/*',
'@vitejs/test-dep-optimize-exports-with-root-glob/**/*.js',
'@vitejs/test-dep-optimize-with-glob/**/*.js',
'@vitejs/test-dep-cjs-with-es-module',
'@vitejs/test-dep-cjs-with-es-module-flag',
],
exclude: ['@vitejs/test-nested-exclude', '@vitejs/test-dep-non-optimized'],
esbuildOptions: {
Expand Down
14 changes: 7 additions & 7 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 aeaf129

Please sign in to comment.