3

Preface

The long-term accumulation of business lines has produced many repetitive tools and methods, business function modules, etc. We can just use rollup to build a npm private server toolkit, which is convenient for later business use and reduces repetitive code writing.

Project configuration

babel

Introduce dependencies

First run the following command to install babel related:

yarn add @babel/core @babel/cli @babel/preset-env -D

Configuration babel.config.js

module.exports = {
    presets: [
        [
            "@babel/preset-env",
            {
                targets: "> 0.25%, not dead"
            }
        ]
    ]
};

With @babel/plugin-transform-runtime and core-js :

yarn add core-js @babel/runtime
yarn add @babel/plugin-transform-runtime -D

Modify babel.config.js as follows:

module.exports = {
    presets: [
        [
            "@babel/preset-env",
            {
                targets: "> 0.25%, not dead",
                useBuiltIns: "usage",
                corejs: "3.6.5"
            }
        ]
    ],
    plugins: ["@babel/plugin-transform-runtime"]
};

Add npm scripts

"scripts:" {
  "babel": "babel ./src/index.js -o ./dist/index.js"
}

Typescript

Facing the future, so typescript is introduced here, and ts is used for development.

yarn add typescript@4.3.5 -D
yarn add @babel/preset-typescript -D

Modify the babel as follows:

module.exports = {
    presets: [
        [
            '@babel/preset-env',
            {
                targets: '> 0.25%, not dead',
                useBuiltIns: 'usage',
                corejs: '3.6.5'
            },
            '@babel/preset-typescript'
        ]
    ],
    plugins: ['@babel/plugin-transform-runtime']
};

rollup

The project is a pure Javascript project, there is no vue , react , so rollup is used for packaging.

Introduce dependencies

yarn add rollup @rollup/plugin-babel @rollup/plugin-commonjs @rollup/plugin-node-resolve rollup-plugin-terser rollup-plugin-typescript2 tslib -D

Configuration rollup.config.js

import babel from '@rollup/plugin-babel'; // 引入babel
import commonjs from '@rollup/plugin-commonjs'; // 引入cjs插件
import { nodeResolve } from '@rollup/plugin-node-resolve'; // 引入resolve
import typescript from 'rollup-plugin-typescript2'; // ts
import { terser } from 'rollup-plugin-terser'; // 压缩打包文件

const extensions = ['.js', '.ts'];

const pkg = require('./package.json'); // 从package.json引入

const version = pkg.version; // 项目版本
const license = pkg.license; // 协议
const author = pkg.author; // 作者

// 打包文件的头部声明
const banner =
    '/*!\n' +
    ` * ${pkg.name} v${version}\n` +
    ` * (c) 2020-${new Date().getFullYear()} ${author}\n` +
    ` * Released under the ${license} License.\n` +
    ' */';

module.exports = {
    input: 'src/index.ts',
    output: [
        // 文件输出配置
        {
            file: 'dist/index.umd.js', // 打包后生产的文件位置,及文件名
            format: 'umd',
            name: 'utools', // 包的全局变量名称
            banner
        },
        {
            file: 'dist/index.esm.js', // 打包后生产的文件位置,及文件名
            format: 'esm',
            name: 'utools', // 包的全局变量名称
            banner
        }
    ],
    plugins: [
        nodeResolve({
            extensions,
            modulesOnly: true
        }),
        commonjs(),
        typescript(),
        babel({
            babelHelpers: 'runtime',
            include: 'src/**',
            exclude: 'node_modules/**',
            extensions
        }),
        terser()
    ]
};

Add npm scripts

"scripts:" {
  "build": "rollup -c"
}

Jest

Introduce dependencies

Just introduce unit testing to facilitate subsequent iterative maintenance of the project

yarn add jest@27.0.2  jest-globals ts-jest@27.0.2 @types/jest babel-jest@27.0.2 -D

Configuration jest.config.js

module.exports = {
    preset: 'ts-jest',
    testEnvironment: 'node',
    transform: {
        '^.+\\.jsx?$': 'babel-jest', // 这个是jest的默认配置
        '^.+\\.ts?$': 'ts-jest' // typescript转换
    }
};

Add npm scripts

"scripts:" {
  "test": "jest"
}

Eslint+commitlint

The department’s internal standardization verification tool used here directly performs the overall standardization verification, so I won’t repeat it too much. It is recommended to refer to eslint-config-alloy https://github.com/AlloyTeam/eslint-config-alloy

Other configuration

rimraf

Here is the introduction of rimraf, before each package, delete the original files in the dist directory

Modify npm scripts

"build": "rimraf dist/* && rollup -c"

release-it

release-it is introduced here to facilitate our quick npm release

Installation dependencies

yarn add release-it -D

Added .release-it.json file

{
    "git": {
        "commitMessage": "chore: release v${version}"
    },
    "npm": {
        "publish": false
    },
    "publishConfig": {
        "registry": "私服地址"
    }
}

Added npm scripts

"scripts:" {
  "release": "release-it",
}

package.json is as follows

{
    "name": "module name",
    "version": "module version",
    "description": "module desc",
    "main": "dist/index.umd.js",
    "types": "types/index.d.ts",
    "module": "dist/index.esm.js",
    "scripts": {
        "test": "jest",
        "babel": "babel ./src/index.js -o ./dist/index.js",
        "build": "rimraf dist/* && rollup -c",
        "release": "release-it",
        "release:beta": "release-it major --preRelease=beta",
        "fix:src": "npx eslint src --fix --ext .ts",
        "fix:test": "npx eslint test --fix --ext .js",
        "lint": "npm run fix:src && npm run fix:test"
    },
    "repository": {
        "type": "git"
    },
    "author": "module author",
    "license": "MIT",
    "devDependencies": {},
    "dependencies": {}
}

兰俊秋雨
5.1k 声望3.5k 粉丝

基于大前端端技术的一些探索反思总结及讨论