(options: CreatePluginsOptions)
| 28 | * 创建 Rollup 插件配置 |
| 29 | */ |
| 30 | export function createPlugins(options: CreatePluginsOptions): RollupPlugin[] { |
| 31 | const { config, minify = false, cwd = process.cwd() } = options |
| 32 | // 使用 unknown[] 避免第三方插件之间的 Rollup 版本类型冲突 |
| 33 | const plugins: unknown[] = [] |
| 34 | |
| 35 | // 路径别名插件 |
| 36 | if (Object.keys(config.alias).length > 0) { |
| 37 | const entries = Object.entries(config.alias).map(([find, replacement]) => ({ |
| 38 | find, |
| 39 | replacement: path.resolve(cwd, replacement), |
| 40 | })) |
| 41 | plugins.push(alias({ entries })) |
| 42 | } |
| 43 | |
| 44 | // 模块解析 |
| 45 | plugins.push( |
| 46 | nodeResolve({ |
| 47 | extensions: DEFAULT_EXTENSIONS, |
| 48 | }), |
| 49 | ) |
| 50 | |
| 51 | // CommonJS 支持 |
| 52 | plugins.push(commonjs()) |
| 53 | |
| 54 | // JSON 支持 |
| 55 | plugins.push(json()) |
| 56 | |
| 57 | // PostCSS / CSS Modules |
| 58 | plugins.push( |
| 59 | postcss({ |
| 60 | modules: { |
| 61 | generateScopedName: config.css.scopedName, |
| 62 | }, |
| 63 | autoModules: true, |
| 64 | minimize: true, |
| 65 | inject: config.css.mode === 'inject', |
| 66 | extract: config.css.mode === 'extract' ? config.css.extractFilename : false, |
| 67 | }), |
| 68 | ) |
| 69 | |
| 70 | // Babel 转译 |
| 71 | plugins.push( |
| 72 | babel({ |
| 73 | extensions: DEFAULT_EXTENSIONS, |
| 74 | exclude: 'node_modules/**', |
| 75 | babelrc: false, |
| 76 | babelHelpers: 'bundled', |
| 77 | presets: [ |
| 78 | ['@babel/preset-react', { runtime: config.jsxRuntime }], |
| 79 | ['@babel/preset-typescript', { allowDeclareFields: true }], |
| 80 | ], |
| 81 | }), |
| 82 | ) |
| 83 | |
| 84 | // 代码清理 |
| 85 | plugins.push( |
| 86 | cleanup({ |
| 87 | comments: ['some', /PURE/], |
no test coverage detected