Skip to content

Commit

Permalink
fix: mock readAsset when serveStatic is disabled
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 committed Apr 7, 2022
1 parent 4170f65 commit e95b9d9
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 23 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@
"std-env": "^3.0.1",
"table": "^6.8.0",
"ufo": "^0.7.11",
"unenv": "^0.4.4",
"unenv": "^0.4.5",
"unimport": "^0.1.0",
"unstorage": "^0.3.3"
},
Expand Down
18 changes: 10 additions & 8 deletions src/rollup/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,15 +168,17 @@ export const getRollupConfig = (nitro: Nitro) => {
rollupConfig.plugins.push(serverAssets(nitro))

// Public assets
rollupConfig.plugins.push({
name: 'dirnames',
renderChunk (code, chunk) {
return {
code: (chunk.isEntry ? 'globalThis.entryURL = import.meta.url;' : '') + code,
map: null
if (nitro.options.serveStatic) {
rollupConfig.plugins.push({
name: 'dirnames',
renderChunk (code, chunk) {
return {
code: (chunk.isEntry ? 'globalThis.entryURL = import.meta.url;' : '') + code,
map: null
}
}
}
})
})
}
rollupConfig.plugins.push(publicAssets(nitro))

// Storage
Expand Down
17 changes: 8 additions & 9 deletions src/rollup/plugins/public-assets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,33 +34,32 @@ export function publicAssets (nitro: Nitro): Plugin {

return virtual({
'#nitro/virtual/public-assets-data': `export default ${JSON.stringify(assets, null, 2)};`,
'#nitro/virtual/public-assets': `
import { promises } from 'fs'
'#nitro/virtual/public-assets-node': `
import { promises as fsp } from 'fs'
import { resolve } from 'pathe'
import { dirname } from 'pathe'
import { fileURLToPath } from 'url'
import assets from '#nitro/virtual/public-assets-data'
const mainDir = dirname(fileURLToPath(globalThis.entryURL))
export function readAsset (id) {
return fsp.readFile(resolve(mainDir, assets[id].path)).catch(() => {})
}`,
'#nitro/virtual/public-assets': `
import assets from '#nitro/virtual/public-assets-data'
${nitro.options.serveStatic ? 'export * from "#nitro/virtual/public-assets-node"' : 'export const readAsset = () => Promise(null)'}
export const publicAssetBases = ${JSON.stringify(publicAssetBases)}
export function isPublicAssetURL(id = '') {
if (assets[id]) {
return
}
for (const base of publicAssetBases) {
if (id.startsWith(base)) { return true }
}
return false
}
export function readAsset (id) {
return promises.readFile(resolve(mainDir, getAsset(id).path)).catch(() => {})
}
export function getAsset (id) {
return assets[id]
}
Expand Down
10 changes: 5 additions & 5 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5679,7 +5679,7 @@ __metadata:
table: ^6.8.0
ufo: ^0.7.11
unbuild: ^0.7.0
unenv: ^0.4.4
unenv: ^0.4.5
unimport: ^0.1.0
unstorage: ^0.3.3
vitest: ^0.6.1
Expand Down Expand Up @@ -7788,9 +7788,9 @@ __metadata:
languageName: node
linkType: hard

"unenv@npm:^0.4.4":
version: 0.4.4
resolution: "unenv@npm:0.4.4"
"unenv@npm:^0.4.5":
version: 0.4.5
resolution: "unenv@npm:0.4.5"
dependencies:
buffer: ^6.0.2
defu: ^6.0.0
Expand All @@ -7801,7 +7801,7 @@ __metadata:
process: ^0.11.10
upath: ^2.0.1
util: ^0.12.4
checksum: b3ff5d06f8c1eccdd4e9d933689c2334eafa37881da15908008d4973bc9d3aee2dff051b3c42388e3f0442aeecf9f8a7e39efbfd8f15c06fb878f478b1dda98e
checksum: 1dc8600607b300dd8b92ea51394e6fa77fe7b2739a2d8016b09d3060479fedf8a6b37859315a2b8c39d6292d040cad4d9995974b74989a5418312bce3c54b9c3
languageName: node
linkType: hard

Expand Down

0 comments on commit e95b9d9

Please sign in to comment.