tsc
優點:可以生成.d.ts類型聲明文件。
tsconfig.json:
{
"compilerOptions": {
"outDir": "dist",
"declaration": true,
"declarationDir": "dist",
},
"files": ["src/index.ts"],
}
缺陷:不能與webpack配合使用。當然,也不是完全不能,畢竟都有watch模式。
webpack + ts-loader
似乎可以使用tsconfig.json文件。
webpack + babel-loader + @babel/plugin-transform-typescript預設
如果使用TypeScript(JavaScript的類型化超集),則建議使用此預設。它包含以下插件:
@babel/plugin-transform-typescript
該插件增加了對TypeScript編程語言使用的語法的支持。但是,此插件未添加對傳遞給它的JavaScript進行類型檢查的功能。為此,您將需要安裝和設置TypeScript。
缺陷:
您對tsconfig.json的更改不會反映在babel中。
Changes to your tsconfig.json are not reflected in babel. The build process will always behave as though isolatedModules is turned on, there are Babel-native alternative ways to set a lot of the tsconfig.json options however.
@see https://babeljs.io/docs/en/babel-plugin-transform-typescript#caveats
我似乎走錯了方向
之前我的處理方法是,ts和js都用babel處理,babel添加了@babel/plugin-transform-typescript預設。
但是我覺得可以讓ts先用ts-loader處理,處理完之后可以主動聲明讓babel-loader處理,但如果不聲明,會被babel處理嗎,畢竟處理后的文件后綴為.js?