Skip to content

Commit

Permalink
fix: dynamic import vars ignored warning (#14006)
Browse files Browse the repository at this point in the history
  • Loading branch information
patak-dev authored Aug 7, 2023
1 parent a1b519e commit 4479431
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 4 deletions.
5 changes: 5 additions & 0 deletions packages/vite/src/node/plugins/dynamicImportVars.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
transformStableResult,
} from '../utils'
import { toAbsoluteGlob } from './importMetaGlob'
import { hasViteIgnoreRE } from './importAnalysis'

export const dynamicImportHelperId = '\0vite/dynamic-import-helper'

Expand Down Expand Up @@ -206,6 +207,10 @@ export function dynamicImportVarsPlugin(config: ResolvedConfig): Plugin {
continue
}

if (hasViteIgnoreRE.test(source.slice(expStart, expEnd))) {
continue
}

s ||= new MagicString(source)
let result
try {
Expand Down
2 changes: 1 addition & 1 deletion packages/vite/src/node/plugins/importAnalysis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ const optimizedDepDynamicRE = /-[A-Z\d]{8}\.js/

const hasImportInQueryParamsRE = /[?&]import=?\b/

const hasViteIgnoreRE = /\/\*\s*@vite-ignore\s*\*\//
export const hasViteIgnoreRE = /\/\*\s*@vite-ignore\s*\*\//

const cleanUpRawUrlRE = /\/\*[\s\S]*?\*\/|([^\\:]|^)\/\/.*$/gm
const urlIsStringRE = /^(?:'.*'|".*"|`.*`)$/
Expand Down
5 changes: 2 additions & 3 deletions packages/vite/src/node/plugins/workerImportMetaUrl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ import { WORKER_FILE_ID, workerFileToUrl } from './worker'
import { fileToUrl } from './asset'
import type { InternalResolveOptions } from './resolve'
import { tryFsResolve } from './resolve'

const ignoreFlagRE = /\/\*\s*@vite-ignore\s*\*\//
import { hasViteIgnoreRE } from './importAnalysis'

interface WorkerOptions {
type?: WorkerType
Expand Down Expand Up @@ -78,7 +77,7 @@ function getWorkerType(raw: string, clean: string, i: number): WorkerType {
.substring(commaIndex + 1, endIndex)
.replace(/\}[\s\S]*,/g, '}') // strip trailing comma for parsing

const hasViteIgnore = ignoreFlagRE.test(workerOptString)
const hasViteIgnore = hasViteIgnoreRE.test(workerOptString)
if (hasViteIgnore) {
return 'ignore'
}
Expand Down
14 changes: 14 additions & 0 deletions playground/dynamic-import/__tests__/dynamic-import.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,20 @@ test('should load dynamic import with vars', async () => {
)
})

test('should load dynamic import with vars ignored', async () => {
await untilUpdated(
() => page.textContent('.dynamic-import-with-vars-ignored'),
'hello',
true,
)
// No warning should be logged as we are using @vite-ignore
expect(
serverLogs.some((log) =>
log.includes('"https" has been externalized for browser compatibility'),
),
).toBe(false)
})

test('should load dynamic import with vars multiline', async () => {
await untilUpdated(
() => page.textContent('.dynamic-import-with-vars-multiline'),
Expand Down
3 changes: 3 additions & 0 deletions playground/dynamic-import/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
<p>dynamic-import-with-vars</p>
<div class="dynamic-import-with-vars">todo</div>

<p>dynamic-import-with-vars-ignored</p>
<div class="dynamic-import-with-vars-ignored">todo</div>

<p>dynamic-import-with-vars-multiline</p>
<div class="dynamic-import-with-vars-multiline">todo</div>

Expand Down
5 changes: 5 additions & 0 deletions playground/dynamic-import/nested/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,11 @@ import(`../alias/${base}.js`).then((mod) => {
text('.dynamic-import-with-vars', mod.hello())
})

import(/*@vite-ignore*/ `https://localhost`).catch((mod) => {
console.log(mod)
text('.dynamic-import-with-vars-ignored', 'hello')
})

// prettier-ignore
import(
/* this messes with */
Expand Down

0 comments on commit 4479431

Please sign in to comment.