@faasjs/load 0.0.2-beta.3 → 0.0.2-beta.302
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/README.md +1 -1
- package/lib/index.es.js +126 -68
- package/lib/index.js +151 -72
- package/lib/load_ts.d.ts +11 -1
- package/package.json +22 -9
package/README.md
CHANGED
|
@@ -2,6 +2,6 @@
|
|
|
2
2
|
|
|
3
3
|
资源加载类库
|
|
4
4
|
|
|
5
|
-
[](https://github.com/faasjs/faasjs/blob/
|
|
5
|
+
[](https://github.com/faasjs/faasjs/blob/main/packages/faasjs/load/LICENSE)
|
|
6
6
|
[](https://www.npmjs.com/package/@faasjs/load)
|
|
7
7
|
[](https://www.npmjs.com/package/@faasjs/load)
|
package/lib/index.es.js
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
|
-
import deepMerge from '@faasjs/deep_merge';
|
|
1
|
+
import { deepMerge } from '@faasjs/deep_merge';
|
|
2
2
|
import { existsSync, readFileSync, unlinkSync } from 'fs';
|
|
3
3
|
import { sep, dirname, join } from 'path';
|
|
4
|
-
import {
|
|
5
|
-
import
|
|
6
|
-
import typescript from 'rollup
|
|
4
|
+
import { load } from 'js-yaml';
|
|
5
|
+
import * as rollup from 'rollup';
|
|
6
|
+
import typescript from '@rollup/plugin-typescript';
|
|
7
|
+
import { NodeVM } from 'vm2';
|
|
7
8
|
|
|
8
9
|
/**
|
|
9
10
|
* 配置类
|
|
@@ -17,51 +18,39 @@ class Config {
|
|
|
17
18
|
*/
|
|
18
19
|
constructor(root, filename) {
|
|
19
20
|
this.root = root;
|
|
20
|
-
if (!this.root.endsWith(sep))
|
|
21
|
+
if (!this.root.endsWith(sep))
|
|
21
22
|
this.root += sep;
|
|
22
|
-
}
|
|
23
23
|
this.filename = filename;
|
|
24
24
|
const configs = [];
|
|
25
25
|
const paths = [this.root, '.'].concat(dirname(filename.replace(root, '')).split(sep));
|
|
26
26
|
paths.reduce(function (base, path) {
|
|
27
27
|
const root = join(base, path);
|
|
28
|
+
if (root === base)
|
|
29
|
+
return base;
|
|
28
30
|
const faas = join(root, 'faas.yaml');
|
|
29
|
-
if (existsSync(faas))
|
|
30
|
-
configs.push(
|
|
31
|
-
}
|
|
31
|
+
if (existsSync(faas))
|
|
32
|
+
configs.push(load(readFileSync(faas).toString()));
|
|
32
33
|
return root;
|
|
33
34
|
});
|
|
34
35
|
this.origin = deepMerge(...configs);
|
|
35
|
-
|
|
36
|
-
throw Error('[faas.yaml] need defaults env.');
|
|
37
|
-
}
|
|
38
|
-
this.defaults = deepMerge(this.origin.defaults);
|
|
36
|
+
this.defaults = deepMerge(this.origin.defaults || {});
|
|
39
37
|
for (const key in this.origin) {
|
|
40
|
-
if (key !== 'defaults')
|
|
38
|
+
if (key !== 'defaults')
|
|
41
39
|
this[key] = deepMerge(this.origin.defaults, this.origin[key]);
|
|
42
|
-
}
|
|
43
40
|
const data = this[key];
|
|
44
|
-
if (
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
if (plugin.provider) {
|
|
54
|
-
if (typeof plugin.provider === 'string') {
|
|
55
|
-
if (!data.providers[plugin.provider]) {
|
|
56
|
-
throw Error(`[faas.yaml] missing provider: ${plugin.provider} <${key}/plugins/${pluginKey}>`);
|
|
41
|
+
if (data.plugins)
|
|
42
|
+
for (const pluginKey in data.plugins) {
|
|
43
|
+
const plugin = data.plugins[pluginKey];
|
|
44
|
+
plugin.name = pluginKey;
|
|
45
|
+
if (plugin.provider)
|
|
46
|
+
if (typeof plugin.provider === 'string') {
|
|
47
|
+
if (!data.providers[plugin.provider])
|
|
48
|
+
throw Error(`[faas.yaml] missing provider: ${plugin.provider} <${key}/plugins/${pluginKey}>`);
|
|
49
|
+
plugin.provider = data.providers[plugin.provider];
|
|
57
50
|
}
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
else {
|
|
61
|
-
plugin.provider = deepMerge(data.providers[plugin.provider], plugin.provider);
|
|
62
|
-
}
|
|
51
|
+
else
|
|
52
|
+
plugin.provider = deepMerge(data.providers[plugin.provider], plugin.provider);
|
|
63
53
|
}
|
|
64
|
-
}
|
|
65
54
|
}
|
|
66
55
|
}
|
|
67
56
|
}
|
|
@@ -85,18 +74,75 @@ const FAAS_PACKAGES = [
|
|
|
85
74
|
'@faasjs/eslint-config-vue',
|
|
86
75
|
'faasjs',
|
|
87
76
|
'@faasjs/func',
|
|
77
|
+
'@faasjs/graphql-server',
|
|
88
78
|
'@faasjs/http',
|
|
79
|
+
'@faasjs/knex',
|
|
89
80
|
'@faasjs/load',
|
|
90
81
|
'@faasjs/logger',
|
|
91
|
-
'@faasjs/
|
|
82
|
+
'@faasjs/react',
|
|
92
83
|
'@faasjs/redis',
|
|
93
84
|
'@faasjs/request',
|
|
94
85
|
'@faasjs/server',
|
|
95
|
-
'@faasjs/sql',
|
|
96
86
|
'@faasjs/tencentcloud',
|
|
97
87
|
'@faasjs/test',
|
|
98
88
|
'@faasjs/vue-plugin'
|
|
99
89
|
];
|
|
90
|
+
const NODE_PACKAGES = [
|
|
91
|
+
'async_hooks',
|
|
92
|
+
'child_process',
|
|
93
|
+
'cluster',
|
|
94
|
+
'crypto',
|
|
95
|
+
'dns',
|
|
96
|
+
'events',
|
|
97
|
+
'fs',
|
|
98
|
+
'http',
|
|
99
|
+
'http2',
|
|
100
|
+
'https',
|
|
101
|
+
'inspector',
|
|
102
|
+
'net',
|
|
103
|
+
'os',
|
|
104
|
+
'path',
|
|
105
|
+
'perf_hooks',
|
|
106
|
+
'process',
|
|
107
|
+
'querystring',
|
|
108
|
+
'readline',
|
|
109
|
+
'repl',
|
|
110
|
+
'stream',
|
|
111
|
+
'string_decoder',
|
|
112
|
+
'tls',
|
|
113
|
+
'trace_events',
|
|
114
|
+
'tty',
|
|
115
|
+
'dgram',
|
|
116
|
+
'udp4',
|
|
117
|
+
'url',
|
|
118
|
+
'util',
|
|
119
|
+
'v8',
|
|
120
|
+
'vm',
|
|
121
|
+
'wasi',
|
|
122
|
+
'worker_threads',
|
|
123
|
+
'zlib'
|
|
124
|
+
];
|
|
125
|
+
function findModule(list, key, basePath, options = { excludes: [] }) {
|
|
126
|
+
if (list[key])
|
|
127
|
+
return;
|
|
128
|
+
if (key.startsWith('@types/') || options.excludes.includes(key))
|
|
129
|
+
return;
|
|
130
|
+
const paths = [join(process.cwd(), 'node_modules', key), join(basePath, 'node_modules', key)];
|
|
131
|
+
let path;
|
|
132
|
+
for (const p of paths)
|
|
133
|
+
if (existsSync(p)) {
|
|
134
|
+
path = p;
|
|
135
|
+
break;
|
|
136
|
+
}
|
|
137
|
+
if (!path)
|
|
138
|
+
return;
|
|
139
|
+
list[key] = path;
|
|
140
|
+
if (existsSync(join(path, 'package.json'))) {
|
|
141
|
+
const pkg = JSON.parse(readFileSync(join(path, 'package.json')).toString());
|
|
142
|
+
const deps = Object.keys(pkg.dependencies || {}).concat(Object.keys(pkg.peerDependencies || {}));
|
|
143
|
+
deps.map(d => findModule(list, d, path, options));
|
|
144
|
+
}
|
|
145
|
+
}
|
|
100
146
|
/**
|
|
101
147
|
* 加载 ts 文件
|
|
102
148
|
*
|
|
@@ -105,50 +151,62 @@ const FAAS_PACKAGES = [
|
|
|
105
151
|
* @param options.input {object} 读取配置
|
|
106
152
|
* @param options.output {object} 写入配置
|
|
107
153
|
* @param options.tmp {boolean} 是否为临时文件,true 则生成的文件会被删除,默认为 false
|
|
154
|
+
* @param options.modules {object} 生成 modules 的配置
|
|
155
|
+
* @param options.modules.excludes {string[]} modules 中需排除的模块
|
|
108
156
|
*/
|
|
109
157
|
async function loadTs(filename, options = Object.create(null)) {
|
|
110
|
-
|
|
158
|
+
var _a;
|
|
159
|
+
// eslint-disable-next-line @typescript-eslint/no-var-requires
|
|
111
160
|
const PackageJSON = require(`${process.cwd()}/package.json`);
|
|
161
|
+
const external = PackageJSON.dependencies ?
|
|
162
|
+
FAAS_PACKAGES.concat(Object.keys(PackageJSON.dependencies)) : FAAS_PACKAGES;
|
|
163
|
+
if ((options.modules) && (options.modules.excludes == null))
|
|
164
|
+
options.modules.excludes = [];
|
|
112
165
|
const input = deepMerge({
|
|
113
166
|
input: filename,
|
|
114
|
-
external
|
|
115
|
-
plugins: [
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
declaration: false
|
|
120
|
-
}
|
|
121
|
-
}
|
|
122
|
-
})
|
|
123
|
-
]
|
|
124
|
-
}, options.input || {});
|
|
125
|
-
const bundle = await rollup(input);
|
|
167
|
+
external,
|
|
168
|
+
plugins: [typescript({ declaration: false })],
|
|
169
|
+
onwarn: () => null
|
|
170
|
+
}, (options.input) || {});
|
|
171
|
+
const bundle = await rollup.rollup(input);
|
|
126
172
|
const dependencies = Object.create(null);
|
|
127
|
-
for (const m of bundle.cache.modules || [])
|
|
128
|
-
for (const d of m.dependencies)
|
|
129
|
-
if (!d.startsWith('/') &&
|
|
173
|
+
for (const m of ((_a = bundle.cache) === null || _a === void 0 ? void 0 : _a.modules) || [])
|
|
174
|
+
for (const d of m.dependencies)
|
|
175
|
+
if (!d.startsWith('/') &&
|
|
176
|
+
!dependencies[d] &&
|
|
177
|
+
!NODE_PACKAGES.includes(d))
|
|
130
178
|
dependencies[d] = '*';
|
|
131
|
-
}
|
|
132
|
-
}
|
|
133
|
-
}
|
|
134
|
-
// 特殊处理,避免引入 tslib
|
|
135
|
-
if (dependencies['\u0000tslib.js']) {
|
|
136
|
-
delete dependencies['\u0000tslib.js'];
|
|
137
|
-
}
|
|
138
179
|
const output = deepMerge({
|
|
139
180
|
file: filename + '.tmp.js',
|
|
140
|
-
format: 'cjs'
|
|
141
|
-
|
|
181
|
+
format: 'cjs',
|
|
182
|
+
exports: 'auto'
|
|
183
|
+
}, (options.output) || {});
|
|
142
184
|
await bundle.write(output);
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
if (options.
|
|
185
|
+
const result = Object.create(null);
|
|
186
|
+
result.dependencies = dependencies;
|
|
187
|
+
if (options.vm) {
|
|
188
|
+
const vm = new NodeVM({
|
|
189
|
+
require: {
|
|
190
|
+
external: true,
|
|
191
|
+
context: 'sandbox',
|
|
192
|
+
builtin: ['*']
|
|
193
|
+
}
|
|
194
|
+
});
|
|
195
|
+
result.module = vm.require(output.file);
|
|
196
|
+
}
|
|
197
|
+
else
|
|
198
|
+
// eslint-disable-next-line @typescript-eslint/no-var-requires
|
|
199
|
+
result.module = require(output.file);
|
|
200
|
+
if (options.tmp)
|
|
146
201
|
unlinkSync(output.file);
|
|
202
|
+
if (options.modules) {
|
|
203
|
+
const modules = Object.create(null);
|
|
204
|
+
Object.keys(dependencies).map(d => findModule(modules, d, process.cwd(), options.modules));
|
|
205
|
+
if (options.modules.additions)
|
|
206
|
+
options.modules.additions.map(d => findModule(modules, d, process.cwd(), options.modules));
|
|
207
|
+
result.modules = modules;
|
|
147
208
|
}
|
|
148
|
-
return
|
|
149
|
-
module,
|
|
150
|
-
dependencies
|
|
151
|
-
};
|
|
209
|
+
return result;
|
|
152
210
|
}
|
|
153
211
|
|
|
154
212
|
export { loadConfig, loadTs };
|
package/lib/index.js
CHANGED
|
@@ -2,14 +2,36 @@
|
|
|
2
2
|
|
|
3
3
|
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
4
|
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
var deepMerge = _interopDefault(require('@faasjs/deep_merge'));
|
|
5
|
+
var deep_merge = require('@faasjs/deep_merge');
|
|
8
6
|
var fs = require('fs');
|
|
9
7
|
var path = require('path');
|
|
10
8
|
var jsYaml = require('js-yaml');
|
|
11
9
|
var rollup = require('rollup');
|
|
12
|
-
var typescript =
|
|
10
|
+
var typescript = require('@rollup/plugin-typescript');
|
|
11
|
+
var vm2 = require('vm2');
|
|
12
|
+
|
|
13
|
+
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
|
|
14
|
+
|
|
15
|
+
function _interopNamespace(e) {
|
|
16
|
+
if (e && e.__esModule) return e;
|
|
17
|
+
var n = Object.create(null);
|
|
18
|
+
if (e) {
|
|
19
|
+
Object.keys(e).forEach(function (k) {
|
|
20
|
+
if (k !== 'default') {
|
|
21
|
+
var d = Object.getOwnPropertyDescriptor(e, k);
|
|
22
|
+
Object.defineProperty(n, k, d.get ? d : {
|
|
23
|
+
enumerable: true,
|
|
24
|
+
get: function () { return e[k]; }
|
|
25
|
+
});
|
|
26
|
+
}
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
n["default"] = e;
|
|
30
|
+
return Object.freeze(n);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
var rollup__namespace = /*#__PURE__*/_interopNamespace(rollup);
|
|
34
|
+
var typescript__default = /*#__PURE__*/_interopDefaultLegacy(typescript);
|
|
13
35
|
|
|
14
36
|
/**
|
|
15
37
|
* 配置类
|
|
@@ -23,51 +45,39 @@ class Config {
|
|
|
23
45
|
*/
|
|
24
46
|
constructor(root, filename) {
|
|
25
47
|
this.root = root;
|
|
26
|
-
if (!this.root.endsWith(path.sep))
|
|
48
|
+
if (!this.root.endsWith(path.sep))
|
|
27
49
|
this.root += path.sep;
|
|
28
|
-
}
|
|
29
50
|
this.filename = filename;
|
|
30
51
|
const configs = [];
|
|
31
52
|
const paths = [this.root, '.'].concat(path.dirname(filename.replace(root, '')).split(path.sep));
|
|
32
53
|
paths.reduce(function (base, path$1) {
|
|
33
54
|
const root = path.join(base, path$1);
|
|
55
|
+
if (root === base)
|
|
56
|
+
return base;
|
|
34
57
|
const faas = path.join(root, 'faas.yaml');
|
|
35
|
-
if (fs.existsSync(faas))
|
|
36
|
-
configs.push(jsYaml.
|
|
37
|
-
}
|
|
58
|
+
if (fs.existsSync(faas))
|
|
59
|
+
configs.push(jsYaml.load(fs.readFileSync(faas).toString()));
|
|
38
60
|
return root;
|
|
39
61
|
});
|
|
40
|
-
this.origin = deepMerge(...configs);
|
|
41
|
-
|
|
42
|
-
throw Error('[faas.yaml] need defaults env.');
|
|
43
|
-
}
|
|
44
|
-
this.defaults = deepMerge(this.origin.defaults);
|
|
62
|
+
this.origin = deep_merge.deepMerge(...configs);
|
|
63
|
+
this.defaults = deep_merge.deepMerge(this.origin.defaults || {});
|
|
45
64
|
for (const key in this.origin) {
|
|
46
|
-
if (key !== 'defaults')
|
|
47
|
-
this[key] = deepMerge(this.origin.defaults, this.origin[key]);
|
|
48
|
-
}
|
|
65
|
+
if (key !== 'defaults')
|
|
66
|
+
this[key] = deep_merge.deepMerge(this.origin.defaults, this.origin[key]);
|
|
49
67
|
const data = this[key];
|
|
50
|
-
if (
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
if (plugin.provider) {
|
|
60
|
-
if (typeof plugin.provider === 'string') {
|
|
61
|
-
if (!data.providers[plugin.provider]) {
|
|
62
|
-
throw Error(`[faas.yaml] missing provider: ${plugin.provider} <${key}/plugins/${pluginKey}>`);
|
|
68
|
+
if (data.plugins)
|
|
69
|
+
for (const pluginKey in data.plugins) {
|
|
70
|
+
const plugin = data.plugins[pluginKey];
|
|
71
|
+
plugin.name = pluginKey;
|
|
72
|
+
if (plugin.provider)
|
|
73
|
+
if (typeof plugin.provider === 'string') {
|
|
74
|
+
if (!data.providers[plugin.provider])
|
|
75
|
+
throw Error(`[faas.yaml] missing provider: ${plugin.provider} <${key}/plugins/${pluginKey}>`);
|
|
76
|
+
plugin.provider = data.providers[plugin.provider];
|
|
63
77
|
}
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
else {
|
|
67
|
-
plugin.provider = deepMerge(data.providers[plugin.provider], plugin.provider);
|
|
68
|
-
}
|
|
78
|
+
else
|
|
79
|
+
plugin.provider = deep_merge.deepMerge(data.providers[plugin.provider], plugin.provider);
|
|
69
80
|
}
|
|
70
|
-
}
|
|
71
81
|
}
|
|
72
82
|
}
|
|
73
83
|
}
|
|
@@ -91,18 +101,75 @@ const FAAS_PACKAGES = [
|
|
|
91
101
|
'@faasjs/eslint-config-vue',
|
|
92
102
|
'faasjs',
|
|
93
103
|
'@faasjs/func',
|
|
104
|
+
'@faasjs/graphql-server',
|
|
94
105
|
'@faasjs/http',
|
|
106
|
+
'@faasjs/knex',
|
|
95
107
|
'@faasjs/load',
|
|
96
108
|
'@faasjs/logger',
|
|
97
|
-
'@faasjs/
|
|
109
|
+
'@faasjs/react',
|
|
98
110
|
'@faasjs/redis',
|
|
99
111
|
'@faasjs/request',
|
|
100
112
|
'@faasjs/server',
|
|
101
|
-
'@faasjs/sql',
|
|
102
113
|
'@faasjs/tencentcloud',
|
|
103
114
|
'@faasjs/test',
|
|
104
115
|
'@faasjs/vue-plugin'
|
|
105
116
|
];
|
|
117
|
+
const NODE_PACKAGES = [
|
|
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
|
+
function findModule(list, key, basePath, options = { excludes: [] }) {
|
|
153
|
+
if (list[key])
|
|
154
|
+
return;
|
|
155
|
+
if (key.startsWith('@types/') || options.excludes.includes(key))
|
|
156
|
+
return;
|
|
157
|
+
const paths = [path.join(process.cwd(), 'node_modules', key), path.join(basePath, 'node_modules', key)];
|
|
158
|
+
let path$1;
|
|
159
|
+
for (const p of paths)
|
|
160
|
+
if (fs.existsSync(p)) {
|
|
161
|
+
path$1 = p;
|
|
162
|
+
break;
|
|
163
|
+
}
|
|
164
|
+
if (!path$1)
|
|
165
|
+
return;
|
|
166
|
+
list[key] = path$1;
|
|
167
|
+
if (fs.existsSync(path.join(path$1, 'package.json'))) {
|
|
168
|
+
const pkg = JSON.parse(fs.readFileSync(path.join(path$1, 'package.json')).toString());
|
|
169
|
+
const deps = Object.keys(pkg.dependencies || {}).concat(Object.keys(pkg.peerDependencies || {}));
|
|
170
|
+
deps.map(d => findModule(list, d, path$1, options));
|
|
171
|
+
}
|
|
172
|
+
}
|
|
106
173
|
/**
|
|
107
174
|
* 加载 ts 文件
|
|
108
175
|
*
|
|
@@ -111,50 +178,62 @@ const FAAS_PACKAGES = [
|
|
|
111
178
|
* @param options.input {object} 读取配置
|
|
112
179
|
* @param options.output {object} 写入配置
|
|
113
180
|
* @param options.tmp {boolean} 是否为临时文件,true 则生成的文件会被删除,默认为 false
|
|
181
|
+
* @param options.modules {object} 生成 modules 的配置
|
|
182
|
+
* @param options.modules.excludes {string[]} modules 中需排除的模块
|
|
114
183
|
*/
|
|
115
184
|
async function loadTs(filename, options = Object.create(null)) {
|
|
116
|
-
|
|
185
|
+
var _a;
|
|
186
|
+
// eslint-disable-next-line @typescript-eslint/no-var-requires
|
|
117
187
|
const PackageJSON = require(`${process.cwd()}/package.json`);
|
|
118
|
-
const
|
|
188
|
+
const external = PackageJSON.dependencies ?
|
|
189
|
+
FAAS_PACKAGES.concat(Object.keys(PackageJSON.dependencies)) : FAAS_PACKAGES;
|
|
190
|
+
if ((options.modules) && (options.modules.excludes == null))
|
|
191
|
+
options.modules.excludes = [];
|
|
192
|
+
const input = deep_merge.deepMerge({
|
|
119
193
|
input: filename,
|
|
120
|
-
external
|
|
121
|
-
plugins: [
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
declaration: false
|
|
126
|
-
}
|
|
127
|
-
}
|
|
128
|
-
})
|
|
129
|
-
]
|
|
130
|
-
}, options.input || {});
|
|
131
|
-
const bundle = await rollup.rollup(input);
|
|
194
|
+
external,
|
|
195
|
+
plugins: [typescript__default["default"]({ declaration: false })],
|
|
196
|
+
onwarn: () => null
|
|
197
|
+
}, (options.input) || {});
|
|
198
|
+
const bundle = await rollup__namespace.rollup(input);
|
|
132
199
|
const dependencies = Object.create(null);
|
|
133
|
-
for (const m of bundle.cache.modules || [])
|
|
134
|
-
for (const d of m.dependencies)
|
|
135
|
-
if (!d.startsWith('/') &&
|
|
200
|
+
for (const m of ((_a = bundle.cache) === null || _a === void 0 ? void 0 : _a.modules) || [])
|
|
201
|
+
for (const d of m.dependencies)
|
|
202
|
+
if (!d.startsWith('/') &&
|
|
203
|
+
!dependencies[d] &&
|
|
204
|
+
!NODE_PACKAGES.includes(d))
|
|
136
205
|
dependencies[d] = '*';
|
|
137
|
-
|
|
138
|
-
}
|
|
139
|
-
}
|
|
140
|
-
// 特殊处理,避免引入 tslib
|
|
141
|
-
if (dependencies['\u0000tslib.js']) {
|
|
142
|
-
delete dependencies['\u0000tslib.js'];
|
|
143
|
-
}
|
|
144
|
-
const output = deepMerge({
|
|
206
|
+
const output = deep_merge.deepMerge({
|
|
145
207
|
file: filename + '.tmp.js',
|
|
146
|
-
format: 'cjs'
|
|
147
|
-
|
|
208
|
+
format: 'cjs',
|
|
209
|
+
exports: 'auto'
|
|
210
|
+
}, (options.output) || {});
|
|
148
211
|
await bundle.write(output);
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
if (options.
|
|
212
|
+
const result = Object.create(null);
|
|
213
|
+
result.dependencies = dependencies;
|
|
214
|
+
if (options.vm) {
|
|
215
|
+
const vm = new vm2.NodeVM({
|
|
216
|
+
require: {
|
|
217
|
+
external: true,
|
|
218
|
+
context: 'sandbox',
|
|
219
|
+
builtin: ['*']
|
|
220
|
+
}
|
|
221
|
+
});
|
|
222
|
+
result.module = vm.require(output.file);
|
|
223
|
+
}
|
|
224
|
+
else
|
|
225
|
+
// eslint-disable-next-line @typescript-eslint/no-var-requires
|
|
226
|
+
result.module = require(output.file);
|
|
227
|
+
if (options.tmp)
|
|
152
228
|
fs.unlinkSync(output.file);
|
|
229
|
+
if (options.modules) {
|
|
230
|
+
const modules = Object.create(null);
|
|
231
|
+
Object.keys(dependencies).map(d => findModule(modules, d, process.cwd(), options.modules));
|
|
232
|
+
if (options.modules.additions)
|
|
233
|
+
options.modules.additions.map(d => findModule(modules, d, process.cwd(), options.modules));
|
|
234
|
+
result.modules = modules;
|
|
153
235
|
}
|
|
154
|
-
return
|
|
155
|
-
module,
|
|
156
|
-
dependencies
|
|
157
|
-
};
|
|
236
|
+
return result;
|
|
158
237
|
}
|
|
159
238
|
|
|
160
239
|
exports.loadConfig = loadConfig;
|
package/lib/load_ts.d.ts
CHANGED
|
@@ -7,6 +7,8 @@ import { Func } from '@faasjs/func';
|
|
|
7
7
|
* @param options.input {object} 读取配置
|
|
8
8
|
* @param options.output {object} 写入配置
|
|
9
9
|
* @param options.tmp {boolean} 是否为临时文件,true 则生成的文件会被删除,默认为 false
|
|
10
|
+
* @param options.modules {object} 生成 modules 的配置
|
|
11
|
+
* @param options.modules.excludes {string[]} modules 中需排除的模块
|
|
10
12
|
*/
|
|
11
13
|
export default function loadTs(filename: string, options?: {
|
|
12
14
|
input?: {
|
|
@@ -16,9 +18,17 @@ export default function loadTs(filename: string, options?: {
|
|
|
16
18
|
[key: string]: any;
|
|
17
19
|
};
|
|
18
20
|
tmp?: boolean;
|
|
21
|
+
modules?: {
|
|
22
|
+
excludes?: string[];
|
|
23
|
+
additions?: string[];
|
|
24
|
+
};
|
|
25
|
+
vm?: boolean;
|
|
19
26
|
}): Promise<{
|
|
20
|
-
module
|
|
27
|
+
module?: Func;
|
|
21
28
|
dependencies: {
|
|
22
29
|
[key: string]: string;
|
|
23
30
|
};
|
|
31
|
+
modules?: {
|
|
32
|
+
[key: string]: string;
|
|
33
|
+
};
|
|
24
34
|
}>;
|
package/package.json
CHANGED
|
@@ -1,31 +1,44 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@faasjs/load",
|
|
3
|
-
"version": "0.0.2-beta.
|
|
3
|
+
"version": "0.0.2-beta.302",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"main": "lib/index.js",
|
|
6
6
|
"types": "lib/index.d.ts",
|
|
7
7
|
"module": "lib/index.es.js",
|
|
8
|
+
"homepage": "https://faasjs.com/doc/load.html",
|
|
9
|
+
"repository": {
|
|
10
|
+
"type": "git",
|
|
11
|
+
"url": "git+https://github.com/faasjs/faasjs.git",
|
|
12
|
+
"directory": "packages/load"
|
|
13
|
+
},
|
|
14
|
+
"bugs": {
|
|
15
|
+
"url": "https://github.com/faasjs/faasjs/issues"
|
|
16
|
+
},
|
|
17
|
+
"funding": "https://github.com/sponsors/faasjs",
|
|
8
18
|
"scripts": {
|
|
9
19
|
"lint": "eslint --ext .ts src",
|
|
10
|
-
"prepack": "rm -rf ./lib && rollup -c && mv lib/*/src/* lib/"
|
|
11
|
-
"ci": "yarn lint && jest --silent"
|
|
20
|
+
"prepack": "rm -rf ./lib && rollup -c && mv lib/*/src/* lib/"
|
|
12
21
|
},
|
|
13
22
|
"files": [
|
|
14
23
|
"lib"
|
|
15
24
|
],
|
|
16
|
-
"
|
|
17
|
-
"@faasjs/deep_merge": "^0.0.2-beta.
|
|
25
|
+
"peerDependencies": {
|
|
26
|
+
"@faasjs/deep_merge": "^0.0.2-beta.244",
|
|
27
|
+
"@rollup/plugin-typescript": "*",
|
|
18
28
|
"js-yaml": "*",
|
|
19
29
|
"rollup": "*",
|
|
20
|
-
"
|
|
21
|
-
"
|
|
30
|
+
"typescript": "*",
|
|
31
|
+
"vm2": "*"
|
|
22
32
|
},
|
|
23
33
|
"devDependencies": {
|
|
24
|
-
"@faasjs/func": "^0.0.2-beta.
|
|
34
|
+
"@faasjs/func": "^0.0.2-beta.302",
|
|
25
35
|
"@types/debug": "*",
|
|
26
36
|
"@types/jest": "*",
|
|
27
37
|
"@types/js-yaml": "*",
|
|
28
38
|
"@types/node": "*"
|
|
29
39
|
},
|
|
30
|
-
"
|
|
40
|
+
"engines": {
|
|
41
|
+
"npm": ">=8.0.0"
|
|
42
|
+
},
|
|
43
|
+
"gitHead": "b53fb35a91369afd0185fb555d98e5c9d90b54bd"
|
|
31
44
|
}
|