使用相对导入路径时,Aurelia捆绑失败

时间:2017-02-23 09:23:43

标签: bundle aurelia relative-path aurelia-cli

我使用aurelia和typescript,我想避免使用相对导入路径,如:

import { DialogBox } from '../../resources/elements/dialog-box';

而是

import { DialogBox } from 'resources/elements/dialog-box';

我修改了我的tsconfig.json,所以编译器通过添加 baseUrl 路径来处理相对路径,如下所示:

"compilerOptions": {
"sourceMap": true,
"target": "es5",
"module": "amd",
"declaration": false,
"noImplicitAny": false,
"removeComments": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"moduleResolution": "node",
"lib": ["es2015", "dom"],
"baseUrl": ".",
"paths": {
  "*":["src/*"]
}

} ...

但是,当我运行cli的命令“运行 - 观察”时,我可以看到所有步骤都正常工作,直到 writeBundle 步骤在跟踪时失败一些文件:

Starting 'processMarkup'...
Starting 'processCSS'...
Starting 'configureEnvironment'...
Finished 'configureEnvironment'
Starting 'buildTypeScript'...
Finished 'processCSS'
Finished 'processMarkup'
Finished 'buildTypeScript'
Starting 'writeBundles'...

该过程失败,并出现以下错误:

Tracing resources/elements/dialog-box...
{ uid: 11,
  name: 'writeBundles',
  branch: false,
  error:
   { [Error: ENOENT: no such file or directory, open 'C:\...\src\resources\elements\dialog-box.js']
     errno: -4058,

奇怪的是:还有其他文件使用非相对路径引用,并且捆绑器不会失败。

另一个奇怪的事情是:如果我使用观察者离开相对路径和捆绑,一切正常。然后,如果我从有问题的导入中删除相对的' ../../' ,我会收到捆绑警告,但一切都有效...

知道我可能做错了吗?

编辑修正:

我只是说明了为什么有些文件似乎捆绑在一起而其他文件却没有。我注意到所有文件都带有" root-relative"实际上没有失败的导入实际上是从具有相对路径的其他文件导入的。所以我想捆绑者会从那里找到它们。这解决了一件事,但基本问题仍然存在:当存在" root-relative"时,aurelia-cli无法捆绑。进口...

为解决方案编辑: 感谢Sinan Bolel的解决方案,通过更新一些软件包解决了相对路径问题:

npm i -D gulp-typescript@^3.1.5 typings@^2.1.0 aurelia-tools@^1.0.0

之后我得到的语义错误来自一些仍然安装且不需要的打字,以及将打字稿作为本地npm包以及全局安装。 我卸载了它们,所有错误都消失了。

npm uninstall @types/es6-promise
npm uninstall @types/es6-collections
npm uninstall typescript

1 个答案:

答案 0 :(得分:1)

看一下这个Gist example

  • 我在 Init
  • 中创建了一个班级src/lib/init.ts
  • import init from 'lib/init' <{1}} <{1}} 没有相对路径
  • 我将 src/main.ts 更改为main.ts而不是import environment from 'environment' - 这也有效。


使用original tsconfig generated by the CLI,我的构建失败并显示错误:

from './environment'

更改为tsconfig in my Gist后,构建成功。

(无效)建议:

在tsconfig中,您可以尝试一下:

a)在src/main.ts(3,18): error TS2307: Cannot find module 'lib/init'. ./之前添加src(这解决了我机器上的问题)

compilerOptions.paths

b)添加 paths: {"*": ["./src/*"]} ^

filesGlob

编辑以前的建议不起作用,如何更新套餐:

"filesGlob": [
  "./src/**/*.ts",
  "./test/**/*.ts",
  "./typings/index.d.ts",
  "./custom_typings/**/*.d.ts"
],

https://github.com/aurelia/cli/issues/494#issuecomment-282103289

中查看结果