我想合并一些文件(具有静态导入的文件),但有些文件不需要合并(具有动态导入的文件)。
我怎么才能告诉typescript呢?
该演示包含4个文件,其中一个索引包含main()函数,以及3个类A、B和C。
当前typescript配置:
{
"compilerOptions": {
"allowJs": true,
"target": "es2017",
"module": "esnext",
"outDir": "lib",
"sourceMap": false,
"strict": true,
"noImplicitAny": false,
"removeComments": true,
"lib": [
"esnext"
],
},
"compileOnSave": true,
"include": [
"src/**/*.ts",
"src/**/*.js"
],
"exclude": [
"node_modules",
"old"
]
}src/index.ts
'use strict';
//@ts-check
import { C } from './c/c'; // I want this one to be merged in index
export const main = async () => {
const c = new C();
c.log();
}
main();src/c.ts
'use strict';
//@ts-check
import { B } from '../b/b'; // I want this one to be merged in c, so in index
export class C {
async log() {
const A = await import('/' + 'a.js');
const a = new A();
a.log();
const b = new B();
b.log();
}
}源/b.ts
'use strict';
//@ts-check
export class B {
async log() {
console.log('b');
}
}src/a.ts
'use strict';
//@ts-check
export class A {
log() {
console.log('a');
}
}目前我得到的是:
- lib/a/a.js
- lib/b/b.js
- lib/c/c.js
- lib/index.js但我想:
- lib/a.js // Apart from index, because it is dynamically imported
- lib/index.js有什么想法吗?
有没有什么装饰器可以告诉typescript把c和b合并到索引中,然后把a放在一边?
尽可能地,我不想添加任何构建层(比如webpack),只使用typescript。
发布于 2018-09-24 00:56:22
我确信只有TypeScript编译器是无法做到这一点的。(您可以使用项目引用和outFile手动合并文件组,但您仍然需要一个AMD或SystemJS模块运行时。)您将需要使用单独的工具,如Webpack。这是理所当然的,但重要的是TypeScript团队的意见。
https://stackoverflow.com/questions/52462324
复制相似问题