在使用新的TypeScript功能(所谓的ES Dynamic Imports)时,我无法使用ts-node
在服务器端运行我的同构应用程序的代码。TypeScript ES dynamic`import()`
当使用webpack模块加载器,它以自己的方式传输代码并在浏览器中运行生成的文件时,似乎不会发生错误。
我已经得到了错误:
case 0: return [4 /*yield*/, import("./component/main")];
^^^^^^
SyntaxError: Unexpected token import
通常打字稿transpiles的import
表达类似的东西:Promise.resolve(require("./component/main"))
,但我看不到它。
如何解决这个问题?它与ts-node
有什么共同点?或者node.js
有一个“polyfill”?
我tsconfig.json
文件:
{
"compilerOptions": {
"declaration": false,
"emitDecoratorMetadata": true,
"allowJs": false,
"experimentalDecorators": true,
"importHelpers": true,
"inlineSourceMap": false,
"inlineSources": false,
"lib": [
"DOM",
"ES5",
"ES6",
"ES7"
],
"listFiles": false,
"module": "commonjs",
"noEmitOnError": true,
"noImplicitAny": true,
"noImplicitReturns": true,
"noImplicitThis": true,
"preserveConstEnums": false,
"pretty": false,
"removeComments": false,
"strict": true,
"target": "es5"
}
}
代码:
import * as m from "mithril";
import LayoutComponent from "./component/layout";
const render = (
layout: m.ComponentTypes<any, any>,
) => ({ tag, attrs }: m.Vnode<any, any>) => m(layout, attrs, m(tag as any, attrs));
export default {
"/:path...": {
onmatch: async (args, path) => (await import("./component/main")).default,
render: render(LayoutComponent),
},
} as m.RouteDefs;
为了得到这个工作,我必须明确地传递了'--compiler'参数TS-节点。使它使用正确版本的打字稿。 –
我会尝试,所以它不使用项目的默认/安装在node_modules? – Roomy
它没有工作,对不起 – Roomy