app-bundler 0.2.6 → 0.2.7

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 +212 -43
  2. package/lib/cli.js +212 -43
  3. package/package.json +1 -1
package/lib/bundler.js CHANGED
@@ -80,6 +80,7 @@ var _src_handlers_script_copy_js_CopyJsBuilder = {};
80
80
  var _src_handlers_script_copy_js_CopyJsHandler = {};
81
81
  var _src_handlers_script_copy_js_CopyJsParser = {};
82
82
  var _src_handlers_script_copy_js_CopyJsRewriter = {};
83
+ var _src_handlers_script_data_DataInliner = {};
83
84
  var _src_handlers_script_global_js_GlobalJsBuilder = {};
84
85
  var _src_handlers_script_global_js_GlobalJsHandler = {};
85
86
  var _src_handlers_script_global_js_GlobalJsParser = {};
@@ -168,7 +169,7 @@ var _src_utils_path;
168
169
 
169
170
  "use strict";
170
171
  Object.defineProperty(exports, "__esModule", { value: true });
171
- exports.path_toAbsolute = exports.path_changeExtension = exports.path_removeQuery = exports.path_sliceQuery = exports.path_sliceHash = exports.path_combine = exports.path_toRelative = exports.path_isRelative = exports.path_resolveUrl = exports.path_normalize = exports.path_resolveCurrent = exports.path_withProtocol = exports.path_appendQuery = exports.path_getExtension = exports.path_getFile = exports.path_getDir = void 0;
172
+ exports.path_toAbsolute = exports.path_hasExtension = exports.path_changeExtension = exports.path_removeQuery = exports.path_sliceQuery = exports.path_sliceHash = exports.path_combine = exports.path_toRelative = exports.path_isRelative = exports.path_resolveUrl = exports.path_normalize = exports.path_resolveCurrent = exports.path_withProtocol = exports.path_appendQuery = exports.path_getExtension = exports.path_getFile = exports.path_getDir = void 0;
172
173
  let isWeb = true;
173
174
  function path_getDir(path) {
174
175
  return path.substring(0, path.lastIndexOf('/') + 1);
@@ -350,9 +351,16 @@ exports.path_removeQuery = path_removeQuery;
350
351
  ;
351
352
  function path_changeExtension(path, ext) {
352
353
  let str = ext ? '.' + ext : '';
354
+ if (path_hasExtension(path) === false) {
355
+ return `${path}${str}`;
356
+ }
353
357
  return path.replace(/(\.\w+)+$/, str);
354
358
  }
355
359
  exports.path_changeExtension = path_changeExtension;
360
+ function path_hasExtension(path) {
361
+ return /\.\w+$/.test(path);
362
+ }
363
+ exports.path_hasExtension = path_hasExtension;
356
364
  function path_toAbsolute(path, parentLocation, rootLocation) {
357
365
  path = path_normalize(path);
358
366
  if (path_isRelative(path) === false) {
@@ -1085,13 +1093,13 @@ function async_whenAll(mix) {
1085
1093
  }
1086
1094
  exports.async_whenAll = async_whenAll;
1087
1095
  ;
1088
- function async_resolve(...args) {
1089
- return (new atma_utils_1.class_Dfr()).resolve(...args);
1096
+ function async_resolve(result) {
1097
+ return (new atma_utils_1.class_Dfr()).resolve(result);
1090
1098
  }
1091
1099
  exports.async_resolve = async_resolve;
1092
1100
  ;
1093
- function async_reject(...args) {
1094
- return (new atma_utils_1.class_Dfr()).reject(...args);
1101
+ function async_reject(error) {
1102
+ return (new atma_utils_1.class_Dfr()).reject(error);
1095
1103
  }
1096
1104
  exports.async_reject = async_reject;
1097
1105
  ;
@@ -1100,7 +1108,7 @@ function async_run(fn) {
1100
1108
  var result = fn();
1101
1109
  if (result && result.then)
1102
1110
  return result;
1103
- return async_resolve();
1111
+ return async_resolve(null);
1104
1112
  }
1105
1113
  return atma_utils_1.class_Dfr.run((resolve, reject) => {
1106
1114
  fn(resolve, reject);
@@ -2619,19 +2627,21 @@ class SolutionOpts extends SolutionOptsBase {
2619
2627
  outputSources: '{output}/build/{build}',
2620
2628
  outputAssets: '{output}/build/{build}/assets',
2621
2629
  outputShareBase: null,
2630
+ outputExtension: 'js',
2622
2631
  package: {
2623
2632
  module: 'commonjs',
2624
2633
  modules: ['commonjs', 'includejs', 'global', 'import'],
2625
2634
  type: 'module',
2626
2635
  types: ['module', 'bundle'],
2627
2636
  moduleWrapper: 'iif',
2628
- moduleWrappers: ['umd', 'iif', 'script', 'custom'],
2637
+ moduleWrappers: ['umd', 'esm', 'iif', 'script', 'custom'],
2629
2638
  moduleName: '',
2630
2639
  import: null,
2631
2640
  },
2632
2641
  extensions: {
2633
2642
  '': { type: 'js' },
2634
2643
  'js': { type: 'js' },
2644
+ 'mjs': { type: 'js' },
2635
2645
  'es6': { type: 'js' },
2636
2646
  'jsx': { type: 'js' },
2637
2647
  'ts': { type: 'js' },
@@ -3006,6 +3016,7 @@ class OutputResources {
3006
3016
  this.items = [];
3007
3017
  this.resources = [];
3008
3018
  this.pagesInput = {};
3019
+ this.dynamicDependencies = [];
3009
3020
  }
3010
3021
  prepare(resources) {
3011
3022
  this.resources = [];
@@ -3049,6 +3060,22 @@ class OutputResources {
3049
3060
  });
3050
3061
  });
3051
3062
  }
3063
+ addDynamicDependency(dep) {
3064
+ let has = this.dynamicDependencies.some(x => x.url === dep.url);
3065
+ if (has === false) {
3066
+ this.dynamicDependencies.push(dep);
3067
+ }
3068
+ }
3069
+ remove(resource) {
3070
+ for (let i = 0; i < this.items.length; i++) {
3071
+ let item = this.items[i];
3072
+ let itemResource = item.resources.find(x => x.filename === resource.filename);
3073
+ if (itemResource !== null) {
3074
+ let idx = item.resources.indexOf(itemResource);
3075
+ item.resources.splice(idx, 1);
3076
+ }
3077
+ }
3078
+ }
3052
3079
  getForPage(page) {
3053
3080
  return this.items.filter(x => x.page === page).map(x => x.resource);
3054
3081
  }
@@ -4535,13 +4562,14 @@ const Include_1 = _src_class_Include;
4535
4562
  const BaseParser_1 = _src_handlers_base_BaseParser;
4536
4563
  const atma_utils_1 = require("atma-utils");
4537
4564
  class CommonJsParser extends BaseParser_1.BaseParser {
4538
- async transpile(content, resource) {
4539
- if (/\.(yml|json)([^\w]|$)/.test(resource.filename)) {
4540
- let str = typeof content !== 'string' ? JSON.stringify(content, null, ' ') : content;
4541
- str = `module.exports = ${str};`;
4542
- return { content: str };
4543
- }
4544
- }
4565
+ // async transpile(content: any, resource: Resource): Promise<{ content: string; }> {
4566
+ // // we will inline the content instead
4567
+ // if (/\.(yml|json)([^\w]|$)/.test(resource.filename)) {
4568
+ // let str = typeof content !== 'string' ? JSON.stringify(content, null, ' ') : content;
4569
+ // str = `module.exports = ${str};`;
4570
+ // return { content: str };
4571
+ // }
4572
+ // }
4545
4573
  getDependencies(ast, ownerResource) {
4546
4574
  let info = {
4547
4575
  dependencies: []
@@ -4957,6 +4985,8 @@ var __register, __require, require;
4957
4985
  `,
4958
4986
  ESM: `
4959
4987
  // source ./ESM.js
4988
+ $CUSTOM_IMPORTS_CODE$
4989
+
4960
4990
  var module = {
4961
4991
  exports: {}
4962
4992
  };
@@ -5223,7 +5253,7 @@ class ModuleWrapper {
5223
5253
  this.solution = solution;
5224
5254
  }
5225
5255
  wrap(body) {
5226
- var wrapper = this.solution.opts.package.moduleWrapper;
5256
+ let wrapper = this.solution.opts.package.moduleWrapper;
5227
5257
  switch (wrapper) {
5228
5258
  case 'script':
5229
5259
  body = this.wrapSimple(body);
@@ -5256,18 +5286,43 @@ class ModuleWrapper {
5256
5286
  .replace('%BUNDLE%', () => body);
5257
5287
  }
5258
5288
  wrapWithUMD(body) {
5259
- var opts = this.solution.opts.package;
5260
- var name = opts.moduleName || '';
5289
+ let opts = this.solution.opts.package;
5290
+ let name = opts.moduleName || '';
5261
5291
  return Templates_1.Templates
5262
5292
  .UMD
5263
5293
  .replace('%MODULE%', () => body)
5264
5294
  .replace('%NAME%', () => name);
5265
5295
  }
5266
5296
  wrapWithESM(body) {
5267
- var opts = this.solution.opts.package;
5268
- var exportsCode = opts.moduleExportsCode || '';
5297
+ let opts = this.solution.opts.package;
5298
+ let exportsCode = opts.moduleExportsCode || '';
5299
+ let importsCode = opts.moduleImportsCode || '';
5300
+ let deps = this.solution.outputResources.dynamicDependencies;
5301
+ if (deps.length > 0) {
5302
+ let arr = deps.map(dep => {
5303
+ let key = `bundlerDynamic_${dep.url.replace(/[^\w]/g, '_')}`;
5304
+ return {
5305
+ url: dep.url,
5306
+ key
5307
+ };
5308
+ });
5309
+ let strImports = arr.map(item => `import ${item.key} from '${item.url}';`).join('\n');
5310
+ let strCollection = `const bundlerDynamicRepository = {};\n`
5311
+ + arr.map(item => `bundlerDynamicRepository['${item.url}'] = ${item.key};`)
5312
+ .join('\n');
5313
+ let strRequire = [
5314
+ `var bundlerOriginalRequire = require || function (url) { throw new Error('Not found: ' + url); };`,
5315
+ `var require = function (url) { return bundlerDynamicRepository[url] || bundlerOriginalRequire(url); };`
5316
+ ].join('\n');
5317
+ importsCode = [
5318
+ strImports,
5319
+ strCollection,
5320
+ strRequire
5321
+ ].join('\n');
5322
+ }
5269
5323
  return Templates_1.Templates
5270
5324
  .ESM
5325
+ .replace('$CUSTOM_IMPORTS_CODE$', () => importsCode)
5271
5326
  .replace('$CUSTOM_EXPORTS_CODE$', () => exportsCode)
5272
5327
  .replace('$MODULE$', () => body);
5273
5328
  }
@@ -6830,13 +6885,13 @@ const ResourceInfo_1 = _src_class_ResourceInfo;
6830
6885
  let Rgx = {
6831
6886
  Imports: {
6832
6887
  full: {
6833
- rgx: /^[ \t]*import\s*['"]([^'"]+)['"][\t ;]*[\r\n]{0,2}/gm,
6888
+ rgx: /^[ \t]*import\s*(\*\s+as\s+\w+\s+from\s+)?['"](?<path>[^'"]+)['"][\t ;]*[\r\n]{0,2}/gm,
6834
6889
  map(match) {
6835
6890
  let $import = new ResourceInfo_1.ImportNode();
6836
6891
  $import.position = match.index;
6837
6892
  $import.length = match[0].length;
6838
6893
  $import.type = 'full';
6839
- $import.path = match[1];
6894
+ $import.path = match.groups.path;
6840
6895
  return $import;
6841
6896
  }
6842
6897
  },
@@ -6852,6 +6907,17 @@ let Rgx = {
6852
6907
  return $import;
6853
6908
  }
6854
6909
  },
6910
+ defaults: {
6911
+ rgx: /^[ \t]*import\s+(?<def>\w+)\s+from\s*['"](?<path>[^'"]+)['"][\t ;]*[\r\n]{0,2}/gm,
6912
+ map(match) {
6913
+ let $import = new ResourceInfo_1.ImportNode();
6914
+ $import.position = match.index;
6915
+ $import.length = match[0].length;
6916
+ $import.type = 'full';
6917
+ $import.path = match.groups.path;
6918
+ return $import;
6919
+ }
6920
+ },
6855
6921
  exportAll: {
6856
6922
  rgx: /^[ \t]*export\s+\*\s+from\s*['"]([^'"]+)['"][\t ;]*[\r\n]{0,2}/gm,
6857
6923
  map(match) {
@@ -7035,17 +7101,18 @@ class ImportJsParser extends BaseParser_1.BaseParser {
7035
7101
  /** @TODO: set to false and handle ast in ScriptParser */
7036
7102
  this.asText = true;
7037
7103
  }
7038
- async transpile(content, resource) {
7039
- if (/\.(yml|json)([^\w]|$)/.test(resource.filename)) {
7040
- let str = typeof content !== 'string' ? JSON.stringify(content, null, ' ') : content;
7041
- str = [
7042
- `const data = ${str};`,
7043
- `export default data;`
7044
- ].join('\n');
7045
- return { content: str };
7046
- }
7047
- }
7048
- getDependencies(content, ownerResource) {
7104
+ // async transpile(content: any, resource: Resource): Promise<{ content: string; }> {
7105
+ // // we will inline the content instead
7106
+ // if (/\.(yml|json)([^\w]|$)/.test(resource.filename)) {
7107
+ // let str = typeof content !== 'string' ? JSON.stringify(content, null, ' ') : content;
7108
+ // str = [
7109
+ // `const data = ${str};`,
7110
+ // `export default data;`
7111
+ // ].join('\n');
7112
+ // return { content: str };
7113
+ // }
7114
+ // }
7115
+ async getDependencies(content, ownerResource) {
7049
7116
  if (Parser_1.Parser.supports(content) === false) {
7050
7117
  return null;
7051
7118
  }
@@ -7704,6 +7771,10 @@ class ScriptParser extends BaseParser_1.BaseParser {
7704
7771
  if (!content) {
7705
7772
  throw new Error(`Content is undefined for ${ownerResource.filename}`);
7706
7773
  }
7774
+ if (typeof content !== 'string') {
7775
+ // The data content is loaded as json
7776
+ return Promise.resolve({ dependencies: [] });
7777
+ }
7707
7778
  let opts = {
7708
7779
  filename: ownerResource.filename
7709
7780
  };
@@ -7763,6 +7834,80 @@ exports.ScriptParser = ScriptParser;
7763
7834
  // end:source ./ModuleSimplified.js
7764
7835
 
7765
7836
 
7837
+ // source ./ModuleSimplified.js
7838
+ var _src_handlers_script_data_DataInliner;
7839
+ (function () {
7840
+ // ensure AMD is not active for the model, so that any UMD exports as commonjs
7841
+ var define = null;
7842
+ var exports = _src_handlers_script_data_DataInliner != null ? _src_handlers_script_data_DataInliner : {};
7843
+ var module = { exports: exports };
7844
+
7845
+ "use strict";
7846
+ Object.defineProperty(exports, "__esModule", { value: true });
7847
+ exports.DataInliner = void 0;
7848
+ const BaseRewriter_1 = _src_handlers_base_BaseRewriter;
7849
+ class DataInliner extends BaseRewriter_1.BaseRewriter {
7850
+ rewriteRoot(resourceInput, resourceOutput) {
7851
+ }
7852
+ rewritePartial(content, ownerResource) {
7853
+ }
7854
+ async rewriteResource(resource) {
7855
+ for (let res of resource.dependencies) {
7856
+ if (/(json|yml)$/.test(res.url) === false) {
7857
+ continue;
7858
+ }
7859
+ if (res.import != null) {
7860
+ await this.rewriteImport(resource, res);
7861
+ continue;
7862
+ }
7863
+ console.error('CJS not implemented');
7864
+ }
7865
+ }
7866
+ accepts(type) {
7867
+ return type === 'js';
7868
+ }
7869
+ rewriteImport(resource, dep) {
7870
+ let rgx = new RegExp(`import\\s+(?<name>[\\w_$]+)\\s+from\\s+['"]${dep.url}['"];?`);
7871
+ let match = rgx.exec(resource.content);
7872
+ if (match == null) {
7873
+ console.error(`No data import found for ${dep.url} in ${resource.filename}`);
7874
+ return;
7875
+ }
7876
+ let name = match.groups.name;
7877
+ let json = dep.resource.content;
7878
+ if (typeof json !== 'string') {
7879
+ json = JSON.stringify(json);
7880
+ }
7881
+ let code = `const ${name} = ${json}`;
7882
+ let content = resource.content.replace(match[0], code);
7883
+ resource.content = content;
7884
+ let i = resource.dependencies.indexOf(dep);
7885
+ resource.dependencies.splice(i, 1);
7886
+ this.solution.outputResources.remove(dep.resource);
7887
+ }
7888
+ }
7889
+ exports.DataInliner = DataInliner;
7890
+ ;
7891
+ //# sourceMappingURL=DataInliner.js.map
7892
+ //# sourceMappingURL=DataInliner.ts.map;
7893
+
7894
+ function __isObj(x) {
7895
+ return x != null && typeof x === 'object' && x.constructor === Object;
7896
+ }
7897
+ if (_src_handlers_script_data_DataInliner === module.exports) {
7898
+ // do nothing if
7899
+ } else if (__isObj(_src_handlers_script_data_DataInliner) && __isObj(module.exports)) {
7900
+ Object.assign(_src_handlers_script_data_DataInliner, module.exports);
7901
+ } else {
7902
+ _src_handlers_script_data_DataInliner = module.exports;
7903
+ }
7904
+
7905
+ ;
7906
+ }());
7907
+
7908
+ // end:source ./ModuleSimplified.js
7909
+
7910
+
7766
7911
  // source ./ModuleSimplified.js
7767
7912
  var _src_handlers_script_ScriptRewriter;
7768
7913
  (function () {
@@ -7776,18 +7921,20 @@ Object.defineProperty(exports, "__esModule", { value: true });
7776
7921
  exports.ScriptRewriter = void 0;
7777
7922
  const IncludeJsHandler_1 = _src_handlers_script_include_js_IncludeJsHandler;
7778
7923
  const BaseRewriter_1 = _src_handlers_base_BaseRewriter;
7924
+ const DataInliner_1 = _src_handlers_script_data_DataInliner;
7779
7925
  class ScriptRewriter extends BaseRewriter_1.BaseRewriter {
7780
7926
  constructor() {
7781
7927
  super(...arguments);
7782
7928
  this.rewriters = [
7783
- new IncludeJsHandler_1.IncludeJsHandler.Rewriter(this.solution, this.handler)
7929
+ new IncludeJsHandler_1.IncludeJsHandler.Rewriter(this.solution, this.handler),
7930
+ new DataInliner_1.DataInliner(this.solution, this.handler),
7784
7931
  ];
7785
7932
  }
7786
7933
  rewritePartial(content, ownerResource) {
7787
7934
  }
7788
7935
  rewriteResource(resource) {
7789
- this.rewriters.forEach(rewriter => {
7790
- rewriter.rewriteResource(resource);
7936
+ return this.rewriters.map(rewriter => {
7937
+ return rewriter.rewriteResource(resource);
7791
7938
  });
7792
7939
  }
7793
7940
  accepts(type) {
@@ -8392,6 +8539,9 @@ class CopyJsBuilder extends BaseScriptBuilder_1.BaseScriptBuilder {
8392
8539
  return;
8393
8540
  }
8394
8541
  if (dep.resource.filename.startsWith(appBase) === false) {
8542
+ // if (path_hasExtension(dep.url) === false) {
8543
+ // rewriteToRelativeUrls[dep.url] = dep.url + '.' + this.solution.opts.outputExtension;
8544
+ // }
8395
8545
  // is project dependency
8396
8546
  return;
8397
8547
  }
@@ -8399,17 +8549,17 @@ class CopyJsBuilder extends BaseScriptBuilder_1.BaseScriptBuilder {
8399
8549
  if (relativePath == null || /(..\/)*?\/?node_modules/.test(relativePath)) {
8400
8550
  return;
8401
8551
  }
8402
- relativePath = (0, path_1.path_changeExtension)(relativePath, null);
8552
+ relativePath = (0, path_1.path_changeExtension)(relativePath, this.solution.opts.outputExtension);
8403
8553
  relativePath = relativePath.startsWith('.')
8404
8554
  ? relativePath
8405
8555
  : './' + relativePath;
8406
8556
  rewriteToRelativeUrls[dep.url] = relativePath;
8407
8557
  })
8408
8558
  .toArray();
8409
- resource.content = rewriteUrls(resource.content, rewriteToRelativeUrls);
8559
+ resource.content = rewriteUrls(resource, rewriteToRelativeUrls);
8410
8560
  }
8411
8561
  let result = [
8412
- createOutputItem(cloneWithExtension(resource, 'js'))
8562
+ createOutputItem(cloneWithExtension(resource, this.solution.opts.outputExtension))
8413
8563
  ];
8414
8564
  let extra = [{
8415
8565
  ext: 'ts.map',
@@ -8421,8 +8571,14 @@ class CopyJsBuilder extends BaseScriptBuilder_1.BaseScriptBuilder {
8421
8571
  let readFile = Configuration_1.Configuration.Instance.get('readFile');
8422
8572
  await (0, alot_1.default)(extra).forEachAsync(async (x) => {
8423
8573
  let cloned = cloneWithExtension(resource, x.ext);
8424
- cloned.content = await readFile(cloned.filename);
8425
- cloned.content = rewriteUrls(cloned.content, rewriteToRelativeUrls);
8574
+ try {
8575
+ cloned.content = await readFile(cloned.filename);
8576
+ }
8577
+ catch (e) {
8578
+ console.error(`Error loading ${resource.filename} from ${parent.resource.filename}`);
8579
+ throw e;
8580
+ }
8581
+ cloned.content = rewriteUrls(cloned, rewriteToRelativeUrls);
8426
8582
  result.push(createOutputItem(cloneWithExtension(cloned, x.output)));
8427
8583
  if (x.ext === 'ts.map' && cloned.content != null) {
8428
8584
  let outputItem = result[result.length - 1];
@@ -8449,9 +8605,16 @@ class CopyJsBuilder extends BaseScriptBuilder_1.BaseScriptBuilder {
8449
8605
  cloned.url = (0, path_1.path_changeExtension)(cloned.url, ext);
8450
8606
  return cloned;
8451
8607
  }
8452
- function rewriteUrls(content, rewriteToRelativeUrls) {
8608
+ function rewriteUrls(resource, rewriteToRelativeUrls) {
8609
+ let content = resource.content;
8610
+ if (content == null) {
8611
+ console.error(`<rewrite urls> No content for ${resource.filename}`);
8612
+ return '';
8613
+ }
8453
8614
  for (let key in rewriteToRelativeUrls) {
8454
- content = content.replaceAll(key, rewriteToRelativeUrls[key]);
8615
+ content = content
8616
+ .replaceAll(`"${key}"`, `"${rewriteToRelativeUrls[key]}"`)
8617
+ .replaceAll(`'${key}'`, `'${rewriteToRelativeUrls[key]}'`);
8455
8618
  }
8456
8619
  return content;
8457
8620
  }
@@ -11414,7 +11577,13 @@ function getDependenciesExternal(deps, resource, solution) {
11414
11577
  .then(() => deps);
11415
11578
  }
11416
11579
  function filterDynamicDeps(info, solution) {
11417
- info.dependencies = info.dependencies.filter(dep => isDynamicDependency(dep, solution) === false);
11580
+ info.dependencies = info.dependencies.filter(dep => {
11581
+ let isDynamic = isDynamicDependency(dep, solution);
11582
+ if (isDynamic) {
11583
+ solution.outputResources.addDynamicDependency(dep);
11584
+ }
11585
+ return isDynamic === false;
11586
+ });
11418
11587
  return info;
11419
11588
  }
11420
11589
  function isDynamicDependency(dep, solution) {
package/lib/cli.js CHANGED
@@ -82,6 +82,7 @@ var _src_handlers_script_copy_js_CopyJsBuilder = {};
82
82
  var _src_handlers_script_copy_js_CopyJsHandler = {};
83
83
  var _src_handlers_script_copy_js_CopyJsParser = {};
84
84
  var _src_handlers_script_copy_js_CopyJsRewriter = {};
85
+ var _src_handlers_script_data_DataInliner = {};
85
86
  var _src_handlers_script_global_js_GlobalJsBuilder = {};
86
87
  var _src_handlers_script_global_js_GlobalJsHandler = {};
87
88
  var _src_handlers_script_global_js_GlobalJsParser = {};
@@ -170,7 +171,7 @@ var _src_utils_path;
170
171
 
171
172
  "use strict";
172
173
  Object.defineProperty(exports, "__esModule", { value: true });
173
- exports.path_toAbsolute = exports.path_changeExtension = exports.path_removeQuery = exports.path_sliceQuery = exports.path_sliceHash = exports.path_combine = exports.path_toRelative = exports.path_isRelative = exports.path_resolveUrl = exports.path_normalize = exports.path_resolveCurrent = exports.path_withProtocol = exports.path_appendQuery = exports.path_getExtension = exports.path_getFile = exports.path_getDir = void 0;
174
+ exports.path_toAbsolute = exports.path_hasExtension = exports.path_changeExtension = exports.path_removeQuery = exports.path_sliceQuery = exports.path_sliceHash = exports.path_combine = exports.path_toRelative = exports.path_isRelative = exports.path_resolveUrl = exports.path_normalize = exports.path_resolveCurrent = exports.path_withProtocol = exports.path_appendQuery = exports.path_getExtension = exports.path_getFile = exports.path_getDir = void 0;
174
175
  let isWeb = true;
175
176
  function path_getDir(path) {
176
177
  return path.substring(0, path.lastIndexOf('/') + 1);
@@ -352,9 +353,16 @@ exports.path_removeQuery = path_removeQuery;
352
353
  ;
353
354
  function path_changeExtension(path, ext) {
354
355
  let str = ext ? '.' + ext : '';
356
+ if (path_hasExtension(path) === false) {
357
+ return `${path}${str}`;
358
+ }
355
359
  return path.replace(/(\.\w+)+$/, str);
356
360
  }
357
361
  exports.path_changeExtension = path_changeExtension;
362
+ function path_hasExtension(path) {
363
+ return /\.\w+$/.test(path);
364
+ }
365
+ exports.path_hasExtension = path_hasExtension;
358
366
  function path_toAbsolute(path, parentLocation, rootLocation) {
359
367
  path = path_normalize(path);
360
368
  if (path_isRelative(path) === false) {
@@ -1087,13 +1095,13 @@ function async_whenAll(mix) {
1087
1095
  }
1088
1096
  exports.async_whenAll = async_whenAll;
1089
1097
  ;
1090
- function async_resolve(...args) {
1091
- return (new atma_utils_1.class_Dfr()).resolve(...args);
1098
+ function async_resolve(result) {
1099
+ return (new atma_utils_1.class_Dfr()).resolve(result);
1092
1100
  }
1093
1101
  exports.async_resolve = async_resolve;
1094
1102
  ;
1095
- function async_reject(...args) {
1096
- return (new atma_utils_1.class_Dfr()).reject(...args);
1103
+ function async_reject(error) {
1104
+ return (new atma_utils_1.class_Dfr()).reject(error);
1097
1105
  }
1098
1106
  exports.async_reject = async_reject;
1099
1107
  ;
@@ -1102,7 +1110,7 @@ function async_run(fn) {
1102
1110
  var result = fn();
1103
1111
  if (result && result.then)
1104
1112
  return result;
1105
- return async_resolve();
1113
+ return async_resolve(null);
1106
1114
  }
1107
1115
  return atma_utils_1.class_Dfr.run((resolve, reject) => {
1108
1116
  fn(resolve, reject);
@@ -2621,19 +2629,21 @@ class SolutionOpts extends SolutionOptsBase {
2621
2629
  outputSources: '{output}/build/{build}',
2622
2630
  outputAssets: '{output}/build/{build}/assets',
2623
2631
  outputShareBase: null,
2632
+ outputExtension: 'js',
2624
2633
  package: {
2625
2634
  module: 'commonjs',
2626
2635
  modules: ['commonjs', 'includejs', 'global', 'import'],
2627
2636
  type: 'module',
2628
2637
  types: ['module', 'bundle'],
2629
2638
  moduleWrapper: 'iif',
2630
- moduleWrappers: ['umd', 'iif', 'script', 'custom'],
2639
+ moduleWrappers: ['umd', 'esm', 'iif', 'script', 'custom'],
2631
2640
  moduleName: '',
2632
2641
  import: null,
2633
2642
  },
2634
2643
  extensions: {
2635
2644
  '': { type: 'js' },
2636
2645
  'js': { type: 'js' },
2646
+ 'mjs': { type: 'js' },
2637
2647
  'es6': { type: 'js' },
2638
2648
  'jsx': { type: 'js' },
2639
2649
  'ts': { type: 'js' },
@@ -3008,6 +3018,7 @@ class OutputResources {
3008
3018
  this.items = [];
3009
3019
  this.resources = [];
3010
3020
  this.pagesInput = {};
3021
+ this.dynamicDependencies = [];
3011
3022
  }
3012
3023
  prepare(resources) {
3013
3024
  this.resources = [];
@@ -3051,6 +3062,22 @@ class OutputResources {
3051
3062
  });
3052
3063
  });
3053
3064
  }
3065
+ addDynamicDependency(dep) {
3066
+ let has = this.dynamicDependencies.some(x => x.url === dep.url);
3067
+ if (has === false) {
3068
+ this.dynamicDependencies.push(dep);
3069
+ }
3070
+ }
3071
+ remove(resource) {
3072
+ for (let i = 0; i < this.items.length; i++) {
3073
+ let item = this.items[i];
3074
+ let itemResource = item.resources.find(x => x.filename === resource.filename);
3075
+ if (itemResource !== null) {
3076
+ let idx = item.resources.indexOf(itemResource);
3077
+ item.resources.splice(idx, 1);
3078
+ }
3079
+ }
3080
+ }
3054
3081
  getForPage(page) {
3055
3082
  return this.items.filter(x => x.page === page).map(x => x.resource);
3056
3083
  }
@@ -4537,13 +4564,14 @@ const Include_1 = _src_class_Include;
4537
4564
  const BaseParser_1 = _src_handlers_base_BaseParser;
4538
4565
  const atma_utils_1 = require("atma-utils");
4539
4566
  class CommonJsParser extends BaseParser_1.BaseParser {
4540
- async transpile(content, resource) {
4541
- if (/\.(yml|json)([^\w]|$)/.test(resource.filename)) {
4542
- let str = typeof content !== 'string' ? JSON.stringify(content, null, ' ') : content;
4543
- str = `module.exports = ${str};`;
4544
- return { content: str };
4545
- }
4546
- }
4567
+ // async transpile(content: any, resource: Resource): Promise<{ content: string; }> {
4568
+ // // we will inline the content instead
4569
+ // if (/\.(yml|json)([^\w]|$)/.test(resource.filename)) {
4570
+ // let str = typeof content !== 'string' ? JSON.stringify(content, null, ' ') : content;
4571
+ // str = `module.exports = ${str};`;
4572
+ // return { content: str };
4573
+ // }
4574
+ // }
4547
4575
  getDependencies(ast, ownerResource) {
4548
4576
  let info = {
4549
4577
  dependencies: []
@@ -4959,6 +4987,8 @@ var __register, __require, require;
4959
4987
  `,
4960
4988
  ESM: `
4961
4989
  // source ./ESM.js
4990
+ $CUSTOM_IMPORTS_CODE$
4991
+
4962
4992
  var module = {
4963
4993
  exports: {}
4964
4994
  };
@@ -5225,7 +5255,7 @@ class ModuleWrapper {
5225
5255
  this.solution = solution;
5226
5256
  }
5227
5257
  wrap(body) {
5228
- var wrapper = this.solution.opts.package.moduleWrapper;
5258
+ let wrapper = this.solution.opts.package.moduleWrapper;
5229
5259
  switch (wrapper) {
5230
5260
  case 'script':
5231
5261
  body = this.wrapSimple(body);
@@ -5258,18 +5288,43 @@ class ModuleWrapper {
5258
5288
  .replace('%BUNDLE%', () => body);
5259
5289
  }
5260
5290
  wrapWithUMD(body) {
5261
- var opts = this.solution.opts.package;
5262
- var name = opts.moduleName || '';
5291
+ let opts = this.solution.opts.package;
5292
+ let name = opts.moduleName || '';
5263
5293
  return Templates_1.Templates
5264
5294
  .UMD
5265
5295
  .replace('%MODULE%', () => body)
5266
5296
  .replace('%NAME%', () => name);
5267
5297
  }
5268
5298
  wrapWithESM(body) {
5269
- var opts = this.solution.opts.package;
5270
- var exportsCode = opts.moduleExportsCode || '';
5299
+ let opts = this.solution.opts.package;
5300
+ let exportsCode = opts.moduleExportsCode || '';
5301
+ let importsCode = opts.moduleImportsCode || '';
5302
+ let deps = this.solution.outputResources.dynamicDependencies;
5303
+ if (deps.length > 0) {
5304
+ let arr = deps.map(dep => {
5305
+ let key = `bundlerDynamic_${dep.url.replace(/[^\w]/g, '_')}`;
5306
+ return {
5307
+ url: dep.url,
5308
+ key
5309
+ };
5310
+ });
5311
+ let strImports = arr.map(item => `import ${item.key} from '${item.url}';`).join('\n');
5312
+ let strCollection = `const bundlerDynamicRepository = {};\n`
5313
+ + arr.map(item => `bundlerDynamicRepository['${item.url}'] = ${item.key};`)
5314
+ .join('\n');
5315
+ let strRequire = [
5316
+ `var bundlerOriginalRequire = require || function (url) { throw new Error('Not found: ' + url); };`,
5317
+ `var require = function (url) { return bundlerDynamicRepository[url] || bundlerOriginalRequire(url); };`
5318
+ ].join('\n');
5319
+ importsCode = [
5320
+ strImports,
5321
+ strCollection,
5322
+ strRequire
5323
+ ].join('\n');
5324
+ }
5271
5325
  return Templates_1.Templates
5272
5326
  .ESM
5327
+ .replace('$CUSTOM_IMPORTS_CODE$', () => importsCode)
5273
5328
  .replace('$CUSTOM_EXPORTS_CODE$', () => exportsCode)
5274
5329
  .replace('$MODULE$', () => body);
5275
5330
  }
@@ -6832,13 +6887,13 @@ const ResourceInfo_1 = _src_class_ResourceInfo;
6832
6887
  let Rgx = {
6833
6888
  Imports: {
6834
6889
  full: {
6835
- rgx: /^[ \t]*import\s*['"]([^'"]+)['"][\t ;]*[\r\n]{0,2}/gm,
6890
+ rgx: /^[ \t]*import\s*(\*\s+as\s+\w+\s+from\s+)?['"](?<path>[^'"]+)['"][\t ;]*[\r\n]{0,2}/gm,
6836
6891
  map(match) {
6837
6892
  let $import = new ResourceInfo_1.ImportNode();
6838
6893
  $import.position = match.index;
6839
6894
  $import.length = match[0].length;
6840
6895
  $import.type = 'full';
6841
- $import.path = match[1];
6896
+ $import.path = match.groups.path;
6842
6897
  return $import;
6843
6898
  }
6844
6899
  },
@@ -6854,6 +6909,17 @@ let Rgx = {
6854
6909
  return $import;
6855
6910
  }
6856
6911
  },
6912
+ defaults: {
6913
+ rgx: /^[ \t]*import\s+(?<def>\w+)\s+from\s*['"](?<path>[^'"]+)['"][\t ;]*[\r\n]{0,2}/gm,
6914
+ map(match) {
6915
+ let $import = new ResourceInfo_1.ImportNode();
6916
+ $import.position = match.index;
6917
+ $import.length = match[0].length;
6918
+ $import.type = 'full';
6919
+ $import.path = match.groups.path;
6920
+ return $import;
6921
+ }
6922
+ },
6857
6923
  exportAll: {
6858
6924
  rgx: /^[ \t]*export\s+\*\s+from\s*['"]([^'"]+)['"][\t ;]*[\r\n]{0,2}/gm,
6859
6925
  map(match) {
@@ -7037,17 +7103,18 @@ class ImportJsParser extends BaseParser_1.BaseParser {
7037
7103
  /** @TODO: set to false and handle ast in ScriptParser */
7038
7104
  this.asText = true;
7039
7105
  }
7040
- async transpile(content, resource) {
7041
- if (/\.(yml|json)([^\w]|$)/.test(resource.filename)) {
7042
- let str = typeof content !== 'string' ? JSON.stringify(content, null, ' ') : content;
7043
- str = [
7044
- `const data = ${str};`,
7045
- `export default data;`
7046
- ].join('\n');
7047
- return { content: str };
7048
- }
7049
- }
7050
- getDependencies(content, ownerResource) {
7106
+ // async transpile(content: any, resource: Resource): Promise<{ content: string; }> {
7107
+ // // we will inline the content instead
7108
+ // if (/\.(yml|json)([^\w]|$)/.test(resource.filename)) {
7109
+ // let str = typeof content !== 'string' ? JSON.stringify(content, null, ' ') : content;
7110
+ // str = [
7111
+ // `const data = ${str};`,
7112
+ // `export default data;`
7113
+ // ].join('\n');
7114
+ // return { content: str };
7115
+ // }
7116
+ // }
7117
+ async getDependencies(content, ownerResource) {
7051
7118
  if (Parser_1.Parser.supports(content) === false) {
7052
7119
  return null;
7053
7120
  }
@@ -7706,6 +7773,10 @@ class ScriptParser extends BaseParser_1.BaseParser {
7706
7773
  if (!content) {
7707
7774
  throw new Error(`Content is undefined for ${ownerResource.filename}`);
7708
7775
  }
7776
+ if (typeof content !== 'string') {
7777
+ // The data content is loaded as json
7778
+ return Promise.resolve({ dependencies: [] });
7779
+ }
7709
7780
  let opts = {
7710
7781
  filename: ownerResource.filename
7711
7782
  };
@@ -7765,6 +7836,80 @@ exports.ScriptParser = ScriptParser;
7765
7836
  // end:source ./ModuleSimplified.js
7766
7837
 
7767
7838
 
7839
+ // source ./ModuleSimplified.js
7840
+ var _src_handlers_script_data_DataInliner;
7841
+ (function () {
7842
+ // ensure AMD is not active for the model, so that any UMD exports as commonjs
7843
+ var define = null;
7844
+ var exports = _src_handlers_script_data_DataInliner != null ? _src_handlers_script_data_DataInliner : {};
7845
+ var module = { exports: exports };
7846
+
7847
+ "use strict";
7848
+ Object.defineProperty(exports, "__esModule", { value: true });
7849
+ exports.DataInliner = void 0;
7850
+ const BaseRewriter_1 = _src_handlers_base_BaseRewriter;
7851
+ class DataInliner extends BaseRewriter_1.BaseRewriter {
7852
+ rewriteRoot(resourceInput, resourceOutput) {
7853
+ }
7854
+ rewritePartial(content, ownerResource) {
7855
+ }
7856
+ async rewriteResource(resource) {
7857
+ for (let res of resource.dependencies) {
7858
+ if (/(json|yml)$/.test(res.url) === false) {
7859
+ continue;
7860
+ }
7861
+ if (res.import != null) {
7862
+ await this.rewriteImport(resource, res);
7863
+ continue;
7864
+ }
7865
+ console.error('CJS not implemented');
7866
+ }
7867
+ }
7868
+ accepts(type) {
7869
+ return type === 'js';
7870
+ }
7871
+ rewriteImport(resource, dep) {
7872
+ let rgx = new RegExp(`import\\s+(?<name>[\\w_$]+)\\s+from\\s+['"]${dep.url}['"];?`);
7873
+ let match = rgx.exec(resource.content);
7874
+ if (match == null) {
7875
+ console.error(`No data import found for ${dep.url} in ${resource.filename}`);
7876
+ return;
7877
+ }
7878
+ let name = match.groups.name;
7879
+ let json = dep.resource.content;
7880
+ if (typeof json !== 'string') {
7881
+ json = JSON.stringify(json);
7882
+ }
7883
+ let code = `const ${name} = ${json}`;
7884
+ let content = resource.content.replace(match[0], code);
7885
+ resource.content = content;
7886
+ let i = resource.dependencies.indexOf(dep);
7887
+ resource.dependencies.splice(i, 1);
7888
+ this.solution.outputResources.remove(dep.resource);
7889
+ }
7890
+ }
7891
+ exports.DataInliner = DataInliner;
7892
+ ;
7893
+ //# sourceMappingURL=DataInliner.js.map
7894
+ //# sourceMappingURL=DataInliner.ts.map;
7895
+
7896
+ function __isObj(x) {
7897
+ return x != null && typeof x === 'object' && x.constructor === Object;
7898
+ }
7899
+ if (_src_handlers_script_data_DataInliner === module.exports) {
7900
+ // do nothing if
7901
+ } else if (__isObj(_src_handlers_script_data_DataInliner) && __isObj(module.exports)) {
7902
+ Object.assign(_src_handlers_script_data_DataInliner, module.exports);
7903
+ } else {
7904
+ _src_handlers_script_data_DataInliner = module.exports;
7905
+ }
7906
+
7907
+ ;
7908
+ }());
7909
+
7910
+ // end:source ./ModuleSimplified.js
7911
+
7912
+
7768
7913
  // source ./ModuleSimplified.js
7769
7914
  var _src_handlers_script_ScriptRewriter;
7770
7915
  (function () {
@@ -7778,18 +7923,20 @@ Object.defineProperty(exports, "__esModule", { value: true });
7778
7923
  exports.ScriptRewriter = void 0;
7779
7924
  const IncludeJsHandler_1 = _src_handlers_script_include_js_IncludeJsHandler;
7780
7925
  const BaseRewriter_1 = _src_handlers_base_BaseRewriter;
7926
+ const DataInliner_1 = _src_handlers_script_data_DataInliner;
7781
7927
  class ScriptRewriter extends BaseRewriter_1.BaseRewriter {
7782
7928
  constructor() {
7783
7929
  super(...arguments);
7784
7930
  this.rewriters = [
7785
- new IncludeJsHandler_1.IncludeJsHandler.Rewriter(this.solution, this.handler)
7931
+ new IncludeJsHandler_1.IncludeJsHandler.Rewriter(this.solution, this.handler),
7932
+ new DataInliner_1.DataInliner(this.solution, this.handler),
7786
7933
  ];
7787
7934
  }
7788
7935
  rewritePartial(content, ownerResource) {
7789
7936
  }
7790
7937
  rewriteResource(resource) {
7791
- this.rewriters.forEach(rewriter => {
7792
- rewriter.rewriteResource(resource);
7938
+ return this.rewriters.map(rewriter => {
7939
+ return rewriter.rewriteResource(resource);
7793
7940
  });
7794
7941
  }
7795
7942
  accepts(type) {
@@ -8394,6 +8541,9 @@ class CopyJsBuilder extends BaseScriptBuilder_1.BaseScriptBuilder {
8394
8541
  return;
8395
8542
  }
8396
8543
  if (dep.resource.filename.startsWith(appBase) === false) {
8544
+ // if (path_hasExtension(dep.url) === false) {
8545
+ // rewriteToRelativeUrls[dep.url] = dep.url + '.' + this.solution.opts.outputExtension;
8546
+ // }
8397
8547
  // is project dependency
8398
8548
  return;
8399
8549
  }
@@ -8401,17 +8551,17 @@ class CopyJsBuilder extends BaseScriptBuilder_1.BaseScriptBuilder {
8401
8551
  if (relativePath == null || /(..\/)*?\/?node_modules/.test(relativePath)) {
8402
8552
  return;
8403
8553
  }
8404
- relativePath = (0, path_1.path_changeExtension)(relativePath, null);
8554
+ relativePath = (0, path_1.path_changeExtension)(relativePath, this.solution.opts.outputExtension);
8405
8555
  relativePath = relativePath.startsWith('.')
8406
8556
  ? relativePath
8407
8557
  : './' + relativePath;
8408
8558
  rewriteToRelativeUrls[dep.url] = relativePath;
8409
8559
  })
8410
8560
  .toArray();
8411
- resource.content = rewriteUrls(resource.content, rewriteToRelativeUrls);
8561
+ resource.content = rewriteUrls(resource, rewriteToRelativeUrls);
8412
8562
  }
8413
8563
  let result = [
8414
- createOutputItem(cloneWithExtension(resource, 'js'))
8564
+ createOutputItem(cloneWithExtension(resource, this.solution.opts.outputExtension))
8415
8565
  ];
8416
8566
  let extra = [{
8417
8567
  ext: 'ts.map',
@@ -8423,8 +8573,14 @@ class CopyJsBuilder extends BaseScriptBuilder_1.BaseScriptBuilder {
8423
8573
  let readFile = Configuration_1.Configuration.Instance.get('readFile');
8424
8574
  await (0, alot_1.default)(extra).forEachAsync(async (x) => {
8425
8575
  let cloned = cloneWithExtension(resource, x.ext);
8426
- cloned.content = await readFile(cloned.filename);
8427
- cloned.content = rewriteUrls(cloned.content, rewriteToRelativeUrls);
8576
+ try {
8577
+ cloned.content = await readFile(cloned.filename);
8578
+ }
8579
+ catch (e) {
8580
+ console.error(`Error loading ${resource.filename} from ${parent.resource.filename}`);
8581
+ throw e;
8582
+ }
8583
+ cloned.content = rewriteUrls(cloned, rewriteToRelativeUrls);
8428
8584
  result.push(createOutputItem(cloneWithExtension(cloned, x.output)));
8429
8585
  if (x.ext === 'ts.map' && cloned.content != null) {
8430
8586
  let outputItem = result[result.length - 1];
@@ -8451,9 +8607,16 @@ class CopyJsBuilder extends BaseScriptBuilder_1.BaseScriptBuilder {
8451
8607
  cloned.url = (0, path_1.path_changeExtension)(cloned.url, ext);
8452
8608
  return cloned;
8453
8609
  }
8454
- function rewriteUrls(content, rewriteToRelativeUrls) {
8610
+ function rewriteUrls(resource, rewriteToRelativeUrls) {
8611
+ let content = resource.content;
8612
+ if (content == null) {
8613
+ console.error(`<rewrite urls> No content for ${resource.filename}`);
8614
+ return '';
8615
+ }
8455
8616
  for (let key in rewriteToRelativeUrls) {
8456
- content = content.replaceAll(key, rewriteToRelativeUrls[key]);
8617
+ content = content
8618
+ .replaceAll(`"${key}"`, `"${rewriteToRelativeUrls[key]}"`)
8619
+ .replaceAll(`'${key}'`, `'${rewriteToRelativeUrls[key]}'`);
8457
8620
  }
8458
8621
  return content;
8459
8622
  }
@@ -11416,7 +11579,13 @@ function getDependenciesExternal(deps, resource, solution) {
11416
11579
  .then(() => deps);
11417
11580
  }
11418
11581
  function filterDynamicDeps(info, solution) {
11419
- info.dependencies = info.dependencies.filter(dep => isDynamicDependency(dep, solution) === false);
11582
+ info.dependencies = info.dependencies.filter(dep => {
11583
+ let isDynamic = isDynamicDependency(dep, solution);
11584
+ if (isDynamic) {
11585
+ solution.outputResources.addDynamicDependency(dep);
11586
+ }
11587
+ return isDynamic === false;
11588
+ });
11420
11589
  return info;
11421
11590
  }
11422
11591
  function isDynamicDependency(dep, solution) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "app-bundler",
3
- "version": "0.2.6",
3
+ "version": "0.2.7",
4
4
  "description": "",
5
5
  "main": "lib/bundler.js",
6
6
  "bin": {