Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
553 views
in Technique[技术] by (71.8m points)

rollup打包不支持async语法吗?

入口文件index.js

const { determineRepoDir } = require('./expo');
// const { determineRepoDir } = require('../lib/profile');

determineRepoDir()

expo.js

import 'babel-polyfill';
const { mark } = require('./out');
// import { mark } from './out';

// const xx = async function(){
//   console.log('**xxxx**')
// }

module.exports = { mark };

out.js

const mark = async function(){
    console.log('***mark***');
}

module.exports = { mark };
// export const mark = async function(){
//     console.log('***mark***');
// }

rollup.config.js

import { terser } from 'rollup-plugin-terser';
import commonjs from '@rollup/plugin-commonjs';
// Rollup plugin to convert CommonJS modules to ES6, so they can be included in a Rollup bundle
import bin from '../rollup-plugin/inject-bin'
import babel from '@rollup/plugin-babel';
import { nodeResolve } from '@rollup/plugin-node-resolve';
//rollup无法识别node_modules中的包

// const treeshake = {
//     moduleSideEffects: false,
//     propertyReadSideEffects: false,
//     tryCatchDeoptimization: false
// };

export default () => {
    const esmBuild = {
        input: {
            'index': 'index',
        },
        output: {
            chunkFileNames: 'shared/[name]',
            dir: 'dist',
            format: 'cjs'
        },
        // treeshake,
        plugins: [nodeResolve(),commonjs(),babel({
            exclude: 'node_modules/**' // 只编译我们的源代码
        }),terser(), bin()]
    };

    return esmBuild;
};

现象就是如果expo.js的fff函数不带async头或polyfill,那么out文件里面的代码就会被打包起来,否则不会打包,只会保留require的代码


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

rollup 默认开启 tree-shaking

你这个代码片段没有被引入的可能 (带不带async一样),都会被忽略。

可以试试
https://www.rollupjs.org/repl/


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...