从节点脚本内的路径列表中调用 class 上的静态方法?
Call a static method on a class from a list of paths inside a node script?
问题:
如何获取文件路径列表 Array<string>
:
// ['apps/morningharwood/src/app/app.component.ts',
// 'apps/morningharwood/src/app/app-shell/app-shell.component.ts']
并且对于每条路径,在节点中对它们调用一个静态方法ref.SomeStaticMethod()
?
注意:我正在为节点使用打字稿(如果重要的话)和 windows 10.
async function prePublish() {
const componentPaths = await globby('apps/morningharwood/src/app/**/*.component.ts');
const dirPaths = componentPaths.map(i => `./${i.split('.ts')[ 0 ]}`);
console.log(dirPaths);
/**
* Logs out this
* [ './apps/morningharwood/src/app/app.component',
* './apps/morningharwood/src/app/app-shell/app-shell.component' ]
*/
const ref = await import('./apps/morningharwood/src/app/app.component');
const ref2 = await import('./apps/morningharwood/src/app/app-shell/app-shell.component');
console.log(ref, ref2); // WORKS AS INTENDED
for (const dp of dirPaths) {
console.log(dp); // ./apps/morningharwood/src/app/app.component
const ref3 = await import(dp); // ERROR?
console.log(ref); // Never runs.
}
}
prepublish();
完整的错误堆栈跟踪:
./apps/morningharwood/src/app/app.component
Unhandled Promise rejection: Cannot find module './apps/morningharwood/src/app/app.component' ; Zone: <root> ; Task: Promise.then ; Value: { Error: Cannot find module './apps/morningharwood/src/app/app.component'
at webpackEmptyContext (webpack:///._sync?:2:10)
at eval (webpack:///./prerender.ts?:126:126)
at ZoneDelegate.invoke (webpack:///./node_modules/zone.js/dist/zone-node.js?:387:26)
at Zone.run (webpack:///./node_modules/zone.js/dist/zone-node.js?:137:43)
at eval (webpack:///./node_modules/zone.js/dist/zone-node.js?:871:34)
at ZoneDelegate.invokeTask (webpack:///./node_modules/zone.js/dist/zone-node.js?:420:31)
at Zone.runTask (webpack:///./node_modules/zone.js/dist/zone-node.js?:187:47)
at drainMicroTaskQueue (webpack:///./node_modules/zone.js/dist/zone-node.js?:594:35)
at ZoneTask.invokeTask (webpack:///./node_modules/zone.js/dist/zone-node.js?:499:21)
at ZoneTask.invoke (webpack:///./node_modules/zone.js/dist/zone-node.js?:484:48) code: 'MODULE_NOT_FOUND' } Error: Cannot find module './apps/morningharwood/src/app/app.component'
at webpackEmptyContext (webpack:///._sync?:2:10)
at eval (webpack:///./prerender.ts?:126:126)
at ZoneDelegate.invoke (webpack:///./node_modules/zone.js/dist/zone-node.js?:387:26)
at Zone.run (webpack:///./node_modules/zone.js/dist/zone-node.js?:137:43)
at eval (webpack:///./node_modules/zone.js/dist/zone-node.js?:871:34)
at ZoneDelegate.invokeTask (webpack:///./node_modules/zone.js/dist/zone-node.js?:420:31)
at Zone.runTask (webpack:///./node_modules/zone.js/dist/zone-node.js?:187:47)
at drainMicroTaskQueue (webpack:///./node_modules/zone.js/dist/zone-node.js?:594:35)
at ZoneTask.invokeTask (webpack:///./node_modules/zone.js/dist/zone-node.js?:499:21)
at ZoneTask.invoke (webpack:///./node_modules/zone.js/dist/zone-node.js?:484:48)
模块路径相对于当前模块解析,文件系统路径相对于当前工作目录解析。
考虑到路径已成功与 glob 匹配,apps/...
是相对于当前工作目录的,可能是项目根目录。依赖当前工作目录本身就是有问题的做法。为了保持一致性,可以明确说明 glob 和模块路径。
可以是:
const componentPaths = await globby('../apps/morningharwood/src/app/**/*.component.ts', { cwd: __dirname });
for (const path of componentPaths) {
const ref = await import(path.join('./', path));
...
或者:
const componentPaths = await globby(path.join(__dirname, '../apps/morningharwood/src/app/**/*.component.ts'));
for (const path of componentPaths) {
const ref = await import(path);
...
问题:
如何获取文件路径列表 Array<string>
:
// ['apps/morningharwood/src/app/app.component.ts',
// 'apps/morningharwood/src/app/app-shell/app-shell.component.ts']
并且对于每条路径,在节点中对它们调用一个静态方法ref.SomeStaticMethod()
?
注意:我正在为节点使用打字稿(如果重要的话)和 windows 10.
async function prePublish() {
const componentPaths = await globby('apps/morningharwood/src/app/**/*.component.ts');
const dirPaths = componentPaths.map(i => `./${i.split('.ts')[ 0 ]}`);
console.log(dirPaths);
/**
* Logs out this
* [ './apps/morningharwood/src/app/app.component',
* './apps/morningharwood/src/app/app-shell/app-shell.component' ]
*/
const ref = await import('./apps/morningharwood/src/app/app.component');
const ref2 = await import('./apps/morningharwood/src/app/app-shell/app-shell.component');
console.log(ref, ref2); // WORKS AS INTENDED
for (const dp of dirPaths) {
console.log(dp); // ./apps/morningharwood/src/app/app.component
const ref3 = await import(dp); // ERROR?
console.log(ref); // Never runs.
}
}
prepublish();
完整的错误堆栈跟踪:
./apps/morningharwood/src/app/app.component
Unhandled Promise rejection: Cannot find module './apps/morningharwood/src/app/app.component' ; Zone: <root> ; Task: Promise.then ; Value: { Error: Cannot find module './apps/morningharwood/src/app/app.component'
at webpackEmptyContext (webpack:///._sync?:2:10)
at eval (webpack:///./prerender.ts?:126:126)
at ZoneDelegate.invoke (webpack:///./node_modules/zone.js/dist/zone-node.js?:387:26)
at Zone.run (webpack:///./node_modules/zone.js/dist/zone-node.js?:137:43)
at eval (webpack:///./node_modules/zone.js/dist/zone-node.js?:871:34)
at ZoneDelegate.invokeTask (webpack:///./node_modules/zone.js/dist/zone-node.js?:420:31)
at Zone.runTask (webpack:///./node_modules/zone.js/dist/zone-node.js?:187:47)
at drainMicroTaskQueue (webpack:///./node_modules/zone.js/dist/zone-node.js?:594:35)
at ZoneTask.invokeTask (webpack:///./node_modules/zone.js/dist/zone-node.js?:499:21)
at ZoneTask.invoke (webpack:///./node_modules/zone.js/dist/zone-node.js?:484:48) code: 'MODULE_NOT_FOUND' } Error: Cannot find module './apps/morningharwood/src/app/app.component'
at webpackEmptyContext (webpack:///._sync?:2:10)
at eval (webpack:///./prerender.ts?:126:126)
at ZoneDelegate.invoke (webpack:///./node_modules/zone.js/dist/zone-node.js?:387:26)
at Zone.run (webpack:///./node_modules/zone.js/dist/zone-node.js?:137:43)
at eval (webpack:///./node_modules/zone.js/dist/zone-node.js?:871:34)
at ZoneDelegate.invokeTask (webpack:///./node_modules/zone.js/dist/zone-node.js?:420:31)
at Zone.runTask (webpack:///./node_modules/zone.js/dist/zone-node.js?:187:47)
at drainMicroTaskQueue (webpack:///./node_modules/zone.js/dist/zone-node.js?:594:35)
at ZoneTask.invokeTask (webpack:///./node_modules/zone.js/dist/zone-node.js?:499:21)
at ZoneTask.invoke (webpack:///./node_modules/zone.js/dist/zone-node.js?:484:48)
模块路径相对于当前模块解析,文件系统路径相对于当前工作目录解析。
考虑到路径已成功与 glob 匹配,apps/...
是相对于当前工作目录的,可能是项目根目录。依赖当前工作目录本身就是有问题的做法。为了保持一致性,可以明确说明 glob 和模块路径。
可以是:
const componentPaths = await globby('../apps/morningharwood/src/app/**/*.component.ts', { cwd: __dirname });
for (const path of componentPaths) {
const ref = await import(path.join('./', path));
...
或者:
const componentPaths = await globby(path.join(__dirname, '../apps/morningharwood/src/app/**/*.component.ts'));
for (const path of componentPaths) {
const ref = await import(path);
...