Skip to content

Commit

Permalink
fix: dep with dynamic import wrong error log (#7313)
Browse files Browse the repository at this point in the history
  • Loading branch information
patak-dev committed Mar 14, 2022
1 parent f8f934a commit 769f535
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 4 deletions.
Expand Up @@ -66,6 +66,12 @@ test('import from dep with .notjs files', async () => {
expect(await page.textContent('.not-js')).toMatch(`[success]`)
})

test('dep with dynamic import', async () => {
expect(await page.textContent('.dep-with-dynamic-import')).toMatch(
`[success]`
)
})

test('dep with css import', async () => {
expect(await getColor('h1')).toBe('red')
})
Expand Down
@@ -0,0 +1 @@
export const foo = '[success] dependency with dynamic import'
@@ -0,0 +1,4 @@
export const lazyFoo = async function () {
const { foo } = await import('./dynamic.js')
return foo
}
@@ -0,0 +1,6 @@
{
"name": "dep-with-dynamic-import",
"private": true,
"version": "0.0.0",
"main": "index.js"
}
8 changes: 8 additions & 0 deletions packages/playground/optimize-deps/index.html
Expand Up @@ -41,6 +41,9 @@ <h2>import * as ...</h2>
<h2>Import from dependency with .notjs files</h2>
<div class="not-js"></div>

<h2>Import from dependency with dynamic import</h2>
<div class="dep-with-dynamic-import"></div>

<h2>Dep w/ special file format supported via plugins</h2>
<div class="plugin"></div>

Expand Down Expand Up @@ -88,6 +91,11 @@ <h2>Reused variable names</h2>
import { notjsValue } from 'dep-not-js'
text('.not-js', notjsValue)

import { lazyFoo } from 'dep-with-dynamic-import'
lazyFoo().then((foo) => {
text('.dep-with-dynamic-import', foo)
})

import { createApp } from 'vue'
import { createStore } from 'vuex'
if (typeof createApp === 'function' && typeof createStore === 'function') {
Expand Down
1 change: 1 addition & 0 deletions packages/playground/optimize-deps/package.json
Expand Up @@ -18,6 +18,7 @@
"dep-linked": "link:./dep-linked",
"dep-linked-include": "link:./dep-linked-include",
"dep-not-js": "file:./dep-not-js",
"dep-with-dynamic-import": "file:./dep-with-dynamic-import",
"lodash-es": "^4.17.21",
"nested-exclude": "file:./nested-exclude",
"phoenix": "^1.6.2",
Expand Down
14 changes: 10 additions & 4 deletions packages/vite/src/node/plugins/importAnalysis.ts
Expand Up @@ -62,6 +62,7 @@ const skipRE = /\.(map|json)$/
const canSkip = (id: string) => skipRE.test(id) || isDirectCSSRequest(id)

const optimizedDepChunkRE = /\/chunk-[A-Z0-9]{8}\.js/
const optimizedDepDynamicRE = /-[A-Z0-9]{8}\.js/

function isExplicitImportRequired(url: string) {
return !isJSRequest(cleanUrl(url)) && !isCSSRequest(url)
Expand Down Expand Up @@ -451,11 +452,16 @@ export function importAnalysisPlugin(config: ResolvedConfig): Plugin {
)

if (needsInterop === undefined) {
config.logger.error(
colors.red(
`Vite Error, ${url} optimized info should be defined`
// Non-entry dynamic imports from dependencies will reach here as there isn't
// optimize info for them, but they don't need es interop. If the request isn't
// a dynamic import, then it is an internal Vite error
if (!file.match(optimizedDepDynamicRE)) {
config.logger.error(
colors.red(
`Vite Error, ${url} optimized info should be defined`
)
)
)
}
} else if (needsInterop) {
debug(`${url} needs interop`)
if (isDynamicImport) {
Expand Down
2 changes: 2 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 769f535

Please sign in to comment.