使用汇总生成打字稿定义文件

问题描述 投票:1回答:1

我正在尝试汇总js来构建我的打字稿项目,但我不知道如何生成定义文件以及如何将它们自动包含在dist文件中。

有谁知道怎么做?

这是我的rollup.config.js

import typescript from "rollup-plugin-typescript";
import handlebars from "rollup-plugin-handlebars";
import babel from 'rollup-plugin-babel';

export default {
  entry: 'src/generator.ts',
  format: 'cjs',
  plugins: [
    typescript(),
    handlebars(),
    babel()
  ],
  dest: 'dist/bundle.js'
};

我正在使用默认的ts配置,但是与declaration = true相同。

编辑:

还尝试使用Webpack:

    module.exports = {
      context: __dirname + '/src',
      entry: {
        index: './generator'
      },
      output: {
        path: __dirname + '/build',
        publicPath: '/',
        filename: 'generator.js'
      },
      resolve: {
        root: __dirname,
        extensions: ['', '.ts', '.js']
      },
      module: {
        loaders: [
          { test: /\.ts$/, loaders: ['ts-loader'], exclude: /node_modules/ },
          { test: /\.hbs/, loaders: ['handlebars-loader'], exclude: /node_modules/ }
        ]
      }
    }

Tsconfig:

    {
      "compilerOptions": {
        "target": "es5",
        "module": "commonjs",
        "declaration": true,
        "outDir": "build"
      },
      "exclude": [
        "node_modules",
        "dist",
        "build"
      ]
    }

生成d.ts看起来像这样:

    import { ExportPageModel } from './models/page-model';
    export declare type ExportType = 'text' | 'html';
    export * from './models/page-model';
    export declare namespace Generator {
        function generateHtml(page: ExportPageModel): string;
        function generateText(page: ExportPageModel): string;
    }

但在我使用该软件包的应用程序中,它无法找到Generator ......

import { ExportPageModel, Generator } from 'emlb-generator';

生成器未定义但自动完成工作正常,所以我找不到问题所在:(

Generator.generateHtml({
 ...
});
typescript webpack typescript-typings rollup
1个回答
1
投票

使用汇总的typescript定义文件

强烈建议您使用tsc进行编译。最终应用程序捆绑的预留汇总。

Example

例如,在typestyle https://github.com/typestyle/typestyle/blob/2349f847abaaddaf3de4ca83f585d293b766959e/package.json#L10中检查构建

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.