Skip to content

Commit

Permalink
fix(optimizer): properly externalize css/asset imports in optimized deps
Browse files Browse the repository at this point in the history
fix #1443
  • Loading branch information
yyx990803 committed Jan 10, 2021
1 parent 91dbb01 commit 5d180db
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 18 deletions.
3 changes: 3 additions & 0 deletions packages/playground/optimize-deps-linked-include/index.js
Expand Up @@ -5,3 +5,6 @@ import { useState } from 'react'
export function useCount() {
return useState(0)
}

// test dep with css/asset imports
import './test.css'
3 changes: 3 additions & 0 deletions packages/playground/optimize-deps-linked-include/test.css
@@ -0,0 +1,3 @@
body {
color: red;
}
@@ -1,3 +1,5 @@
import { getColor } from '../../testUtils'

test('default + named imports from cjs dep (react)', async () => {
expect(await page.textContent('.cjs button')).toBe('count is 0')
await page.click('.cjs button')
Expand Down Expand Up @@ -27,3 +29,7 @@ test('dep from linked dep (lodash-es)', async () => {
test('forced include', async () => {
expect(await page.textContent('.force-include')).toMatch(`[success]`)
})

test('dep with css import', async () => {
expect(await getColor('h1')).toBe('red')
})
16 changes: 8 additions & 8 deletions packages/playground/optimize-deps/index.html
Expand Up @@ -22,15 +22,15 @@ <h2>Optimizing force included dep even when it's linked</h2>
<div class="force-include"></div>

<script type="module">
import axios from 'axios'
import axios from 'axios'

axios.get('/ping').then(res => {
document.querySelector('.cjs-browser-field').textContent = res.data
})
axios.get('/ping').then((res) => {
document.querySelector('.cjs-browser-field').textContent = res.data
})

import { camelCase } from 'optimize-deps-linked'
document.querySelector('.deps-linked').textContent = camelCase('foo-bar-baz')
import { camelCase } from 'optimize-deps-linked'
document.querySelector('.deps-linked').textContent = camelCase('foo-bar-baz')

import { msg } from 'optimize-deps-linked-include'
document.querySelector('.force-include').textContent = msg
import { msg } from 'optimize-deps-linked-include'
document.querySelector('.force-include').textContent = msg
</script>
20 changes: 13 additions & 7 deletions packages/vite/src/node/optimizer/depAssetPlugin.ts
Expand Up @@ -3,8 +3,9 @@ import { Plugin } from 'rollup'
import { init, parse } from 'es-module-lexer'
import { isCSSRequest } from '../plugins/css'
import MagicString from 'magic-string'
import { bareImportRE, normalizePath, resolveFrom } from '../utils'
import { normalizePath } from '../utils'
import { ResolvedConfig } from '../config'
import { idToPkgMap } from '../plugins/resolve'

export const depAssetExternalPlugin = (config: ResolvedConfig): Plugin => ({
name: 'vite:dep-assets-external',
Expand Down Expand Up @@ -47,12 +48,17 @@ export const depAssetRewritePlugin = (config: ResolvedConfig): Plugin => {
s.remove(statementStart, statementEnd)
continue
}
const deepPath = `/@fs/${normalizePath(
bareImportRE.test(importee)
? resolveFrom(config.root, importee)
: path.resolve(path.dirname(id), importee)
)}`
s.overwrite(start, end, deepPath)
if (importee.startsWith('.')) {
const pkg = idToPkgMap.get(id)
if (pkg) {
const fsPath = path.resolve(path.dirname(id), importee)
const deepPath =
pkg.data.name +
'/' +
normalizePath(path.relative(pkg.dir, fsPath))
s.overwrite(start, end, deepPath)
}
}
}
} else {
// ignore dynamic import
Expand Down
4 changes: 2 additions & 2 deletions packages/vite/src/node/optimizer/index.ts
Expand Up @@ -326,12 +326,12 @@ async function resolveQualifiedDeps(
if (i.startsWith('.')) {
debug(`optimizing ${id} (contains relative imports)`)
qualified[id] = filePath
continue
break
}
if (!deps.includes(i)) {
debug(`optimizing ${id} (imports sub dependencies)`)
qualified[id] = filePath
continue
break
}
}
debug(`skipping ${id} (single esm file, doesn't need optimization)`)
Expand Down
2 changes: 1 addition & 1 deletion packages/vite/src/node/plugins/resolve.ts
Expand Up @@ -221,7 +221,7 @@ function tryResolveFile(
}
}

const idToPkgMap = new Map<string, PackageData>()
export const idToPkgMap = new Map<string, PackageData>()

export function tryNodeResolve(
id: string,
Expand Down

0 comments on commit 5d180db

Please sign in to comment.