Skip to content
This repository has been archived by the owner on Apr 6, 2023. It is now read-only.

Commit

Permalink
refactor: use new renderBuiltUrl API
Browse files Browse the repository at this point in the history
  • Loading branch information
danielroe committed Jun 28, 2022
1 parent 4e1b406 commit cc594b3
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 17 deletions.
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -42,7 +42,7 @@
"nuxi": "link:./packages/nuxi",
"nuxt": "link:./packages/nuxt",
"nuxt3": "link:./packages/nuxt",
"vite": "~3.0.0-beta.1",
"vite": "~3.0.0-beta.5",
"@vitejs/plugin-vue": "~3.0.0-beta.0",
"@vitejs/plugin-vue-jsx": "~2.0.0-beta.0",
"unbuild": "^0.7.4"
Expand Down
4 changes: 2 additions & 2 deletions packages/nuxt/src/core/templates.ts
Expand Up @@ -213,8 +213,8 @@ export const publicPathTemplate: NuxtTemplate = {
' return path.length ? joinURL(publicBase, ...path) : publicBase',
'}',

'globalThis.buildAssetsURL = buildAssetsURL',
'globalThis.publicAssetsURL = publicAssetsURL'
'globalThis.__buildAssetsURL = buildAssetsURL',
'globalThis.__publicAssetsURL = publicAssetsURL'
].filter(Boolean).join('\n')
}
}
2 changes: 1 addition & 1 deletion packages/schema/package.json
Expand Up @@ -17,7 +17,7 @@
"@types/lodash.template": "^4",
"@types/semver": "^7",
"unbuild": "latest",
"vite": "~3.0.0-beta.1"
"vite": "~3.0.0-beta.5"
},
"dependencies": {
"c12": "^0.2.7",
Expand Down
2 changes: 1 addition & 1 deletion packages/vite/package.json
Expand Up @@ -45,7 +45,7 @@
"rollup-plugin-visualizer": "^5.6.0",
"ufo": "^0.8.4",
"unplugin": "^0.7.0",
"vite": "~3.0.0-beta.1",
"vite": "~3.0.0-beta.5",
"vite-node": "^0.15.2",
"vite-plugin-checker": "^0.4.6"
},
Expand Down
10 changes: 9 additions & 1 deletion packages/vite/src/client.ts
Expand Up @@ -22,7 +22,15 @@ export async function buildClient (ctx: ViteBuildContext) {
ports: Array.from({ length: 20 }, (_, i) => hmrPortDefault + 1 + i)
})
const clientConfig: vite.InlineConfig = vite.mergeConfig(ctx.config, {
base: ctx.nuxt.options.dev ? undefined : './',
experimental: {
renderBuiltUrl: (filename, { type, hostType }) => {
if (hostType !== 'js' || type === 'asset') {
// In CSS we only use relative paths until we craft a clever runtime CSS hack
return { relative: true }
}
return { runtime: `__publicAssetsURL(${JSON.stringify(filename)})` }
}
},
define: {
'process.server': false,
'process.client': true,
Expand Down
18 changes: 14 additions & 4 deletions packages/vite/src/server.ts
Expand Up @@ -6,7 +6,7 @@ import { logger, resolveModule, isIgnored } from '@nuxt/kit'
import fse from 'fs-extra'
import { debounce } from 'perfect-debounce'
import replace from '@rollup/plugin-replace'
import { joinURL } from 'ufo'
import { joinURL, withoutLeadingSlash, withTrailingSlash } from 'ufo'
import { ViteBuildContext, ViteOptions } from './vite'
import { wpfs } from './utils/wpfs'
import { cacheDirPlugin } from './plugins/cache-dir'
Expand All @@ -22,9 +22,19 @@ export async function buildServer (ctx: ViteBuildContext) {
? joinURL(ctx.nuxt.options.app.baseURL, ctx.nuxt.options.app.buildAssetsDir)
: undefined,
experimental: {
buildAdvancedBaseOptions: {
assets: { runtime: (url: string) => `buildAssetsURL(${url})` },
public: { runtime: (url: string) => `publicAssetsURL(${url})` }
renderBuiltUrl: (filename, { type, hostType }) => {
if (hostType !== 'js') {
// In CSS we only use relative paths until we craft a clever runtime CSS hack
return { relative: true }
}
switch (type) {
case 'public':
return { runtime: `__publicAssetsURL(${JSON.stringify(filename)})` }
case 'asset': {
const relativeFilename = filename.replace(withTrailingSlash(withoutLeadingSlash(ctx.nuxt.options.app.buildAssetsDir)), '')
return { runtime: `__buildAssetsURL(${JSON.stringify(relativeFilename)})` }
}
}
}
},
define: {
Expand Down
14 changes: 7 additions & 7 deletions yarn.lock
Expand Up @@ -1663,7 +1663,7 @@ __metadata:
ufo: ^0.8.4
unbuild: latest
unimport: ^0.3.0
vite: ~3.0.0-beta.1
vite: ~3.0.0-beta.5
languageName: unknown
linkType: soft

Expand Down Expand Up @@ -1779,7 +1779,7 @@ __metadata:
ufo: ^0.8.4
unbuild: latest
unplugin: ^0.7.0
vite: ~3.0.0-beta.1
vite: ~3.0.0-beta.5
vite-node: ^0.15.2
vite-plugin-checker: ^0.4.6
vue: 3.2.37
Expand Down Expand Up @@ -13722,11 +13722,11 @@ __metadata:
languageName: node
linkType: hard

"vite@npm:~3.0.0-beta.1":
version: 3.0.0-beta.1
resolution: "vite@npm:3.0.0-beta.1"
"vite@npm:~3.0.0-beta.5":
version: 3.0.0-beta.5
resolution: "vite@npm:3.0.0-beta.5"
dependencies:
esbuild: ^0.14.43
esbuild: ^0.14.47
fsevents: ~2.3.2
postcss: ^8.4.14
resolve: ^1.22.1
Expand All @@ -13750,7 +13750,7 @@ __metadata:
optional: true
bin:
vite: bin/vite.js
checksum: 9a2ec7a25d35ef8af2107da010f8994d373726d3a275bdf0378c58e853a7acd1d59ce4c8d073eac609ddee8f66882b1a89b3898c964808827615eaf20c3bdbf6
checksum: 93a19bae089007236813e2ee95f9fdf342b8abc76a3c39ef2fd08f2bdbe2801bfb3473c7c1812253aad04fd2bda198b5e77bf24b4fb525d72b0003ee15fb4d42
languageName: node
linkType: hard

Expand Down

0 comments on commit cc594b3

Please sign in to comment.