使用 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 以利用它而不是对库不做任何事情?
首先,你有两个选择:
- 要么使用 Rollup 编译项目中的库,要么
@rollup/plugin-babel
- 或者引用包的
/build
目录而不是ES6
包的版本,使用 @rollup/plugin-alias 的别名
我将采用第二种方法,因为这是您要求的方法:
- 安装插件
npm i @rollup/plugin-alias
- 在
rollup.config.js
导入它import alias from '@rollup/plugin-alias';
- 最后添加到
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') },
]
})
]
};
我需要使用 ES6 库 (Luxon) 并想将文件编译成 ES5,但 Rollup 将文件添加为 ES6。
该库有一个特殊的 /build
文件夹,其中包含不同的输出格式。
我如何配置 Rollup 以利用它而不是对库不做任何事情?
首先,你有两个选择:
- 要么使用 Rollup 编译项目中的库,要么 @rollup/plugin-babel
- 或者引用包的
/build
目录而不是ES6 包的版本,使用 @rollup/plugin-alias 的别名
我将采用第二种方法,因为这是您要求的方法:
- 安装插件
npm i @rollup/plugin-alias
- 在
rollup.config.js
导入它import alias from '@rollup/plugin-alias';
- 最后添加到
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') },
]
})
]
};