【问题标题】:Call a static method on a class from a list of paths inside a node script?从节点脚本内的路径列表中调用类的静态方法?
【发布时间】:2018-08-12 02:30:14
【问题描述】:

问题:

如何获取文件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)

【问题讨论】:

    标签: node.js typescript import ts-node dynamic-import


    【解决方案1】:

    模块路径相对于当前模块进行解析,文件系统路径相对于当前工作目录进行解析。

    考虑到路径已被 glob 成功匹配,apps/... 相对于当前工作目录,可能是项目根目录。依赖当前工作目录本身就是有问题的做法。为了保持一致性,全局和模块路径都可以明确说明。

    可以是:

      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);
        ...
    

    【讨论】:

    • 嘿estus 感谢您的帮助。我已经用更多反馈更新了这个错误。我仍然无法让它工作。有什么想法吗?
    • import('./apps/morningharwood/src/app/app.component') 有效吗?这个问题没有提到你使用 Webpack。这不是一个常见的场景,因为通常你不需要它,特别是 Node 或 ts-node。预计动态导入不适用于 Webpack,因为捆绑的模块在构建时应该是已知的。
    • 啊,我不认为 webpack 是问题的一部分。是的,我正在使用 webpack。是的,导入确实有效。我将重构并把这个任务从 webpack 中拿出来。是最好的解决方案吗?
    • 我建议从这个设置中放弃 Webpack,除非你大量使用 Webpack 特定的功能。 ES 模块将以这种方式回退到 Node require。否则,您可以使用特定于 Webpack 的方法,例如require.context 而不是 import,例如参见 stackoverflow.com/questions/29421409/…
    • 是的,我刚刚为这部分删除了 webpack。这没有意义。我从上一个问题stackoverflow.com/questions/51797603/… 中混合了各种方法,我现在明白为什么它不起作用了。我将您的答案标记为正确。如果你想用上面的评论上下文更新你的答案,那就太棒了!或者我可以删除这个
    猜你喜欢
    • 2021-02-10
    • 1970-01-01
    • 2012-12-19
    • 1970-01-01
    • 1970-01-01
    • 2012-12-16
    • 2012-05-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多