Skip to content
This repository has been archived by the owner on Aug 11, 2024. It is now read-only.

Commit

Permalink
feat: v2.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
surmon-china committed Mar 16, 2022
1 parent 8df10d8 commit 8f3681c
Show file tree
Hide file tree
Showing 45 changed files with 925 additions and 1,726 deletions.
4 changes: 2 additions & 2 deletions .prettierrc
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
"tabWidth": 2,
"semi": false,
"singleQuote": true,
"printWidth": 88,
"printWidth": 96,
"endOfLine": "auto"
}
}
17 changes: 2 additions & 15 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,21 +1,8 @@
{
"cSpell.enabled": false,
"editor.defaultFormatter": "esbenp.prettier-vscode",
"editor.detectIndentation": false,
"editor.formatOnSave": true,
"editor.formatOnType": true,
"editor.codeActionsOnSave": {
"source.fixAll": true
},
"typescript.tsdk": "node_modules/typescript/lib",
"cSpell.words": [
"buble",
"camelized",
"consola",
"kebabcase",
"libundler",
"pascalify",
"rolluper",
"surmon",
"ubundler"
]
}
}
14 changes: 14 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,20 @@

All notable changes to this project will be documented in this file.

### 2.0.0 (2022-03-16)

**Breaking Change**

- Remove Vue SFC support, move to [Vite library-mode](https://vitejs.dev/guide/build.html#library-mode)

**Feature**

- Support `.ts` `esm` `cjs` config file

**Chore**

- Add `test` script

### 1.1.1 (2021-12-05)

**Bug fix**
Expand Down
53 changes: 38 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,11 @@

📦 Universal JavaScript library bundler, powered by [Rollup](https://github.com/rollup).

🏭 通用的 JavaScript 库打包工具,零配置、开箱即用。

---

### Usage

1. **install**
**1. install**

```bash
npm install @surmon-china/libundler --save-dev
Expand All @@ -30,7 +28,7 @@ or
yarn add @surmon-china/libundler --dev
```

2. **add `build` script to `package.json`**
**2. add `build` script to `package.json`**

```json
"scripts": {
Expand All @@ -46,7 +44,7 @@ or
}
```

3. **run build**
**3. run build**

```bash
yarn build
Expand All @@ -56,23 +54,24 @@ yarn build

### Config

By default, you do not need to specify a configuration file, libundler will generate a nearly perfect configuration for the bundle according to your `package.json`.
By default, you do not need to specify configuration file, libundler will generate a nearly perfect configuration for the bundle according to your `package.json`.

But if you have more specific needs, you can create `libundler.config.js` in your project root.
But if you have more specific needs, you can create `libundler.config.js` or `libundler.config.ts` in your project root.

[**`libundler.config.js` interface**](https://github.com/surmon-china/libundler/blob/main/src/interface.ts)
[**libundler config interface**](/src/interface.ts)

- `LibundlerConfigObject`
- `LibundlerConfigObject[]`
- `(defaultRollupConfig) => RollupConfig`

**config example projects:**

- [javascript](https://github.com/surmon-china/libundler/tree/main/tests/javascript)
- [typescript](https://github.com/surmon-china/libundler/tree/main/tests/typescript)
- [react-jsx-scss](https://github.com/surmon-china/libundler/tree/main/tests/react-jsx-scss)
- [react-tsx](https://github.com/surmon-china/libundler/tree/main/tests/react-tsx)
- [vue-ts](https://github.com/surmon-china/libundler/tree/main/tests/vue-ts)
- [javascript nope config](/tests/nope-config)
- [javascript `cjs` format config](/tests/cjs-config)
- [javascript `esm` format config](/tests/esm-config)
- [typescript](/tests/typescript)
- [react-jsx-scss](/tests/react-jsx-scss)
- [react-tsx](/tests/react-tsx)

**object config example:**

Expand Down Expand Up @@ -110,7 +109,7 @@ module.exports = [
// libundler.config.js

/** @type {import('@surmon-china/libundler/lib/interface').LibundlerConfigFn} */
module.exports = (rollupConfig): RollupConfig => {
module.exports = (rollupConfig) => {
// overwrite the Rollup config
rollupConfig.plugins.push(/* ... */)

Expand All @@ -119,13 +118,37 @@ module.exports = (rollupConfig): RollupConfig => {
}
```

**`esm` config example:**

```ts
// libundler.config.js
export default {
entry: 'src/index.js',
// ...
}
```

**`.ts` config example:**

```ts
// libundler.config.ts
import { defineConfig } from '@surmon-china/libundler'

export default defineConfig({
entry: 'src/index.js',
// ...
})
```

### JavaScript API

```js
const libundler = require('@surmon-china/libundler')

libundler
.bundle(/* bundlerConfig */)
.bundle({
/* LibundlerConfig */
})
.then((result) => {
console.log('bundle success', result)
})
Expand Down
47 changes: 23 additions & 24 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@surmon-china/libundler",
"version": "1.1.1",
"version": "2.0.0",
"description": "Universal JavaScript library bundler",
"author": "Surmon",
"license": "MIT",
Expand All @@ -13,8 +13,6 @@
"TypeScript bundler",
"zero config bundler",
"react component bundler",
"vue component bundler",
"vue SFC bundler",
"rollup",
"webpack"
],
Expand All @@ -33,48 +31,49 @@
"scripts": {
"dev": "tsc -w",
"build": "tsc",
"test": "node ./tests",
"lint": "eslint --ext .ts src/**",
"format": "prettier --write --parser typescript \"src/**/*.ts\"",
"rebirth": "npm run lint && npm run build",
"rebirth": "npm run lint && npm run test && npm run build",
"release": ". ./scripts/release.sh"
},
"dependencies": {
"@babel/core": "^7.16",
"@babel/core": "^7.17",
"@rollup/plugin-alias": "^3.1",
"@rollup/plugin-babel": "^5.3",
"@rollup/plugin-buble": "^0.21",
"@rollup/plugin-commonjs": "^21.0",
"@rollup/plugin-eslint": "^8.0",
"@rollup/plugin-json": "^4.1",
"@rollup/plugin-node-resolve": "^13.0",
"@rollup/plugin-replace": "^3.0",
"@vue/compiler-sfc": "^3",
"@rollup/plugin-node-resolve": "^13.1",
"@rollup/plugin-replace": "^4.0",
"chalk": "^4.1.2",
"commander": "^8.3",
"commander": "^9.0",
"consola": "^2.15",
"postcss": "^8.4.4",
"rollup": "^2.60",
"esbuild": "^0.14.27",
"postcss": "^8.4.11",
"rollup": "^2.70",
"rollup-plugin-postcss": "^4.0",
"rollup-plugin-terser": "^7.0",
"rollup-plugin-ts": "^2.0.4",
"rollup-plugin-visualizer": "^5.5",
"rollup-plugin-vue": "^6.0",
"rollup-plugin-ts": "^2.0.5",
"rollup-plugin-visualizer": "^5.6",
"tslib": "^2.3.1"
},
"devDependencies": {
"@types/babel__core": "^7.1.16",
"@types/eslint": "^7.28.0",
"eslint": "^7.31.0",
"eslint-config-prettier": "^8.3.0",
"@types/babel__core": "^7.1.18",
"@types/eslint": "^7.x",
"@typescript-eslint/eslint-plugin": "^5.15.0",
"@typescript-eslint/parser": "^5.15.0",
"eslint": "^7.x",
"eslint-config-prettier": "^8.5.0",
"eslint-plugin-prettier": "^4.0.0",
"@typescript-eslint/eslint-plugin": "^5.5.0",
"@typescript-eslint/parser": "^5.5.0",
"prettier": "^2.5.1",
"rollup": "^2.60",
"sass": "^1.44.0",
"typescript": "^4.5.2"
"rollup": "^2.70",
"sass": "^1.49.9",
"shelljs": "^0.8.5",
"typescript": "^4.6.2"
},
"engines": {
"node": ">=12"
"node": ">=14"
}
}
14 changes: 8 additions & 6 deletions src/bin.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { program } from 'commander'
import consola from 'consola'
import { LIB_NAME, LIB_PACKAGE_JSON, LIB_CONFIG_FILE_NAME } from './constant'
import { program } from 'commander'
import { LIB_NAME, LIB_PACKAGE_JSON } from './constant'
import { logger, link, red } from './logger'
import { loadProjectFile } from './utils'
import { loadUserConfig } from './loader'
import { bundle } from '.'

consola.wrapAll()
Expand All @@ -21,9 +21,11 @@ program
.command('build', { isDefault: true })
.description('Run bundler')
.action(() => {
bundle(loadProjectFile(LIB_CONFIG_FILE_NAME))
.then(() => process.exit(0))
.catch(() => process.exit(1))
loadUserConfig().then((config) => {
bundle(config || void 0)
.then(() => process.exit(0))
.catch(() => process.exit(1))
})
})

program.parse(process.argv)
12 changes: 2 additions & 10 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,11 @@ import postcss from 'rollup-plugin-postcss'
import { terser } from 'rollup-plugin-terser'
import visualizer from 'rollup-plugin-visualizer'
import typescript from 'rollup-plugin-ts'
import vue from 'rollup-plugin-vue'
import { TargetBundleModuleType, RollupParserType } from './constant'
import { LibundlerConfigObject } from './interface'
import { logger } from './logger'

export const configToRollupConfig = (
bundlerConfig: LibundlerConfigObject
): RollupOptions => {
export const configToRollupConfig = (bundlerConfig: LibundlerConfigObject): RollupOptions => {
const rollupOutput: OutputOptions[] = []
const rollupPlugins: Plugin[] = []
const rollupConfig: RollupOptions = {
Expand Down Expand Up @@ -85,11 +82,6 @@ export const configToRollupConfig = (
// JSON
rollupPlugins.push(json())

// Vue
if (bundlerConfig.vue) {
rollupPlugins.push(vue(bundlerConfig.vue))
}

// postcss
rollupPlugins.push(
postcss({
Expand Down Expand Up @@ -126,7 +118,7 @@ export const configToRollupConfig = (
)
)
} else {
logger.warn('请指定正确的 parser 选项,可选:babel | buble')
logger.warn(`'parser' only supports 'babel' or 'buble' types`)
}
}

Expand Down
5 changes: 4 additions & 1 deletion src/constant.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import path from 'path'
import process from 'process'

export const PROJECT_ROOT_PATH = process.cwd()

export const LIB_NAME = 'libundler'
export const LIB_CONFIG_FILE_NAME = 'libundler.config.js'
export const LIB_CONFIG_FILE_NAME = 'libundler.config'
export const LIB_PACKAGE_JSON = require(path.resolve(__dirname, '..', 'package.json'))

export const enum TargetBundleModuleType {
Expand Down
3 changes: 0 additions & 3 deletions src/default.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import { pascalify, kebabcase, loadProjectFile } from './utils'
const PACKAGE_NAME = Object.freeze({
TypeScript: 'typescript',
React: 'react',
Vue: 'vue',
ESLint: 'eslint',
})

Expand All @@ -27,7 +26,6 @@ export const getDefaultConfig = (): Partial<LibundlerConfigObject> => {

const isEnabledTS = [...depNames, ...devDepNames].includes(PACKAGE_NAME.TypeScript)
const isEnabledESLint = Boolean(allDependencies[PACKAGE_NAME.ESLint])
const isEnabledVue = Boolean(allDependencies[PACKAGE_NAME.Vue])
// const isEnabledReact = Boolean(allDependencies[PACKAGE_NAME.React])

const banner = getDefaultBanner({
Expand Down Expand Up @@ -69,7 +67,6 @@ export const getDefaultConfig = (): Partial<LibundlerConfigObject> => {
parser: RollupParserType.Buble,
parserOptions: {},
postcss: {},
vue: isEnabledVue ? {} : false,
eslint: isEnabledESLint ? {} : false,
typescript: isEnabledTS ? {} : false,
minimize: true,
Expand Down
6 changes: 5 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ import { getDefaultConfig } from './default'
import { configToRollupConfig } from './config'
import { logger, br, dir, yellow } from './logger'

export * from './interface'
export { configToRollupConfig } from './config'
export const bundle = (bundlerConfig: LibundlerConfig) => {

export const bundle = (bundlerConfig?: LibundlerConfig) => {
// config
const defaultConfig: Partial<LibundlerConfigObject> = getDefaultConfig()
let rollupConfig: RollupOptions | Array<RollupOptions> | null = null
Expand Down Expand Up @@ -93,10 +95,12 @@ export const bundle = (bundlerConfig: LibundlerConfig) => {
: doBundle(rollupConfig)
)
.then((result) => {
br()
logger.success(`${bundlePrefix} bundle done!`)
return result
})
.catch((error) => {
br()
logger.error(`bundle error!`, error)
throw error
})
Expand Down
Loading

0 comments on commit 8f3681c

Please sign in to comment.