【问题标题】:Aurelia bundling fails when using relative import path使用相对导入路径时,Aurelia 捆绑失败
【发布时间】:2017-02-23 09:23:43
【问题描述】:

我将 aurelia 与 typescript 一起使用,我想避免使用相对导入路径,例如:

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

而是

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

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

"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 的命令 'au run --watch' 时,我可以看到所有步骤都可以正常工作,直到跟踪某些文件时失败的 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”导入的文件实际上都是从具有相对路径的其他文件中导入的。所以我想捆绑商会从那里找到它们。这解决了一件事,但基本问题仍然存在:当存在“根相对”导入时,aurelia-cli 捆绑失败......

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

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

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

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

【问题讨论】:

标签: bundle relative-path aurelia


【解决方案1】:

看看这个Gist example,其中:

  • 我在src/lib/init.ts中创建了一个类Init
  • import init from 'lib/init'src/main.ts 没有相对路径
  • 我将 main.ts 更改为 import environment from 'environment' 而不是 from './environment' -- 这也可以。


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

src/main.ts(3,18): error TS2307: Cannot find module 'lib/init'.

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

(无效)建议:

在tsconfig中,你能不能试试:

a) 在compilerOptions.paths 中在src 之前添加./(这解决了我机器上的问题)

  paths: {"*": ["./src/*"]}
                 ^

b) 添加filesGlob

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

编辑:以前的建议不起作用,更新包怎么样:

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

https://github.com/aurelia/cli/issues/494#issuecomment-282103289查看结果

【讨论】:

  • 感谢您的意见。不幸的是,这并不能解决我的问题。打字稿编译没有问题,只是捆绑有问题。
  • 通过github.com/aurelia/cli/issues/494#issuecomment-282103289 -- npm i -D gulp-typescript@^3.1.5 typings@^2.1.0 aurelia-tools@^1.0.0
  • 我很抱歉@Gaet 我没有很好地阅读你的问题?让我知道更新是否有帮助!
  • 谢谢!相对路径现在似乎可以正常工作了!正如我在 github 问题上所说,我最终遇到了很多打字稿错误,但这并没有阻止编译和捆绑。当这个问题也得到解决时,我会在这里发布更多信息。
  • 谢谢@Gaet!我会做公关。
猜你喜欢
  • 2016-10-27
  • 2019-11-25
  • 1970-01-01
  • 1970-01-01
  • 2011-01-29
  • 1970-01-01
  • 2015-08-07
  • 2016-03-05
  • 1970-01-01
相关资源
最近更新 更多