From 990395f807cfc41320b5c29c0dfbada0d827a005 Mon Sep 17 00:00:00 2001 From: "greenkeeper[bot]" Date: Wed, 25 Oct 2017 13:39:40 +0000 Subject: [PATCH 01/44] fix(package): update ascjs to version 1.0.2 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index f8cf2a5..586c6f5 100644 --- a/package.json +++ b/package.json @@ -30,7 +30,7 @@ }, "homepage": "https://github.com/WebReflection/asbundle#readme", "dependencies": { - "ascjs": "1.0.1", + "ascjs": "1.0.2", "esprima": "^4.0.0" } } From aadc8eaa2528eeb5cb3f5a73607904532fba0beb Mon Sep 17 00:00:00 2001 From: Andrea Giammarchi Date: Wed, 25 Oct 2017 10:46:05 -0300 Subject: [PATCH 02/44] 1.0.2 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 586c6f5..70f91e3 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "asbundle", - "version": "1.0.1", + "version": "1.0.2", "description": "A minimalistic CommonJS bundler", "bin": "bin.js", "main": "index.js", From 72b366833df8bed45c61fd7292307dab884d2ccb Mon Sep 17 00:00:00 2001 From: Andrea Giammarchi Date: Thu, 26 Oct 2017 11:33:29 -0300 Subject: [PATCH 03/44] 1.0.3 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 70f91e3..8c23330 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "asbundle", - "version": "1.0.2", + "version": "1.0.3", "description": "A minimalistic CommonJS bundler", "bin": "bin.js", "main": "index.js", From e1abfc4355ee1b3d320edeadae50df676307f38e Mon Sep 17 00:00:00 2001 From: Andrea Giammarchi Date: Thu, 26 Oct 2017 11:34:00 -0300 Subject: [PATCH 04/44] 1.0.4 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 8c23330..53ddcb0 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "asbundle", - "version": "1.0.3", + "version": "1.0.4", "description": "A minimalistic CommonJS bundler", "bin": "bin.js", "main": "index.js", From 97102c2e6ed77481511b0f1883a86d3cfc5138be Mon Sep 17 00:00:00 2001 From: Andrea Giammarchi Date: Thu, 26 Oct 2017 11:40:39 -0300 Subject: [PATCH 05/44] improved 10X module path resolution --- index.js | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/index.js b/index.js index 7b86eb5..b23949d 100644 --- a/index.js +++ b/index.js @@ -7,7 +7,6 @@ const defaultOptions = { tolerant: true }; -const execSync = require('child_process').execSync; const fs = require('fs'); const path = require('path'); @@ -72,12 +71,8 @@ const parse = (options, base, file, cache, modules) => { let name = path.resolve(base, module.value); addChunk(module, /\.js$/.test(name) ? name : (name + '.js')); } else { - let name = execSync( - `node -e 'console.log(require.resolve(${ - JSON.stringify(module.value) - }))'`, - {cwd: base} - ).toString().trim(); + process.chdir(base); + let name = require.resolve(module.value); if (name === module.value) { throw `unable to find "${name}" via file://${file}\n`; } From 6c67aac74e972776b3c4c0711d59843a40c145aa Mon Sep 17 00:00:00 2001 From: Andrea Giammarchi Date: Thu, 26 Oct 2017 11:40:57 -0300 Subject: [PATCH 06/44] 1.1.0 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 53ddcb0..9053f65 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "asbundle", - "version": "1.0.4", + "version": "1.1.0", "description": "A minimalistic CommonJS bundler", "bin": "bin.js", "main": "index.js", From e8e45355b538f2f4b0d89f9a2de0a9943b0d9cc8 Mon Sep 17 00:00:00 2001 From: Andrea Giammarchi Date: Thu, 26 Oct 2017 22:08:29 -0300 Subject: [PATCH 07/44] compatible with .json required files too --- index.js | 14 +++++++++----- test/json.js | 3 +++ 2 files changed, 12 insertions(+), 5 deletions(-) create mode 100644 test/json.js diff --git a/index.js b/index.js index b23949d..685e517 100644 --- a/index.js +++ b/index.js @@ -68,14 +68,18 @@ const parse = (options, base, file, cache, modules) => { if (item.type === 'CallExpression' && item.callee.name === 'require') { const module = item.arguments[0]; if (/^[./]/.test(module.value)) { - let name = path.resolve(base, module.value); - addChunk(module, /\.js$/.test(name) ? name : (name + '.js')); + const name = require.resolve(path.resolve(base, module.value)); + if (/\.m?js$/.test(name)) addChunk(module, name); + else { + if (cache.indexOf(name) < 0) cache.push(name); + addChunk(module, name); + modules[cache.indexOf(name)] = `module.exports = ${JSON.stringify(require(name))};`; + } } else { process.chdir(base); - let name = require.resolve(module.value); - if (name === module.value) { + const name = require.resolve(module.value); + if (name === module.value) throw `unable to find "${name}" via file://${file}\n`; - } addChunk(module, name); } } diff --git a/test/json.js b/test/json.js new file mode 100644 index 0000000..4baebbc --- /dev/null +++ b/test/json.js @@ -0,0 +1,3 @@ +const json = require('../package.json'); + +console.log(json); \ No newline at end of file From fd4f13566e0339c0bc0a4f37c570c6fbf25838a9 Mon Sep 17 00:00:00 2001 From: Andrea Giammarchi Date: Thu, 26 Oct 2017 22:08:32 -0300 Subject: [PATCH 08/44] 1.1.1 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 9053f65..e470d53 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "asbundle", - "version": "1.1.0", + "version": "1.1.1", "description": "A minimalistic CommonJS bundler", "bin": "bin.js", "main": "index.js", From d2f112624fcd9cab73990269fbba1429fe288dc6 Mon Sep 17 00:00:00 2001 From: Andrea Giammarchi Date: Thu, 26 Oct 2017 22:11:45 -0300 Subject: [PATCH 09/44] faster multiple json require --- index.js | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index 685e517..535108f 100644 --- a/index.js +++ b/index.js @@ -71,9 +71,12 @@ const parse = (options, base, file, cache, modules) => { const name = require.resolve(path.resolve(base, module.value)); if (/\.m?js$/.test(name)) addChunk(module, name); else { - if (cache.indexOf(name) < 0) cache.push(name); + const i = cache.indexOf(name); + if (i < 0) cache.push(name); addChunk(module, name); - modules[cache.indexOf(name)] = `module.exports = ${JSON.stringify(require(name))};`; + if (i < 0) { + modules[cache.length - 1] = `module.exports = ${JSON.stringify(require(name))};`; + } } } else { process.chdir(base); From 1ffdfa2bec08f0dbe36c152de3d135e1e96bc2fc Mon Sep 17 00:00:00 2001 From: Andrea Giammarchi Date: Thu, 26 Oct 2017 22:11:51 -0300 Subject: [PATCH 10/44] 1.1.2 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index e470d53..c4461b1 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "asbundle", - "version": "1.1.1", + "version": "1.1.2", "description": "A minimalistic CommonJS bundler", "bin": "bin.js", "main": "index.js", From b1bdb26c8875ab922a578b4dd1e140b94a0278c8 Mon Sep 17 00:00:00 2001 From: Andrea Giammarchi Date: Sat, 28 Oct 2017 19:41:32 -0300 Subject: [PATCH 11/44] Added support for imports from unpkg/registry CDNs If an ESM module imports directly from a CDN as bundle will look for that module as installed locally and eventually will use instead of the remote one, enabling native browser support, bundling only installed npm versions. --- index.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index 535108f..51f29c0 100644 --- a/index.js +++ b/index.js @@ -79,8 +79,10 @@ const parse = (options, base, file, cache, modules) => { } } } else { + let name = module.value; + if (/^https?:\/\/(?:unpkg\.com|registry\.npmjs\.org)\/([^@/]+)/.test(name)) name = RegExp.$1; process.chdir(base); - const name = require.resolve(module.value); + name = require.resolve(name); if (name === module.value) throw `unable to find "${name}" via file://${file}\n`; addChunk(module, name); From 43ce68c84f3f80263770962553f1a63cbcc11898 Mon Sep 17 00:00:00 2001 From: Andrea Giammarchi Date: Sat, 28 Oct 2017 19:43:00 -0300 Subject: [PATCH 12/44] 1.1.3 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index c4461b1..5249637 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "asbundle", - "version": "1.1.2", + "version": "1.1.3", "description": "A minimalistic CommonJS bundler", "bin": "bin.js", "main": "index.js", From 5d2bd3964512efdf9232e6242e0f970d36798846 Mon Sep 17 00:00:00 2001 From: "greenkeeper[bot]" Date: Mon, 30 Oct 2017 11:50:17 +0000 Subject: [PATCH 13/44] fix(package): update ascjs to version 1.0.3 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 5249637..ba9649f 100644 --- a/package.json +++ b/package.json @@ -30,7 +30,7 @@ }, "homepage": "https://github.com/WebReflection/asbundle#readme", "dependencies": { - "ascjs": "1.0.2", + "ascjs": "1.0.3", "esprima": "^4.0.0" } } From 51a3a41575cfc28a0903738bb6a4a6aaeb161470 Mon Sep 17 00:00:00 2001 From: Andrea Giammarchi Date: Mon, 30 Oct 2017 08:52:18 -0300 Subject: [PATCH 14/44] included ascjs with CDN to local module resolution --- index.js | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/index.js b/index.js index 51f29c0..7cd81a5 100644 --- a/index.js +++ b/index.js @@ -79,10 +79,8 @@ const parse = (options, base, file, cache, modules) => { } } } else { - let name = module.value; - if (/^https?:\/\/(?:unpkg\.com|registry\.npmjs\.org)\/([^@/]+)/.test(name)) name = RegExp.$1; process.chdir(base); - name = require.resolve(name); + const name = require.resolve(name); if (name === module.value) throw `unable to find "${name}" via file://${file}\n`; addChunk(module, name); From 073655822ffff099bcba405ce11fe3a1b7757f81 Mon Sep 17 00:00:00 2001 From: Andrea Giammarchi Date: Mon, 30 Oct 2017 08:52:24 -0300 Subject: [PATCH 15/44] 1.1.4 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index ba9649f..f7f1a7d 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "asbundle", - "version": "1.1.3", + "version": "1.1.4", "description": "A minimalistic CommonJS bundler", "bin": "bin.js", "main": "index.js", From b85222962d55c9f76e170ab7397cb79ed8cd05ef Mon Sep 17 00:00:00 2001 From: "greenkeeper[bot]" Date: Mon, 30 Oct 2017 19:47:14 +0000 Subject: [PATCH 16/44] fix(package): update ascjs to version 2.0.0 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index f7f1a7d..33ee2ad 100644 --- a/package.json +++ b/package.json @@ -30,7 +30,7 @@ }, "homepage": "https://github.com/WebReflection/asbundle#readme", "dependencies": { - "ascjs": "1.0.3", + "ascjs": "2.0.0", "esprima": "^4.0.0" } } From f83d1d6cc0cb35eb0108ab752768c4b1b196d49b Mon Sep 17 00:00:00 2001 From: Andrea Giammarchi Date: Mon, 30 Oct 2017 17:27:53 -0300 Subject: [PATCH 17/44] Switching to a more relaxed babylon ASTree parser * compatible with stage 0 proposals too * more suitable for generic projects --- README.md | 2 +- index.js | 85 ++++++++++++++++++++++++++++++++++------------------ package.json | 2 +- 3 files changed, 58 insertions(+), 31 deletions(-) diff --git a/README.md b/README.md index 898be3f..dd30368 100644 --- a/README.md +++ b/README.md @@ -52,7 +52,7 @@ asbundle(sourceFileName); ### Features - * extremely lightweight, based on [esprima](http://esprima.org) for performance and reliability + * extremely lightweight, based on [babylon](https://github.com/babel/babylon) for performance and reliability * it uses _ascjs_ to automatically transform, when needed, ES2015+ modules into CommonJS code * understands both relative files and installed packages too (based on `require.resolve(...)`) * reproduces a modern and minimalistic CommonJS environments ideal for browsers diff --git a/index.js b/index.js index 7cd81a5..17d52f8 100644 --- a/index.js +++ b/index.js @@ -1,10 +1,33 @@ const ascjs = require('ascjs'); -const esprima = require('esprima'); +const parser = require('babylon'); const defaultOptions = { sourceType: 'module', - jsx: true, - range: true, - tolerant: true + plugins: [ + 'estree', + 'jsx', + 'flow', + 'typescript', + 'doExpressions', + 'objectRestSpread', + 'decorators', + 'decorators2', + 'classProperties', + 'classPrivateProperties', + 'classPrivateMethods', + 'exportExtensions', + 'asyncGenerators', + 'functionBind', + 'functionSent', + 'dynamicImport', + 'numericSeparator', + 'optionalChaining', + 'importMeta', + 'bigInt', + 'optionalCatchBinding', + 'throwExpressions', + 'pipelineOperator', + 'nullishCoalescingOperator' + ] }; const fs = require('fs'); @@ -53,41 +76,45 @@ const parse = (options, base, file, cache, modules) => { const addChunk = (module, name) => { const i = cache.indexOf(name); chunks.push({ - start: module.range[0], - end: module.range[1], + start: module.start, + end: module.end, value: i < 0 ? (cache.push(name) - 1) : i }); if (i < 0) { modules[cache.length - 1] = parse(options, path.dirname(name), name, cache, modules); } }; - esprima.parse( - code, - options, - item => { - if (item.type === 'CallExpression' && item.callee.name === 'require') { - const module = item.arguments[0]; - if (/^[./]/.test(module.value)) { - const name = require.resolve(path.resolve(base, module.value)); - if (/\.m?js$/.test(name)) addChunk(module, name); - else { - const i = cache.indexOf(name); - if (i < 0) cache.push(name); - addChunk(module, name); - if (i < 0) { - modules[cache.length - 1] = `module.exports = ${JSON.stringify(require(name))};`; - } - } - } else { - process.chdir(base); - const name = require.resolve(name); - if (name === module.value) - throw `unable to find "${name}" via file://${file}\n`; + const findRequire = item => { + if (item.type === 'CallExpression' && item.callee.name === 'require') { + const module = item.arguments[0]; + if (/^[./]/.test(module.value)) { + const name = require.resolve(path.resolve(base, module.value)); + if (/\.m?js$/.test(name)) addChunk(module, name); + else { + const i = cache.indexOf(name); + if (i < 0) cache.push(name); addChunk(module, name); + if (i < 0) { + modules[cache.length - 1] = `module.exports = ${JSON.stringify(require(name))};`; + } + } + } else { + process.chdir(base); + const name = require.resolve(name); + if (name === module.value) + throw `unable to find "${name}" via file://${file}\n`; + addChunk(module, name); + } + } else { + for (let key in item) { + if (typeof item[key] === 'object') { + findRequire(item[key] || {}); } } } - ); + }; + const parsed = parser.parse(code, options); + parsed.program.body.forEach(findRequire); const length = chunks.length; let c = 0; for (let i = 0; i < length; i++) { diff --git a/package.json b/package.json index 33ee2ad..8d01438 100644 --- a/package.json +++ b/package.json @@ -31,6 +31,6 @@ "homepage": "https://github.com/WebReflection/asbundle#readme", "dependencies": { "ascjs": "2.0.0", - "esprima": "^4.0.0" + "babylon": "^6.18.0" } } From 879633bfdc560bdadc6c346e5b933dd096c29af2 Mon Sep 17 00:00:00 2001 From: Andrea Giammarchi Date: Mon, 30 Oct 2017 17:28:50 -0300 Subject: [PATCH 18/44] 2.0.0 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 8d01438..63d93e6 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "asbundle", - "version": "1.1.4", + "version": "2.0.0", "description": "A minimalistic CommonJS bundler", "bin": "bin.js", "main": "index.js", From 396c71a4fff2bd57c080ca7b93460dfe5c4025de Mon Sep 17 00:00:00 2001 From: "greenkeeper[bot]" Date: Mon, 30 Oct 2017 20:30:01 +0000 Subject: [PATCH 19/44] fix(package): update ascjs to version 2.0.1 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 63d93e6..e5b56a8 100644 --- a/package.json +++ b/package.json @@ -30,7 +30,7 @@ }, "homepage": "https://github.com/WebReflection/asbundle#readme", "dependencies": { - "ascjs": "2.0.0", + "ascjs": "2.0.1", "babylon": "^6.18.0" } } From 6abe9cd985dc3dfd6ff21e50e875aeb2ea2155a8 Mon Sep 17 00:00:00 2001 From: Andrea Giammarchi Date: Mon, 30 Oct 2017 17:31:07 -0300 Subject: [PATCH 20/44] 2.0.1 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index e5b56a8..5aa105c 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "asbundle", - "version": "2.0.0", + "version": "2.0.1", "description": "A minimalistic CommonJS bundler", "bin": "bin.js", "main": "index.js", From 92408e3785a6e881c1198f03b15d3e1bfa8e6089 Mon Sep 17 00:00:00 2001 From: Andrea Giammarchi Date: Tue, 31 Oct 2017 14:14:08 -0300 Subject: [PATCH 21/44] fixed typo in code --- index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.js b/index.js index 17d52f8..d20aef2 100644 --- a/index.js +++ b/index.js @@ -100,7 +100,7 @@ const parse = (options, base, file, cache, modules) => { } } else { process.chdir(base); - const name = require.resolve(name); + const name = require.resolve(module.value); if (name === module.value) throw `unable to find "${name}" via file://${file}\n`; addChunk(module, name); From 7724f6ab9377d95212b52b0baf7c24cabc859519 Mon Sep 17 00:00:00 2001 From: Andrea Giammarchi Date: Tue, 31 Oct 2017 14:14:11 -0300 Subject: [PATCH 22/44] 2.0.2 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 5aa105c..e0df06c 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "asbundle", - "version": "2.0.1", + "version": "2.0.2", "description": "A minimalistic CommonJS bundler", "bin": "bin.js", "main": "index.js", From 61fb9e896e1f67660b9016e01fdd90dc026c80b9 Mon Sep 17 00:00:00 2001 From: Andrea Giammarchi Date: Tue, 31 Oct 2017 14:58:49 -0300 Subject: [PATCH 23/44] bumped ascjs --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index e0df06c..249af93 100644 --- a/package.json +++ b/package.json @@ -30,7 +30,7 @@ }, "homepage": "https://github.com/WebReflection/asbundle#readme", "dependencies": { - "ascjs": "2.0.1", + "ascjs": "^2.0.1", "babylon": "^6.18.0" } } From 1810197388fc220bfea3a00aef0302ce0a0a213d Mon Sep 17 00:00:00 2001 From: Andrea Giammarchi Date: Tue, 31 Oct 2017 14:58:55 -0300 Subject: [PATCH 24/44] 2.1.0 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 249af93..5bcab0e 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "asbundle", - "version": "2.0.2", + "version": "2.1.0", "description": "A minimalistic CommonJS bundler", "bin": "bin.js", "main": "index.js", From 31af89f01a5a49b873a043be2ca195e786789c4e Mon Sep 17 00:00:00 2001 From: Andrea Giammarchi Date: Tue, 31 Oct 2017 15:56:08 -0300 Subject: [PATCH 25/44] re-bumped ascjs --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 5bcab0e..228469a 100644 --- a/package.json +++ b/package.json @@ -30,7 +30,7 @@ }, "homepage": "https://github.com/WebReflection/asbundle#readme", "dependencies": { - "ascjs": "^2.0.1", + "ascjs": "^2.2.0", "babylon": "^6.18.0" } } From 5996b6e50724ee2f01f874e9065201353b144dcb Mon Sep 17 00:00:00 2001 From: Andrea Giammarchi Date: Tue, 31 Oct 2017 15:56:10 -0300 Subject: [PATCH 26/44] 2.2.0 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 228469a..9b4d93d 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "asbundle", - "version": "2.1.0", + "version": "2.2.0", "description": "A minimalistic CommonJS bundler", "bin": "bin.js", "main": "index.js", From d2a724c01f7620438908168a77cfb5c83b91ee1e Mon Sep 17 00:00:00 2001 From: Andrea Giammarchi Date: Tue, 7 Nov 2017 11:08:18 -0300 Subject: [PATCH 27/44] Compact output avoiding repeated import/export churn. Using latest asbundle ability to use custom import/export so that there's no arrow function or Object.defineProperty repetition across every single module. --- README.md | 2 +- index.js | 23 ++++++++++++++++++++--- package.json | 2 +- test/out.js | 11 ++++++----- 4 files changed, 28 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index dd30368..78a1fd5 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@ [![donate](https://img.shields.io/badge/$-donate-ff69b4.svg?maxAge=2592000&style=flat)](https://github.com/WebReflection/donate) [![License: ISC](https://img.shields.io/badge/License-ISC-yellow.svg)](https://opensource.org/licenses/ISC) [![Build Status](https://travis-ci.org/WebReflection/asbundle.svg?branch=master)](https://travis-ci.org/WebReflection/asbundle) [![Greenkeeper badge](https://badges.greenkeeper.io/WebReflection/asbundle.svg)](https://greenkeeper.io/) -A minimalistic CommonJS bundler +A minimalistic CommonJS bundler. - - - diff --git a/index.js b/index.js index d20aef2..8e86b19 100644 --- a/index.js +++ b/index.js @@ -32,7 +32,8 @@ const defaultOptions = { const fs = require('fs'); const path = require('path'); - +const IMPORT = 'require.I'; +const EXPORT = 'require.E(exports)'; const bundle = (main, options) => { let base = path.dirname(main); main = path.resolve(base, main); @@ -50,7 +51,7 @@ const bundle = (main, options) => { cache, modules ); - return ` + let output = ` (function (cache, modules) { function require(i) { return cache[i] || get(i); } function get(i) { @@ -66,13 +67,29 @@ ${code} } `.trim()).join(',')}])); `.trim(); + if (output.includes('require.E(exports)')) { + output = output.replace( + 'var main =', + 'require.E = exports => Object.defineProperty(exports, \'__esModule\', {value: true});\n var main =' + ); + } + if (output.includes(IMPORT)) { + output = output.replace( + 'var main =', + IMPORT + ' = m => m.__esModule ? m.default : m;\n var main =' + ).replace( + 'var main = require(0);\n return main.__esModule ? main.default : main;', + 'return require.I(require(0));' + ); + } + return output; }; const parse = (options, base, file, cache, modules) => { const out = []; const chunks = []; let code = fs.readFileSync(file).toString(); - if (/^(?:import|export)\s+/m.test(code)) code = ascjs(code); + if (/^(?:import|export)\s+/m.test(code)) code = ascjs(code, {IMPORT, EXPORT}); const addChunk = (module, name) => { const i = cache.indexOf(name); chunks.push({ diff --git a/package.json b/package.json index 9b4d93d..d010f91 100644 --- a/package.json +++ b/package.json @@ -30,7 +30,7 @@ }, "homepage": "https://github.com/WebReflection/asbundle#readme", "dependencies": { - "ascjs": "^2.2.0", + "ascjs": "^2.3.0", "babylon": "^6.18.0" } } diff --git a/test/out.js b/test/out.js index d1236bd..8d45cfb 100644 --- a/test/out.js +++ b/test/out.js @@ -5,18 +5,19 @@ modules[i].call(exports, window, require, module, exports); return (cache[i] = module.exports); } - var main = require(0); - return main.__esModule ? main.default : main; + require.E = exports => Object.defineProperty(exports, '__esModule', {value: true}); + require.I = m => m.__esModule ? m.default : m; + return require.I(require(0)); }([],[function (global, require, module, exports) { // main.js 'use strict'; -const func = (m => m.__esModule ? m.default : m)(require(1)); +const func = require.I(require(1)); const {a, b} = require(1); const val = 123; function test() { console.log('asbundle'); } -Object.defineProperty(exports, '__esModule', {value: true}).default = test; +require.E(exports).default = test; exports.func = func; exports.val = val; },function (global, require, module, exports) { @@ -25,7 +26,7 @@ exports.val = val; const a = 1, b = 2; exports.a = a; exports.b = b; -Object.defineProperty(exports, '__esModule', {value: true}).default = function () { +require.E(exports).default = function () { console.log('module'); }; }])); \ No newline at end of file From c4aad4ce8049ecac73d95a25048610f5c662ab37 Mon Sep 17 00:00:00 2001 From: Andrea Giammarchi Date: Tue, 7 Nov 2017 11:09:16 -0300 Subject: [PATCH 28/44] 2.3.0 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index d010f91..3b53473 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "asbundle", - "version": "2.2.0", + "version": "2.3.0", "description": "A minimalistic CommonJS bundler", "bin": "bin.js", "main": "index.js", From 7f469d6929e781754ff39a0b0e64825a512546ca Mon Sep 17 00:00:00 2001 From: Andrea Giammarchi Date: Tue, 7 Nov 2017 16:36:21 -0300 Subject: [PATCH 29/44] updated ascjs --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 3b53473..891c1a8 100644 --- a/package.json +++ b/package.json @@ -30,7 +30,7 @@ }, "homepage": "https://github.com/WebReflection/asbundle#readme", "dependencies": { - "ascjs": "^2.3.0", + "ascjs": "^2.4.0", "babylon": "^6.18.0" } } From 16f6f88d21952a2f98e18c7c41d16bab6ad39988 Mon Sep 17 00:00:00 2001 From: Andrea Giammarchi Date: Tue, 7 Nov 2017 16:36:25 -0300 Subject: [PATCH 30/44] 2.4.0 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 891c1a8..da6fa80 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "asbundle", - "version": "2.3.0", + "version": "2.4.0", "description": "A minimalistic CommonJS bundler", "bin": "bin.js", "main": "index.js", From 62028279048cd54579849e7dd789c9054b57260d Mon Sep 17 00:00:00 2001 From: Andrea Giammarchi Date: Mon, 13 Nov 2017 12:44:48 -0300 Subject: [PATCH 31/44] added support for CGJS --- bin.js | 5 +++-- index.js | 14 ++++++++++---- test/cgjs.js | 4 ++++ 3 files changed, 17 insertions(+), 6 deletions(-) create mode 100644 test/cgjs.js diff --git a/bin.js b/bin.js index 16ffd44..1ee019a 100755 --- a/bin.js +++ b/bin.js @@ -1,8 +1,9 @@ #!/usr/bin/env node const asbundle = require('./index.js'); +const argv = process.argv.filter(arg => !/^-/.test(arg)); -let source = process.argv[2]; +let source = argv[2]; if (!source) { const info = require('./package.json'); console.log(` @@ -21,7 +22,7 @@ ${' '.repeat(info.description.length) } else { const fs = require('fs'); const path = require('path'); - let dest = process.argv[3]; + let dest = argv.filter(arg => !/^-/.test(arg))[3]; source = path.resolve(process.cwd(), source); fs.stat(source, (err, stat) => { if (err) throw err; diff --git a/index.js b/index.js index 8e86b19..b51f420 100644 --- a/index.js +++ b/index.js @@ -32,6 +32,7 @@ const defaultOptions = { const fs = require('fs'); const path = require('path'); +const CGJS = process.argv.some(arg => arg === '--cgjs'); const IMPORT = 'require.I'; const EXPORT = 'require.E(exports)'; const bundle = (main, options) => { @@ -67,6 +68,12 @@ ${code} } `.trim()).join(',')}])); `.trim(); + if (CGJS) { + output = output.replace( + 'cache[i] || get(i)', + 'typeof i === "string" ? window.require(i) : (cache[i] || get(i))' + ); + } if (output.includes('require.E(exports)')) { output = output.replace( 'var main =', @@ -115,12 +122,11 @@ const parse = (options, base, file, cache, modules) => { modules[cache.length - 1] = `module.exports = ${JSON.stringify(require(name))};`; } } - } else { + } else if (!/^[A-Z]/.test(module.value)) { process.chdir(base); const name = require.resolve(module.value); - if (name === module.value) - throw `unable to find "${name}" via file://${file}\n`; - addChunk(module, name); + if (name !== module.value) addChunk(module, name); + else if (!CGJS) throw `unable to find "${name}" via file://${file}\n`; } } else { for (let key in item) { diff --git a/test/cgjs.js b/test/cgjs.js new file mode 100644 index 0000000..7c61c57 --- /dev/null +++ b/test/cgjs.js @@ -0,0 +1,4 @@ +const Gtk = require('Gtk'); +const util = require('util'); + +console.log(util.inspect(Gtk.init)); \ No newline at end of file From 195baaa08a02de900083f96ff9561424d8d452af Mon Sep 17 00:00:00 2001 From: Andrea Giammarchi Date: Thu, 15 Mar 2018 23:07:11 +0100 Subject: [PATCH 32/44] using ES5 syntax all over --- index.js | 4 ++-- test/out.js | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/index.js b/index.js index b51f420..b32e33a 100644 --- a/index.js +++ b/index.js @@ -77,13 +77,13 @@ ${code} if (output.includes('require.E(exports)')) { output = output.replace( 'var main =', - 'require.E = exports => Object.defineProperty(exports, \'__esModule\', {value: true});\n var main =' + 'require.E = function (exports) { return Object.defineProperty(exports, \'__esModule\', {value: true}); };\n var main =' ); } if (output.includes(IMPORT)) { output = output.replace( 'var main =', - IMPORT + ' = m => m.__esModule ? m.default : m;\n var main =' + IMPORT + ' = function (m) { return m.__esModule ? m.default : m; };\n var main =' ).replace( 'var main = require(0);\n return main.__esModule ? main.default : main;', 'return require.I(require(0));' diff --git a/test/out.js b/test/out.js index 8d45cfb..c4a2ecb 100644 --- a/test/out.js +++ b/test/out.js @@ -5,8 +5,8 @@ modules[i].call(exports, window, require, module, exports); return (cache[i] = module.exports); } - require.E = exports => Object.defineProperty(exports, '__esModule', {value: true}); - require.I = m => m.__esModule ? m.default : m; + require.E = function (exports) { return Object.defineProperty(exports, '__esModule', {value: true}); }; + require.I = function (m) { return m.__esModule ? m.default : m; }; return require.I(require(0)); }([],[function (global, require, module, exports) { // main.js From 5695d5b188f5634a1ffcf3125dd8c82efcd3d9fc Mon Sep 17 00:00:00 2001 From: Andrea Giammarchi Date: Thu, 15 Mar 2018 23:07:18 +0100 Subject: [PATCH 33/44] 2.5.0 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index da6fa80..845675f 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "asbundle", - "version": "2.4.0", + "version": "2.5.0", "description": "A minimalistic CommonJS bundler", "bin": "bin.js", "main": "index.js", From e4e3d8a144591b0d582496f3d85626e60b750930 Mon Sep 17 00:00:00 2001 From: Andrea Giammarchi Date: Thu, 12 Apr 2018 12:51:52 +0200 Subject: [PATCH 34/44] added --ignore flag --- index.js | 44 ++++++++++++++++++++++++++++++-------------- package.json | 2 +- test/main.js | 4 +++- test/out.js | 2 ++ 4 files changed, 36 insertions(+), 16 deletions(-) diff --git a/index.js b/index.js index b32e33a..b37de93 100644 --- a/index.js +++ b/index.js @@ -33,6 +33,9 @@ const defaultOptions = { const fs = require('fs'); const path = require('path'); const CGJS = process.argv.some(arg => arg === '--cgjs'); +const IGNORE = process.argv.filter(arg => /^--ignore=/.test(arg)) + .map(arr => arr.slice(9).split(','))[0] || []; + const IMPORT = 'require.I'; const EXPORT = 'require.E(exports)'; const bundle = (main, options) => { @@ -111,22 +114,35 @@ const parse = (options, base, file, cache, modules) => { const findRequire = item => { if (item.type === 'CallExpression' && item.callee.name === 'require') { const module = item.arguments[0]; - if (/^[./]/.test(module.value)) { - const name = require.resolve(path.resolve(base, module.value)); - if (/\.m?js$/.test(name)) addChunk(module, name); - else { - const i = cache.indexOf(name); - if (i < 0) cache.push(name); - addChunk(module, name); - if (i < 0) { - modules[cache.length - 1] = `module.exports = ${JSON.stringify(require(name))};`; + if (IGNORE.indexOf(module.value) < 0) { + if (/^[./]/.test(module.value)) { + const name = require.resolve(path.resolve(base, module.value)); + if (/\.m?js$/.test(name)) addChunk(module, name); + else { + const i = cache.indexOf(name); + if (i < 0) cache.push(name); + addChunk(module, name); + if (i < 0) { + modules[cache.length - 1] = `module.exports = ${JSON.stringify(require(name))};`; + } } + } else if (!/^[A-Z]/.test(module.value)) { + process.chdir(base); + const name = require.resolve(module.value); + if (name !== module.value) addChunk(module, name); + else if (!CGJS) throw `unable to find "${name}" via file://${file}\n`; } - } else if (!/^[A-Z]/.test(module.value)) { - process.chdir(base); - const name = require.resolve(module.value); - if (name !== module.value) addChunk(module, name); - else if (!CGJS) throw `unable to find "${name}" via file://${file}\n`; + } else { + chunks.push({ + start: item.callee.start, + end: item.callee.end, + value: '' + }, + { + start: module.start, + end: module.end, + value: 'null' + }); } } else { for (let key in item) { diff --git a/package.json b/package.json index 845675f..730525f 100644 --- a/package.json +++ b/package.json @@ -5,7 +5,7 @@ "bin": "bin.js", "main": "index.js", "scripts": { - "test": "if [ \"$(node bin.js test/main.js)\" != \"$(cat test/out.js)\" ]; then exit 1; fi" + "test": "if [ \"$(node bin.js test/main.js --ignore=m1,m2)\" != \"$(cat test/out.js)\" ]; then exit 1; fi" }, "repository": { "type": "git", diff --git a/test/main.js b/test/main.js index 9023225..31a4593 100644 --- a/test/main.js +++ b/test/main.js @@ -3,4 +3,6 @@ const val = 123; export default function test() { console.log('asbundle'); }; -export {func, val}; \ No newline at end of file +export {func, val}; +require('m1'); +require('m2'); \ No newline at end of file diff --git a/test/out.js b/test/out.js index c4a2ecb..fbbbc4e 100644 --- a/test/out.js +++ b/test/out.js @@ -20,6 +20,8 @@ function test() { require.E(exports).default = test; exports.func = func; exports.val = val; +(null); +(null); },function (global, require, module, exports) { // module.js 'use strict'; From 4a2f28f736d028316f1d0309cea60fa68a37c9f8 Mon Sep 17 00:00:00 2001 From: Andrea Giammarchi Date: Thu, 12 Apr 2018 12:52:03 +0200 Subject: [PATCH 35/44] 2.6.0 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 730525f..2d963f7 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "asbundle", - "version": "2.5.0", + "version": "2.6.0", "description": "A minimalistic CommonJS bundler", "bin": "bin.js", "main": "index.js", From cfc129d5f1cb94ee007fbb9811dcb38d20b9664b Mon Sep 17 00:00:00 2001 From: "greenkeeper[bot]" Date: Thu, 17 May 2018 14:35:43 +0000 Subject: [PATCH 36/44] Update to node 10 in .travis.yml --- .travis.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.travis.yml b/.travis.yml index a3986c5..8c12d19 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,6 +1,7 @@ language: node_js node_js: - 8 + - 10 git: depth: 1 branches: From 9c2ec0063ea07027a62fadba01a589bc2e04e131 Mon Sep 17 00:00:00 2001 From: "greenkeeper[bot]" Date: Fri, 11 Jan 2019 20:07:25 +0000 Subject: [PATCH 37/44] fix(package): update ascjs to version 3.0.0 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 2d963f7..1bdb15b 100644 --- a/package.json +++ b/package.json @@ -30,7 +30,7 @@ }, "homepage": "https://github.com/WebReflection/asbundle#readme", "dependencies": { - "ascjs": "^2.4.0", + "ascjs": "^3.0.0", "babylon": "^6.18.0" } } From 0a39642f7714f2f0fbc2be380136866033d723a5 Mon Sep 17 00:00:00 2001 From: Jonatan Nilsson Date: Sun, 5 Apr 2020 07:08:16 +0000 Subject: [PATCH 38/44] Fix bug in bin where process.cwd would change during processing --- bin.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/bin.js b/bin.js index 1ee019a..b8dd29e 100755 --- a/bin.js +++ b/bin.js @@ -24,12 +24,14 @@ ${' '.repeat(info.description.length) const path = require('path'); let dest = argv.filter(arg => !/^-/.test(arg))[3]; source = path.resolve(process.cwd(), source); + if (dest) { + dest = path.resolve(process.cwd(), dest) + } fs.stat(source, (err, stat) => { if (err) throw err; if (!stat.isFile()) throw `unknown file ${source}`; const result = asbundle(source); if (dest) { - dest = path.resolve(process.cwd(), dest); fs.writeFileSync(dest, result); } else process.stdout.write(result); From 7d3d86ac0bfc5ff1053dd2a84f8f36eba9e5871c Mon Sep 17 00:00:00 2001 From: Jonatan Nilsson Date: Sun, 5 Apr 2020 07:08:37 +0000 Subject: [PATCH 39/44] Update error description if require fails to find file. --- index.js | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index b37de93..ef383dc 100644 --- a/index.js +++ b/index.js @@ -116,7 +116,13 @@ const parse = (options, base, file, cache, modules) => { const module = item.arguments[0]; if (IGNORE.indexOf(module.value) < 0) { if (/^[./]/.test(module.value)) { - const name = require.resolve(path.resolve(base, module.value)); + let name + try { + name = require.resolve(path.resolve(base, module.value)); + } catch (e) { + process.stderr.write('Error resolving ' + module.value + ' from ' + base + ': ' + e.message) + throw e + } if (/\.m?js$/.test(name)) addChunk(module, name); else { const i = cache.indexOf(name); From 8b22d66ae7503c921912e7618c306347e1ba6891 Mon Sep 17 00:00:00 2001 From: Jonatan Nilsson Date: Sun, 5 Apr 2020 07:10:31 +0000 Subject: [PATCH 40/44] Update version --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 1bdb15b..26b6df1 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "asbundle", - "version": "2.6.0", + "version": "2.6.1", "description": "A minimalistic CommonJS bundler", "bin": "bin.js", "main": "index.js", From 4db34de19cb08f7451ee1b1dfffb32f8928ec538 Mon Sep 17 00:00:00 2001 From: Andrea Giammarchi Date: Sun, 5 Apr 2020 12:10:22 +0200 Subject: [PATCH 41/44] updated dependencies --- package.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index 26b6df1..e690e47 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "asbundle", - "version": "2.6.1", + "version": "2.6.0", "description": "A minimalistic CommonJS bundler", "bin": "bin.js", "main": "index.js", @@ -30,7 +30,7 @@ }, "homepage": "https://github.com/WebReflection/asbundle#readme", "dependencies": { - "ascjs": "^3.0.0", + "ascjs": "^3.1.2", "babylon": "^6.18.0" } } From a549cadfbe56a7aebfd9ec8949acbf2c4a7b9070 Mon Sep 17 00:00:00 2001 From: Andrea Giammarchi Date: Sun, 5 Apr 2020 12:10:26 +0200 Subject: [PATCH 42/44] 2.6.1 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index e690e47..5d7530b 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "asbundle", - "version": "2.6.0", + "version": "2.6.1", "description": "A minimalistic CommonJS bundler", "bin": "bin.js", "main": "index.js", From e20f012e5911c551f894d5580a352940b37210b9 Mon Sep 17 00:00:00 2001 From: Andrea Giammarchi Date: Thu, 10 Sep 2020 08:25:23 +0200 Subject: [PATCH 43/44] updated dependencies --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 5d7530b..0751158 100644 --- a/package.json +++ b/package.json @@ -30,7 +30,7 @@ }, "homepage": "https://github.com/WebReflection/asbundle#readme", "dependencies": { - "ascjs": "^3.1.2", + "ascjs": "^4.0.1", "babylon": "^6.18.0" } } From a17d9bdcd4a78a730ad67acbe3d96d66a752f76b Mon Sep 17 00:00:00 2001 From: Andrea Giammarchi Date: Thu, 10 Sep 2020 08:25:26 +0200 Subject: [PATCH 44/44] 2.7.0 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 0751158..8cae6d4 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "asbundle", - "version": "2.6.1", + "version": "2.7.0", "description": "A minimalistic CommonJS bundler", "bin": "bin.js", "main": "index.js",