当前位置: 首页 > 工具软件 > Rollup > 使用案例 >

rollup babel 配置

艾敏学
2023-12-01
import path from 'path';
import json from '@rollup/plugin-json';
import postcss from 'rollup-plugin-postcss';
import resolve from '@rollup/plugin-node-resolve';
import typescript from 'rollup-plugin-typescript2';
import replace from '@rollup/plugin-replace';
import { uglify } from 'rollup-plugin-uglify';
import commonjs from '@rollup/plugin-commonjs';
import { babel } from '@rollup/plugin-babel';

const resolveFile = function (filePath) {
    return path.join(__dirname, filePath);
};

const pluginName = 'bundle';

function getPlugin(isCompress = false) {
    const result = [
        resolve(),
        commonjs(),      
        typescript(),
        json({
            compact: true,
        }),
        postcss({
            extensions: ['.css'],
            minimize: true,
        }),
        replace({
            'process.env.NODE_ENV': JSON.stringify(process.env.NODE_ENV),
            preventAssignment: true,
        }),
        babel({
            extensions: ['.js', '.vue', '.ts'],
            babelHelpers: 'runtime',
            plugins: [
                [
                    '@babel/plugin-transform-runtime',
                ],
            ],
            presets: [
                [
                    '@babel/preset-env',
                    {
                        useBuiltIns: 'usage',
                    },
                ],
            ],
        }),
    ];

    if (isCompress) {
         result.push(uglify());
    }

    return result;
}

export default {
    input: resolveFile('src/index.ts'),
    output: {
        file: resolveFile(`dist-component/${pluginName}.js`),
        format: 'iife',
        sourcemap: true,
    },
    plugins: getPlugin(true),
};

报错 [!] Error: 'default' is not exported by ../../node_modules/@babel/runtime/regenerator/index.js, imported by src/modes/normal.ts 是因为没有引入 @rollup/plugin-commonjs 依赖。

 类似资料: