@faasjs/load 0.0.3-beta.63 → 0.0.3-beta.64

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.
package/dist/index.js CHANGED
@@ -25,12 +25,44 @@ __export(src_exports, {
25
25
  });
26
26
  module.exports = __toCommonJS(src_exports);
27
27
 
28
+ // ../deep_merge/src/index.ts
29
+ var shouldMerge = function(item) {
30
+ const type = Object.prototype.toString.call(item);
31
+ return type === "[object Object]" || type === "[object Array]";
32
+ };
33
+ function deepMerge(...sources) {
34
+ let acc = /* @__PURE__ */ Object.create(null);
35
+ for (const source of sources)
36
+ if (source instanceof Array) {
37
+ if (!(acc instanceof Array))
38
+ acc = [];
39
+ acc = [...new Set(source.concat(...acc))];
40
+ } else if (shouldMerge(source))
41
+ for (const [key, value] of Object.entries(source)) {
42
+ let val;
43
+ if (shouldMerge(value))
44
+ val = deepMerge(acc[key], value);
45
+ else
46
+ val = value;
47
+ acc = {
48
+ ...acc,
49
+ [key]: val
50
+ };
51
+ }
52
+ return acc;
53
+ }
54
+
28
55
  // src/load_config.ts
29
- var import_deep_merge = require("@faasjs/deep_merge");
30
56
  var import_fs = require("fs");
31
57
  var import_path = require("path");
32
58
  var import_js_yaml = require("js-yaml");
33
59
  var Config = class {
60
+ /**
61
+ * 创建配置类,并自动读取配置内容
62
+ *
63
+ * @param root {string} 根目录
64
+ * @param filename {filename} 目标文件,用于读取目录层级
65
+ */
34
66
  constructor(root, filename) {
35
67
  this.root = root;
36
68
  if (!this.root.endsWith(import_path.sep))
@@ -47,11 +79,11 @@ var Config = class {
47
79
  configs.push((0, import_js_yaml.load)((0, import_fs.readFileSync)(faas).toString()));
48
80
  return root2;
49
81
  });
50
- this.origin = (0, import_deep_merge.deepMerge)(...configs);
51
- this.defaults = (0, import_deep_merge.deepMerge)(this.origin.defaults || {});
82
+ this.origin = deepMerge(...configs);
83
+ this.defaults = deepMerge(this.origin.defaults || {});
52
84
  for (const key in this.origin) {
53
85
  if (key !== "defaults")
54
- this[key] = (0, import_deep_merge.deepMerge)(this.origin.defaults, this.origin[key]);
86
+ this[key] = deepMerge(this.origin.defaults, this.origin[key]);
55
87
  const data = this[key];
56
88
  if (data.plugins)
57
89
  for (const pluginKey in data.plugins) {
@@ -63,7 +95,7 @@ var Config = class {
63
95
  throw Error(`[faas.yaml] missing provider: ${plugin.provider} <${key}/plugins/${pluginKey}>`);
64
96
  plugin.provider = data.providers[plugin.provider];
65
97
  } else
66
- plugin.provider = (0, import_deep_merge.deepMerge)(data.providers[plugin.provider], plugin.provider);
98
+ plugin.provider = deepMerge(data.providers[plugin.provider], plugin.provider);
67
99
  }
68
100
  }
69
101
  }
@@ -73,11 +105,86 @@ function loadConfig(root, filename) {
73
105
  }
74
106
 
75
107
  // src/load_ts.ts
76
- var import_deep_merge2 = require("@faasjs/deep_merge");
77
- var import_fs2 = require("fs");
108
+ var import_fs3 = require("fs");
78
109
  var import_rollup = require("rollup/dist/rollup.js");
110
+ var import_path3 = require("path");
111
+
112
+ // ../ts-transform/src/index.ts
113
+ var import_core = require("@swc/core");
114
+ var import_spack = require("@swc/core/spack");
115
+ var import_fs2 = require("fs");
79
116
  var import_path2 = require("path");
80
- var import_ts_transform = require("@faasjs/ts-transform");
117
+ var NodeBuiltinModules = [
118
+ "async_hooks",
119
+ "child_process",
120
+ "cluster",
121
+ "crypto",
122
+ "dns",
123
+ "events",
124
+ "fs",
125
+ "http",
126
+ "http2",
127
+ "https",
128
+ "inspector",
129
+ "net",
130
+ "os",
131
+ "path",
132
+ "perf_hooks",
133
+ "process",
134
+ "querystring",
135
+ "readline",
136
+ "repl",
137
+ "stream",
138
+ "string_decoder",
139
+ "tls",
140
+ "trace_events",
141
+ "tty",
142
+ "dgram",
143
+ "udp4",
144
+ "url",
145
+ "util",
146
+ "v8",
147
+ "vm",
148
+ "wasi",
149
+ "worker_threads",
150
+ "zlib"
151
+ ];
152
+ async function bundle(options) {
153
+ var _a;
154
+ if (!options.root)
155
+ options.root = process.cwd();
156
+ if (!options.jscTarget)
157
+ options.jscTarget = "es2019";
158
+ const tsconfig = JSON.parse((0, import_fs2.readFileSync)((0, import_path2.join)(options.root, "tsconfig.json")).toString());
159
+ if (!tsconfig.compilerOptions)
160
+ tsconfig.compilerOptions = {};
161
+ tsconfig.compilerOptions.baseUrl = ((_a = tsconfig.compilerOptions.baseUrl) == null ? void 0 : _a.replace(".", options.root)) || options.root;
162
+ if (tsconfig.compilerOptions.paths) {
163
+ for (const key of Object.keys(tsconfig.compilerOptions.paths))
164
+ tsconfig.compilerOptions.paths[key] = tsconfig.compilerOptions.paths[key].map((item) => item.replace(".", tsconfig.compilerOptions.baseUrl));
165
+ } else
166
+ tsconfig.compilerOptions.paths = {};
167
+ return (0, import_core.bundle)((0, import_spack.config)(deepMerge({
168
+ mode: "production",
169
+ entry: { index: options.filename },
170
+ module: { type: "commonjs" },
171
+ options: {
172
+ jsc: {
173
+ parser: {
174
+ syntax: "typescript",
175
+ tsx: true
176
+ },
177
+ target: options.jscTarget,
178
+ baseUrl: tsconfig.compilerOptions.baseUrl,
179
+ paths: tsconfig.compilerOptions.paths,
180
+ transform: { react: { runtime: "automatic" } }
181
+ }
182
+ },
183
+ externalModules: NodeBuiltinModules.concat(options.externalModules || [])
184
+ }, options))).then((res) => res.index);
185
+ }
186
+
187
+ // src/load_ts.ts
81
188
  var FaasPackages = [
82
189
  "@faasjs/cloud_function",
83
190
  "@faasjs/deep_merge",
@@ -99,10 +206,10 @@ function resolveModuleBasePath(moduleName) {
99
206
  let searchForPathSection;
100
207
  if (moduleName.startsWith("@") && moduleNameParts.length > 1) {
101
208
  const [org, mod] = moduleNameParts;
102
- searchForPathSection = `node_modules${import_path2.sep}${org}${import_path2.sep}${mod}`;
209
+ searchForPathSection = `node_modules${import_path3.sep}${org}${import_path3.sep}${mod}`;
103
210
  } else {
104
211
  const [mod] = moduleNameParts;
105
- searchForPathSection = `node_modules${import_path2.sep}${mod}`;
212
+ searchForPathSection = `node_modules${import_path3.sep}${mod}`;
106
213
  }
107
214
  const lastIndex = moduleMainFilePath.lastIndexOf(searchForPathSection);
108
215
  if (lastIndex === -1) {
@@ -117,7 +224,7 @@ function findModule(list, key, options = { excludes: [] }) {
117
224
  if (key.startsWith("@types/") || options.excludes.includes(key))
118
225
  return;
119
226
  try {
120
- list[key] = (0, import_path2.dirname)(require.resolve((0, import_path2.join)(key, "package.json")));
227
+ list[key] = (0, import_path3.dirname)(require.resolve((0, import_path3.join)(key, "package.json")));
121
228
  } catch (error) {
122
229
  console.warn(error);
123
230
  try {
@@ -128,7 +235,7 @@ function findModule(list, key, options = { excludes: [] }) {
128
235
  }
129
236
  if (!list[key])
130
237
  return;
131
- const pkg = JSON.parse((0, import_fs2.readFileSync)((0, import_path2.join)(list[key], "package.json")).toString());
238
+ const pkg = JSON.parse((0, import_fs3.readFileSync)((0, import_path3.join)(list[key], "package.json")).toString());
132
239
  const deps = Object.keys(pkg.dependencies || {}).concat(Object.keys(pkg.peerDependencies || {}));
133
240
  if (pkg.peerDependenciesMeta) {
134
241
  Object.keys(pkg.peerDependenciesMeta).forEach((key2) => {
@@ -145,7 +252,7 @@ function swc(externalModules) {
145
252
  return {
146
253
  name: "swc",
147
254
  async transform(code, filename) {
148
- return (0, import_ts_transform.bundle)({
255
+ return bundle({
149
256
  filename,
150
257
  externalModules
151
258
  });
@@ -158,7 +265,7 @@ async function loadTs(filename, options = /* @__PURE__ */ Object.create(null)) {
158
265
  const external = PackageJSON.dependencies ? Object.keys(PackageJSON.dependencies) : [];
159
266
  if (options.modules && options.modules.excludes == null)
160
267
  options.modules.excludes = [];
161
- const input = (0, import_deep_merge2.deepMerge)({
268
+ const input = deepMerge({
162
269
  input: filename,
163
270
  external,
164
271
  plugins: [swc(external.concat(FaasPackages))],
@@ -168,9 +275,9 @@ async function loadTs(filename, options = /* @__PURE__ */ Object.create(null)) {
168
275
  const dependencies = /* @__PURE__ */ Object.create(null);
169
276
  for (const m of ((_a = bundle2.cache) == null ? void 0 : _a.modules) || [])
170
277
  for (const d of m.dependencies)
171
- if (!d.startsWith("/") && !dependencies[d] && !import_ts_transform.NodeBuiltinModules.includes(d))
278
+ if (!d.startsWith("/") && !dependencies[d] && !NodeBuiltinModules.includes(d))
172
279
  dependencies[d] = "*";
173
- const output = (0, import_deep_merge2.deepMerge)({
280
+ const output = deepMerge({
174
281
  file: filename + ".tmp.js",
175
282
  format: "cjs",
176
283
  exports: "auto"
@@ -180,7 +287,7 @@ async function loadTs(filename, options = /* @__PURE__ */ Object.create(null)) {
180
287
  result.dependencies = dependencies;
181
288
  result.module = require(output.file);
182
289
  if (options.tmp)
183
- (0, import_fs2.unlinkSync)(output.file);
290
+ (0, import_fs3.unlinkSync)(output.file);
184
291
  if (options.modules) {
185
292
  const modules = /* @__PURE__ */ Object.create(null);
186
293
  Object.keys(dependencies).map((d) => findModule(modules, d, options.modules));
package/dist/index.mjs CHANGED
@@ -6,8 +6,34 @@ var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require
6
6
  throw new Error('Dynamic require of "' + x + '" is not supported');
7
7
  });
8
8
 
9
+ // ../deep_merge/src/index.ts
10
+ var shouldMerge = function(item) {
11
+ const type = Object.prototype.toString.call(item);
12
+ return type === "[object Object]" || type === "[object Array]";
13
+ };
14
+ function deepMerge(...sources) {
15
+ let acc = /* @__PURE__ */ Object.create(null);
16
+ for (const source of sources)
17
+ if (source instanceof Array) {
18
+ if (!(acc instanceof Array))
19
+ acc = [];
20
+ acc = [...new Set(source.concat(...acc))];
21
+ } else if (shouldMerge(source))
22
+ for (const [key, value] of Object.entries(source)) {
23
+ let val;
24
+ if (shouldMerge(value))
25
+ val = deepMerge(acc[key], value);
26
+ else
27
+ val = value;
28
+ acc = {
29
+ ...acc,
30
+ [key]: val
31
+ };
32
+ }
33
+ return acc;
34
+ }
35
+
9
36
  // src/load_config.ts
10
- import { deepMerge } from "@faasjs/deep_merge";
11
37
  import { existsSync, readFileSync } from "fs";
12
38
  import {
13
39
  sep,
@@ -16,6 +42,12 @@ import {
16
42
  } from "path";
17
43
  import { load } from "js-yaml";
18
44
  var Config = class {
45
+ /**
46
+ * 创建配置类,并自动读取配置内容
47
+ *
48
+ * @param root {string} 根目录
49
+ * @param filename {filename} 目标文件,用于读取目录层级
50
+ */
19
51
  constructor(root, filename) {
20
52
  this.root = root;
21
53
  if (!this.root.endsWith(sep))
@@ -58,15 +90,93 @@ function loadConfig(root, filename) {
58
90
  }
59
91
 
60
92
  // src/load_ts.ts
61
- import { deepMerge as deepMerge2 } from "@faasjs/deep_merge";
62
- import { readFileSync as readFileSync2, unlinkSync } from "fs";
93
+ import { readFileSync as readFileSync3, unlinkSync } from "fs";
63
94
  import { rollup } from "rollup/dist/rollup.js";
64
95
  import {
65
- join as join2,
96
+ join as join3,
66
97
  sep as sep2,
67
98
  dirname as dirname2
68
99
  } from "path";
69
- import { bundle, NodeBuiltinModules } from "@faasjs/ts-transform";
100
+
101
+ // ../ts-transform/src/index.ts
102
+ import {
103
+ transformSync,
104
+ bundle as swcBundle
105
+ } from "@swc/core";
106
+ import { config } from "@swc/core/spack";
107
+ import { readFileSync as readFileSync2 } from "fs";
108
+ import { join as join2 } from "path";
109
+ var NodeBuiltinModules = [
110
+ "async_hooks",
111
+ "child_process",
112
+ "cluster",
113
+ "crypto",
114
+ "dns",
115
+ "events",
116
+ "fs",
117
+ "http",
118
+ "http2",
119
+ "https",
120
+ "inspector",
121
+ "net",
122
+ "os",
123
+ "path",
124
+ "perf_hooks",
125
+ "process",
126
+ "querystring",
127
+ "readline",
128
+ "repl",
129
+ "stream",
130
+ "string_decoder",
131
+ "tls",
132
+ "trace_events",
133
+ "tty",
134
+ "dgram",
135
+ "udp4",
136
+ "url",
137
+ "util",
138
+ "v8",
139
+ "vm",
140
+ "wasi",
141
+ "worker_threads",
142
+ "zlib"
143
+ ];
144
+ async function bundle(options) {
145
+ var _a;
146
+ if (!options.root)
147
+ options.root = process.cwd();
148
+ if (!options.jscTarget)
149
+ options.jscTarget = "es2019";
150
+ const tsconfig = JSON.parse(readFileSync2(join2(options.root, "tsconfig.json")).toString());
151
+ if (!tsconfig.compilerOptions)
152
+ tsconfig.compilerOptions = {};
153
+ tsconfig.compilerOptions.baseUrl = ((_a = tsconfig.compilerOptions.baseUrl) == null ? void 0 : _a.replace(".", options.root)) || options.root;
154
+ if (tsconfig.compilerOptions.paths) {
155
+ for (const key of Object.keys(tsconfig.compilerOptions.paths))
156
+ tsconfig.compilerOptions.paths[key] = tsconfig.compilerOptions.paths[key].map((item) => item.replace(".", tsconfig.compilerOptions.baseUrl));
157
+ } else
158
+ tsconfig.compilerOptions.paths = {};
159
+ return swcBundle(config(deepMerge({
160
+ mode: "production",
161
+ entry: { index: options.filename },
162
+ module: { type: "commonjs" },
163
+ options: {
164
+ jsc: {
165
+ parser: {
166
+ syntax: "typescript",
167
+ tsx: true
168
+ },
169
+ target: options.jscTarget,
170
+ baseUrl: tsconfig.compilerOptions.baseUrl,
171
+ paths: tsconfig.compilerOptions.paths,
172
+ transform: { react: { runtime: "automatic" } }
173
+ }
174
+ },
175
+ externalModules: NodeBuiltinModules.concat(options.externalModules || [])
176
+ }, options))).then((res) => res.index);
177
+ }
178
+
179
+ // src/load_ts.ts
70
180
  var FaasPackages = [
71
181
  "@faasjs/cloud_function",
72
182
  "@faasjs/deep_merge",
@@ -106,7 +216,7 @@ function findModule(list, key, options = { excludes: [] }) {
106
216
  if (key.startsWith("@types/") || options.excludes.includes(key))
107
217
  return;
108
218
  try {
109
- list[key] = dirname2(__require.resolve(join2(key, "package.json")));
219
+ list[key] = dirname2(__require.resolve(join3(key, "package.json")));
110
220
  } catch (error) {
111
221
  console.warn(error);
112
222
  try {
@@ -117,7 +227,7 @@ function findModule(list, key, options = { excludes: [] }) {
117
227
  }
118
228
  if (!list[key])
119
229
  return;
120
- const pkg = JSON.parse(readFileSync2(join2(list[key], "package.json")).toString());
230
+ const pkg = JSON.parse(readFileSync3(join3(list[key], "package.json")).toString());
121
231
  const deps = Object.keys(pkg.dependencies || {}).concat(Object.keys(pkg.peerDependencies || {}));
122
232
  if (pkg.peerDependenciesMeta) {
123
233
  Object.keys(pkg.peerDependenciesMeta).forEach((key2) => {
@@ -147,7 +257,7 @@ async function loadTs(filename, options = /* @__PURE__ */ Object.create(null)) {
147
257
  const external = PackageJSON.dependencies ? Object.keys(PackageJSON.dependencies) : [];
148
258
  if (options.modules && options.modules.excludes == null)
149
259
  options.modules.excludes = [];
150
- const input = deepMerge2({
260
+ const input = deepMerge({
151
261
  input: filename,
152
262
  external,
153
263
  plugins: [swc(external.concat(FaasPackages))],
@@ -159,7 +269,7 @@ async function loadTs(filename, options = /* @__PURE__ */ Object.create(null)) {
159
269
  for (const d of m.dependencies)
160
270
  if (!d.startsWith("/") && !dependencies[d] && !NodeBuiltinModules.includes(d))
161
271
  dependencies[d] = "*";
162
- const output = deepMerge2({
272
+ const output = deepMerge({
163
273
  file: filename + ".tmp.js",
164
274
  format: "cjs",
165
275
  exports: "auto"
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@faasjs/load",
3
- "version": "0.0.3-beta.63",
3
+ "version": "0.0.3-beta.64",
4
4
  "license": "MIT",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -22,9 +22,9 @@
22
22
  "dist"
23
23
  ],
24
24
  "dependencies": {
25
- "@faasjs/deep_merge": "^0.0.3-beta.63",
26
- "@faasjs/func": "^0.0.3-beta.63",
27
- "@faasjs/ts-transform": "^0.0.3-beta.63",
25
+ "@faasjs/deep_merge": "^0.0.3-beta.64",
26
+ "@faasjs/func": "^0.0.3-beta.64",
27
+ "@faasjs/ts-transform": "^0.0.3-beta.64",
28
28
  "js-yaml": "*",
29
29
  "rollup": "*"
30
30
  },