Skip to content

Commit

Permalink
fix(resolve): resolve inline package
Browse files Browse the repository at this point in the history
  • Loading branch information
yyx990803 authored and aleclarson committed Jan 2, 2021
1 parent 5bcdd57 commit f424511
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 20 deletions.
4 changes: 4 additions & 0 deletions packages/playground/resolve/__tests__/resolve.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,7 @@ test('monorepo linked dep', async () => {
test('plugin resolved virutal file', async () => {
expect(await page.textContent('.virtual')).toMatch('[success]')
})

test('resolve inline package', async () => {
expect(await page.textContent('.inline-pkg')).toMatch('[success]')
})
6 changes: 6 additions & 0 deletions packages/playground/resolve/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ <h2>Monorepo linked dep</h2>
<h2>Plugin resolved virtual file</h2>
<p class="virtual"></p>

<h2>Inline package</h2>
<p class="inline-pkg"></p>

<script type="module">
function text(selector, text) {
document.querySelector(selector).textContent = text
Expand Down Expand Up @@ -78,4 +81,7 @@ <h2>Plugin resolved virtual file</h2>

import { msg as virtualMsg } from '@virtual-file'
text('.virtual', virtualMsg)

import { msg as inlineMsg } from './inline-package'
text('.inline-pkg', inlineMsg)
</script>
1 change: 1 addition & 0 deletions packages/playground/resolve/inline-package/inline.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const msg = '[success] from inline package'
6 changes: 6 additions & 0 deletions packages/playground/resolve/inline-package/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"name": "inline-package",
"private": true,
"sideEffects": false,
"main": "./inline"
}
49 changes: 29 additions & 20 deletions packages/vite/src/node/plugins/resolve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,12 @@ function tryResolveFile(
const index = tryFsResolve(file + '/index', false)
if (index) return normalizePath(index) + query
}
const pkgPath = file + '/package.json'
if (fs.existsSync(pkgPath)) {
// path points to a node package
const pkg = loadPackageData(pkgPath)
return resolvePackageEntry(file, pkg)
}
} else {
return normalizePath(file) + query
}
Expand Down Expand Up @@ -270,33 +276,36 @@ function resolvePackageData(
if (packageCache.has(cacheKey)) {
return packageCache.get(cacheKey)
}
let data
try {
const pkgPath = resolveFrom(`${id}/package.json`, basedir)
data = JSON.parse(fs.readFileSync(pkgPath, 'utf-8'))
const pkgDir = path.dirname(pkgPath)
const { sideEffects } = data
let hasSideEffects
if (typeof sideEffects === 'boolean') {
hasSideEffects = () => sideEffects
} else if (Array.isArray(sideEffects)) {
hasSideEffects = createFilter(sideEffects, null, { resolve: pkgDir })
} else {
hasSideEffects = () => true
}

const pkg = {
dir: pkgDir,
data,
hasSideEffects
}
packageCache.set(cacheKey, pkg)
return pkg
return loadPackageData(pkgPath, cacheKey)
} catch (e) {
isDebug && debug(`${chalk.red(`[failed loading package.json]`)} ${id}`)
}
}

function loadPackageData(pkgPath: string, cacheKey = pkgPath) {
const data = JSON.parse(fs.readFileSync(pkgPath, 'utf-8'))
const pkgDir = path.dirname(pkgPath)
const { sideEffects } = data
let hasSideEffects
if (typeof sideEffects === 'boolean') {
hasSideEffects = () => sideEffects
} else if (Array.isArray(sideEffects)) {
hasSideEffects = createFilter(sideEffects, null, { resolve: pkgDir })
} else {
hasSideEffects = () => true
}

const pkg = {
dir: pkgDir,
data,
hasSideEffects
}
packageCache.set(cacheKey, pkg)
return pkg
}

export function resolvePackageEntry(
id: string,
{ dir, data }: PackageData
Expand Down

0 comments on commit f424511

Please sign in to comment.