@open-norantec/herbal 1.0.2-alpha.10 → 1.0.2-alpha.12

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.
@@ -35,10 +35,20 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
35
35
  if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
36
36
  }
37
37
  };
38
+ var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
39
+ if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
40
+ if (ar || !(i in from)) {
41
+ if (!ar) ar = Array.prototype.slice.call(from, 0, i);
42
+ ar[i] = from[i];
43
+ }
44
+ }
45
+ return to.concat(ar || Array.prototype.slice.call(from));
46
+ };
38
47
  Object.defineProperty(exports, "__esModule", { value: true });
39
48
  exports.ClientUtil = void 0;
40
49
  var ts = require("typescript");
41
50
  var path = require("node:path");
51
+ var fs = require("fs-extra");
42
52
  var _ = require("lodash");
43
53
  var reflect_declaration_1 = require("../transformers/reflect-declaration");
44
54
  var esbuild = require("esbuild");
@@ -46,11 +56,28 @@ var requireFromString = require("require-from-string");
46
56
  var utilities_1 = require("@open-norantec/utilities");
47
57
  var create_1 = require("../create");
48
58
  var zod_1 = require("zod");
59
+ var es_module_lexer_1 = require("es-module-lexer");
60
+ var babel = require("@babel/core");
61
+ var module = require("node:module");
49
62
  var OPTIONS_SCHEMA = zod_1.z.object({
50
63
  entry: zod_1.z.string(),
51
64
  outputFile: zod_1.z.string(),
52
65
  group: zod_1.z.string().optional(),
53
66
  });
67
+ function maybeESModule(code) {
68
+ return __awaiter(this, void 0, void 0, function () {
69
+ var _a, imports, exports;
70
+ return __generator(this, function (_b) {
71
+ switch (_b.label) {
72
+ case 0: return [4, es_module_lexer_1.init];
73
+ case 1:
74
+ _b.sent();
75
+ _a = (0, es_module_lexer_1.parse)(code), imports = _a[0], exports = _a[1];
76
+ return [2, imports.length > 0 || exports.length > 0];
77
+ }
78
+ });
79
+ });
80
+ }
54
81
  var ClientUtil = (function () {
55
82
  function ClientUtil(inputOptions, onFile, onLog) {
56
83
  this.inputOptions = inputOptions;
@@ -59,12 +86,12 @@ var ClientUtil = (function () {
59
86
  this.options = OPTIONS_SCHEMA.parse(this.inputOptions);
60
87
  }
61
88
  ClientUtil.prototype.generateClientCode = function () {
62
- var _a, _b, _c, _d, _e;
89
+ var _a, _b, _c, _d, _e, _f;
63
90
  return __awaiter(this, void 0, void 0, function () {
64
91
  var configPath, configFile, parsed, program, outputMap, outputFile, esbuildResult, text, client, requireResult, clientCode, absoluteOutputFile;
65
92
  var _this = this;
66
- return __generator(this, function (_f) {
67
- switch (_f.label) {
93
+ return __generator(this, function (_g) {
94
+ switch (_g.label) {
68
95
  case 0:
69
96
  configPath = ts.findConfigFile('.', ts.sys.fileExists, 'tsconfig.json');
70
97
  configFile = ts.readConfigFile(configPath, ts.sys.readFile);
@@ -97,6 +124,10 @@ var ClientUtil = (function () {
97
124
  entryPoints: [path.resolve(outputFile)],
98
125
  bundle: true,
99
126
  platform: 'node',
127
+ loader: {
128
+ '.node': 'base64',
129
+ },
130
+ logLevel: 'silent',
100
131
  format: 'cjs',
101
132
  write: false,
102
133
  plugins: [
@@ -112,33 +143,68 @@ var ClientUtil = (function () {
112
143
  return null;
113
144
  }
114
145
  var resolvedModule = ts.nodeModuleNameResolver(args.path, args.importer, parsed.options || {}, ts.sys).resolvedModule;
115
- if (!resolvedModule) {
146
+ if (!resolvedModule)
116
147
  return null;
117
- }
118
148
  var resolvedFileName = resolvedModule.resolvedFileName;
119
- if (!resolvedFileName || resolvedFileName.endsWith('.d.ts')) {
149
+ if (!resolvedFileName || resolvedFileName.endsWith('.d.ts'))
120
150
  return null;
121
- }
122
151
  var resolved = ts.sys.resolvePath(resolvedFileName);
123
152
  (_b = _this.onLog) === null || _b === void 0 ? void 0 : _b.call(_this, 'info', "Resolved file using TypeScript paths: ".concat(args.path, " -> ").concat(resolved, ")"));
124
- return {
125
- path: resolved,
126
- };
153
+ return { path: resolved };
127
154
  });
128
155
  },
129
156
  },
130
157
  {
131
- name: 'vfs',
158
+ name: 'herbal',
132
159
  setup: function (build) {
133
- build.onResolve({ filter: /.*/ }, function (args) {
134
- var fullPath = path.resolve(path.dirname(args.importer), args.path);
135
- if (!fullPath.endsWith('.js'))
136
- fullPath += '.js';
137
- if (outputMap.has(fullPath)) {
138
- return { path: fullPath, namespace: 'vfs' };
139
- }
140
- return { path: args.path, external: true };
141
- });
160
+ build.onResolve({ filter: /.*/ }, function (args) { return __awaiter(_this, void 0, void 0, function () {
161
+ var targetPaths, absoluteImportPath, _i, targetPaths_1, targetPath, requiredPath;
162
+ return __generator(this, function (_a) {
163
+ if (args.path.startsWith('node:') ||
164
+ module.builtinModules.some(function (moduleName) { return args.path.startsWith(moduleName) || args.path.startsWith("".concat(moduleName, "/")); })) {
165
+ return [2, { path: args.path, external: true }];
166
+ }
167
+ if (outputMap.has(args.path))
168
+ return [2, { path: args.path, namespace: 'vfs' }];
169
+ if (outputMap.has(args.importer)) {
170
+ targetPaths = [];
171
+ absoluteImportPath = path.resolve(path.dirname(args.importer), args.path);
172
+ if (!['.js', '.cjs'].includes(path.extname(absoluteImportPath))) {
173
+ targetPaths.push(absoluteImportPath + '.js');
174
+ targetPaths.push(absoluteImportPath + '.cjs');
175
+ targetPaths.push(path.resolve(absoluteImportPath, 'index.js'));
176
+ targetPaths.push(path.resolve(absoluteImportPath, 'index.cjs'));
177
+ }
178
+ else {
179
+ targetPaths.push(absoluteImportPath);
180
+ }
181
+ for (_i = 0, targetPaths_1 = targetPaths; _i < targetPaths_1.length; _i++) {
182
+ targetPath = targetPaths_1[_i];
183
+ if (outputMap.has(targetPath)) {
184
+ return [2, { path: targetPath, namespace: 'vfs' }];
185
+ }
186
+ }
187
+ }
188
+ requiredPath = _.attempt(function () {
189
+ return require.resolve(args.path, {
190
+ paths: __spreadArray(__spreadArray([], (function () {
191
+ var result = [];
192
+ var currentDir = path.dirname(args.importer);
193
+ result.push(currentDir);
194
+ while (currentDir !== path.dirname(currentDir)) {
195
+ result.push(path.dirname(currentDir));
196
+ currentDir = path.dirname(currentDir);
197
+ }
198
+ return result;
199
+ })(), true), (require.resolve.paths('') || []), true),
200
+ });
201
+ });
202
+ if (!(requiredPath instanceof Error)) {
203
+ return [2, { path: requiredPath, namespace: outputMap.has(requiredPath) ? 'vfs' : undefined }];
204
+ }
205
+ return [2, { path: args.path, external: true }];
206
+ });
207
+ }); });
142
208
  build.onLoad({ filter: /.*/, namespace: 'vfs' }, function (args) {
143
209
  var contents = outputMap.get(args.path);
144
210
  return {
@@ -146,12 +212,43 @@ var ClientUtil = (function () {
146
212
  loader: 'js',
147
213
  };
148
214
  });
215
+ build.onLoad({ filter: /node_modules\/.*.(mjs|js)$/ }, function (args) { return __awaiter(_this, void 0, void 0, function () {
216
+ var code, transformed;
217
+ return __generator(this, function (_a) {
218
+ switch (_a.label) {
219
+ case 0:
220
+ code = fs.readFileSync(args.path, { encoding: 'utf-8' });
221
+ return [4, maybeESModule(code)];
222
+ case 1:
223
+ if (_a.sent()) {
224
+ transformed = babel.transformSync(code, {
225
+ plugins: [require.resolve('@babel/plugin-transform-modules-commonjs')],
226
+ });
227
+ return [2, {
228
+ contents: (transformed === null || transformed === void 0 ? void 0 : transformed.code) || code,
229
+ loader: 'js',
230
+ }];
231
+ }
232
+ return [2, {
233
+ contents: code,
234
+ loader: 'js',
235
+ }];
236
+ }
237
+ });
238
+ }); });
239
+ build.onLoad({ filter: /.*/, namespace: 'json' }, function (args) {
240
+ var contents = fs.readJsonSync(args.path, { encoding: 'utf-8' });
241
+ return {
242
+ contents: "module.exports = ".concat(JSON.stringify(contents)),
243
+ loader: 'js',
244
+ };
245
+ });
149
246
  },
150
247
  },
151
248
  ],
152
249
  }))];
153
250
  case 1:
154
- esbuildResult = _f.sent();
251
+ esbuildResult = _g.sent();
155
252
  if (esbuildResult instanceof Error) {
156
253
  (_c = this.onLog) === null || _c === void 0 ? void 0 : _c.call(this, 'error', "Failed to build client code: ".concat(esbuildResult.message));
157
254
  return [2];
@@ -162,20 +259,25 @@ var ClientUtil = (function () {
162
259
  }
163
260
  text = esbuildResult.outputFiles[0].text;
164
261
  client = undefined;
262
+ fs.writeFileSync(path.resolve('dist/test.js'), text, { encoding: 'utf-8' });
165
263
  try {
166
264
  requireResult = requireFromString(text, { appendPaths: [path.resolve(process.cwd())] });
167
265
  client = requireResult;
168
266
  if (!(client instanceof create_1.Client))
169
267
  client = client === null || client === void 0 ? void 0 : client.default;
170
268
  }
171
- catch (_g) { }
172
- if (!(client instanceof create_1.Client)) {
173
- (_d = this.onLog) === null || _d === void 0 ? void 0 : _d.call(this, 'error', 'Failed to load client code');
269
+ catch (error) {
270
+ if (error instanceof Error) {
271
+ (_d = this.onLog) === null || _d === void 0 ? void 0 : _d.call(this, 'error', "Failed to load client code: ".concat(error.message));
272
+ }
273
+ }
274
+ if (!(typeof (client === null || client === void 0 ? void 0 : client.generateClientSourceFile) === 'function')) {
275
+ (_e = this.onLog) === null || _e === void 0 ? void 0 : _e.call(this, 'error', 'Failed to load client code');
174
276
  return [2];
175
277
  }
176
278
  clientCode = _.attempt(function () { return client.generateClientSourceFile(_this.options.group); });
177
279
  if (clientCode instanceof Error) {
178
- (_e = this.onLog) === null || _e === void 0 ? void 0 : _e.call(this, 'error', "Failed to generate client code: ".concat(clientCode.message));
280
+ (_f = this.onLog) === null || _f === void 0 ? void 0 : _f.call(this, 'error', "Failed to generate client code: ".concat(clientCode.message));
179
281
  return [2];
180
282
  }
181
283
  absoluteOutputFile = path.resolve(this.options.outputFile);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@open-norantec/herbal",
3
- "version": "1.0.2-alpha.10",
3
+ "version": "1.0.2-alpha.12",
4
4
  "description": "Herbal is a builder and toolchain for Nest.js applications",
5
5
  "main": "dist/index.js",
6
6
  "scripts": {
@@ -35,12 +35,16 @@
35
35
  "typescript": ">=5.1.0 <5.2.0"
36
36
  },
37
37
  "dependencies": {
38
+ "@alcalzone/esm2cjs": "^1.4.2",
39
+ "@babel/core": "^7.29.0",
40
+ "@babel/plugin-transform-modules-commonjs": "^7.28.6",
38
41
  "@nestjs/common": "^10.0.0",
39
42
  "@nestjs/core": "^10.4.19",
40
43
  "@nestjs/sequelize": "^10.0.1",
41
44
  "@open-norantec/forge": "latest",
42
45
  "@open-norantec/utilities": "latest",
43
46
  "commander": "^12.1.0",
47
+ "es-module-lexer": "^2.0.0",
44
48
  "esbuild": "^0.28.0",
45
49
  "fs-extra": "^11.3.4",
46
50
  "lodash": "^4.17.21",
@@ -60,6 +64,7 @@
60
64
  },
61
65
  "devDependencies": {
62
66
  "@stylistic/eslint-plugin": "^2.12.1",
67
+ "@types/babel__core": "^7.20.5",
63
68
  "@types/express": "^5.0.3",
64
69
  "@types/fs-extra": "^11.0.4",
65
70
  "@types/lodash": "^4.17.16",