app-bundler 0.2.15 → 0.2.17

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/lib/bundler.js +206 -88
  2. package/lib/cli.js +206 -88
  3. package/package.json +3 -2
package/lib/bundler.js CHANGED
@@ -1310,7 +1310,8 @@ const global_1 = _src_global;
1310
1310
  const atma_io_1 = require("atma-io");
1311
1311
  var Npm;
1312
1312
  (function (Npm) {
1313
- function resolveAppUrl(path, currentLocation, base) {
1313
+ function resolveAppUrl(path, currentLocation, opts) {
1314
+ let base = opts.base;
1314
1315
  let name = /^(@?[^\\\/]+)/.exec(path)[0];
1315
1316
  let resource = path.substring(name.length + 1);
1316
1317
  while (path != null) {
@@ -1329,14 +1330,9 @@ var Npm;
1329
1330
  dirname = (0, path_1.path_combine)(dirname, `/${subPackage}`, '/');
1330
1331
  resource = resource.substring(subPackage.length + 1);
1331
1332
  }
1332
- if (resource === '') {
1333
- let pkg = (0, path_1.path_combine)(dirname, 'package.json');
1334
- let json = global_1.io.File.read(pkg);
1335
- if (json) {
1336
- resource = json.main;
1337
- }
1338
- }
1339
- let filename = combineMain(dirname, resource);
1333
+ let pkg = (0, path_1.path_combine)(dirname, 'package.json');
1334
+ let json = global_1.io.File.read(pkg);
1335
+ let filename = resolveAppUrlFromPackageJson(dirname, json, resource, opts);
1340
1336
  if (base) {
1341
1337
  if (base.endsWith('/') === false) {
1342
1338
  // Base path must be a folder
@@ -1353,6 +1349,89 @@ var Npm;
1353
1349
  return null;
1354
1350
  }
1355
1351
  Npm.resolveAppUrl = resolveAppUrl;
1352
+ function resolveAppUrlFromPackageJson(pkgDir, pkg, subPath, opts) {
1353
+ var _a;
1354
+ if (pkg.main == null && pkg.exports == null) {
1355
+ return combineMain(pkgDir, subPath);
1356
+ }
1357
+ if (pkg.exports == null) {
1358
+ return combineMain(pkgDir, subPath || pkg.main);
1359
+ }
1360
+ if (typeof pkg.exports === 'string') {
1361
+ return combineMain(pkgDir, subPath || pkg.exports);
1362
+ }
1363
+ const moduleExportsMap = 'require';
1364
+ const conditionExportsMap = (_a = opts.package.platform) !== null && _a !== void 0 ? _a : 'node';
1365
+ let libraryFilePath = fromExports(pkg.exports, subPath, {
1366
+ moduleExports: moduleExportsMap,
1367
+ conditionExports: conditionExportsMap,
1368
+ pkgDir,
1369
+ });
1370
+ return combineMain(pkgDir, libraryFilePath);
1371
+ }
1372
+ Npm.resolveAppUrlFromPackageJson = resolveAppUrlFromPackageJson;
1373
+ function fromExports(exports, subPath, opts) {
1374
+ var _a;
1375
+ if (exports == null) {
1376
+ throw new Error(`Invalid exports for ${subPath}`);
1377
+ }
1378
+ if (typeof exports === 'object') {
1379
+ exports = extractConditional(exports, opts);
1380
+ }
1381
+ if (typeof exports === 'object') {
1382
+ // TExportsMap
1383
+ let map = exports;
1384
+ if (!subPath) {
1385
+ let dot = '.';
1386
+ if (map[dot] == null) {
1387
+ throw new Error(`Missing default "." export in exports for ${JSON.stringify(exports)}`);
1388
+ }
1389
+ return extractConditional(map[dot], opts);
1390
+ }
1391
+ for (let tpl in exports) {
1392
+ let mapped = extractConditional(exports[tpl], opts);
1393
+ if (typeof mapped !== 'string') {
1394
+ throw new Error(`Invalid conditional exports for ${JSON.stringify(exports)} in ${(_a = opts.pkgDir) !== null && _a !== void 0 ? _a : subPath}`);
1395
+ }
1396
+ if (tpl.startsWith('./')) {
1397
+ tpl = tpl.substring(2);
1398
+ if (tpl === '*') {
1399
+ return mapped.replace('*', subPath);
1400
+ }
1401
+ let globIndex = tpl.indexOf('*');
1402
+ if (globIndex === -1 && tpl === subPath) {
1403
+ return mapped;
1404
+ }
1405
+ if (globIndex > -1) {
1406
+ let tplPfx = tpl.substring(0, globIndex).replace(/\/+$/, '');
1407
+ if (subPath.startsWith(tplPfx)) {
1408
+ let subPathSfx = subPath.replace(tplPfx, '');
1409
+ return mapped.replace('*', subPathSfx);
1410
+ }
1411
+ }
1412
+ }
1413
+ }
1414
+ }
1415
+ if (typeof exports === 'string') {
1416
+ return exports;
1417
+ }
1418
+ throw new Error(`Path can't be mapped ${JSON.stringify(exports)}`);
1419
+ }
1420
+ function extractConditional(exports, opts) {
1421
+ if (exports == null || typeof exports !== 'object') {
1422
+ return exports;
1423
+ }
1424
+ if (opts.conditionExports in exports) {
1425
+ return extractConditional(exports[opts.conditionExports], opts);
1426
+ }
1427
+ if (opts.moduleExports in exports) {
1428
+ return extractConditional(exports[opts.moduleExports], opts);
1429
+ }
1430
+ if ('default' in exports) {
1431
+ return extractConditional(exports['default'], opts);
1432
+ }
1433
+ return exports;
1434
+ }
1356
1435
  function combineMain(directory, filename) {
1357
1436
  filename !== null && filename !== void 0 ? filename : (filename = 'index.js');
1358
1437
  let path = (0, path_1.path_combine)(directory, filename);
@@ -1515,7 +1594,7 @@ class Resource {
1515
1594
  }
1516
1595
  if (Include_1.Include.PathResolver.isNpm(includeData.url)) {
1517
1596
  this.aliases.push(includeData.url);
1518
- let byNpmResolver = npm_1.Npm.resolveAppUrl(includeData.url, parent && parent.location, solution.opts.base);
1597
+ let byNpmResolver = npm_1.Npm.resolveAppUrl(includeData.url, parent && parent.location, solution.opts);
1519
1598
  if (byNpmResolver != null) {
1520
1599
  // otherwise "url" could be previously resolved with pathResolver.resolve
1521
1600
  url = byNpmResolver;
@@ -1525,6 +1604,15 @@ class Resource {
1525
1604
  url = includeData.url;
1526
1605
  }
1527
1606
  }
1607
+ if (includeData.route != null) {
1608
+ let { path, alias } = includeData.route;
1609
+ if (path && Include_1.Include.PathResolver.isNpm(path) && this.aliases.includes(path) === false) {
1610
+ this.aliases.push(path);
1611
+ }
1612
+ if (alias && Include_1.Include.PathResolver.isNpm(alias) && this.aliases.includes(alias) === false) {
1613
+ this.aliases.push(alias);
1614
+ }
1615
+ }
1528
1616
  let mappedUrl = this.solution.mapUrl(url);
1529
1617
  if (mappedUrl != url) {
1530
1618
  console.log(`URL mapped: ${url} => ${mappedUrl}`);
@@ -4697,10 +4785,10 @@ const global_1 = _src_global;
4697
4785
  exports.Templates = {
4698
4786
  Module: `
4699
4787
  // source ./Module.js
4700
- __register("%MODULE_PATH%", function(require, module, exports, __filename, __dirname){
4701
- %MODULE%
4788
+ __register(%MODULE_PATH%, %MODULE_ALIASES%, function(require, module, exports, __filename, __dirname){
4789
+ %MODULE%
4702
4790
 
4703
- %FOOTER%
4791
+ %FOOTER%
4704
4792
  });
4705
4793
 
4706
4794
  // end:source ./Module.js
@@ -4734,6 +4822,7 @@ var ___VAR_ID___;
4734
4822
  `,
4735
4823
  Header: `
4736
4824
  // source ./Header.js
4825
+
4737
4826
  var __register, __require, require;
4738
4827
 
4739
4828
  (function(){
@@ -4875,31 +4964,49 @@ var __register, __require, require;
4875
4964
 
4876
4965
  var __global = typeof global !== 'undefined' && global ? global : window;
4877
4966
  var __nativeRequire = __global.require || require;
4878
- var __originalRequire = function (path_) {
4879
- var location = this.location;
4880
- var path = path_resolveUrl(path_, location);
4881
-
4882
- if (modules[path]) {
4883
- return modules[path].runOnce();
4967
+ var __bundlerRequire = function (path_) {
4968
+ var arr = [
4969
+ path_resolveUrl(path_, this.location)
4970
+ ];
4971
+ if (typeof include !== 'undefined' && typeof include.resolvePath === 'function') {
4972
+ arr.push(include.resolvePath(path_));
4973
+ }
4974
+ if (/^[@\\w]/.test(path_)) {
4975
+ arr.push(path_);
4976
+ }
4977
+ for (var i = 0; i < arr.length; i++) {
4978
+ if (arr[i] in modules) {
4979
+ return modules[arr[i]].runOnce();
4980
+ }
4884
4981
  }
4885
- var pathWithoutExt = path.replace(/\\.\\w+$/, '').toLowerCase();
4982
+
4886
4983
  for (var key in modules) {
4887
4984
  var $pathWithoutExt = key.replace(/\\.\\w+$/, '').toLowerCase();
4888
- if ($pathWithoutExt === pathWithoutExt) {
4889
- let module = modules[path] = modules[key];
4890
- return module.runOnce();
4985
+ for (var i = 0; i < arr.length; i++) {
4986
+ var path = arr[i];
4987
+ var pathWithoutExt = path.replace(/\\.\\w+$/, '').toLowerCase();
4988
+ if ($pathWithoutExt === pathWithoutExt) {
4989
+ let module = modules[path] = modules[key];
4990
+ return module.runOnce();
4991
+ }
4891
4992
  }
4892
4993
  }
4893
4994
 
4894
4995
  return __nativeRequire(path_);
4895
4996
  };
4896
4997
 
4897
- __register = function (path, factory) {
4998
+ __register = function (path, aliases, factory) {
4898
4999
  var filename = path_resolveUrl(path);
4899
- modules[filename] = new Module(filename, factory);
5000
+ var module = new Module(filename, factory);
5001
+ modules[filename] = module;
5002
+ if (aliases) {
5003
+ aliases.forEach(alias => {
5004
+ modules[alias] = module;
5005
+ })
5006
+ }
4900
5007
  };
4901
5008
 
4902
- __require =__originalRequire.bind({ location: path_getDir(path_resolveUrl('%ROOT_DIR%')) });
5009
+ __require =__bundlerRequire.bind({ location: path_getDir(path_resolveUrl('%ROOT_DIR%')) });
4903
5010
 
4904
5011
  var modules = {};
4905
5012
  var Module = function(filename, factory){
@@ -4912,7 +5019,7 @@ var __register, __require, require;
4912
5019
  if (this.exports != null) {
4913
5020
  return this.exports;
4914
5021
  }
4915
- var require = __originalRequire.bind({
5022
+ var require = __bundlerRequire.bind({
4916
5023
  location: this.dirname
4917
5024
  });
4918
5025
  this.exports = {};
@@ -4928,7 +5035,7 @@ var __register, __require, require;
4928
5035
 
4929
5036
  require = __require;
4930
5037
 
4931
- if (__nativeRequire == null) {
5038
+ if (__global.require != null) {
4932
5039
  __global.require = __require;
4933
5040
  }
4934
5041
  }());
@@ -6105,7 +6212,7 @@ class CommonJsBuilder extends BaseScriptBuilder_1.BaseScriptBuilder {
6105
6212
  super(solution, handler);
6106
6213
  this.solution = solution;
6107
6214
  this.handler = handler;
6108
- var opts = this.solution.opts.package.commonjs;
6215
+ let opts = this.solution.opts.package.commonjs;
6109
6216
  if (opts && opts.output === 'simplified') {
6110
6217
  this.wrapModule = CommonJsBuilderSimplified_1.CommonJsBuilderSimplified.wrapModule;
6111
6218
  this.getRootContent = CommonJsBuilderSimplified_1.CommonJsBuilderSimplified.getRootContent;
@@ -6123,7 +6230,7 @@ class CommonJsBuilder extends BaseScriptBuilder_1.BaseScriptBuilder {
6123
6230
  return module === 'commonjs';
6124
6231
  }
6125
6232
  wrapModule(resource, outputItem, outputItems, setts) {
6126
- var opts = this.solution.iteration;
6233
+ let opts = this.solution.iteration;
6127
6234
  if (opts.commonjs == null) {
6128
6235
  opts.commonjs = {
6129
6236
  addHeading: true,
@@ -6131,21 +6238,22 @@ class CommonJsBuilder extends BaseScriptBuilder_1.BaseScriptBuilder {
6131
6238
  };
6132
6239
  }
6133
6240
  ;
6134
- var body = '';
6241
+ let body = '';
6135
6242
  if (opts.commonjs.hasHeading === false && opts.commonjs.addHeading === true) {
6136
6243
  opts.commonjs.hasHeading = true;
6137
6244
  body = this.getHeaderContent();
6138
6245
  }
6139
- var { url, content } = resource;
6140
- var module = Templates_1.Templates
6246
+ let { url, content, aliases } = resource;
6247
+ let module = Templates_1.Templates
6141
6248
  .Module
6142
- .replace('%MODULE_PATH%', () => url)
6249
+ .replace('%MODULE_PATH%', () => `"${url}"`)
6250
+ .replace('%MODULE_ALIASES%', JSON.stringify(aliases !== null && aliases !== void 0 ? aliases : []))
6143
6251
  .replace('%MODULE%', () => content)
6144
6252
  .replace('%FOOTER%', () => { var _a, _b; return ((_b = (_a = setts === null || setts === void 0 ? void 0 : setts.partials) === null || _a === void 0 ? void 0 : _a.footer) !== null && _b !== void 0 ? _b : ''); });
6145
6253
  return body + module;
6146
6254
  }
6147
6255
  getHeaderContent() {
6148
- var mainUrl = this.solution.outputResources.rootInput.url;
6256
+ let mainUrl = this.solution.outputResources.rootInput.url;
6149
6257
  return Templates_1.Templates
6150
6258
  .Header
6151
6259
  .replace('%ROOT_DIR%', () => mainUrl);
@@ -6166,7 +6274,7 @@ class CommonJsBuilder extends BaseScriptBuilder_1.BaseScriptBuilder {
6166
6274
  })
6167
6275
  .concat([content])
6168
6276
  .join('\n');
6169
- var wrapper = new ModuleWrapper_1.ModuleWrapper(this.solution);
6277
+ let wrapper = new ModuleWrapper_1.ModuleWrapper(this.solution);
6170
6278
  root.content = wrapper.wrap(body);
6171
6279
  }
6172
6280
  getRootContent(root, outputDependencies) {
@@ -6207,8 +6315,7 @@ var _src_handlers_script_common_js_CommonJsPathResolver;
6207
6315
  Object.defineProperty(exports, "__esModule", { value: true });
6208
6316
  exports.CommonJsPathResolver = void 0;
6209
6317
  const BasePathResolver_1 = _src_handlers_base_BasePathResolver;
6210
- const path_1 = _src_utils_path;
6211
- const global_1 = _src_global;
6318
+ const npm_1 = _src_utils_npm;
6212
6319
  class CommonJsPathResolver extends BasePathResolver_1.BasePathResolver {
6213
6320
  accepts(includeData) {
6214
6321
  if (includeData.type !== 'js' || includeData.module !== 'commonjs') {
@@ -6223,7 +6330,7 @@ class CommonJsPathResolver extends BasePathResolver_1.BasePathResolver {
6223
6330
  return true;
6224
6331
  }
6225
6332
  resolve(includeData, parentResource) {
6226
- let filename = nodeModuleResolve(includeData.url, parentResource.directory);
6333
+ let filename = npm_1.Npm.resolveAppUrl(includeData.url, parentResource.directory, this.solution.opts);
6227
6334
  if (filename) {
6228
6335
  let url = this.solution.opts.toAppUrl(filename);
6229
6336
  this.solution.opts.mappings[includeData.url] = url;
@@ -6239,49 +6346,49 @@ const nodeCoreModules = [
6239
6346
  'process', 'punycode', 'querystring', 'readline', 'repl', 'stream', 'string_decoder', 'sys', 'timers',
6240
6347
  'tls', 'tty', 'url', 'util', 'vm', 'zlib'
6241
6348
  ];
6242
- function nodeModuleResolve(path, location_) {
6243
- let location = location_.replace(/[\\\/]+$/, '');
6244
- let name = /^([\w\-]+)/.exec(path)[0];
6245
- let resource = path.substring(name.length + 1);
6246
- let current = location;
6247
- let root_ = current + '/node_modules/' + name + '/';
6248
- while (true) {
6249
- let nodeModules = (0, path_1.path_combine)(current, '/node_modules/' + name + '/');
6250
- let pckg = nodeModules + 'package.json';
6251
- if (global_1.io.File.exists(pckg) === false) {
6252
- let next = current.replace(/[^\/\\]+[\/\\]?$/, '');
6253
- if (next === current) {
6254
- return root_ + 'package.json';
6255
- }
6256
- current = next;
6257
- continue;
6258
- }
6259
- if (resource) {
6260
- let path = nodeModules + resource;
6261
- if (hasExt(path) === false) {
6262
- let likelyTs = path.includes('/src/');
6263
- if (likelyTs) {
6264
- if (global_1.io.File.exists(path + '.ts')) {
6265
- return path + '.ts';
6266
- }
6267
- }
6268
- return path + '.js';
6269
- }
6270
- return path;
6271
- }
6272
- let json = global_1.io.File.read(pckg);
6273
- if (typeof json === 'string') {
6274
- json = JSON.parse(json);
6275
- }
6276
- if (json.main) {
6277
- return (0, path_1.path_combine)(nodeModules, json.main);
6278
- }
6279
- return nodeModules + 'index.js';
6280
- }
6281
- }
6282
- function hasExt(path) {
6283
- return /\.[\w]{1,8}($|\?)/.test(path);
6284
- }
6349
+ // function nodeModuleResolve(path, location_){
6350
+ // let location = location_.replace(/[\\\/]+$/, '');
6351
+ // let name = /^([\w\-]+)/.exec(path)[0];
6352
+ // let resource = path.substring(name.length + 1);
6353
+ // let current = location;
6354
+ // let root_ = current + '/node_modules/' + name + '/';
6355
+ // while (true) {
6356
+ // let nodeModules = path_combine(current, '/node_modules/' + name + '/');
6357
+ // let pkg = nodeModules + 'package.json';
6358
+ // if (io.File.exists(pkg) === false) {
6359
+ // let next = current.replace(/[^\/\\]+[\/\\]?$/, '');
6360
+ // if (next === current) {
6361
+ // return root_ + 'package.json';
6362
+ // }
6363
+ // current = next;
6364
+ // continue;
6365
+ // }
6366
+ // if (resource) {
6367
+ // let path = nodeModules + resource;
6368
+ // if (hasExt(path) === false) {
6369
+ // let likelyTs = path.includes('/src/');
6370
+ // if (likelyTs) {
6371
+ // if (io.File.exists(path + '.ts')) {
6372
+ // return path + '.ts';
6373
+ // }
6374
+ // }
6375
+ // return path + '.js';
6376
+ // }
6377
+ // return path;
6378
+ // }
6379
+ // let json = io.File.read(pkg);
6380
+ // if (typeof json === 'string') {
6381
+ // json = JSON.parse(json);
6382
+ // }
6383
+ // if (json.main) {
6384
+ // return path_combine(nodeModules, json.main)
6385
+ // }
6386
+ // return nodeModules + 'index.js';
6387
+ // }
6388
+ // }
6389
+ // function hasExt(path) {
6390
+ // return /\.[\w]{1,8}($|\?)/.test(path);
6391
+ // }
6285
6392
  //# sourceMappingURL=CommonJsPathResolver.js.map
6286
6393
  //# sourceMappingURL=CommonJsPathResolver.ts.map;
6287
6394
 
@@ -7873,8 +7980,11 @@ class DataInliner extends BaseRewriter_1.BaseRewriter {
7873
7980
  rewritePartial(content, ownerResource) {
7874
7981
  }
7875
7982
  async rewriteResource(resource) {
7983
+ if (resource.dependencies == null) {
7984
+ return;
7985
+ }
7876
7986
  for (let res of resource.dependencies) {
7877
- if (/(json|yml)$/.test(res.url) === false) {
7987
+ if (/\.(json|yml)$/.test(res.url) === false) {
7878
7988
  continue;
7879
7989
  }
7880
7990
  if (res.import != null) {
@@ -7905,6 +8015,7 @@ class DataInliner extends BaseRewriter_1.BaseRewriter {
7905
8015
  let i = resource.dependencies.indexOf(dep);
7906
8016
  resource.dependencies.splice(i, 1);
7907
8017
  this.solution.outputResources.remove(dep.resource);
8018
+ this.solution.reporter.info(`Inlining import data from ${dep.url}`);
7908
8019
  }
7909
8020
  rewriteRequire(resource, dep) {
7910
8021
  let rgx = new RegExp(`require\\s*\\(\\s*['"]${dep.url}['"]\\s*\\)\\s*;?`);
@@ -7923,6 +8034,7 @@ class DataInliner extends BaseRewriter_1.BaseRewriter {
7923
8034
  let i = resource.dependencies.indexOf(dep);
7924
8035
  resource.dependencies.splice(i, 1);
7925
8036
  this.solution.outputResources.remove(dep.resource);
8037
+ this.solution.reporter.info(`Inlining require data from ${dep.url}`);
7926
8038
  }
7927
8039
  }
7928
8040
  exports.DataInliner = DataInliner;
@@ -11749,6 +11861,13 @@ var ResourceLoader;
11749
11861
  loader.process();
11750
11862
  }
11751
11863
  else {
11864
+ // Merge aliases to the current cached resource
11865
+ if (resource.aliases.length > 0) {
11866
+ loader.resource.aliases = (0, alot_1.default)([
11867
+ ...loader.resource.aliases,
11868
+ ...resource.aliases
11869
+ ]).distinct().toArray();
11870
+ }
11752
11871
  // Try to find the resource in ancestors
11753
11872
  let res = tryGetCyclicRoot(resource);
11754
11873
  if (res != null) {
@@ -12138,8 +12257,8 @@ exports.Builder = {
12138
12257
  .then(buildRoot)
12139
12258
  .then(() => solution.outputResources.getAll());
12140
12259
  });
12141
- function rewriteDependenciesInternal(resources) {
12142
- let promises = resources.map(resource => {
12260
+ async function rewriteDependenciesInternal(resources) {
12261
+ await (0, alot_1.default)(resources).forEachAsync(async (resource, i) => {
12143
12262
  let ext = (0, path_1.path_getExtension)(resource.url);
12144
12263
  let handler = solution.handlers.find(x => x.rewriter.accepts(ext));
12145
12264
  if (handler == null) {
@@ -12148,9 +12267,8 @@ exports.Builder = {
12148
12267
  if (handler == null) {
12149
12268
  throw Error('Rewriter not found for the resource: ' + resource.url);
12150
12269
  }
12151
- return handler.rewriter.rewriteResource(resource);
12152
- });
12153
- return (0, async_1.async_whenAll)(promises);
12270
+ await handler.rewriter.rewriteResource(resource);
12271
+ }).toArrayAsync({ threads: 1 });
12154
12272
  }
12155
12273
  function rewriteRoot() {
12156
12274
  return (0, async_1.async_run)(() => {
package/lib/cli.js CHANGED
@@ -1312,7 +1312,8 @@ const global_1 = _src_global;
1312
1312
  const atma_io_1 = require("atma-io");
1313
1313
  var Npm;
1314
1314
  (function (Npm) {
1315
- function resolveAppUrl(path, currentLocation, base) {
1315
+ function resolveAppUrl(path, currentLocation, opts) {
1316
+ let base = opts.base;
1316
1317
  let name = /^(@?[^\\\/]+)/.exec(path)[0];
1317
1318
  let resource = path.substring(name.length + 1);
1318
1319
  while (path != null) {
@@ -1331,14 +1332,9 @@ var Npm;
1331
1332
  dirname = (0, path_1.path_combine)(dirname, `/${subPackage}`, '/');
1332
1333
  resource = resource.substring(subPackage.length + 1);
1333
1334
  }
1334
- if (resource === '') {
1335
- let pkg = (0, path_1.path_combine)(dirname, 'package.json');
1336
- let json = global_1.io.File.read(pkg);
1337
- if (json) {
1338
- resource = json.main;
1339
- }
1340
- }
1341
- let filename = combineMain(dirname, resource);
1335
+ let pkg = (0, path_1.path_combine)(dirname, 'package.json');
1336
+ let json = global_1.io.File.read(pkg);
1337
+ let filename = resolveAppUrlFromPackageJson(dirname, json, resource, opts);
1342
1338
  if (base) {
1343
1339
  if (base.endsWith('/') === false) {
1344
1340
  // Base path must be a folder
@@ -1355,6 +1351,89 @@ var Npm;
1355
1351
  return null;
1356
1352
  }
1357
1353
  Npm.resolveAppUrl = resolveAppUrl;
1354
+ function resolveAppUrlFromPackageJson(pkgDir, pkg, subPath, opts) {
1355
+ var _a;
1356
+ if (pkg.main == null && pkg.exports == null) {
1357
+ return combineMain(pkgDir, subPath);
1358
+ }
1359
+ if (pkg.exports == null) {
1360
+ return combineMain(pkgDir, subPath || pkg.main);
1361
+ }
1362
+ if (typeof pkg.exports === 'string') {
1363
+ return combineMain(pkgDir, subPath || pkg.exports);
1364
+ }
1365
+ const moduleExportsMap = 'require';
1366
+ const conditionExportsMap = (_a = opts.package.platform) !== null && _a !== void 0 ? _a : 'node';
1367
+ let libraryFilePath = fromExports(pkg.exports, subPath, {
1368
+ moduleExports: moduleExportsMap,
1369
+ conditionExports: conditionExportsMap,
1370
+ pkgDir,
1371
+ });
1372
+ return combineMain(pkgDir, libraryFilePath);
1373
+ }
1374
+ Npm.resolveAppUrlFromPackageJson = resolveAppUrlFromPackageJson;
1375
+ function fromExports(exports, subPath, opts) {
1376
+ var _a;
1377
+ if (exports == null) {
1378
+ throw new Error(`Invalid exports for ${subPath}`);
1379
+ }
1380
+ if (typeof exports === 'object') {
1381
+ exports = extractConditional(exports, opts);
1382
+ }
1383
+ if (typeof exports === 'object') {
1384
+ // TExportsMap
1385
+ let map = exports;
1386
+ if (!subPath) {
1387
+ let dot = '.';
1388
+ if (map[dot] == null) {
1389
+ throw new Error(`Missing default "." export in exports for ${JSON.stringify(exports)}`);
1390
+ }
1391
+ return extractConditional(map[dot], opts);
1392
+ }
1393
+ for (let tpl in exports) {
1394
+ let mapped = extractConditional(exports[tpl], opts);
1395
+ if (typeof mapped !== 'string') {
1396
+ throw new Error(`Invalid conditional exports for ${JSON.stringify(exports)} in ${(_a = opts.pkgDir) !== null && _a !== void 0 ? _a : subPath}`);
1397
+ }
1398
+ if (tpl.startsWith('./')) {
1399
+ tpl = tpl.substring(2);
1400
+ if (tpl === '*') {
1401
+ return mapped.replace('*', subPath);
1402
+ }
1403
+ let globIndex = tpl.indexOf('*');
1404
+ if (globIndex === -1 && tpl === subPath) {
1405
+ return mapped;
1406
+ }
1407
+ if (globIndex > -1) {
1408
+ let tplPfx = tpl.substring(0, globIndex).replace(/\/+$/, '');
1409
+ if (subPath.startsWith(tplPfx)) {
1410
+ let subPathSfx = subPath.replace(tplPfx, '');
1411
+ return mapped.replace('*', subPathSfx);
1412
+ }
1413
+ }
1414
+ }
1415
+ }
1416
+ }
1417
+ if (typeof exports === 'string') {
1418
+ return exports;
1419
+ }
1420
+ throw new Error(`Path can't be mapped ${JSON.stringify(exports)}`);
1421
+ }
1422
+ function extractConditional(exports, opts) {
1423
+ if (exports == null || typeof exports !== 'object') {
1424
+ return exports;
1425
+ }
1426
+ if (opts.conditionExports in exports) {
1427
+ return extractConditional(exports[opts.conditionExports], opts);
1428
+ }
1429
+ if (opts.moduleExports in exports) {
1430
+ return extractConditional(exports[opts.moduleExports], opts);
1431
+ }
1432
+ if ('default' in exports) {
1433
+ return extractConditional(exports['default'], opts);
1434
+ }
1435
+ return exports;
1436
+ }
1358
1437
  function combineMain(directory, filename) {
1359
1438
  filename !== null && filename !== void 0 ? filename : (filename = 'index.js');
1360
1439
  let path = (0, path_1.path_combine)(directory, filename);
@@ -1517,7 +1596,7 @@ class Resource {
1517
1596
  }
1518
1597
  if (Include_1.Include.PathResolver.isNpm(includeData.url)) {
1519
1598
  this.aliases.push(includeData.url);
1520
- let byNpmResolver = npm_1.Npm.resolveAppUrl(includeData.url, parent && parent.location, solution.opts.base);
1599
+ let byNpmResolver = npm_1.Npm.resolveAppUrl(includeData.url, parent && parent.location, solution.opts);
1521
1600
  if (byNpmResolver != null) {
1522
1601
  // otherwise "url" could be previously resolved with pathResolver.resolve
1523
1602
  url = byNpmResolver;
@@ -1527,6 +1606,15 @@ class Resource {
1527
1606
  url = includeData.url;
1528
1607
  }
1529
1608
  }
1609
+ if (includeData.route != null) {
1610
+ let { path, alias } = includeData.route;
1611
+ if (path && Include_1.Include.PathResolver.isNpm(path) && this.aliases.includes(path) === false) {
1612
+ this.aliases.push(path);
1613
+ }
1614
+ if (alias && Include_1.Include.PathResolver.isNpm(alias) && this.aliases.includes(alias) === false) {
1615
+ this.aliases.push(alias);
1616
+ }
1617
+ }
1530
1618
  let mappedUrl = this.solution.mapUrl(url);
1531
1619
  if (mappedUrl != url) {
1532
1620
  console.log(`URL mapped: ${url} => ${mappedUrl}`);
@@ -4699,10 +4787,10 @@ const global_1 = _src_global;
4699
4787
  exports.Templates = {
4700
4788
  Module: `
4701
4789
  // source ./Module.js
4702
- __register("%MODULE_PATH%", function(require, module, exports, __filename, __dirname){
4703
- %MODULE%
4790
+ __register(%MODULE_PATH%, %MODULE_ALIASES%, function(require, module, exports, __filename, __dirname){
4791
+ %MODULE%
4704
4792
 
4705
- %FOOTER%
4793
+ %FOOTER%
4706
4794
  });
4707
4795
 
4708
4796
  // end:source ./Module.js
@@ -4736,6 +4824,7 @@ var ___VAR_ID___;
4736
4824
  `,
4737
4825
  Header: `
4738
4826
  // source ./Header.js
4827
+
4739
4828
  var __register, __require, require;
4740
4829
 
4741
4830
  (function(){
@@ -4877,31 +4966,49 @@ var __register, __require, require;
4877
4966
 
4878
4967
  var __global = typeof global !== 'undefined' && global ? global : window;
4879
4968
  var __nativeRequire = __global.require || require;
4880
- var __originalRequire = function (path_) {
4881
- var location = this.location;
4882
- var path = path_resolveUrl(path_, location);
4883
-
4884
- if (modules[path]) {
4885
- return modules[path].runOnce();
4969
+ var __bundlerRequire = function (path_) {
4970
+ var arr = [
4971
+ path_resolveUrl(path_, this.location)
4972
+ ];
4973
+ if (typeof include !== 'undefined' && typeof include.resolvePath === 'function') {
4974
+ arr.push(include.resolvePath(path_));
4975
+ }
4976
+ if (/^[@\\w]/.test(path_)) {
4977
+ arr.push(path_);
4978
+ }
4979
+ for (var i = 0; i < arr.length; i++) {
4980
+ if (arr[i] in modules) {
4981
+ return modules[arr[i]].runOnce();
4982
+ }
4886
4983
  }
4887
- var pathWithoutExt = path.replace(/\\.\\w+$/, '').toLowerCase();
4984
+
4888
4985
  for (var key in modules) {
4889
4986
  var $pathWithoutExt = key.replace(/\\.\\w+$/, '').toLowerCase();
4890
- if ($pathWithoutExt === pathWithoutExt) {
4891
- let module = modules[path] = modules[key];
4892
- return module.runOnce();
4987
+ for (var i = 0; i < arr.length; i++) {
4988
+ var path = arr[i];
4989
+ var pathWithoutExt = path.replace(/\\.\\w+$/, '').toLowerCase();
4990
+ if ($pathWithoutExt === pathWithoutExt) {
4991
+ let module = modules[path] = modules[key];
4992
+ return module.runOnce();
4993
+ }
4893
4994
  }
4894
4995
  }
4895
4996
 
4896
4997
  return __nativeRequire(path_);
4897
4998
  };
4898
4999
 
4899
- __register = function (path, factory) {
5000
+ __register = function (path, aliases, factory) {
4900
5001
  var filename = path_resolveUrl(path);
4901
- modules[filename] = new Module(filename, factory);
5002
+ var module = new Module(filename, factory);
5003
+ modules[filename] = module;
5004
+ if (aliases) {
5005
+ aliases.forEach(alias => {
5006
+ modules[alias] = module;
5007
+ })
5008
+ }
4902
5009
  };
4903
5010
 
4904
- __require =__originalRequire.bind({ location: path_getDir(path_resolveUrl('%ROOT_DIR%')) });
5011
+ __require =__bundlerRequire.bind({ location: path_getDir(path_resolveUrl('%ROOT_DIR%')) });
4905
5012
 
4906
5013
  var modules = {};
4907
5014
  var Module = function(filename, factory){
@@ -4914,7 +5021,7 @@ var __register, __require, require;
4914
5021
  if (this.exports != null) {
4915
5022
  return this.exports;
4916
5023
  }
4917
- var require = __originalRequire.bind({
5024
+ var require = __bundlerRequire.bind({
4918
5025
  location: this.dirname
4919
5026
  });
4920
5027
  this.exports = {};
@@ -4930,7 +5037,7 @@ var __register, __require, require;
4930
5037
 
4931
5038
  require = __require;
4932
5039
 
4933
- if (__nativeRequire == null) {
5040
+ if (__global.require != null) {
4934
5041
  __global.require = __require;
4935
5042
  }
4936
5043
  }());
@@ -6107,7 +6214,7 @@ class CommonJsBuilder extends BaseScriptBuilder_1.BaseScriptBuilder {
6107
6214
  super(solution, handler);
6108
6215
  this.solution = solution;
6109
6216
  this.handler = handler;
6110
- var opts = this.solution.opts.package.commonjs;
6217
+ let opts = this.solution.opts.package.commonjs;
6111
6218
  if (opts && opts.output === 'simplified') {
6112
6219
  this.wrapModule = CommonJsBuilderSimplified_1.CommonJsBuilderSimplified.wrapModule;
6113
6220
  this.getRootContent = CommonJsBuilderSimplified_1.CommonJsBuilderSimplified.getRootContent;
@@ -6125,7 +6232,7 @@ class CommonJsBuilder extends BaseScriptBuilder_1.BaseScriptBuilder {
6125
6232
  return module === 'commonjs';
6126
6233
  }
6127
6234
  wrapModule(resource, outputItem, outputItems, setts) {
6128
- var opts = this.solution.iteration;
6235
+ let opts = this.solution.iteration;
6129
6236
  if (opts.commonjs == null) {
6130
6237
  opts.commonjs = {
6131
6238
  addHeading: true,
@@ -6133,21 +6240,22 @@ class CommonJsBuilder extends BaseScriptBuilder_1.BaseScriptBuilder {
6133
6240
  };
6134
6241
  }
6135
6242
  ;
6136
- var body = '';
6243
+ let body = '';
6137
6244
  if (opts.commonjs.hasHeading === false && opts.commonjs.addHeading === true) {
6138
6245
  opts.commonjs.hasHeading = true;
6139
6246
  body = this.getHeaderContent();
6140
6247
  }
6141
- var { url, content } = resource;
6142
- var module = Templates_1.Templates
6248
+ let { url, content, aliases } = resource;
6249
+ let module = Templates_1.Templates
6143
6250
  .Module
6144
- .replace('%MODULE_PATH%', () => url)
6251
+ .replace('%MODULE_PATH%', () => `"${url}"`)
6252
+ .replace('%MODULE_ALIASES%', JSON.stringify(aliases !== null && aliases !== void 0 ? aliases : []))
6145
6253
  .replace('%MODULE%', () => content)
6146
6254
  .replace('%FOOTER%', () => { var _a, _b; return ((_b = (_a = setts === null || setts === void 0 ? void 0 : setts.partials) === null || _a === void 0 ? void 0 : _a.footer) !== null && _b !== void 0 ? _b : ''); });
6147
6255
  return body + module;
6148
6256
  }
6149
6257
  getHeaderContent() {
6150
- var mainUrl = this.solution.outputResources.rootInput.url;
6258
+ let mainUrl = this.solution.outputResources.rootInput.url;
6151
6259
  return Templates_1.Templates
6152
6260
  .Header
6153
6261
  .replace('%ROOT_DIR%', () => mainUrl);
@@ -6168,7 +6276,7 @@ class CommonJsBuilder extends BaseScriptBuilder_1.BaseScriptBuilder {
6168
6276
  })
6169
6277
  .concat([content])
6170
6278
  .join('\n');
6171
- var wrapper = new ModuleWrapper_1.ModuleWrapper(this.solution);
6279
+ let wrapper = new ModuleWrapper_1.ModuleWrapper(this.solution);
6172
6280
  root.content = wrapper.wrap(body);
6173
6281
  }
6174
6282
  getRootContent(root, outputDependencies) {
@@ -6209,8 +6317,7 @@ var _src_handlers_script_common_js_CommonJsPathResolver;
6209
6317
  Object.defineProperty(exports, "__esModule", { value: true });
6210
6318
  exports.CommonJsPathResolver = void 0;
6211
6319
  const BasePathResolver_1 = _src_handlers_base_BasePathResolver;
6212
- const path_1 = _src_utils_path;
6213
- const global_1 = _src_global;
6320
+ const npm_1 = _src_utils_npm;
6214
6321
  class CommonJsPathResolver extends BasePathResolver_1.BasePathResolver {
6215
6322
  accepts(includeData) {
6216
6323
  if (includeData.type !== 'js' || includeData.module !== 'commonjs') {
@@ -6225,7 +6332,7 @@ class CommonJsPathResolver extends BasePathResolver_1.BasePathResolver {
6225
6332
  return true;
6226
6333
  }
6227
6334
  resolve(includeData, parentResource) {
6228
- let filename = nodeModuleResolve(includeData.url, parentResource.directory);
6335
+ let filename = npm_1.Npm.resolveAppUrl(includeData.url, parentResource.directory, this.solution.opts);
6229
6336
  if (filename) {
6230
6337
  let url = this.solution.opts.toAppUrl(filename);
6231
6338
  this.solution.opts.mappings[includeData.url] = url;
@@ -6241,49 +6348,49 @@ const nodeCoreModules = [
6241
6348
  'process', 'punycode', 'querystring', 'readline', 'repl', 'stream', 'string_decoder', 'sys', 'timers',
6242
6349
  'tls', 'tty', 'url', 'util', 'vm', 'zlib'
6243
6350
  ];
6244
- function nodeModuleResolve(path, location_) {
6245
- let location = location_.replace(/[\\\/]+$/, '');
6246
- let name = /^([\w\-]+)/.exec(path)[0];
6247
- let resource = path.substring(name.length + 1);
6248
- let current = location;
6249
- let root_ = current + '/node_modules/' + name + '/';
6250
- while (true) {
6251
- let nodeModules = (0, path_1.path_combine)(current, '/node_modules/' + name + '/');
6252
- let pckg = nodeModules + 'package.json';
6253
- if (global_1.io.File.exists(pckg) === false) {
6254
- let next = current.replace(/[^\/\\]+[\/\\]?$/, '');
6255
- if (next === current) {
6256
- return root_ + 'package.json';
6257
- }
6258
- current = next;
6259
- continue;
6260
- }
6261
- if (resource) {
6262
- let path = nodeModules + resource;
6263
- if (hasExt(path) === false) {
6264
- let likelyTs = path.includes('/src/');
6265
- if (likelyTs) {
6266
- if (global_1.io.File.exists(path + '.ts')) {
6267
- return path + '.ts';
6268
- }
6269
- }
6270
- return path + '.js';
6271
- }
6272
- return path;
6273
- }
6274
- let json = global_1.io.File.read(pckg);
6275
- if (typeof json === 'string') {
6276
- json = JSON.parse(json);
6277
- }
6278
- if (json.main) {
6279
- return (0, path_1.path_combine)(nodeModules, json.main);
6280
- }
6281
- return nodeModules + 'index.js';
6282
- }
6283
- }
6284
- function hasExt(path) {
6285
- return /\.[\w]{1,8}($|\?)/.test(path);
6286
- }
6351
+ // function nodeModuleResolve(path, location_){
6352
+ // let location = location_.replace(/[\\\/]+$/, '');
6353
+ // let name = /^([\w\-]+)/.exec(path)[0];
6354
+ // let resource = path.substring(name.length + 1);
6355
+ // let current = location;
6356
+ // let root_ = current + '/node_modules/' + name + '/';
6357
+ // while (true) {
6358
+ // let nodeModules = path_combine(current, '/node_modules/' + name + '/');
6359
+ // let pkg = nodeModules + 'package.json';
6360
+ // if (io.File.exists(pkg) === false) {
6361
+ // let next = current.replace(/[^\/\\]+[\/\\]?$/, '');
6362
+ // if (next === current) {
6363
+ // return root_ + 'package.json';
6364
+ // }
6365
+ // current = next;
6366
+ // continue;
6367
+ // }
6368
+ // if (resource) {
6369
+ // let path = nodeModules + resource;
6370
+ // if (hasExt(path) === false) {
6371
+ // let likelyTs = path.includes('/src/');
6372
+ // if (likelyTs) {
6373
+ // if (io.File.exists(path + '.ts')) {
6374
+ // return path + '.ts';
6375
+ // }
6376
+ // }
6377
+ // return path + '.js';
6378
+ // }
6379
+ // return path;
6380
+ // }
6381
+ // let json = io.File.read(pkg);
6382
+ // if (typeof json === 'string') {
6383
+ // json = JSON.parse(json);
6384
+ // }
6385
+ // if (json.main) {
6386
+ // return path_combine(nodeModules, json.main)
6387
+ // }
6388
+ // return nodeModules + 'index.js';
6389
+ // }
6390
+ // }
6391
+ // function hasExt(path) {
6392
+ // return /\.[\w]{1,8}($|\?)/.test(path);
6393
+ // }
6287
6394
  //# sourceMappingURL=CommonJsPathResolver.js.map
6288
6395
  //# sourceMappingURL=CommonJsPathResolver.ts.map;
6289
6396
 
@@ -7875,8 +7982,11 @@ class DataInliner extends BaseRewriter_1.BaseRewriter {
7875
7982
  rewritePartial(content, ownerResource) {
7876
7983
  }
7877
7984
  async rewriteResource(resource) {
7985
+ if (resource.dependencies == null) {
7986
+ return;
7987
+ }
7878
7988
  for (let res of resource.dependencies) {
7879
- if (/(json|yml)$/.test(res.url) === false) {
7989
+ if (/\.(json|yml)$/.test(res.url) === false) {
7880
7990
  continue;
7881
7991
  }
7882
7992
  if (res.import != null) {
@@ -7907,6 +8017,7 @@ class DataInliner extends BaseRewriter_1.BaseRewriter {
7907
8017
  let i = resource.dependencies.indexOf(dep);
7908
8018
  resource.dependencies.splice(i, 1);
7909
8019
  this.solution.outputResources.remove(dep.resource);
8020
+ this.solution.reporter.info(`Inlining import data from ${dep.url}`);
7910
8021
  }
7911
8022
  rewriteRequire(resource, dep) {
7912
8023
  let rgx = new RegExp(`require\\s*\\(\\s*['"]${dep.url}['"]\\s*\\)\\s*;?`);
@@ -7925,6 +8036,7 @@ class DataInliner extends BaseRewriter_1.BaseRewriter {
7925
8036
  let i = resource.dependencies.indexOf(dep);
7926
8037
  resource.dependencies.splice(i, 1);
7927
8038
  this.solution.outputResources.remove(dep.resource);
8039
+ this.solution.reporter.info(`Inlining require data from ${dep.url}`);
7928
8040
  }
7929
8041
  }
7930
8042
  exports.DataInliner = DataInliner;
@@ -11751,6 +11863,13 @@ var ResourceLoader;
11751
11863
  loader.process();
11752
11864
  }
11753
11865
  else {
11866
+ // Merge aliases to the current cached resource
11867
+ if (resource.aliases.length > 0) {
11868
+ loader.resource.aliases = (0, alot_1.default)([
11869
+ ...loader.resource.aliases,
11870
+ ...resource.aliases
11871
+ ]).distinct().toArray();
11872
+ }
11754
11873
  // Try to find the resource in ancestors
11755
11874
  let res = tryGetCyclicRoot(resource);
11756
11875
  if (res != null) {
@@ -12140,8 +12259,8 @@ exports.Builder = {
12140
12259
  .then(buildRoot)
12141
12260
  .then(() => solution.outputResources.getAll());
12142
12261
  });
12143
- function rewriteDependenciesInternal(resources) {
12144
- let promises = resources.map(resource => {
12262
+ async function rewriteDependenciesInternal(resources) {
12263
+ await (0, alot_1.default)(resources).forEachAsync(async (resource, i) => {
12145
12264
  let ext = (0, path_1.path_getExtension)(resource.url);
12146
12265
  let handler = solution.handlers.find(x => x.rewriter.accepts(ext));
12147
12266
  if (handler == null) {
@@ -12150,9 +12269,8 @@ exports.Builder = {
12150
12269
  if (handler == null) {
12151
12270
  throw Error('Rewriter not found for the resource: ' + resource.url);
12152
12271
  }
12153
- return handler.rewriter.rewriteResource(resource);
12154
- });
12155
- return (0, async_1.async_whenAll)(promises);
12272
+ await handler.rewriter.rewriteResource(resource);
12273
+ }).toArrayAsync({ threads: 1 });
12156
12274
  }
12157
12275
  function rewriteRoot() {
12158
12276
  return (0, async_1.async_run)(() => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "app-bundler",
3
- "version": "0.2.15",
3
+ "version": "0.2.17",
4
4
  "description": "",
5
5
  "main": "lib/bundler.js",
6
6
  "bin": {
@@ -9,7 +9,8 @@
9
9
  "scripts": {
10
10
  "test": "atma test",
11
11
  "watch": "atma run tools/build --watch",
12
- "build": "atma run tools/build && atma run tools/build --cli",
12
+ "build": "atma run tools/build && atma run tools/build --cli && npm run cp",
13
+ "cp": "copy \"lib\\cli.js\" \"D:\\eth\\0xweb\\node_modules\\app-bundler\\lib\\cli.js\"",
13
14
  "server": "atma server",
14
15
  "release": "echo \"Run atma pub -m \"message\"\""
15
16
  },