feat: standalone bundler
This commit is contained in:
+8
-35
@@ -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()
|
||||
}
|
||||
|
||||
+9
-4
@@ -1,10 +1,11 @@
|
||||
import { normalize, relative } from 'path'
|
||||
import { normalize, relative, resolve } from 'path'
|
||||
import { createWriteStream, chmodSync, statSync } from 'fs'
|
||||
import { dequote } from '../util'
|
||||
import { Readable } from 'stream'
|
||||
import { NexeCompiler } from '../compiler'
|
||||
import { NexeTarget } from '../target'
|
||||
|
||||
function getStdIn(stdin: NodeJS.ReadStream): Promise<string> {
|
||||
function getStdIn(stdin: Readable): Promise<string> {
|
||||
return new Promise(resolve => {
|
||||
let out = ''
|
||||
stdin
|
||||
@@ -38,9 +39,13 @@ export default async function cli(compiler: NexeCompiler, next: () => Promise<vo
|
||||
let stdInUsed = false
|
||||
if (!process.stdin.isTTY && compiler.options.enableStdIn) {
|
||||
stdInUsed = true
|
||||
compiler.input = dequote(await getStdIn(process.stdin))
|
||||
compiler.entrypoint = './__nexe_stdin.js'
|
||||
const code = dequote(await getStdIn(process.stdin))
|
||||
await compiler.addResource(resolve(compiler.options.cwd, compiler.entrypoint), code)
|
||||
} else {
|
||||
compiler.entrypoint = './' + relative(compiler.options.cwd, compiler.options.input)
|
||||
}
|
||||
|
||||
compiler.startup = ';require("module").runMain();'
|
||||
await next()
|
||||
|
||||
const target = compiler.options.targets.shift() as NexeTarget
|
||||
|
||||
@@ -1,11 +1,9 @@
|
||||
import { readFileAsync, isDirectoryAsync, each } from '../util'
|
||||
import { isDirectoryAsync, each } from '../util'
|
||||
import * as globs from 'globby'
|
||||
import { NexeCompiler } from '../compiler'
|
||||
|
||||
export default async function resource(compiler: NexeCompiler, next: () => Promise<any>) {
|
||||
const resources = compiler.resources
|
||||
const { cwd } = compiler.options
|
||||
|
||||
if (!compiler.options.resources.length) {
|
||||
return next()
|
||||
}
|
||||
@@ -17,9 +15,8 @@ export default async function resource(compiler: NexeCompiler, next: () => Promi
|
||||
}
|
||||
count++
|
||||
step.log(`Including file: ${file}`)
|
||||
const contents = await readFileAsync(file)
|
||||
compiler.addResource(file, contents)
|
||||
await compiler.addResource(file)
|
||||
})
|
||||
step.log(`Included ${count} file(s). ${(resources.bundle.byteLength / 1e6).toFixed(3)} MB`)
|
||||
step.log(`Included ${count} file(s). ${(compiler.resourceSize / 1e6).toFixed(3)} MB`)
|
||||
return next()
|
||||
}
|
||||
|
||||
+10
-16
@@ -1,13 +1,12 @@
|
||||
import { NexeCompiler } from '../compiler'
|
||||
import { relative } from 'path'
|
||||
import { wrap } from '../util'
|
||||
|
||||
export default function(compiler: NexeCompiler, next: () => Promise<void>) {
|
||||
compiler.shims.push(
|
||||
wrap(
|
||||
`process.__nexe = ${JSON.stringify({ resources: compiler.resources.index })};\n` +
|
||||
'{{replace:lib/bundle/fs/patch.js}}' +
|
||||
'\nshimFs(process.__nexe, require("fs"))'
|
||||
`process.__nexe = ${JSON.stringify(compiler.binaryConfiguration)};\n` +
|
||||
'{{replace:lib/fs/patch.js}}' +
|
||||
'\nshimFs(process.__nexe)'
|
||||
)
|
||||
)
|
||||
compiler.shims.push(
|
||||
@@ -20,18 +19,13 @@ export default function(compiler: NexeCompiler, next: () => Promise<void>) {
|
||||
`)
|
||||
)
|
||||
|
||||
if (compiler.options.fakeArgv !== false) {
|
||||
const nty = !process.stdin.isTTY
|
||||
const input = nty
|
||||
? '[stdin]'
|
||||
: JSON.stringify(relative(compiler.options.cwd, compiler.options.input))
|
||||
|
||||
compiler.shims.push(
|
||||
wrap(`
|
||||
var r = require('path').resolve;
|
||||
process.argv.splice(1,0, ${nty ? `'${input}'` : `r(${input})`});`)
|
||||
)
|
||||
}
|
||||
compiler.shims.push(
|
||||
wrap(`
|
||||
if (!process.send) {
|
||||
process.argv.splice(1,0, require.resolve("${compiler.entrypoint}"))
|
||||
}
|
||||
`)
|
||||
)
|
||||
|
||||
return next()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user