feat: use yarn fslib/zip for fs shimming (#1048)

* refactor: compile with esmoudle interop

* Rigidify code replacement by using Lodash templates.

* Generate zip file bundle.

* Use yarn's fslib to implement the fake filesystem.

Leverage the work done by yarn, rather than reimplementing from scratch.

This means the fake fs is now reading from a zip file in the binary.

A few methods need custom implementations as the fake fs is not
writable. These were copied and adjusted from the yarn ZipOpenFS.

* Add test suite using the built artefact.

Smoke tests the different fs methods.

Not perfect in any world, but better than nothing.

* Fix tsc on Windows - globbing doesn't work there.

There's probably a cleaner way to do this...

* Bump version.

* Dep bumps.

* fixup 9d57b32: make fs patch code clearer.

* fixup 3771c720: simplify test to just use require directly.

---------

Co-authored-by: calebboyd <caleb.boyd@gmail.com>
This commit is contained in:
bruce-one
2023-08-21 05:43:06 +10:00
committed by GitHub
parent fdd9d97db3
commit ca657b5b06
18 changed files with 6051 additions and 2204 deletions
+7 -4
View File
@@ -61,9 +61,11 @@ export default async function bundle(compiler: NexeCompiler, next: any) {
compiler.entrypoint = './' + relative(cwd, input)
}
const { files, warnings } = resolveFiles(
const step = compiler.log.step('Resolving dependencies...')
const { files, warnings } = await resolveFiles(
input,
...Object.keys(compiler.bundle.list).filter((x) => x.endsWith('.js')),
...Object.keys(compiler.bundle.list).filter((x) => x.endsWith('.js') || x.endsWith('.mjs')),
{ cwd, expand: 'variable', loadContent: false }
)
@@ -76,8 +78,9 @@ export default async function bundle(compiler: NexeCompiler, next: any) {
//TODO: warnings.forEach((x) => console.log(x))
await Promise.all(
Object.entries(files).map(([key, file]) => {
return compiler.addResource(key, file)
Object.entries(files).map(([key]) => {
step.log(`Including dependency: ${key}`)
return compiler.addResource(key)
})
)
+1 -1
View File
@@ -1,5 +1,5 @@
import { each } from '../util'
import * as globs from 'globby'
import globs from 'globby'
import { resolve } from 'path'
import { NexeCompiler } from '../compiler'
+11 -4
View File
@@ -5,10 +5,17 @@ export default async function (compiler: NexeCompiler, next: () => Promise<void>
await next()
compiler.shims.push(
wrap(
'' +
'{{replace:lib/fs/patch.js}}' +
'\nshimFs(process.__nexe)' +
`\n${compiler.options.fs ? '' : 'restoreFs()'}`
[
'process.__nexe = {};',
'const fsPatcher = (function() {',
'const module = {exports: {}};',
'const exports = module.exports;',
'{{file("lib/fs/patch.bundle.js")}}',
'return module.exports;',
'})()',
'fsPatcher.shimFs(process.__nexe);',
compiler.options.fs ? '' : 'restoreFs();',
].join('\n')
//TODO support only restoring specific methods
)
)