简体   繁体   English

如何避免 Rollup 入口点之间的代码重复?

[英]How to avoid code duplication between entry points in Rollup?

I want to configure rollup to take a bunch of files on input & produce a bunch of files in dist that share some common code between them.我想配置 rollup 以在输入时获取一堆文件并在dist中生成一堆文件,这些文件在它们之间共享一些公共代码

Here's the rollup config I use:这是我使用的汇总配置:

import path from 'path';
import pathsTransformer from 'ts-transform-paths';
import alias from '@rollup/plugin-alias';
import commonjs from '@rollup/plugin-commonjs';
import { nodeResolve } from '@rollup/plugin-node-resolve';
import typescript from 'rollup-plugin-typescript2';
import peerDepsExternal from 'rollup-plugin-peer-deps-external';

const plugins = [
  peerDepsExternal(),
  alias({
    entries: [
      { find: '@', replacement: path.join(__dirname, '/src') },
      { find: '$root', replacement: __dirname },
    ],
  }),
  nodeResolve(),
  typescript({
    transformers: [() => pathsTransformer()],
  }),
  commonjs({
    extensions: ['.js', '.ts'],
  }),
];

export default [
  {
    input: './src/a.ts',
    output: {
      file: 'dist/a.js',
      format: 'esm',
      sourcemap: true,
    },
    plugins,
  },
  {
    input: './src/b.ts',
    output: {
      file: 'dist/b.js',
      format: 'esm',
      sourcemap: true,
    },
    plugins,
  },
];

The problem with this set-up is that if a.ts & b.ts both depend on some code, this common code gets bundled into each output file unnecessarily increasing the bundle size.此设置的问题在于,如果a.tsb.ts都依赖于某些代码,则此公共代码会捆绑到每个 output 文件中,从而不必要地增加捆绑包的大小。

Since the output.format is esm (so, import s are available in the output), I'd rather expect rollup to split the shared code between the 2 files in a separate chunk & then make both files import that common code ( which seems to be the thing rollup does by default anyway ).由于output.formatesm (因此, import s 在输出中可用),我宁愿期望汇总将 2 个文件之间的共享代码拆分为一个单独的块,然后使两个文件都import该公共代码(这似乎无论如何都是汇总默认做的事情)。

I assume the problem to be somewhere around nodeResolve or commonjs calls, but, I want my dependencies to be bundled.我假设问题出在nodeResolvecommonjs调用周围,但是,我希望捆绑我的依赖项。 I just don't want to have them duplicated .我只是不想让它们重复

How do I optimize my output?如何优化我的 output? Here's a reproduction to visualize it ( dist included).这是一个可视化的复制品(包括dist )。

By returning an array of objects, you specify to rollup that you want to have independent bundles.通过返回一个对象数组,您可以向 rollup 指定您想要拥有独立的包。 If you just want to produce multiple files (which was exactly my case) you can just specify input as an object.如果您只想生成多个文件(这正是我的情况),您只需将input指定为 object。

import path from 'path';
import pathsTransformer from 'ts-transform-paths';
import alias from '@rollup/plugin-alias';
import commonjs from '@rollup/plugin-commonjs';
import { nodeResolve } from '@rollup/plugin-node-resolve';
import typescript from 'rollup-plugin-typescript2';
import peerDepsExternal from 'rollup-plugin-peer-deps-external';

const plugins = [
  peerDepsExternal(),
  alias({
    entries: [
      { find: '@', replacement: path.join(__dirname, '/src') },
      { find: '$root', replacement: __dirname },
    ],
  }),
  nodeResolve(),
  typescript({
    transformers: [() => pathsTransformer()],
  }),
  commonjs({
    extensions: ['.js', '.ts'],
  }),
];

export default   {
  input: {
    a: './src/a.ts',
    b: './src/b.ts',
  },
  output: {
    dir: 'dist',
    format: 'esm',
    sourcemap: true,
  },
  plugins,
};

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

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