fix: addresses static assets/azure issues (#49)

This commit is contained in:
Daniel Roe 2020-12-01 23:28:42 +00:00 committed by GitHub
parent a9b9d19e66
commit 9c25d68511
2 changed files with 15 additions and 6 deletions

View File

@ -19,7 +19,7 @@ import { dynamicRequire } from './plugins/dynamic-require'
import { externals } from './plugins/externals'
import { timing } from './plugins/timing'
import { autoMock } from './plugins/automock'
import { staticAssets } from './plugins/static'
import { staticAssets, dirnames } from './plugins/static'
import { middleware } from './plugins/middleware'
import { esbuild } from './plugins/esbuild'
@ -128,6 +128,7 @@ export const getRollupConfig = (sigmaContext: SigmaContext) => {
// Static
if (sigmaContext.serveStatic) {
rollupConfig.plugins.push(dirnames())
rollupConfig.plugins.push(staticAssets(sigmaContext))
}

View File

@ -4,6 +4,7 @@ import mime from 'mime'
import { relative, resolve } from 'upath'
import virtual from '@rollup/plugin-virtual'
import globby from 'globby'
import type { Plugin } from 'rollup'
import type { SigmaContext } from '../../context'
export function staticAssets (context: SigmaContext) {
@ -29,14 +30,12 @@ export function staticAssets (context: SigmaContext) {
return virtual({
'~static-assets': `export default ${JSON.stringify(assets, null, 2)};`,
'~static': `
import { readFile } from 'fs/promises'
import { resolve, dirname } from 'path'
import { promises } from 'fs'
import { resolve } from 'path'
import assets from '~static-assets'
const mainDir = dirname(require.main.filename)
export function readAsset (id) {
return readFile(resolve(mainDir, getAsset(id).path))
return promises.readFile(resolve(mainDir, getAsset(id).path))
}
export function getAsset (id) {
@ -45,3 +44,12 @@ export function getAsset (id) {
`
})
}
export function dirnames (): Plugin {
return {
name: 'dirnames',
renderChunk (code, chunk) {
return code + (chunk.isEntry ? 'global.mainDir="undefined"!=typeof __dirname?__dirname:require.main.filename;' : '')
}
}
}