Skip to content

Commit

Permalink
fix: support fallback for import.meta per chunk
Browse files Browse the repository at this point in the history
(non lazy) chunks are evaluated even before entry. this adds a temporary fallback value for `globalThis._importMeta_` for chunks
  • Loading branch information
pi0 committed May 6, 2022
1 parent 6e1a0e7 commit 5a83ec6
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 16 deletions.
16 changes: 2 additions & 14 deletions src/rollup/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import { handlers } from './plugins/handlers'
import { esbuild } from './plugins/esbuild'
import { raw } from './plugins/raw'
import { storage } from './plugins/storage'
import { importMeta } from './plugins/import-meta'

export type RollupConfig = InputOptions & { output: OutputOptions }

Expand Down Expand Up @@ -143,20 +144,7 @@ export const getRollupConfig = (nitro: Nitro) => {
}

// Universal import.meta
rollupConfig.plugins.push({
name: 'import-meta',
renderChunk (code, chunk) {
if (!chunk.isEntry) {
return
}
const url = nitro.options.node ? '_import_meta_url_' : '"file://_entry.js"'
const env = nitro.options.node ? 'process.env' : '{}'
return {
code: `globalThis._importMeta_={url:${url},env:${env}};` + code,
map: null
}
}
})
rollupConfig.plugins.push(importMeta(nitro))

// https://github.com/rollup/plugins/tree/master/packages/replace
rollupConfig.plugins.push(replace({
Expand Down
28 changes: 28 additions & 0 deletions src/rollup/plugins/import-meta.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import type { Plugin } from 'rollup'
import { Nitro } from '../../types'

export function importMeta (nitro: Nitro): Plugin {
const ImportMetaRe = /import\.meta|globalThis._importMeta_/

return {
name: 'import-meta',
renderChunk (code, chunk) {
const isEntry = chunk.isEntry
if (!isEntry && (!ImportMetaRe.test(code) || code.includes('ROLLUP_NO_REPLACE'))) {
return
}
const url = (nitro.options.node && isEntry) ? '_import_meta_url_' : '"file:///_entry.js"'
const env = nitro.options.node ? 'process.env' : '{}'
const ref = 'globalThis._importMeta_'
const stub = `{url:${url},env:${env}}`
const stubInit = isEntry
? `${ref}=${stub};`
: `${ref}=${ref}||${stub};`

return {
code: stubInit + code,
map: null
}
}
}
}
4 changes: 2 additions & 2 deletions src/rollup/plugins/public-assets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ import { resolve } from 'pathe'
import { dirname } from 'pathe'
import { fileURLToPath } from 'url'
import assets from '#internal/nitro/virtual/public-assets-data'
const mainDir = dirname(fileURLToPath(import.meta.url))
export function readAsset (id) {
return fsp.readFile(resolve(mainDir, assets[id].path)).catch(() => {})
const serverDir = dirname(fileURLToPath(import.meta.url))
return fsp.readFile(resolve(serverDir, assets[id].path))
}`,
'#internal/nitro/virtual/public-assets': `
import assets from '#internal/nitro/virtual/public-assets-data'
Expand Down

0 comments on commit 5a83ec6

Please sign in to comment.