From 1e12eaae1f84ccafb7e9344f131cd3d16427cff0 Mon Sep 17 00:00:00 2001 From: Joseph Werle Date: Thu, 11 Nov 2021 14:25:54 -0500 Subject: [PATCH 1/5] chore(): update everything --- compiler.js | 3 +-- loader.js | 1 + package.json | 16 ++++++++-------- test/unpack.js | 4 ++++ unpack.js | 6 +++--- 5 files changed, 17 insertions(+), 13 deletions(-) diff --git a/compiler.js b/compiler.js index a9c7a63..0a97ea6 100644 --- a/compiler.js +++ b/compiler.js @@ -10,7 +10,7 @@ const magic = require('./magic') const ready = require('nanoresource-ready') const path = require('path') const raf = require('random-access-file') -const ncc = require('@zeit/ncc') +const ncc = require('@vercel/ncc') const vm = require('vm') // quick util @@ -143,7 +143,6 @@ class Compiler extends Pool { try { for (const target of targets) { const { filename } = target - const extname = path.extname(filename) const basename = path.basename(filename) const targetName = path.resolve(target.output) .replace(path.join(path.resolve(this.cwd), path.sep), '') diff --git a/loader.js b/loader.js index 6c62d61..ff3b3d3 100644 --- a/loader.js +++ b/loader.js @@ -136,6 +136,7 @@ class Loader extends Pool { // istanbul ignore next const createRequireFromPath = Module.createRequire || Module.createRequireFromPath + // istanbul ignore next const contextRequire = 'function' === typeof createRequireFromPath ? createRequireFromPath(filename) : makeRequireFunction(contextModule) diff --git a/package.json b/package.json index 83ce8f5..3430ab1 100644 --- a/package.json +++ b/package.json @@ -27,13 +27,13 @@ "author": "Joseph Werle ", "license": "MIT", "dependencies": { - "@zeit/ncc": "^0.20.5", + "@vercel/ncc": "^0.31.1", "batch": "^0.6.1", "debug": "^4.1.1", "glob": "^7.1.5", - "isutf8": "^2.1.0", + "isutf8": "^4.0.0", "minimist": "^1.2.0", - "mkdirp": "^0.5.1", + "mkdirp": "^1.0.4", "nanoassert": "^2.0.0", "nanoresource": "^1.2.0", "nanoresource-pool": "^0.3.1", @@ -42,18 +42,18 @@ "protocol-buffers": "^4.1.0", "random-access-file": "^2.1.3", "rimraf": "^3.0.0", - "semver": "^6.3.0", + "semver": "^7.3.5", "tinybox": "0.0.0", - "uint64be": "^2.0.2", - "varint": "^5.0.0" + "uint64be": "^3.0.0", + "varint": "^6.0.0" }, "directories": { "test": "test" }, "devDependencies": { - "nyc": "^14.1.1", + "nyc": "^15.1.0", "random-access-memory": "^3.1.1", - "tape": "^4.11.0" + "tape": "^5.3.1" }, "repository": { "type": "git", diff --git a/test/unpack.js b/test/unpack.js index 76a0a58..5212338 100644 --- a/test/unpack.js +++ b/test/unpack.js @@ -9,7 +9,9 @@ test('unpack(target, callback)', (t) => { const target = filename + '.a' const cwd = process.cwd() compile(filename, (err, objects) => { + t.error(err) archive(target, objects, (err) => { + t.error(err) unpack(target, (err, entries) => { t.error(err) t.ok(Array.isArray(entries), 'entries') @@ -28,7 +30,9 @@ test('unpack(target, callback) - from storage', (t) => { const target = filename + '.a' const cwd = process.cwd() compile(filename, { storag: ram }, (err, objects) => { + t.error(err) archive(target, objects, { storage }, (err) => { + t.error(err) unpack(storage, { storage: ram }, (err, entries) => { t.error(err) t.ok(Array.isArray(entries), 'entries') diff --git a/unpack.js b/unpack.js index 61d3b4b..fb3fa38 100644 --- a/unpack.js +++ b/unpack.js @@ -81,9 +81,9 @@ function unpack(target, opts, callback) { if (!hasCustomStorage) { const dirname = path.dirname(filename) - return mkdirp(dirname, (err) => { - // istanbul ignore next - if (err) { return next(err) } + const promise = mkdirp(dirname) + promise.catch((err) => next(err)) + return promise.then(() => { write(raf(filename), buffer, (err) => { // istanbul ignore next if (err) { return next(err) } From 92b7970151075827ee03b5c2288b9526d2ec3d4a Mon Sep 17 00:00:00 2001 From: Joseph Werle Date: Thu, 11 Nov 2021 14:38:52 -0500 Subject: [PATCH 2/5] chore(package.json): add 'sodium-universal' dev dep --- package.json | 1 + 1 file changed, 1 insertion(+) diff --git a/package.json b/package.json index 3430ab1..c36921a 100644 --- a/package.json +++ b/package.json @@ -53,6 +53,7 @@ "devDependencies": { "nyc": "^15.1.0", "random-access-memory": "^3.1.1", + "sodium-universal": "^3.0.4", "tape": "^5.3.1" }, "repository": { From 8278b4a1819ddc357cd0ee3a1e417b6b0e885bf8 Mon Sep 17 00:00:00 2001 From: Joseph Werle Date: Thu, 11 Nov 2021 14:39:34 -0500 Subject: [PATCH 3/5] chore(release): 1.3.0 :tada: --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index c36921a..72c90da 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "tiny-module-compiler", - "version": "1.2.2", + "version": "1.3.0", "description": "Compile, archive, unpack, and load compiled modules leveraging v8 cached data.", "main": "index.js", "scripts": { From f4b7d3487f5efb9e9f36f9b1ae3f1af96d40cc1e Mon Sep 17 00:00:00 2001 From: Joseph Werle Date: Thu, 11 Nov 2021 16:34:26 -0500 Subject: [PATCH 4/5] feat(import): introdce 'import' and external asset copying --- bin/tmc | 107 +++++++++++++++++++++++++++++++++++++++---------- compiler.js | 2 +- index.js | 39 +++++++++++++++++- package.json | 2 + test/import.js | 26 ++++++++++++ 5 files changed, 151 insertions(+), 25 deletions(-) create mode 100644 test/import.js diff --git a/bin/tmc b/bin/tmc index fa5fcc1..d082dd9 100755 --- a/bin/tmc +++ b/bin/tmc @@ -4,15 +4,42 @@ const { compile, archive, unpack, load } = require('../') const { version } = require('../package.json') const prettyBytes = require('pretty-bytes') const minimist = require('minimist') +const mirror = require('mirror-folder') const mkdirp = require('mkdirp') const Batch = require('batch') const debug = require('debug')('tiny-module-compiler') const path = require('path') +const copy = require('cp-file') const glob = require('glob') const fs = require('fs') const os = require('os') +const PACKAGE_JSON = 'package.json' +const NODE_MODULES = 'node_modules' + +const usage = `usage: tmc [-hV] [-acu] [-vCDMOS] [options] ...input +where options can be: + + -a, --archive If present, will archive input into "tinybox" format + -c, --compile If present, will compile input into header prefixed v8 cached data + , --concurrency An alias for '--jobs' + -C, --copy-assets If present, will copy assets to directory of output + -D, --debug If present, will enable debug output (DEBUG=tiny-module-compiler) + -e, --external Specifies an external dependency that will be linked at runtime + -h, --help If present, will print this message + -l, --load If present, will load inputs + -j, --jobs Specifies the number of concurrent jobs for batch tasks (--load, --archive, --copy-assets) + -M, --source-map If present, a source map will be generated + -o, --output If present, will change the output path. Assumes directory if multiple inputs given + -O, --optimize If present, will optimize output by minifying JavaScript source prior to compilation + -u, --unpack If present, will treat input as an archive and will unpack files to path specified by '--output' + -v, --verbose If present, will emit verbose output to stdout/stderr + -V, --version If present, will print the version number + -x An alias for '--external' +`.trim() + const argv = minimist(process.argv.slice(2), { + '--': true, boolean: [ 'archive', 'compile', @@ -22,12 +49,14 @@ const argv = minimist(process.argv.slice(2), { 'load', 'optimize', 'source-map', + 'strict', 'unpack', 'verbose', 'version', ], string: [ + 'cwd', 'output' ], @@ -52,34 +81,20 @@ const argv = minimist(process.argv.slice(2), { M: 'source-map', O: 'optimize', o: 'output', + S: 'strict', u: 'unpack', v: 'verbose', V: 'version', x: 'external', + }, + + unknown(arg) { + console.error('error: unknown option: %s', arg) + process.nextTick(() => process.exit(1)) + return false } }) -const usage = `usage: tmc [-hV] [-acu] [-vCDMO] [options] ...input -where options can be: - - -a, --archive If present, will archive input into "tinybox" format - -c, --compile If present, will compile input into header prefixed v8 cached data - , --concurrency An alias for '--jobs' - -C, --copy-assets If present, will copy assets to directory of output - -D, --debug If present, will enable debug output (DEBUG=tiny-module-compiler) - -e, --external Specifies an external dependency that will be linked at runtime - -h, --help If present, will print this message - -l, --load If present, will load inputs - -j, --jobs Specifies the number of concurrent jobs for batch tasks (--load, --archive, --copy-assets) - -M, --source-map If present, a source map will be generated - -o, --output If present, will change the output path. Assumes directory if multiple inputs given - -O, --optimize If present, will optimize output by minifying JavaScript source prior to compilation - -u, --unpack If present, will treat input as an archive and will unpack files to path specified by '--output' - -v, --verbose If present, will emit verbose output to stdout/stderr - -V, --version If present, will print the version number - -x An alias for '--external' -`.trim() - if (argv.version) { console.log(version) process.exit(0) @@ -129,7 +144,7 @@ if (argv.compile || (!argv.archive && !argv.unpack && !argv.load)) { // default } } - compile(argv._, opts, (err, objects, assets) => { + compile(argv._, opts, (err, _, assets) => { if (err) { console.error('error:', err.message) debug(err) @@ -151,6 +166,54 @@ if (argv.compile || (!argv.archive && !argv.unpack && !argv.load)) { // default }) } + for (const external of opts.externals) { + writes.push((next) => { + try { + const modulePackagePathName = external + path.sep + PACKAGE_JSON + const modulePackagePath = require.resolve(modulePackagePathName) + const modulePath = path.dirname(modulePackagePath) + + const cwd = argv.cwd || process.cwd() + const src = modulePath + const dst = cwd + path.sep + NODE_MODULES + path.sep + external + + mkdirp.sync(path.dirname(dst)) + mirror(src, dst, { ensureParents: true }, next) + + } catch (err) { + try { + const cwd = argv.cwd || process.cwd() + const stats = fs.statSync(external) + if (stats && stats.isFile()) { + return copy(external, path.resolve(cwd, path.basename(external))) + .then(() => next(), (err) => next(err)) + } else if (stats && stats.isDirectory()) { + mkdirp.sync(path.join(cwd, path.basename(external))) + return mirror( + external, + path.join(cwd, path.basename(external)), + { ensureParents: true }, + next) + } + } catch (err) { + debug(err) + if (argv.strict) { + return next(err) + } + } + + debug(err) + if (argv.strict) { + return next(err) + } + + console.warn(' warn: failed to copy external dependency asset:%s', external) + next(null) + } + }) + + } + writes.end((err) => { if (err) { console.error('error:', err.message) diff --git a/compiler.js b/compiler.js index 0a97ea6..1151651 100644 --- a/compiler.js +++ b/compiler.js @@ -134,7 +134,7 @@ class Compiler extends Pool { opts = {} } - const { targets } = this + const { targets, cwd } = this const objects = new Map() const assets = new Map() const writes = new Batch() diff --git a/index.js b/index.js index 28b88e7..f19ebf9 100644 --- a/index.js +++ b/index.js @@ -6,9 +6,44 @@ const { load } = require('./load') /** * Module exports. */ -module.exports = { +module.exports = Object.create({ archive, compile, unpack, load, -} + + /** + * Imports a module at path returning its exports object. + * @param {String} pathspec + * @param {?(Object)} opts + * @param {?(Map)} opts.cache + * @param {?(String)} opts.cwd + * @param {?(Object)} opts.storage + * @param {Function} callback + * @return {Object} + */ + async import(pathspec, opts) { + return await new Promise((resolve, reject) => { + try { + const target = load(pathspec, opts, (err, exports) => { + // istanbul ignore next + if (err) { + // istanbul ignore next + return reject(err) + } + + // istanbul ignore next + Object.defineProperty(exports, '__target__', { + enumerable: false, + get: () => target + }) + + resolve(exports) + }) + } catch (err) { + // istanbul ignore next + return reject(err) + } + }) + } +}) diff --git a/package.json b/package.json index 72c90da..460399d 100644 --- a/package.json +++ b/package.json @@ -29,10 +29,12 @@ "dependencies": { "@vercel/ncc": "^0.31.1", "batch": "^0.6.1", + "cp-file": "^9.1.0", "debug": "^4.1.1", "glob": "^7.1.5", "isutf8": "^4.0.0", "minimist": "^1.2.0", + "mirror-folder": "^3.1.0", "mkdirp": "^1.0.4", "nanoassert": "^2.0.0", "nanoresource": "^1.2.0", diff --git a/test/import.js b/test/import.js new file mode 100644 index 0000000..5c87920 --- /dev/null +++ b/test/import.js @@ -0,0 +1,26 @@ +const path = require('path') +const test = require('tape') + +const { compile, import: _import } = require('../') + +test('load(target, callback) - source module', async (t) => { + const filename = path.resolve(__dirname, 'fixtures', 'simple.js') + const { hello } = await _import(filename) + t.equal('function', typeof hello, 'hello') + t.equal('hello', hello(), 'hello()') +}) + +test('load(target, callback) - compiled module', async (t) => { + const filename = path.resolve(__dirname, 'fixtures', 'simple.js') + const destname = filename.replace(process.cwd() + path.sep, '') + '.out' + + await new Promise((resolve, reject) => compile(filename, (err) => { + if (err) { return reject(err) } + resolve() + })) + + const { hello } = await _import(destname) + + t.equal('function', typeof hello, 'hello') + t.equal('hello', hello(), 'hello()') +}) From 67002da04e25121934b373e4d1306319188f8880 Mon Sep 17 00:00:00 2001 From: Joseph Werle Date: Thu, 11 Nov 2021 16:53:11 -0500 Subject: [PATCH 5/5] chore(release): 1.3.1 :tada: --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 460399d..4b2fb28 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "tiny-module-compiler", - "version": "1.3.0", + "version": "1.3.1", "description": "Compile, archive, unpack, and load compiled modules leveraging v8 cached data.", "main": "index.js", "scripts": {