简体   繁体   English

使用 Typescript 时将捆绑 node_modules 汇总到应用程序中

[英]Rollup bundle node_modules into app when using Typescript

I have a Typescript application.我有一个 Typescript 应用程序。 I want to bundle the dependencies inside node_modules into the resulting bundle.我想将 node_modules 中的依赖项捆绑到生成的包中。 Here's what I've tried so far based on these posts ( Does rollup bundle node_modules into bundle.js? , Does rollup bundle node_modules into bundle.js? ).到目前为止,这是我根据这些帖子尝试过的( Does rollup bundle node_modules into bundle.js?Does rollup bundle node_modules into bundle.js? )。

Here's my rollup config and the simple file I've tried bundling:这是我的汇总配置和我尝试捆绑的简单文件:

import typescript from "@rollup/plugin-typescript";
import resolve from "@rollup/plugin-node-resolve";
import commonjs from "@rollup/plugin-commonjs";
import globals from "rollup-plugin-node-globals";
import builtins from "rollup-plugin-node-builtins";

export default {
  input: "src/test.ts",
  output: {
    dir: "dist/",
    format: "iife",
  },
  plugins: [
    resolve({jsnext: true}),
    commonjs({ include: ["src/test.ts", "node_modules/**"] }),
    // I need to polyfill some node stuff
    globals(),
    builtins(),
    typescript({ noEmitOnError: false, outDir: "dist/" }),
  ],
};

The file:文件:

import * as obj from 'some-module'
console.log(obj)

How can I get dependencies in node_modules to be bundled into the final output when using rollup + typescript?使用汇总 + typescript 时,如何将 node_modules 中的依赖项捆绑到最终的 output 中? I suspect the issue is that the Typescript plugin is outputting files which use require and rollup cannot recognize require .我怀疑问题是 Typescript 插件正在输出使用require和 rollup 无法识别require的文件。

In package.json , make sure the dependencies you want to include is registered in dependencies property not peerDependencies .package.json中,确保要包含的依赖项注册在dependencies属性而不是peerDependencies中。 Rollup will bundled them in the output then.然后汇总将它们捆绑在 output 中。

You need to configure TypeScript to output ES modules for it to work.您需要将 TypeScript 配置为 output ES 模块才能工作。 Set module to "ESNext" in your tsconfig.json:在 tsconfig.json 中将模块设置为“ESNext”:

{
    "compilerOptions": {
      "module": "ESNext"
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM