father icon indicating copy to clipboard operation
father copied to clipboard

CJS打包时rollup抛出warning

Open dcison opened this issue 5 years ago • 2 comments

问题描述

使用 rollup 打包文件时,rollup抛出异常警告

Entry module "src/index.js" is using named and default exports together. Consumers of your bundle will have to use chunk["default"] to access the default export, which may not be what you want. Use output.exports: "named" to disable this warning

.fatherrc.js配置

.fatherrc.js配置如下

export default {
    esm: 'rollup',
    cjs: {
       type: 'rollup',
       minify: true,
    },
    doc: {
        propsParser: false, // 不自动解析 props
        htmlContext: {
            // 样式控制
            head: {
                links: [
                    // 以下样式(因为 antd 实现的缘故)需要手动引入
                    {
                        rel: 'stylesheet',
                        // antd 的样式
                        href: 'https://cdnjs.cloudflare.com/ajax/libs/antd/3.26.15/antd.min.css',
                    },
                    {
                        rel: 'stylesheet',
                        // antd-mobile 的样式
                        href: 'https://mobile.ant.design/kitchen-sink/kitchen-sink.css',
                    },
                ],
                raw: [ // 移动端预览效果适配 rem
                    `<style>
                        html{
                            font-size: 100px;
                        }
                    </style>`
                ],
            },
        },
        // mdx 中图片的控制
        public: '/public'
    },
}

原因

入口文件(src/index.js)下,除使用 export default 外,还使用了 export 额外的变量,导致rollup 抛出上述警告,代码如下:

export default Index;

export function initFunction(x,y) {
  // do something
}

当注释掉 initFunction 后,rollup 不会抛出上述 waring

猜想的解决方案

按 rollup warning 提示,配置 output.exports: "named" 能解决上述 waring,但是 father-build 中会校验参数是否在维护的白名单中,导致无法加入该参数,

配置的JSON 代码路径:node_modules/father-build/lib/schema.js

cjs: {
      oneOf: [noEmptyStr, {
        type: 'boolean'
      }, {
        type: 'object',
        additionalProperties: false,
        properties: {
          type: {
            type: 'string',
            pattern: '^(rollup|babel)$'
          },
          file: noEmptyStr,
          minify: {
            type: 'boolean'
          },
          lazy: {
            type: 'boolean'
          }
        }
      }]
},

相关环境信息

  • father 版本:2.29.3
  • father-build 版本:1.18.0

dcison avatar Aug 04 '20 03:08 dcison

+1

zhengxs2018 avatar Jan 01 '21 03:01 zhengxs2018

+1 @PeachScript 求添加

ShaofeiZi avatar Feb 26 '21 03:02 ShaofeiZi

father 4 已发布,可尝试升级到最新版:https://github.com/umijs/father/blob/master/docs/guide/migrate.md

旧版本不再维护,感谢支持

PeachScript avatar Aug 31 '22 11:08 PeachScript