Skip to content

Commit

Permalink
feat: add lib template rollup
Browse files Browse the repository at this point in the history
  • Loading branch information
baiwusanyu-c committed Sep 15, 2022
1 parent a3ef3da commit 9d0b35b
Show file tree
Hide file tree
Showing 31 changed files with 552 additions and 63 deletions.
36 changes: 1 addition & 35 deletions PLAN.md
Original file line number Diff line number Diff line change
@@ -1,37 +1,3 @@
# 计划
Be-cli
1.支持vue3 + vite模板
2.支持react + vite模板
3.纯ts库开发模板

## v1.0.0
### vue3 + vite + ts模板

## v1.2.0
### 纯ts库开发模板

## v1.3.0
gulp + rollup
集成hasky、commitlint 🐶
集成eslint与格式修复 🐶
支持热更新开发调试
自定打包义脚本,
支持打包类型生成、
支持产物压缩
支持多种格式cjs、esm
支持总包、分包(分包时替换路径)
支持指令参数修改环境变量
选择脚本是运行在
浏览器还是
node
单测集成
vite
jest

选择模板
gulp + rollup
tsup + gulp 🐶


## v1.x.x
react + vite 模板
unbuild 模板
28 changes: 19 additions & 9 deletions core/rumtime/runtime-lib/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {BUILDLIBTYPE, ILibOption, RUNENVTYPE, templatePath} from "../../../utils";
import {ILibOption, libTemplateName, RUNENVTYPE, templatePath} from "../../../utils";
import ora from 'ora'
import fs from 'fs-extra'
import chalk from 'chalk'
Expand All @@ -17,23 +17,26 @@ export const runRuntimeLib = async (option: ILibOption) => {
// 复制模板项目到目标路径
spinner.color = 'blue'
await fs.copySync(templatePath[buildLibType as keyof typeof templatePath], projectPath)
const templateName = libTemplateName[buildLibType as keyof typeof libTemplateName]

// 读取 package.json
console.log(chalk.blueBright.bold('\nstart creating package.json ...'))
await fs.ensureDirSync(projectPath)
const packageJson = await fs.readJsonSync(`${projectPath}/package.json`)
packageJson.name = projectName
let packageJson = await fs.readJsonSync(`${projectPath}/package.json`)
let packageJsonStr = JSON.stringify(packageJson).replaceAll(templateName,projectName)
packageJson = JSON.parse(packageJsonStr)

// 添加浏览器环境play
if(envType === RUNENVTYPE.BROWSER){
const libTemplatePath = templatePath[envType as keyof typeof templatePath]
// 1.移删除node下dist的play
await fs.removeSync(`${projectPath}/play`)
// 2.移动browser的play到dist
await fs.copySync(templatePath[envType as keyof typeof templatePath], `${projectPath}/play`)
// 3.修改package.json dev指令为play的dev指令
packageJson.scripts.dev = 'pnpm run --filter @template-node-tsup/play dev'
await fs.copySync(libTemplatePath, `${projectPath}/play`)
// 3.修改主仓库 package.json dev指令为play的dev指令
packageJson.scripts.dev = `pnpm run --filter @${projectName}/play dev`
}
// 添加单元测试
// 添加单元测试 vitest
if (unitTestLibType === 'vitest') {
console.log(chalk.blueBright.bold('\nstart setting vitest ...'))
// package.json添加依赖
Expand All @@ -51,7 +54,7 @@ export const runRuntimeLib = async (option: ILibOption) => {
await fs.copySync(templatePath[`${unitTestLibType}Lib` as keyof typeof templatePath], projectPath)
console.log(chalk.greenBright.bold('\nset vitest success !'))
}

// 添加单元测试 jest
if (unitTestLibType === 'jest') {
console.log(chalk.blueBright.bold('\nstart setting jest ...'))
// package.json添加依赖
Expand All @@ -74,7 +77,14 @@ export const runRuntimeLib = async (option: ILibOption) => {
for await (const entry of readdirp(projectPath,{fileFilter:['!.DS_Store']})) {
const { fullPath } = entry
let context = await fs.readFile(`${fullPath}`)
let contextStr = context.toString().replace('template-node-tsup',projectName)
let contextStr = context.toString().replaceAll(templateName,projectName)
await fs.outputFileSync(`${fullPath}`, contextStr)
}
// 修改 play 的子仓库名
for await (const entry of readdirp(`${projectPath}/play`,{fileFilter:['!.DS_Store']})) {
const { fullPath } = entry
let context = await fs.readFile(`${fullPath}`)
let contextStr = context.toString().replaceAll('template-play',projectName)
await fs.outputFileSync(`${fullPath}`, contextStr)
}

Expand Down
4 changes: 2 additions & 2 deletions template/lib-play/package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "@template-node-tsup/play",
"name": "@template-play/play",
"type": "module",
"version": "1.0.0",
"private": true,
Expand All @@ -13,7 +13,7 @@
"react-dom": "^18.2.0"
},
"devDependencies": {
"@template-node-tsup/entry": "*",
"@template-play/entry": "*",
"@types/react": "^18.0.15",
"@types/react-dom": "^18.0.6",
"@vitejs/plugin-react": "^2.0.0",
Expand Down
2 changes: 1 addition & 1 deletion template/lib-play/src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

import {useState} from 'react'
import reactLogo from './assets/react.svg'
import {runtimeBrowser} from '@template-node-tsup/entry'
import {runtimeBrowser} from '@template-play/entry'
import './App.css'
// @ts-ignore
import {ABTest} from '../../dist/web/esm/ab-test-sdk-web.esm'
Expand Down
6 changes: 6 additions & 0 deletions template/lib-template/gulp-rollup/.eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"extends": "@baiwusanyu",
"rules": {
"no-console": ["warn", { "allow": ["log"] }]
}
}
26 changes: 26 additions & 0 deletions template/lib-template/gulp-rollup/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
pnpm-debug.log*
lerna-debug.log*

node_modules
dist
dist-ssr
*.local

# Editor directories and files
.vscode/extensions.json
!project/.vscode/extensions.json
.idea
.DS_Store
*.suo
*.ntvs*
*.njsproj
*.sln
*.sw?
auto-imports.d.ts
components.d.ts
5 changes: 5 additions & 0 deletions template/lib-template/gulp-rollup/.husky/commit-msg
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/bin/sh
. "$(dirname "$0")/_/husky.sh"


npx --no -- commitlint --edit
4 changes: 4 additions & 0 deletions template/lib-template/gulp-rollup/.husky/pre-commit
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/bin/sh
. "$(dirname "$0")/_/husky.sh"

npm exec lint-staged --allow-empty
67 changes: 67 additions & 0 deletions template/lib-template/gulp-rollup/build/build-all.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import resolve from '@rollup/plugin-node-resolve'
import babel from '@rollup/plugin-babel'
import commonjs from '@rollup/plugin-commonjs'
import typescript from 'rollup-plugin-typescript2'
import cleanup from 'rollup-plugin-cleanup'
import { terser } from 'rollup-plugin-terser'
import dts from 'rollup-plugin-dts'
import { parallel } from 'gulp'
import replace from 'rollup-plugin-replace'
import { build } from './utils'

const config = {
input: '../packages/entry/index.ts', // 必须,入口文件
external: ['chalk'],
plugins: [
// 引入的插件在这里配置
resolve(),
typescript(),
replace({
'process.env.CURRENT_ENV': '`web`',
}),
babel({
presets: ['@babel/preset-env'],
exclude: '**/node_modules/**',
babelHelpers: 'runtime',
}),
commonjs(),
cleanup({ comments: 'none' }),
terser(),
],
}

const buildConfig = [
{
file: '../dist/index.js',
format: 'es',
inlineDynamicImports: true,
name: 'index',
},
{
file: '../dist/index.cjs',
format: 'cjs',
inlineDynamicImports: true,
name: 'index',
},
]

const typeConfig = {
input: '../packages/entry/index.ts', // 必须,入口文件
plugins: [
resolve(),
typescript(),
dts(),
],
}
const buildTypeConfig = [
{
file: '../dist/index.d.ts',
format: 'es',
},
]

// 打包处理
export const buildPackages = () => {
return parallel(() => build(config, buildConfig), () => build(typeConfig, buildTypeConfig))
}
export default buildPackages()
99 changes: 99 additions & 0 deletions template/lib-template/gulp-rollup/build/build-split.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
import resolve from '@rollup/plugin-node-resolve'
import babel from '@rollup/plugin-babel'
import commonjs from '@rollup/plugin-commonjs'
import typescript from 'rollup-plugin-typescript2'
import cleanup from 'rollup-plugin-cleanup'
import { terser } from 'rollup-plugin-terser'
import dts from 'rollup-plugin-dts'
import replace from 'rollup-plugin-replace'
import { build } from './utils'
import {OutputOptions} from "rollup";

const config = {
input: '../packages/entry/index.ts',
external: [
'chalk',
'@template-node-rollup/utils',
'@template-node-rollup/runtime'
],
output:[],
plugins: [],
}
const plugins = [
// 引入的插件在这里配置
resolve(),
typescript(),
replace({
'process.env.CURRENT_ENV': '`web`',
}),
babel({
presets: ['@babel/preset-env'],
exclude: '**/node_modules/**',
babelHelpers: 'runtime',
}),
commonjs(),
cleanup({ comments: 'none' }),
terser(),
]
let entry = {
index: {entry:'../packages/entry/index.ts',output:'../dist/index',},
runtime: {entry:'../packages/runtime/index.ts',output:'../dist/runtime/index',},
utils: {entry:'../utils/index.ts',output:'../dist/utils/index',},
}

const typeConfig = {
input: '../packages/entry/index.ts', // 必须,入口文件
plugins: [],
}

const typePlugins = [
resolve(),
typescript(),
dts(),
]
const createBuildOption = (config,plugins,isType?:boolean) =>{
let option = []
let len = 0
for (const entryKey in entry) {
let inputConfig = JSON.parse(JSON.stringify(config))
inputConfig.input = entry[entryKey].entry
inputConfig.plugins = plugins
let outputConfig = []
if(isType){
outputConfig = [
{
file: `${entry[entryKey].output}.d.ts`,
format: 'es',
},
]
}else{
outputConfig = [
{
file: `${entry[entryKey].output}.js`,
format: 'es',
inlineDynamicImports: true,
name: 'index',
},
{
file: `${entry[entryKey].output}.cjs`,
format: 'cjs',
inlineDynamicImports: true,
name: 'index',
}
] as OutputOptions[]
}
option.push(async () => await build(inputConfig, outputConfig))
len++
}
return option
}



// 打包处理
const buildPackages = () => {
const buildList = createBuildOption(config,plugins)
const buildTypeList = createBuildOption(typeConfig,typePlugins,true)
return [...buildList,...buildTypeList]
}
export default buildPackages()
22 changes: 22 additions & 0 deletions template/lib-template/gulp-rollup/build/gulp-file.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { series } from 'gulp'
import { rewritePath } from './rewirte-path'
import buildAllPackages from './build-all'
import buildSplitPackages from './build-Split'

const buildMode = process.env.BUILD_MODE

let taskList = []
if (buildMode === 'all') {
taskList = [
buildAllPackages,
]
}

if (buildMode === 'split') {
taskList = [
...buildSplitPackages,
...rewritePath(),
]
}

export default series(...taskList)
33 changes: 33 additions & 0 deletions template/lib-template/gulp-rollup/build/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{
"name": "@template-node-rollup/build",
"type": "module",
"version": "1.0.0",
"description": "",
"author": "",
"license": "ISC",
"keywords": [],
"scripts": {
"build:split": "cross-env BUILD_MODE=split gulp -require sucrase/register/ts -f gulp-file.ts",
"build:all": "cross-env BUILD_MODE=all gulp -require sucrase/register/ts -f gulp-file.ts"
},
"devDependencies": {
"@babel/core": "^7.19.0",
"@babel/preset-env": "^7.19.0",
"@babel/preset-typescript": "^7.16.0",
"@rollup/plugin-babel": "^5.3.1",
"@rollup/plugin-commonjs": "^22.0.2",
"@rollup/plugin-node-resolve": "^14.1.0",
"@template-node-rollup/entry": "*",
"cross-env": "^7.0.3",
"esno": "^0.16.3",
"rimraf": "^3.0.2",
"rollup": "^2.79.0",
"rollup-plugin-cleanup": "^3.2.1",
"rollup-plugin-dts": "^4.2.1",
"rollup-plugin-replace": "^2.2.0",
"rollup-plugin-terser": "^7.0.2",
"rollup-plugin-typescript2": "^0.34.0",
"sucrase": "^3.21.0",
"typescript": "4.7.4"
}
}
Loading

0 comments on commit 9d0b35b

Please sign in to comment.