【问题标题】:Use ES6 library's ES5 module without compiling it on my own project with rollup使用 ES6 库的 ES5 模块,无需在我自己的项目中使用 rollup 编译它
【发布时间】:2020-11-14 13:06:03
【问题描述】:

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

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

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

【问题讨论】:

  • 你能解释清楚吗?

标签: javascript node.js ecmascript-6 rollup luxon


【解决方案1】:

首先,这里有两个选择:

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

  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') },

      ]
    })
  ]
};

【讨论】:

  • 这个解决方案最初并不适合我。事实证明,如果您使用的是@rollup/plugin-commonjs 和/或@rollup/plugin-node-resolve,则需要在别名插件之后加载它们。否则在进入别名插件之前,条目将由他们处理。
猜你喜欢
  • 1970-01-01
  • 2019-03-19
  • 1970-01-01
  • 2021-04-15
  • 1970-01-01
  • 2020-12-27
  • 1970-01-01
  • 2020-03-20
  • 1970-01-01
相关资源
最近更新 更多