Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: legacy no resolve asset urls #9507

Merged
merged 42 commits into from Aug 19, 2022
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
Show all changes
42 commits
Select commit Hold shift + click to select a range
8900c1c
test: vue legacy
poyoho Aug 3, 2022
b9130cc
feat: resolveAssetUrlsInCss in legacy mode
poyoho Aug 3, 2022
536cd5e
feat: test hook for legacy
poyoho Aug 4, 2022
72d8aef
fix: relative path for legacy mode
poyoho Aug 5, 2022
d909dee
fix: test
poyoho Aug 5, 2022
6e08b1d
feat: ignore entry chunk insert css
poyoho Aug 6, 2022
c1eb9b0
fix: error
poyoho Aug 6, 2022
a0caa7c
chore: update comment
poyoho Aug 6, 2022
18bec1b
chore: update
poyoho Aug 10, 2022
0b4c3cc
chore: update
poyoho Aug 10, 2022
fb734fa
chore: update
poyoho Aug 10, 2022
53bfcf6
chore: revert avoid generate the css insert script
poyoho Aug 10, 2022
43c64ae
chore: update
poyoho Aug 10, 2022
2d7812a
chore: update
poyoho Aug 10, 2022
f684b78
chore: renderAssetUrl helper
poyoho Aug 16, 2022
b6083fb
chore: update
poyoho Aug 16, 2022
2fd5069
fix: cssAssetDirname
poyoho Aug 16, 2022
2ae6788
chore: rebase
poyoho Aug 16, 2022
46d57b0
feat: opts
poyoho Aug 16, 2022
1a50884
fix: types
poyoho Aug 16, 2022
ecd950b
chore: don't modify resolveAssetUrlsInCss
poyoho Aug 17, 2022
57b3595
chore: revert resolveAssetUrlsInCss
poyoho Aug 17, 2022
e91fe24
chore: update
poyoho Aug 17, 2022
86af81d
chore: update
poyoho Aug 17, 2022
45dfb22
feat: no wrap
poyoho Aug 17, 2022
32fbeb3
chore: update
poyoho Aug 17, 2022
b682325
chore: update
poyoho Aug 17, 2022
297b765
chore: update
poyoho Aug 17, 2022
9e179df
fix: test
poyoho Aug 17, 2022
59f6a26
chore: update
poyoho Aug 17, 2022
b30a5c9
feat: vitest __test__ hooks
poyoho Aug 17, 2022
7f49b78
fix: systemjs wrapper
poyoho Aug 17, 2022
8422fbc
feat: one more judge
poyoho Aug 17, 2022
58eeb69
chore: update
poyoho Aug 18, 2022
7e97059
chore: update
poyoho Aug 18, 2022
e3c098e
test: for async legacy chunk
poyoho Aug 18, 2022
8e95e3f
feat: systemjsWrapCompletePlugin
poyoho Aug 18, 2022
4b0b731
chore: update
poyoho Aug 18, 2022
0307aeb
chore: update
poyoho Aug 18, 2022
9c1a625
chore: update
poyoho Aug 18, 2022
3b72036
chore: update
poyoho Aug 19, 2022
8df22e9
chore: update
poyoho Aug 19, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
28 changes: 19 additions & 9 deletions packages/vite/src/node/plugins/css.ts
Expand Up @@ -467,7 +467,11 @@ export function cssPostPlugin(config: ResolvedConfig): Plugin {
const publicAssetUrlMap = publicAssetUrlCache.get(config)!

// resolve asset URL placeholders to their built file URLs
function resolveAssetUrlsInCss(chunkCSS: string, cssAssetName: string) {
function resolveAssetUrlsInCss(
chunkCSS: string,
cssAssetName: string,
relativeFromPage: boolean = false
) {
const encodedPublicUrls = encodePublicUrlsInCSS(config)

const relative = config.base === './' || config.base === ''
Expand All @@ -479,14 +483,17 @@ export function cssPostPlugin(config: ResolvedConfig): Plugin {
const toRelative = (filename: string, importer: string) => {
// relative base + extracted CSS
const relativePath = path.posix.relative(cssAssetDirname!, filename)
return relativePath.startsWith('.')
const resolvedRelativePath = relativePath.startsWith('.')
? relativePath
: './' + relativePath
return relativeFromPage
? `\${relativeFromPage('${filename}')}`
: resolvedRelativePath
}

// replace asset url references with resolved url.
chunkCSS = chunkCSS.replace(assetUrlRE, (_, fileHash, postfix = '') => {
const filename = getAssetFilename(fileHash, config) + postfix
const filename = getAssetFilename(fileHash, config)! + postfix
chunk.viteMetadata.importedAssets.add(cleanUrl(filename))
return toOutputFilePathInCss(
filename,
Expand Down Expand Up @@ -556,16 +563,19 @@ export function cssPostPlugin(config: ResolvedConfig): Plugin {
})
chunk.viteMetadata.importedCss.add(this.getFileName(fileHandle))
} else if (!config.build.ssr) {
// legacy build and inline css

// __VITE_ASSET__ and __VITE_PUBLIC_ASSET__ urls are processed by
// the vite:asset plugin, don't call resolveAssetUrlsInCss here
// legacy build, inline css
// the entry css(style.css) will be inlined in the html template
// so don't need to generate the css insert script
if (chunk.isEntry) {
return { code }
poyoho marked this conversation as resolved.
Show resolved Hide resolved
poyoho marked this conversation as resolved.
Show resolved Hide resolved
}
poyoho marked this conversation as resolved.
Show resolved Hide resolved
chunkCSS = resolveAssetUrlsInCss(chunkCSS, chunk.name, true)
poyoho marked this conversation as resolved.
Show resolved Hide resolved
chunkCSS = await finalizeCss(chunkCSS, true, config)

const style = `__vite_style__`
const injectCode =
`var ${style} = document.createElement('style');` +
`${style}.innerHTML = ${JSON.stringify(chunkCSS)};` +
`var relativeFromPage = (u) => new URL(u, self.location).href;` +
poyoho marked this conversation as resolved.
Show resolved Hide resolved
`${style}.innerHTML = \`${chunkCSS}\`;` +
poyoho marked this conversation as resolved.
Show resolved Hide resolved
`document.head.appendChild(${style});`
if (config.build.sourcemap) {
const s = new MagicString(code)
Copy link
Member Author

@poyoho poyoho Aug 17, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why this don't need to add the injectCode 🤦

I think it is bug too, I make it to injectCode + code

Expand Down
32 changes: 32 additions & 0 deletions playground/vue-legacy/Main.vue
@@ -0,0 +1,32 @@
<script>
import { defineComponent } from 'vue'
import css from './inline.css?inline'

export default defineComponent({
components: {
module: () => import('./module.vue')
poyoho marked this conversation as resolved.
Show resolved Hide resolved
},
setup() {
return {
css
}
}
})
</script>
<template>
<div class="container">
<p>hello</p>
<module />
<code>
{{ css }}
</code>
</div>
</template>
<style scoped>
.container {
height: 100vh;
background: url('@/assets/asset.png') no-repeat;
poyoho marked this conversation as resolved.
Show resolved Hide resolved
background-size: contain;
background-position: center;
}
</style>
5 changes: 5 additions & 0 deletions playground/vue-legacy/__tests__/vue-legacy.spec.ts
@@ -0,0 +1,5 @@
import { untilUpdated } from '~utils'

test('vue legacy assets', async () => {
untilUpdated(() => '.container', 'asset')
poyoho marked this conversation as resolved.
Show resolved Hide resolved
})
Binary file added playground/vue-legacy/assets/asset.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added playground/vue-legacy/assets/asset2.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions playground/vue-legacy/env.d.ts
@@ -0,0 +1 @@
declare module '*.png'
7 changes: 7 additions & 0 deletions playground/vue-legacy/index.html
@@ -0,0 +1,7 @@
<div id="app" style="background: 'white'"></div>
<script type="module">
import { createApp } from 'vue'
import App from './Main.vue'

createApp(App).mount('#app')
</script>
3 changes: 3 additions & 0 deletions playground/vue-legacy/inline.css
@@ -0,0 +1,3 @@
.inline-css {
color: #0088ff;
}
9 changes: 9 additions & 0 deletions playground/vue-legacy/module.vue
@@ -0,0 +1,9 @@
<template>module.vue</template>
<style scoped>
.container {
height: 100vh;
background: url('@/assets/asset2.png') no-repeat;
background-size: contain;
background-position: center;
}
</style>
18 changes: 18 additions & 0 deletions playground/vue-legacy/package.json
@@ -0,0 +1,18 @@
{
"name": "test-vue-legacy",
"private": true,
"version": "0.0.0",
"scripts": {
"dev": "vite",
"build": "vite build",
"debug": "node --inspect-brk ../../packages/vite/bin/vite",
"preview": "vite preview"
},
"dependencies": {
"vue": "^3.2.37"
},
"devDependencies": {
"@vitejs/plugin-vue": "workspace:*",
"@vitejs/plugin-legacy": "workspace:*"
}
}
35 changes: 35 additions & 0 deletions playground/vue-legacy/vite.config.ts
@@ -0,0 +1,35 @@
import path from 'node:path'
import fs from 'node:fs'
import { defineConfig } from 'vite'
import vuePlugin from '@vitejs/plugin-vue'
import legacyPlugin from '@vitejs/plugin-legacy'

export default defineConfig({
base: '',
resolve: {
alias: {
'@': __dirname
}
},
plugins: [
legacyPlugin({
targets: ['defaults', 'not IE 11', 'chrome > 48']
}),
vuePlugin()
],
build: {
minify: false
},
// special test only hook
// for tests, remove `<script type="module">` tags and remove `nomodule`
// attrs so that we run the legacy bundle instead.
// @ts-ignore
__test__() {
poyoho marked this conversation as resolved.
Show resolved Hide resolved
const indexPath = path.resolve(__dirname, './dist/index.html')
let index = fs.readFileSync(indexPath, 'utf-8')
index = index
.replace(/<script type="module".*?<\/script>/g, '')
.replace(/<script nomodule/g, '<script')
fs.writeFileSync(indexPath, index)
}
poyoho marked this conversation as resolved.
Show resolved Hide resolved
})
11 changes: 11 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.