使用 ES6 库的 ES5 模块,无需在我自己的项目中使用 rollup 进行编译

Use ES6 library's ES5 module without compiling it on my own project with rollup

我需要使用 ES6 库 (Luxon) 并想将文件编译成 ES5,但 Rollup 将文件添加为 ES6。

该库有一个特殊的 /build 文件夹,其中包含不同的输出格式。

我如何配置 Rollup 以利用它而不是对库不做任何事情?

首先,你有两个选择:

我将采用第二种方法,因为这是您要求的方法:

  1. 安装插件npm i @rollup/plugin-alias
  2. rollup.config.js导入它import alias from '@rollup/plugin-alias';
  3. 最后添加到plugins:

const path = require('path');
module.exports = {
  input: 'src/index.js',
  output: {
    dir: 'output',
    format: 'cjs'
  },
  plugins: [
    alias({
      entries: [
        { find: 'luxon', replacement: path.resolve(process.cwd(), 'node_modules/luxon/build') },

      ]
    })
  ]
};