---
# System prepended metadata

title: 【筆記】 Rollup
tags: [前端工具]

---

---
tags: 前端工具
---
【筆記】 Rollup
===
```
rollup -c
```
- rollup.config.js
```javascript=
import alias from '@rollup/plugin-alias'
import multi from '@rollup/plugin-multi-entry'
import { nodeResolve } from '@rollup/plugin-node-resolve';
import commonjs from '@rollup/plugin-commonjs';
export default {
  input: [
    'src/test2/**/*.js',
    'src/test/**/*.js'
  ],
  output: [{
    file: __dirname + '/xxxxxx.js',
  }],
  plugins: [
    nodeResolve(),
    commonjs(),
    multi(),
    alias({
        entries: [
          { find: '@src', replacement: __dirname + '/src' },
        ]
    })
  ]
};
```

- 套件
    - @rollup/plugin-alias: 使用別名
    - @rollup/plugin-multi-entry: 多個進入點

- 多個output
```javascript=
const arry = [
  'configs',
  'helper'
]

export default arry.map((name)=> ({
  input: `src/${name}/index.js`,
  output: {
    file: __dirname + `/${name}.js`,
  },
  plugins: [
    alias({
        entries: [
          { find: '@', replacement: __dirname + '/src' },
        ]
    })
  ]
}))
```

```javascript=
const arry = [
  {
    folderName: 'aaa',
    files: [
      'index',
    ]
  },
  {
    folderName: 'bbb',
    files: [
      'xxx',
      'yyy',
      'zzz',
    ]
  },
  ...
]

let a = []
arry.forEach(folder => {
  const folderName = folder.folderName
  folder.files.forEach(fileName => {
    a.push({
      folder: folderName,
      fileName: fileName
    })
  }) 
})

export default a.map(
  (item) => (
    {
      input: `src/${item.folder}/${item.fileName}.js`,
      output: {
        file: `dist/${item.folder}/${item.fileName}.js`,
      },
      plugins: [
        nodeResolve(),
        commonjs(),
        json(),
        alias({
            entries: [
              { find: '@root', replacement: __dirname},
              { find: '@', replacement: __dirname + '/src' },
            ]
        })
      ]
    }
  )
)

```