From 5dcf6e83221bfc2b47b7e95002e768e84143287a Mon Sep 17 00:00:00 2001 From: keuby <32365592+keuby@users.noreply.github.com> Date: Wed, 8 Jun 2022 23:25:53 +0800 Subject: [PATCH] build: add umd build script (#3060) Co-authored-by: knight.chen --- package.json | 14 ++++++++-- rollup.config.js | 69 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 81 insertions(+), 2 deletions(-) create mode 100644 rollup.config.js diff --git a/package.json b/package.json index bf967875f33..c86cf63886b 100644 --- a/package.json +++ b/package.json @@ -5,12 +5,14 @@ "main": "lib/index.js", "module": "es/index.js", "types": "es/index.d.ts", + "unpkg": "dist/naive.prod.js", + "jsdelivr": "dist/naive.prod.js", "scripts": { "start": "pnpm run dev", "dev": "pnpm run clean && pnpm run gen-version && pnpm run gen-volar-dts && cross-env NODE_ENV=development vite", - "build:package": "pnpm run gen-version && pnpm run clean && pnpm run gen-volar-dts && tsc -b --force tsconfig.esm.json && node scripts/pre-build/pre-cjs-build.js && tsc -b --force tsconfig.cjs.json && node scripts/post-build && rimraf {es,lib}/*.tsbuildinfo", + "build:package": "pnpm run gen-version && pnpm run clean && pnpm run gen-volar-dts && tsc -b --force tsconfig.esm.json && node scripts/pre-build/pre-cjs-build.js && tsc -b --force tsconfig.cjs.json && rollup -c && node scripts/post-build && rimraf {es,lib}/*.tsbuildinfo", "build:site": "./scripts/pre-build-site/pre-build-site.sh && cross-env NODE_ENV=production NODE_OPTIONS=--max-old-space-size=4096 vite build && ./scripts/post-build-site/post-build-site.sh", - "clean": "rimraf site lib es node_modules/naive-ui themes/**/es themes/**/lib", + "clean": "rimraf site lib es dist node_modules/naive-ui themes/**/es themes/**/lib", "release:package": "pnpm run test && pnpm run build:package && pnpm publish --no-git-checks", "release:changelog": "node scripts/release-changelog", "lint": "pnpm run lint:code && pnpm run lint:type", @@ -38,6 +40,7 @@ "files": [ "es", "lib", + "dist", "volar.d.ts", "web-types.json", "README.md" @@ -75,6 +78,9 @@ "@babel/preset-env": "^7.16.11", "@babel/traverse": "^7.17.9", "@rollup/plugin-babel": "^5.3.1", + "@rollup/plugin-commonjs": "^22.0.0", + "@rollup/plugin-node-resolve": "^13.3.0", + "@rollup/plugin-replace": "^4.0.0", "@types/estree": "^0.0.51", "@types/jest": "^27.4.1", "@typescript-eslint/eslint-plugin": "^5.18.0", @@ -93,6 +99,7 @@ "codesandbox": "^2.2.3", "cross-env": "^7.0.3", "cssnano": "^5.1.7", + "deepmerge": "^4.2.2", "esbuild": "0.14.42", "eslint": "^8.13.0", "eslint-config-prettier": "^8.5.0", @@ -117,6 +124,9 @@ "marked": "^4.0.13", "prettier": "^2.6.2", "rimraf": "^3.0.2", + "rollup": "^2.75.6", + "rollup-plugin-esbuild": "^4.9.1", + "rollup-plugin-terser": "^7.0.2", "superagent": "^7.1.3", "ts-jest": "^28.0.1", "typescript": "~4.6.3", diff --git a/rollup.config.js b/rollup.config.js new file mode 100644 index 00000000000..25c6c54a641 --- /dev/null +++ b/rollup.config.js @@ -0,0 +1,69 @@ +const path = require('path') +const merge = require('deepmerge') +const { defineConfig } = require('rollup') +const nodeResolve = require('@rollup/plugin-node-resolve').default +const babel = require('@rollup/plugin-babel').default +const replace = require('@rollup/plugin-replace') +const commonjs = require('@rollup/plugin-commonjs') +const esbuild = require('rollup-plugin-esbuild').default +const { terser } = require('rollup-plugin-terser') + +const extensions = ['.mjs', '.js', '.json', '.ts'] + +const baseConfig = defineConfig({ + input: path.resolve('./src/index.ts'), + plugins: [ + nodeResolve({ extensions }), + esbuild({ + target: 'esnext', + sourceMap: true + }), + babel({ + extensions, + babelHelpers: 'bundled' + }), + commonjs() + ], + external: ['vue'], + output: { + name: 'naive', + format: 'umd', + exports: 'named', + globals: { + vue: 'Vue' + } + } +}) + +const devConfig = defineConfig({ + plugins: [ + replace({ + values: { + __DEV__: JSON.stringify(true), + 'process.env.NODE_ENV': JSON.stringify('development') + }, + preventAssignment: true + }) + ], + output: { + file: path.resolve('dist/naive.js') + } +}) + +const prodConfig = defineConfig({ + plugins: [ + replace({ + values: { + __DEV__: JSON.stringify(false), + 'process.env.NODE_ENV': JSON.stringify('production') + }, + preventAssignment: true + }), + terser() + ], + output: { + file: path.resolve('dist/naive.prod.js') + } +}) + +module.exports = [merge(baseConfig, devConfig), merge(baseConfig, prodConfig)]