Skip to content

Commit

Permalink
fix(optimizer): fix ?raw import and import with queries in pre-bundling
Browse files Browse the repository at this point in the history
fix #1759
  • Loading branch information
yyx990803 committed Jan 28, 2021
1 parent 6fe72f0 commit 2f1efa3
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 5 deletions.
4 changes: 4 additions & 0 deletions packages/playground/assets/__tests__/assets.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,3 +117,7 @@ describe('svg fragments', () => {
expect(await img.getAttribute('src')).toMatch(/svg#icon-heart-view$/)
})
})

test('?raw import', async () => {
expect(await page.textContent('.raw')).toMatch('SVG')
})
6 changes: 6 additions & 0 deletions packages/playground/assets/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@ <h2>SVG Fragments via JS Import</h2>
<img class="svg-frag-import" alt="" />
</div>

<h2>?raw import</h2>
<code class="raw"></code>

<script type="module">
import './css/fonts.css'
import './css/css-url.css'
Expand All @@ -98,6 +101,9 @@ <h2>SVG Fragments via JS Import</h2>
text('.svg-frag-import-path', svgFrag)
document.querySelector('.svg-frag-import').src = svgFrag + '#icon-heart-view'

import rawSvg from './nested/fragment.svg?raw'
text('.raw', rawSvg)

function text(el, text) {
document.querySelector(el).textContent = text
}
Expand Down
16 changes: 11 additions & 5 deletions packages/vite/src/node/optimizer/scan.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ import {
isDataUrl,
isExternalUrl,
normalizePath,
isObject
isObject,
cleanUrl
} from '../utils'
import { browserExternalId } from '../plugins/resolve'
import {
Expand Down Expand Up @@ -201,6 +202,14 @@ function esbuildScanPlugin(
({ path }) => ({ path, external: true })
)

// known vite query types: ?worker, ?raw
build.onResolve(
{
filter: /\?(worker|raw)\b/
},
({ path }) => ({ path, external: true })
)

// bare imports: record and externalize
build.onResolve(
{
Expand Down Expand Up @@ -259,14 +268,11 @@ function esbuildScanPlugin(
filter: /.*/
},
async ({ path: id, importer }) => {
if (id.includes(`?worker`)) {
return { path: id, external: true }
}
// use vite resolver to support urls and omitted extensions
const resolved = await resolve(id, importer)
if (resolved && resolved !== id) {
return {
path: path.resolve(resolved)
path: path.resolve(cleanUrl(resolved))
}
} else {
// resolve failed... probably usupported type
Expand Down

0 comments on commit 2f1efa3

Please sign in to comment.