Skip to content

Commit

Permalink
fix: 使用 rollup 改造
Browse files Browse the repository at this point in the history
  • Loading branch information
CaoMeiYouRen committed May 1, 2021
1 parent c779e43 commit 8820f52
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 34 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"scripts": {
"lint": "cross-env NODE_ENV=production eslint src --fix --ext .ts,.js",
"prebuild": "rimraf dist",
"prebuild2": "rimraf dist",
"build": "rimraf dist && tsc",
"build2": "cross-env NODE_ENV=production rollup -c",
"dev": "cross-env NODE_ENV=development ts-node-dev src/index.ts",
Expand Down Expand Up @@ -89,4 +90,4 @@
"commit-msg": "validate-commit-msg"
}
}
}
}
54 changes: 35 additions & 19 deletions rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import typescript from '@rollup/plugin-typescript'
import json from '@rollup/plugin-json'
import _ from 'lodash'
import { dependencies, name } from './package.json'
const external = Object.keys(dependencies) // 默认不打包 dependencies
const external = Object.keys({ ...dependencies }) // 默认不打包 dependencies
const outputName = _.upperFirst(_.camelCase(name))// 导出的模块名称 PascalCase
const env = process.env
const IS_PROD = env.NODE_ENV === 'production'
Expand All @@ -22,6 +22,8 @@ function getPlugins({ isBrowser = false, isMin = false, isDeclaration = false })
tsconfig: isDeclaration ? 'tsconfig.json' : 'tsconfig.build.json',
esModuleInterop: true,
allowSyntheticDefaultImports: true,
module: 'esnext',
target: 'esnext',
}),
)
plugins.push(
Expand Down Expand Up @@ -72,31 +74,45 @@ export default [
}),
},
{
input: 'src/index.ts',
external,
output: {
file: 'dist/index.umd.js', // 生成 umd
format: 'umd',
name: outputName,
},
plugins: getPlugins({
isBrowser: false,
isDeclaration: false,
isMin: true,
}),
},
{
input: 'src/index.ts',
input: 'src/plopfile.ts',
external,
output: {
file: 'dist/index.esm.js', // 生成 esm
format: 'esm',
name: outputName,
file: 'dist/plopfile.js', // 生成 cjs
format: 'cjs',
name: 'Plopfile',
},
plugins: getPlugins({
isBrowser: false,
isDeclaration: false,
isMin: false,
}),
},
// {
// input: 'src/index.ts',
// external,
// output: {
// file: 'dist/index.umd.js', // 生成 umd
// format: 'umd',
// name: outputName,
// },
// plugins: getPlugins({
// isBrowser: false,
// isDeclaration: false,
// isMin: true,
// }),
// },
// {
// input: 'src/index.ts',
// external,
// output: {
// file: 'dist/index.esm.js', // 生成 esm
// format: 'esm',
// name: outputName,
// },
// plugins: getPlugins({
// isBrowser: false,
// isDeclaration: false,
// isMin: false,
// }),
// },
]
2 changes: 1 addition & 1 deletion src/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ export const NODE_ENV = env.NODE_ENV
/**
* 是否为开发环境
*/
export const __DEV__ = env.NODE_ENV === 'development'
export const __DEV__ = env.NODE_ENV === 'development'
6 changes: 3 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env node
// #!/usr/bin/env node
import { Plop, run } from 'plop'
import { Command } from 'commander'
import minimist from 'minimist'
Expand All @@ -18,7 +18,7 @@ const create = new Command('create')
configPath: path.resolve(__dirname, './plopfile.js'),
require: argv.require,
completion: argv.completion,
}, env => run(env, undefined, true))
}, (env) => run(env, undefined, true))
})
program.addCommand(create)

Expand All @@ -29,4 +29,4 @@ const opts = program.opts()
if (opts.debug) {
console.log(argv)
console.log(opts)
}
}
16 changes: 9 additions & 7 deletions src/plopfile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { NodePlopAPI, ActionType } from 'plop'
import { __DEV__ } from './env'
import { downloadGitRepo, init } from './utils'

module.exports = function(plop: NodePlopAPI) {
module.exports = function (plop: NodePlopAPI) {
plop.setActionType('initProject', async (answers: any, config) => {
const name = answers.name as string
const author = answers.author as string
Expand All @@ -23,7 +23,7 @@ module.exports = function(plop: NodePlopAPI) {
type: 'input',
name: 'name',
message: '请输入项目名称',
validate(input: string, answers){
validate(input: string, answers) {
return input.trim().length !== 0
},
default: __DEV__ ? 'temp' : '',
Expand All @@ -33,10 +33,11 @@ module.exports = function(plop: NodePlopAPI) {
type: 'input',
name: 'author',
message: '请输入作者名称',
validate(input: string, answers){
validate(input: string, answers) {
return input.trim().length !== 0
},
default: __DEV__ ? 'CaoMeiYouRen' : '',
// default: __DEV__ ? 'CaoMeiYouRen' : '',
default: 'CaoMeiYouRen',
filter: (e: string) => e.trim(),
},
{
Expand All @@ -61,16 +62,17 @@ module.exports = function(plop: NodePlopAPI) {
'auto-release',
'rollup',
'webpack',
].map(e => `${e}-template`)
'github-action',
].map((e) => `${e}-template`)
},
},
],
actions(answers){
actions(answers) {
const actions: ActionType[] = []
actions.push({
type: 'initProject',
})
return actions
},
})
}
}
6 changes: 3 additions & 3 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ export async function init(projectPath: string, pkgData: IPackage) {
const forEachSetVersion = (dep: Record<string, string>) => {
const promises: Promise<unknown>[] = []
const manager = 'npm'
Object.keys(dep).forEach(key => {
Object.keys(dep).forEach((key) => {
// TODO: 优化逻辑
if (/^[0-9]+/.test(dep[key])) { // 开头为 数字 直接跳过。
return
Expand Down Expand Up @@ -154,8 +154,8 @@ export async function updateDependencies(pkg: IPackage) {
export function sortKey(obj: Record<string, unknown>) {
const keys = Object.keys(obj).sort((a, b) => a.localeCompare(b))
const obj2: Record<string, unknown> = {}
keys.forEach(e => {
keys.forEach((e) => {
obj2[e] = obj[e]
})
return obj2
}
}

0 comments on commit 8820f52

Please sign in to comment.