※TypeScript公式を見ると、質問あるならStack Overflowでやれと書いてあったので、向こうでも聞いてきます。答えが分かったら愛する日本のためにここにも書きます。
前提・実現したいこと
TypeScriptで構成したプロジェクトにおいて、一部のファイルや関数に限ってd.tsをビルドにより生成し、それをバンドルしたファイルを作成したいです。
生成するファイルのイメージは以下です。
folder
1out 2┣ bundle.js // 全てのコードがバンドルされた実行用JSファイル 3┗ bundle.d.ts // 【見せたいもの】のみの型定義がすべてバンドルされたファイル
npmパッケージとして、動作するJSとinterfaceとなるTS用の型情報だけ提供したいというのが狙いです。
ビルド対象のツリー構成
folder
1src 2┣ main.ts // sub1,sub2をimport 3┣ sub 4┃ ┣ sub1.ts // この中の一部のfunction/classの型情報だけd.tsに出したい 5┃ ┗ sub2.ts // 型情報出力したくない(exportしているものはいくつかある) 6┣ tsconfig.json // "declaration": trueにすると、全部でてきてしまいます。 7┗ webpack.config.js // jsファイルはバンドルできますが、d.tsがバンドルできません。
configファイルの内容
webpack.config.js
javascript
1/** ビルドの共通設定 */ 2module.exports = { 3 mode: "production", 4 entry: { 5 index: "./src/main.ts" 6 }, 7 output: { 8 filename: "[name].js", 9 path: __dirname + "/out" 10 }, 11 12 resolve: { 13 // Add '.ts' and '.tsx' as resolvable extensions. 14 extensions: [".ts", ".js", ".json"], 15 }, 16 17 module: { 18 rules: [ 19 { 20 test: /.ts$/, 21 exclude: /node_modules|vue/src/, 22 use: [ 23 { 24 loader: 'ts-loader', 25 options: { 26 appendTsSuffixTo: [/.vue$/] 27 } 28 }], 29 } 30 ] 31 }, 32};
tsconfig.json
json
1{ 2 "compilerOptions": { 3 "target": "es5", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017','ES2018' or 'ESNEXT'. */ 4 "module": "commonjs", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */ 5 "declaration": true, /* Generates corresponding '.d.ts' file. */ 6 "sourceMap": true, /* Generates corresponding '.map' file. */ 7 "outDir": "./out/", /* Redirect output structure to the directory. */ 8 "rootDir": "./src/", /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */ 9 "strict": true, /* Enable all strict type-checking options. */ 10 "esModuleInterop": true /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */ 11 } 12}
補足情報(FW/ツールのバージョンなど)
- tsc: Version 3.2.4
- webpack: Version 4.29.0
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
退会済みユーザー
2019/01/30 05:32