Rollup:打包 TypeScript - React 组件库

1、前提

1.1 通过 create-react-app take-photo --template 创建前端应用
1.2 添加组件 TakePhoto (拍照组件)
1.3 App 为测试组件
1.4 目录结构
在这里插入图片描述

1、安装依赖

@rollup/plugin-commonjs 
@rollup/plugin-image
@rollup/plugin-node-resolve
@rollup/plugin-terser
@rollup/plugin-typescript
autoprefixer
postcss
postcss-preset-env
postcss-url
rollup
rollup-plugin-delete
rollup-plugin-postcss

2、添加 rollup.config.js 配置

const {
    nodeResolve } = require('@rollup/plugin-node-resolve');
// 允许使用 node 或 umd 包
const commonjs = require( '@rollup/plugin-commonjs');
// 打包前清空输出目录
const del = require( 'rollup-plugin-delete');
// 压缩代码
const terser = require('@rollup/plugin-terser');
// 编译 TS 代码
const typescript = require( '@rollup/plugin-typescript');
// 处理scss
const postcss = require( 'rollup-plugin-postcss');
// 处理scss 添加前缀
const autoprefixer = require( 'autoprefixer');
// 处理 css 中引入的图片
const postCssUrl = require("postcss-url");
// 处理组件引入的图片
const image = require('@rollup/plugin-image');

module.exports = {
   
  input: 'src/TakePhoto/index.tsx',
  output: [
    {
   
      file: 'dist/TakePhoto/index.js',
      format: 'umd',
      name: 'TakePhoto',
      globals: {
   
        react: 'React',
        'react-dom': 'ReactDom'
      }
    }
  ],
  external: ['react', 'react-dom'],
  plugins: [
    del({
    targets: 'dist/*', verbose: true }),
    nodeResolve(),
    commonjs(),
    terser(),
    typescript({
   
      "include": [
        "src/TakePhoto/*",
        "src/types.d.ts"
      ]
    }),
    postcss({
   
      extensions: ['.css', '.scss'],
      inject: true,
      minimize: true,
      plugins: [
        postCssUrl({
   
          url: 'inline'
        }),
        autoprefixer()
      ],
    }),
    image()
  ]
};

3、修改 package.json

3.1 添加打包命令

"scripts": {
   
    // 其他命令...
    "build": "rollup --config"
},

3.2 添加组件入口

"main": "dist/TakePhoto/index.js",

3.3 添加组件声明入口

"types": "dist/TakePhoto/index.d.ts",

3.4 浏览器支持

"browserslist": [
    ">0.2%",
    "not dead",
    "not op_mini all",
    "Firefox >= 52",
    "IE >= 10"
],

相关推荐

  1. ReactReact API

    2024-01-26 20:58:02       22 阅读
  2. react

    2024-01-26 20:58:02       35 阅读
  3. react:profiler

    2024-01-26 20:58:02       12 阅读
  4. react :Suspense

    2024-01-26 20:58:02       16 阅读
  5. react:strictmode

    2024-01-26 20:58:02       15 阅读
  6. react:fragment

    2024-01-26 20:58:02       13 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-01-26 20:58:02       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-01-26 20:58:02       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-01-26 20:58:02       19 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-01-26 20:58:02       20 阅读

热门阅读

  1. React进阶 - 13(说一说 React 中的虚拟 DOM)

    2024-01-26 20:58:02       45 阅读
  2. Hive之set参数大全-14

    2024-01-26 20:58:02       30 阅读
  3. SpringBoot实现自定义异常+全局异常统一处理

    2024-01-26 20:58:02       36 阅读
  4. 深入理解高阶函数与函数柯里化在React中的应用

    2024-01-26 20:58:02       40 阅读
  5. MySQL之约束

    2024-01-26 20:58:02       33 阅读
  6. CGAL::Plane_3<K>平面结构

    2024-01-26 20:58:02       36 阅读
  7. webpack常见的loader和plugin

    2024-01-26 20:58:02       37 阅读
  8. Android JNI中设置全局的jbyteArray

    2024-01-26 20:58:02       35 阅读