feat: standalone bundler

This commit is contained in:
calebboyd
2018-08-16 20:52:39 -05:00
committed by Caleb Boyd
parent c70d93b72f
commit 53ef02f87a
28 changed files with 638 additions and 2538 deletions
+8 -35
View File
@@ -1,49 +1,22 @@
import { NexeCompiler } from '../compiler'
import { readFileAsync, writeFileAsync } from '../util'
import { resolve, relative } from 'path'
import { resolve } from 'path'
import { each } from '@calebboyd/semaphore'
import resolveFiles from 'resolve-dependencies'
function makeRelative(cwd: string, path: string) {
return './' + relative(cwd, path)
}
let producer = async function(compiler: NexeCompiler): Promise<string> {
const { cwd, input } = compiler.options
const { files, entries } = await resolveFiles(input, { cwd, expand: true })
const mainFileContents = (entries[input].contents as string) || ''
Object.keys(files).forEach(filename => {
const file = files[filename]!
if (file && file.contents) {
compiler.addResource(makeRelative(cwd, filename), Buffer.from(file.contents))
}
})
return Promise.resolve(mainFileContents)
}
export default async function bundle(compiler: NexeCompiler, next: any) {
const { bundle, cwd, empty, input } = compiler.options
const { bundle, cwd, input } = compiler.options
if (!bundle) {
compiler.input = await readFileAsync(resolve(cwd, input), 'utf-8')
await compiler.addResource(resolve(cwd, input))
return next()
}
if (!input) {
compiler.input = ''
return next()
}
if (typeof bundle === 'string') {
producer = require(resolve(cwd, bundle)).createBundle
}
compiler.input = await producer(compiler)
const debugBundle = compiler.options.debugBundle
if (debugBundle) {
let bundleDebugFile = typeof debugBundle === 'string' ? debugBundle : 'nexe-debug.bundle.js'
await writeFileAsync(resolve(cwd, bundleDebugFile), compiler.input)
}
const { files } = await resolveFiles(input, { cwd, expand: true, loadContent: false })
await each(Object.keys(files), (filename: string) => compiler.addResource(filename), {
concurrency: 10
})
return next()
}