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

Commit 08bf0cf

Browse files
committed
feat: v1.1.0
1 parent ed931c2 commit 08bf0cf

File tree

10 files changed

+1219
-2659
lines changed

10 files changed

+1219
-2659
lines changed

CHANGELOG.md

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,21 @@
22

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

5+
### 1.1.0 (2021-12-05)
6+
7+
**Feature**
8+
9+
- Support JavaScript API
10+
11+
**Chore**
12+
13+
- Upgrade deps
14+
515
### 1.0.1 (2021-07-26)
616

717
**Chore**
818

9-
- fix npm & CI config
19+
- Fix npm & CI config
1020

1121
### 1.0.0 (2021-07-24)
1222

README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,21 @@ module.exports = (rollupConfig): RollupConfig => {
115115
}
116116
```
117117

118+
### JavaScript API
119+
120+
```js
121+
const libundler = require('@surmon-china/libundler')
122+
123+
libundler
124+
.bundle(/* bundlerConfig */)
125+
.then((result) => {
126+
console.log('bundle success', result)
127+
})
128+
.catch((error) => {
129+
console.log('bundle error', error)
130+
})
131+
```
132+
118133
### Development
119134

120135
```bash

bin/libundler.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
#!/usr/bin/env node
2-
require('../lib/index.js')
2+
require('../lib/bin.js')

package.json

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@surmon-china/libundler",
3-
"version": "1.0.2",
3+
"version": "1.1.0",
44
"description": "Universal JavaScript library bundler",
55
"author": "Surmon",
66
"license": "MIT",
@@ -39,40 +39,40 @@
3939
"release": ". ./scripts/release.sh"
4040
},
4141
"dependencies": {
42-
"@babel/core": "^7.14",
42+
"@babel/core": "^7.16",
4343
"@rollup/plugin-alias": "^3.1",
4444
"@rollup/plugin-babel": "^5.3",
4545
"@rollup/plugin-buble": "^0.21",
46-
"@rollup/plugin-commonjs": "^19.0",
46+
"@rollup/plugin-commonjs": "^21.0",
4747
"@rollup/plugin-eslint": "^8.0",
4848
"@rollup/plugin-json": "^4.1",
4949
"@rollup/plugin-node-resolve": "^13.0",
5050
"@rollup/plugin-replace": "^3.0",
5151
"@vue/compiler-sfc": "^3",
52-
"chalk": "^4.1",
53-
"commander": "^8.0",
52+
"chalk": "^5.0",
53+
"commander": "^8.3",
5454
"consola": "^2.15",
55-
"postcss": "^8.3.6",
56-
"rollup": "^2.54",
55+
"postcss": "^8.4.4",
56+
"rollup": "^2.60",
5757
"rollup-plugin-postcss": "^4.0",
5858
"rollup-plugin-terser": "^7.0",
59-
"rollup-plugin-ts": "^1.4.0",
59+
"rollup-plugin-ts": "^2.0.4",
6060
"rollup-plugin-visualizer": "^5.5",
6161
"rollup-plugin-vue": "^6.0",
62-
"tslib": "^2.3.0"
62+
"tslib": "^2.3.1"
6363
},
6464
"devDependencies": {
65-
"@types/babel__core": "^7.1.15",
65+
"@types/babel__core": "^7.1.16",
6666
"@types/eslint": "^7.28.0",
6767
"eslint": "^7.31.0",
6868
"eslint-config-prettier": "^8.3.0",
69-
"eslint-plugin-prettier": "^3.4.0",
70-
"@typescript-eslint/eslint-plugin": "^4.28.4",
71-
"@typescript-eslint/parser": "^4.28.4",
72-
"prettier": "^2.3.2",
73-
"rollup": "^2.54",
74-
"sass": "^1.36.0",
75-
"typescript": "^4.3.5"
69+
"eslint-plugin-prettier": "^4.0.0",
70+
"@typescript-eslint/eslint-plugin": "^5.5.0",
71+
"@typescript-eslint/parser": "^5.5.0",
72+
"prettier": "^2.5.1",
73+
"rollup": "^2.60",
74+
"sass": "^1.44.0",
75+
"typescript": "^4.5.2"
7676
},
7777
"engines": {
7878
"node": ">=12"

src/bin.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { program } from 'commander'
2+
import consola from 'consola'
3+
import { LIB_NAME, LIB_PACKAGE_JSON, LIB_CONFIG_FILE_NAME } from './constant'
4+
import { logger, link, red } from './logger'
5+
import { loadProjectFile } from './utils'
6+
import { bundle } from '.'
7+
8+
consola.wrapAll()
9+
10+
program.configureOutput({
11+
writeOut: (str) => logger.info(str),
12+
writeErr: (str) => logger.warn(str),
13+
outputError: (str, write) => write(red(str)),
14+
})
15+
16+
program
17+
.name(LIB_NAME)
18+
.usage('[build]')
19+
.helpOption('-h, --help', `${link(LIB_PACKAGE_JSON.repository.url)}`)
20+
.version(LIB_PACKAGE_JSON.version, '-v, --version')
21+
.command('build', { isDefault: true })
22+
.description('Run bundler')
23+
.action(() => {
24+
bundle(loadProjectFile(LIB_CONFIG_FILE_NAME))
25+
.then(() => process.exit(0))
26+
.catch(() => process.exit(1))
27+
})
28+
29+
program.parse(process.argv)

src/config.normalize.ts renamed to src/config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import { TargetBundleModuleType, RollupParserType } from './constant'
1717
import { LibundlerConfigObject } from './interface'
1818
import { logger } from './logger'
1919

20-
export const normalizeRollupConfig = (
20+
export const configToRollupConfig = (
2121
bundlerConfig: LibundlerConfigObject
2222
): RollupOptions => {
2323
const rollupOutput: OutputOptions[] = []
File renamed without changes.

src/index.ts

Lines changed: 103 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,103 @@
1-
import { program } from 'commander'
2-
import consola from 'consola'
3-
import { LIB_NAME, LIB_PACKAGE_JSON } from './constant'
4-
import { logger, link, red } from './logger'
5-
import { runRollup } from './rollup'
6-
7-
consola.wrapAll()
8-
9-
program.configureOutput({
10-
writeOut: (str) => logger.info(str),
11-
writeErr: (str) => logger.warn(str),
12-
outputError: (str, write) => write(red(str)),
13-
})
14-
15-
program
16-
.name(LIB_NAME)
17-
.usage('[build]')
18-
.helpOption('-h, --help', `${link(LIB_PACKAGE_JSON.repository.url)}`)
19-
.version(LIB_PACKAGE_JSON.version, '-v, --version')
20-
.command('build', { isDefault: true })
21-
.description('Run bundler')
22-
.action(() => runRollup())
23-
24-
program.parse(process.argv)
1+
import { rollup, RollupOptions, OutputOptions } from 'rollup'
2+
import { LibundlerConfig, LibundlerConfigObject } from './interface'
3+
import { getDefaultConfig } from './default'
4+
import { configToRollupConfig } from './config'
5+
import { logger, br, dir, yellow } from './logger'
6+
7+
export { configToRollupConfig } from './config'
8+
export const bundle = (bundlerConfig: LibundlerConfig) => {
9+
// config
10+
const defaultConfig: Partial<LibundlerConfigObject> = getDefaultConfig()
11+
let rollupConfig: RollupOptions | Array<RollupOptions> | null = null
12+
13+
/** !bundlerConfig > use default config */
14+
if (!bundlerConfig) {
15+
if (!defaultConfig.libName) {
16+
logger.error(`Invalid name of package.json`)
17+
process.exit(1)
18+
} else {
19+
rollupConfig = configToRollupConfig(defaultConfig as any)
20+
}
21+
}
22+
23+
// function config > function(defaultRollupConfig)
24+
if (typeof bundlerConfig === 'function') {
25+
rollupConfig = bundlerConfig(configToRollupConfig(defaultConfig as any))
26+
}
27+
28+
// array
29+
if (Array.isArray(bundlerConfig)) {
30+
rollupConfig = bundlerConfig.map((item) => {
31+
return configToRollupConfig({ ...defaultConfig, ...item })
32+
})
33+
}
34+
35+
// simple config
36+
const libundlerConfig = {
37+
...defaultConfig,
38+
...bundlerConfig,
39+
} as LibundlerConfigObject
40+
rollupConfig = configToRollupConfig(libundlerConfig)
41+
42+
if (libundlerConfig.verbose) {
43+
// log config
44+
br()
45+
logger.log(`libundler config ↓\n`)
46+
dir(libundlerConfig)
47+
br()
48+
logger.log(`rollup config ↓\n`)
49+
dir(rollupConfig)
50+
}
51+
52+
// log title
53+
const bundlePrefix = Array.isArray(rollupConfig)
54+
? `${rollupConfig.length} libraries`
55+
: `library ${yellow(libundlerConfig.libName)}`
56+
57+
// start
58+
logger.info(`${bundlePrefix} bundling...`)
59+
60+
// rollup bundle
61+
const doBundle = async (options: RollupOptions) => {
62+
const bundle = await rollup(options)
63+
if (!options.output) {
64+
return null
65+
}
66+
67+
// rollup write
68+
const write = async (outputOptions: OutputOptions) => {
69+
// write the bundle to disk
70+
const { output } = await bundle.write(outputOptions)
71+
const outChunk = output[0]
72+
// closes the bundle
73+
await bundle.close()
74+
br()
75+
logger.success(`${bundlePrefix} - ${yellow(outChunk.fileName)} is bundled!`)
76+
if (libundlerConfig.verbose) {
77+
dir(outChunk)
78+
}
79+
return output
80+
}
81+
82+
// write item or list
83+
if (Array.isArray(options.output)) {
84+
return Promise.all(options.output.map(write))
85+
} else {
86+
return write(options.output)
87+
}
88+
}
89+
90+
return (
91+
Array.isArray(rollupConfig)
92+
? Promise.all(rollupConfig.map(doBundle))
93+
: doBundle(rollupConfig)
94+
)
95+
.then((result) => {
96+
logger.success(`${bundlePrefix} bundle done!`)
97+
return result
98+
})
99+
.catch((error) => {
100+
logger.error(`bundle error!`, error)
101+
throw error
102+
})
103+
}

src/rollup.ts

Lines changed: 0 additions & 103 deletions
This file was deleted.

0 commit comments

Comments
 (0)