-
Notifications
You must be signed in to change notification settings - Fork 509
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
93 additions
and
75 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import hasha from 'hasha' | ||
import virtual from '@rollup/plugin-virtual' | ||
import type { ServerMiddleware } from '../../context' | ||
|
||
export function middleware (middleware: ServerMiddleware[]) { | ||
const getImportId = p => '_' + hasha(p).substr(0, 6) | ||
|
||
return virtual({ | ||
'~serverMiddleware': ` | ||
${middleware.filter(m => m.lazy === false).map(m => `import ${getImportId(m.handle)} from '${m.handle}';`).join('\n')} | ||
${middleware.filter(m => m.lazy !== false).map(m => `const ${getImportId(m.handle)} = () => import('${m.handle}');`).join('\n')} | ||
const middleware = [ | ||
${middleware.map(m => `{ route: '${m.route}', handle: ${getImportId(m.handle)}, lazy: ${m.lazy || true}, promisify: ${m.promisify !== undefined ? m.promisify : true} }`).join(',\n')} | ||
]; | ||
export default middleware | ||
` | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import createEtag from 'etag' | ||
import { readFileSync, statSync } from 'fs-extra' | ||
import mime from 'mime' | ||
import { relative, resolve } from 'upath' | ||
import virtual from '@rollup/plugin-virtual' | ||
import globby from 'globby' | ||
import type { SigmaContext } from '../../context' | ||
|
||
export function staticAssets (context: SigmaContext) { | ||
const assets: Record<string, { type: string, etag: string, mtime: string, path: string }> = {} | ||
|
||
const files = globby.sync('**/*.*', { cwd: context.output.publicDir, absolute: false }) | ||
|
||
for (const id of files) { | ||
let type = mime.getType(id) || 'text/plain' | ||
if (type.startsWith('text')) { type += '; charset=utf-8' } | ||
const fullPath = resolve(context.output.publicDir, id) | ||
const etag = createEtag(readFileSync(fullPath)) | ||
const stat = statSync(fullPath) | ||
|
||
assets[id] = { | ||
type, | ||
etag, | ||
mtime: stat.mtime.toJSON(), | ||
path: relative(context.output.serverDir, fullPath) | ||
} | ||
} | ||
|
||
return virtual({ | ||
'~static-assets': `export default ${JSON.stringify(assets, null, 2)};`, | ||
'~static': ` | ||
import { readFile } from 'fs/promises' | ||
import { resolve, dirname } from 'path' | ||
import assets from '~static-assets' | ||
const mainDir = dirname(require.main.filename) | ||
export function readAsset (id) { | ||
return readFile(resolve(mainDir, getAsset(id).path)) | ||
} | ||
export function getAsset (id) { | ||
return assets[id] | ||
} | ||
` | ||
}) | ||
} |
File renamed without changes.