Skip to content

Commit

Permalink
fix: addresses static assets/azure issues (#49)
Browse files Browse the repository at this point in the history
  • Loading branch information
danielroe authored Dec 1, 2020
1 parent af42712 commit 88e8bd9
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
3 changes: 2 additions & 1 deletion src/rollup/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand Down Expand Up @@ -128,6 +128,7 @@ export const getRollupConfig = (sigmaContext: SigmaContext) => {

// Static
if (sigmaContext.serveStatic) {
rollupConfig.plugins.push(dirnames())
rollupConfig.plugins.push(staticAssets(sigmaContext))
}

Expand Down
18 changes: 13 additions & 5 deletions src/rollup/plugins/static.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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) {
Expand All @@ -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;' : '')
}
}
}

0 comments on commit 88e8bd9

Please sign in to comment.