最新公告
  • 欢迎您光临起源地模板网,本站秉承服务宗旨 履行“站长”责任,销售只是起点 服务永无止境!立即加入钻石VIP
  • 使用rollup打包React Native插件并发布

    正文概述 掘金(洛竹)   2021-04-18   687

    安装 rollup

    $ yarn add -D rollup
    

    package.json

    {
      "name": "react-native-refined-components",
      "version": "0.1.0",
      "description": "refined react-native components",
      "main": "dist/cjs/index.js",
      "module": "dist/es/index.js",
      "browser": "dist/umd/index.js",
      "types": "dist/es/index.d.ts",
      "scripts": {
        "build": "rimraf dist/* && rollup -c",
        "dev": "rollup -c -w"
      }
    }
    

    rollup配置文件

    在根目录新建 rollup.config.js:

    import pkg from './package.json';
    
    export default [
      // browser-friendly UMD build
      {
        input: "./src/index.ts",
        // 输出
        output: {
          file: pkg.browser, // 文件
          format: 'umd', // 格式
          name: 'refined-components', // 生成包名称,代表你的 iife/umd 包
        },
      },
      // CommonJS (for Node) and ES module (for bundlers) build.
      {
    		input: "./src/index.ts",
    		output: [
    			{ file: pkg.main, format: 'cjs' },
    			{ file: pkg.module, format: 'es' }
        ],
    	}
    ]
    

    转换 .json 文件为 ES6 modules

    安装 @rollup/plugin-json:

    $ yarn add -D @rollup/plugin-json
    

    配置 rollup.config.js

    // rollup.config.js
    import json from '@rollup/plugin-json';
    import pkg from './package.json';
    
    export default [
      // browser-friendly UMD build
      {
        input: "./src/index.ts",
        // 输出
        output: {
          file: pkg.browser, // 文件
          format: 'umd', // 格式
          name: 'refined-components', // 生成包名称,代表你的 iife/umd 包
        },
        plugins: [
          json(),
        ]
      },
      // CommonJS (for Node) and ES module (for bundlers) build.
      {
    		input: "./src/index.ts",
    		output: [
    			{ file: pkg.main, format: 'cjs' },
    			{ file: pkg.module, format: 'es' }
        ],
        plugins: [json()]
    	}
    ]
    

    加载并解析 CommonJS 模块

    我们写组件库或工具库时不可避免会用到外部库,这些外部库可能是符合 CommonJS 规范的。而 Rollup 力图实现 ES 模块的规范, 因此,加载 CommonJS 模块和使用 Node 模块位置解析逻辑都被实现为可选插件,默认情况下不在 Rollup 内核中。我们需要安装并配置 CommonJS 和 node-resolve 插件。

    安装

    $ yarn add -D @rollup/plugin-node-resolve @rollup/plugin-commonjs
    

    配置

    一般我们打的 cjs 和 esm 格式文件需要把第三放包打进来

    // rollup.config.js
    import commonjs from '@rollup/plugin-commonjs';
    import { nodeResolve } from '@rollup/plugin-node-resolve';
    import pkg from './package.json';
    
    export default [
      // browser-friendly UMD build
      {
        input: "./src/index.ts",
        // 输出
        output: {
          file: "./dist/umd/index.js", // 文件
          format: 'umd', // 格式
          name: 'refined-components', // 生成包名称,代表你的 iife/umd 包
        },
        plugins: [
          json(),
          commonjs(), // 加载 commonjs 模块
          nodeResolve() // 将 commonjs 转换为 ES 模块
        ]
      }
    ]
    

    忽略 warning-treating-module-as-external-dependency

    Rollup 默认只会解析相对路径的模块,像是 import _ from 'lodash' 不会被打包进 bundle,并且打包时会有警告。如果你想忽略这些警告,你需要在 external 中指明这些外部模块。那么有没有更优雅的方式呢?答案是肯定的,我们只需要安装并配置 rollup-plugin-node-externals 插件即可。

    $ yarn add -D rollup-plugin-node-externals
    
    // rollup.config.js
    import externals from 'rollup-plugin-node-externals';
    
    export default [
      // CommonJS (for Node) and ES module (for bundlers) build.
      {
    		output: [
    			{
            file: './dist/cjs/index.js',
            format: 'cjs',
            exports: 'auto',
          },
    			{
            file: './dist/es/index.js',
            format: 'es'
          }
        ],
        plugins: [
          externals({deps: true}),
        ]
    	}
    ]
    

    打包 ts 文件

    安装依赖

    $ yarn add -D rollup-plugin-typescript2 typescript
    

    配置

    import typescript from 'rollup-plugin-typescript2';
    
    export default [
      // browser-friendly UMD build
      {
        input: "./src/index.ts",
        // 输出
        output: {
          file: './dist/umd/index.js', // 文件
          format: 'umd', // 格式
          name: 'refined-components', // 生成包名称,代表你的 iife/umd 包
          globals: {
            'react': 'React',
            'react-native': 'reactNative'
          },
          sourcemap: true
        },
        plugins: [
          // 如果用了 rollup-plugin-node-resolve, 则必须将它放在 typescript 插件前面
          typescript({
            tsconfigOverride: {
              compilerOptions: { declaration: false }
            }
          }),
        ],
        external: ["react","react-native"],
      },
      // CommonJS (for Node) and ES module (for bundlers) build.
      {
    		input: './src/index.ts',
    		output: [
    			{ dir: './dist/cjs/index.js', format: 'cjs', exports: 'auto' },
    			{ dir: './dist/es/index.js', format: 'es' }
        ],
        plugins: [
          typescript(),
        ],
        external: ["react","react-native"],
    	}
    ]
    

    tsconfig.json

    {
      "compilerOptions": {
        "target": "es5",
        "module": "esnext",
        "declaration": true,
        "jsx": "react",
        "strict": true,
        "noImplicitAny": false,
        "skipLibCheck": true,
        "moduleResolution": "node",
        "allowSyntheticDefaultImports": true,
        "esModuleInterop": true,
        "resolveJsonModule": true,
      },
      "exclude": ["dist","rollup.config.js"]
    }
    

    rollup-plugin-multi-input 使用

    组件库比较大时,我们可能需要让我们的库支持 tree-shaking。那么你就不能将所有的文件都打入到一个文件中。rollup-plugin-multi-input 便是一个将打包产物输出到各自的文件中的插件。就像:

    src
      - A.ts
      - B.ts
      - index.ts
    ->
    dist
      - A.js
      - B.js
      - index.js
    

    安装依赖

    $ yarn add -D rollup-plugin-multi-input
    

    配置

    import multiInput from 'rollup-plugin-multi-input';
    
    export default [
      ...
      // CommonJS (for Node) and ES module (for bundlers) build.
      {
    		input: ['src/**/*.ts','src/**/*.tsx'],
    		output: [
    			{ dir: './src', format: 'cjs', exports: 'auto' },
    			{ dir: './src', format: 'es' }
        ],
        plugins: [
          multiInput(),
        ],
    	}
    ]
    

    其他插件

    • rollup-plugin-progress: 打包进度条
    • rollup-plugin-terser: 压缩文件

    起源地下载网 » 使用rollup打包React Native插件并发布

    常见问题FAQ

    免费下载或者VIP会员专享资源能否直接商用?
    本站所有资源版权均属于原作者所有,这里所提供资源均只能用于参考学习用,请勿直接商用。若由于商用引起版权纠纷,一切责任均由使用者承担。更多说明请参考 VIP介绍。
    提示下载完但解压或打开不了?
    最常见的情况是下载不完整: 可对比下载完压缩包的与网盘上的容量,若小于网盘提示的容量则是这个原因。这是浏览器下载的bug,建议用百度网盘软件或迅雷下载。若排除这种情况,可在对应资源底部留言,或 联络我们.。
    找不到素材资源介绍文章里的示例图片?
    对于PPT,KEY,Mockups,APP,网页模版等类型的素材,文章内用于介绍的图片通常并不包含在对应可供下载素材包内。这些相关商业图片需另外购买,且本站不负责(也没有办法)找到出处。 同样地一些字体文件也是这种情况,但部分素材会在素材包内有一份字体下载链接清单。
    模板不会安装或需要功能定制以及二次开发?
    请QQ联系我们

    发表评论

    还没有评论,快来抢沙发吧!

    如需帝国cms功能定制以及二次开发请联系我们

    联系作者

    请选择支付方式

    ×
    迅虎支付宝
    迅虎微信
    支付宝当面付
    余额支付
    ×
    微信扫码支付 0 元