Skip to content

Commit

Permalink
refactor: ensure normalized ids during dev
Browse files Browse the repository at this point in the history
also avoid root check false positive
close #1378
  • Loading branch information
yyx990803 committed Jan 7, 2021
1 parent 28fe201 commit da3dfdf
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 9 deletions.
7 changes: 3 additions & 4 deletions packages/vite/src/node/plugins/importsAnalysis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import {
isDataUrl,
isExternalUrl,
isJSRequest,
normalizePath,
prettifyUrl,
timeFrom
} from '../utils'
Expand Down Expand Up @@ -88,7 +87,7 @@ export function importAnalysisPlugin(config: ResolvedConfig): Plugin {
},

async transform(source, importer) {
const prettyImporter = prettifyUrl(normalizePath(importer), config.root)
const prettyImporter = prettifyUrl(importer, config.root)

if (canSkip(importer)) {
isDebug && debugRewrite(chalk.dim(`[skipped] ${prettyImporter}`))
Expand Down Expand Up @@ -241,12 +240,12 @@ export function importAnalysisPlugin(config: ResolvedConfig): Plugin {
// normalize all imports into resolved URLs
// e.g. `import 'foo'` -> `import '/@fs/.../node_modules/foo/index.js`
if (url !== resolved.id) {
if (resolved.id.startsWith(config.root)) {
if (resolved.id.startsWith(config.root + '/')) {
// in root: infer short absolute path from root
url = resolved.id.slice(config.root.length)
} else if (fs.existsSync(cleanUrl(resolved.id))) {
// exists but out of root: rewrite to absolute /@fs/ paths
url = FS_PREFIX + normalizePath(resolved.id)
url = FS_PREFIX + resolved.id
} else {
url = resolved.id
}
Expand Down
12 changes: 7 additions & 5 deletions packages/vite/src/node/server/pluginContainer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ import { FSWatcher } from 'chokidar'
import {
createDebugger,
generateCodeFrame,
normalizePath,
numberToPos,
prettifyUrl,
timeFrom
Expand Down Expand Up @@ -415,10 +416,6 @@ export async function createPluginContainer(
break
}

if (id) {
partial.id = id
}

nestedResolveCall--
if (
isDebug &&
Expand All @@ -436,7 +433,12 @@ export async function createPluginContainer(
}
}

return id ? (partial as PartialResolvedId) : null
if (id) {
partial.id = normalizePath(id)
return partial as PartialResolvedId
} else {
return null
}
},

async load(id) {
Expand Down

0 comments on commit da3dfdf

Please sign in to comment.