angular - 在 tsconfig.json 中将模块更改为 "es2015" 时,运行 protractor 会出现 "Unexpected token import"
我正在尝试运行 Protractor,但在为我的 Angular 2 应用程序转译 TypeScript 时使用了 ES2015 模块。这对应用程序本身工作正常,但在 Protractor 中失败了。
当在我的 tsconfig 中使用模块 es2015 时,我收到以下错误:
(node:9096) DeprecationWarning: os.tmpDir() is deprecated. Use os.tmpdir() instead.
[15:23:36] I/direct - Using ChromeDriver directly...
[15:23:36] I/launcher - Running 1 instances of WebDriver
[15:23:40] E/launcher - Error: C:\xxx\test1.e2e.ts:1
(function (exports, require, module, __filename, __dirname) { import { browser, element, by, $ } from 'protractor';
^^^^^^
SyntaxError: Unexpected token import
at Object.exports.runInThisContext (vm.js:78:16)
at Module._compile (module.js:543:28)
at Module.m._compile (C:\xxx\ts-node\src\index.ts:406:23)
at Module._extensions..js (module.js:580:10)
at Object.require.extensions.(anonymous function) [as .ts] (C:\vmob-apps\purple-chicken\node_modules\ts-node\src\index.ts:409:12)
at Module.load (module.js:488:32)
at tryModuleLoad (module.js:447:12)
at Function.Module._load (module.js:439:3)
at Module.require (module.js:498:17)
at require (internal/module.js:20:19)
[15:23:40] E/launcher - Process exited with error code 100
这是我的 tsconfig.json
{
"compilerOptions": {
"baseUrl": "",
"declaration": false,
"target": "es5",
"mapRoot": "./",
"module": "es2015",
"moduleResolution": "node",
"experimentalDecorators": true,
"emitDecoratorMetadata": true,
"sourceMap": true,
"typeRoots": [
"node_modules/@types"
],
"types": [
"jasmine",
"node"
],
"lib": [
"dom",
"es2015"
]
},
"exclude": [
"node_modules",
"dist",
"**/*.ngfactory.ts",
"**/*.shim.ts",
"src/main*.ts"
],
"awesomeTypescriptLoaderOptions": {
"forkChecker": true,
"useWebpackText": true
},
"angularCompilerOptions": {
"genDir": ".",
"entryModule": "src/app/app.module#AppModule"
}
}
当将其更改为 commonjs 时,它可以正常工作。
我需要在我的 Protractor 配置中添加什么来支持它吗?或者有办法让我指定一个单独的 tsconfig 文件吗?
谢谢!
编辑:我已经弄清楚如何针对一个单独的 tsconfig 文件,你可以通过 ts-node 的 require 并传入一些选项来实现。ts-node 实际上是用来转译 TypeScript 的,之前一直很难搞明白是什么让它神奇地进行转译。
require('ts-node').register({ lazy: true, project: './config/tsconfig-commonjs.json' });