简体   繁体   English

从包访问流星应用程序的imports目录?

[英]Accessing meteor application's imports directory from a package?

Meteor application directory layout: 流星应用程序目录布局:

imports/
  api/
    collections/
      MyCollectionFile.js

packages/
  mypackage/
      mypackageMain.js

I can export anything from the package file and use it inside the application, that's ok. 我可以从包文件中导出任何内容,然后在应用程序中使用它,这没关系。 But how can I use "import" in the package, like the other way around? 但是,如何像其他方法那样在包中使用“导入”呢?

// mypackageMain.js
if (Meteor.isServer) {
    require ('/imports/api/collections/MyCollectionFile.js');
};

OR 要么

import '/imports/api/collections/MyCollectionFile.js';

I tried using the path '../../imports/api/collections/MyCollectionFile.js' but it simply does not work. 我尝试使用路径“ ../../imports/api/collections/MyCollectionFile.js”,但它根本无法正常工作。 I can not access this file from a package. 我无法从包访问此文件。

I get the following Error for both the import and the require: 对于导入和需求,我都收到以下错误消息:

W20160618-23:25:59.486(3)? (STDERR) Error: Cannot find module '../../imports/api/collections/MyCollectionFile.js'
W20160618-23:25:59.487(3)? (STDERR)     at require (packages/modules-runtime/.npm/package/node_modules/install/install.js:85:1)

Figured out that this was not possible. 弄清楚这是不可能的。

However, moving the collections to a package and exporting them would make the collections available to other packages and the application. 但是,将集合移至包并导出它们将使该集合可用于其他包和应用程序。

I've also been running in to this issue and have found two solutions, both are sub-par though. 我也一直在处理这个问题,并且找到了两个解决方案,尽管都没有达到标准。

  1. Install the thing you want to import as a npm package. 将要导入的东西安装为npm软件包。 $npm install --save ./imports/<the thing> . $npm install --save ./imports/<the thing>
  2. Use npm link to create a link to the thing you want to import. 使用npm link创建指向您要导入的内容的链接。

Both solutions require that you have a package.json in the directory you want to import and both won't be transpile the code and just run it against the provided version of node. 两种解决方案都要求您在要导入的目录中具有package.json ,并且都不会转译代码,而只能在提供的node版本上运行它。

A possible solution for the transpile issue would be using a loader plugin, or somehow provide a additional configuration to System.js to tell it to transpile the code on import. 解决该问题的可能方法是使用加载程序插件,或者以某种方式为System.js提供额外的配置,以指示其在导入时对代码进行翻译。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM