使用相对导入路径时 Aurelia 捆绑失败
Aurelia bundling fails when using relative import path
我将 aurelia 与 typescript 一起使用,我想避免使用像这样的相对导入路径:
import { DialogBox } from '../../resources/elements/dialog-box';
而是
import { DialogBox } from 'resources/elements/dialog-box';
我修改了 tsconfig.json 以便编译器通过添加 baseUrl 和 paths 来处理相对路径,如下所示:
"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" 导入文件实际上都是从具有相对路径的其他文件导入的。所以我想捆绑器是从那里找到它们的。这解决了一件事,但基本问题仍然存在:当有 "root-relative" 导入时,aurelia-cli 无法捆绑...
为解决方案编辑:
在此感谢思南博雷的解决方案,通过更新部分包解决了相对路径问题:
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
看看这个 Gist example 其中:
- 我在
src/lib/init.ts
中创建了一个classInit
- 我
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
中的结果
我将 aurelia 与 typescript 一起使用,我想避免使用像这样的相对导入路径:
import { DialogBox } from '../../resources/elements/dialog-box';
而是
import { DialogBox } from 'resources/elements/dialog-box';
我修改了 tsconfig.json 以便编译器通过添加 baseUrl 和 paths 来处理相对路径,如下所示:
"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" 导入文件实际上都是从具有相对路径的其他文件导入的。所以我想捆绑器是从那里找到它们的。这解决了一件事,但基本问题仍然存在:当有 "root-relative" 导入时,aurelia-cli 无法捆绑...
为解决方案编辑: 在此感谢思南博雷的解决方案,通过更新部分包解决了相对路径问题:
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
看看这个 Gist example 其中:
- 我在
src/lib/init.ts
中创建了一个class - 我
import init from 'lib/init'
在src/main.ts
没有相对路径 - 我将
main.ts
更改为import environment from 'environment'
而不是from './environment'
-- 这也有效。
Init
使用 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
中的结果