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

Commit

Permalink
feat: v1.1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
surmon-china committed Dec 5, 2021
1 parent ed931c2 commit 08bf0cf
Show file tree
Hide file tree
Showing 10 changed files with 1,219 additions and 2,659 deletions.
12 changes: 11 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,21 @@

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

### 1.1.0 (2021-12-05)

**Feature**

- Support JavaScript API

**Chore**

- Upgrade deps

### 1.0.1 (2021-07-26)

**Chore**

- fix npm & CI config
- Fix npm & CI config

### 1.0.0 (2021-07-24)

Expand Down
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,21 @@ module.exports = (rollupConfig): RollupConfig => {
}
```

### JavaScript API

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

libundler
.bundle(/* bundlerConfig */)
.then((result) => {
console.log('bundle success', result)
})
.catch((error) => {
console.log('bundle error', error)
})
```

### Development

```bash
Expand Down
2 changes: 1 addition & 1 deletion bin/libundler.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
#!/usr/bin/env node
require('../lib/index.js')
require('../lib/bin.js')
34 changes: 17 additions & 17 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@surmon-china/libundler",
"version": "1.0.2",
"version": "1.1.0",
"description": "Universal JavaScript library bundler",
"author": "Surmon",
"license": "MIT",
Expand Down Expand Up @@ -39,40 +39,40 @@
"release": ". ./scripts/release.sh"
},
"dependencies": {
"@babel/core": "^7.14",
"@babel/core": "^7.16",
"@rollup/plugin-alias": "^3.1",
"@rollup/plugin-babel": "^5.3",
"@rollup/plugin-buble": "^0.21",
"@rollup/plugin-commonjs": "^19.0",
"@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",
"chalk": "^4.1",
"commander": "^8.0",
"chalk": "^5.0",
"commander": "^8.3",
"consola": "^2.15",
"postcss": "^8.3.6",
"rollup": "^2.54",
"postcss": "^8.4.4",
"rollup": "^2.60",
"rollup-plugin-postcss": "^4.0",
"rollup-plugin-terser": "^7.0",
"rollup-plugin-ts": "^1.4.0",
"rollup-plugin-ts": "^2.0.4",
"rollup-plugin-visualizer": "^5.5",
"rollup-plugin-vue": "^6.0",
"tslib": "^2.3.0"
"tslib": "^2.3.1"
},
"devDependencies": {
"@types/babel__core": "^7.1.15",
"@types/babel__core": "^7.1.16",
"@types/eslint": "^7.28.0",
"eslint": "^7.31.0",
"eslint-config-prettier": "^8.3.0",
"eslint-plugin-prettier": "^3.4.0",
"@typescript-eslint/eslint-plugin": "^4.28.4",
"@typescript-eslint/parser": "^4.28.4",
"prettier": "^2.3.2",
"rollup": "^2.54",
"sass": "^1.36.0",
"typescript": "^4.3.5"
"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"
},
"engines": {
"node": ">=12"
Expand Down
29 changes: 29 additions & 0 deletions src/bin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { program } from 'commander'
import consola from 'consola'
import { LIB_NAME, LIB_PACKAGE_JSON, LIB_CONFIG_FILE_NAME } from './constant'
import { logger, link, red } from './logger'
import { loadProjectFile } from './utils'
import { bundle } from '.'

consola.wrapAll()

program.configureOutput({
writeOut: (str) => logger.info(str),
writeErr: (str) => logger.warn(str),
outputError: (str, write) => write(red(str)),
})

program
.name(LIB_NAME)
.usage('[build]')
.helpOption('-h, --help', `${link(LIB_PACKAGE_JSON.repository.url)}`)
.version(LIB_PACKAGE_JSON.version, '-v, --version')
.command('build', { isDefault: true })
.description('Run bundler')
.action(() => {
bundle(loadProjectFile(LIB_CONFIG_FILE_NAME))
.then(() => process.exit(0))
.catch(() => process.exit(1))
})

program.parse(process.argv)
2 changes: 1 addition & 1 deletion src/config.normalize.ts → src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { TargetBundleModuleType, RollupParserType } from './constant'
import { LibundlerConfigObject } from './interface'
import { logger } from './logger'

export const normalizeRollupConfig = (
export const configToRollupConfig = (
bundlerConfig: LibundlerConfigObject
): RollupOptions => {
const rollupOutput: OutputOptions[] = []
Expand Down
File renamed without changes.
127 changes: 103 additions & 24 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,103 @@
import { program } from 'commander'
import consola from 'consola'
import { LIB_NAME, LIB_PACKAGE_JSON } from './constant'
import { logger, link, red } from './logger'
import { runRollup } from './rollup'

consola.wrapAll()

program.configureOutput({
writeOut: (str) => logger.info(str),
writeErr: (str) => logger.warn(str),
outputError: (str, write) => write(red(str)),
})

program
.name(LIB_NAME)
.usage('[build]')
.helpOption('-h, --help', `${link(LIB_PACKAGE_JSON.repository.url)}`)
.version(LIB_PACKAGE_JSON.version, '-v, --version')
.command('build', { isDefault: true })
.description('Run bundler')
.action(() => runRollup())

program.parse(process.argv)
import { rollup, RollupOptions, OutputOptions } from 'rollup'
import { LibundlerConfig, LibundlerConfigObject } from './interface'
import { getDefaultConfig } from './default'
import { configToRollupConfig } from './config'
import { logger, br, dir, yellow } from './logger'

export { configToRollupConfig } from './config'
export const bundle = (bundlerConfig: LibundlerConfig) => {
// config
const defaultConfig: Partial<LibundlerConfigObject> = getDefaultConfig()
let rollupConfig: RollupOptions | Array<RollupOptions> | null = null

/** !bundlerConfig > use default config */
if (!bundlerConfig) {
if (!defaultConfig.libName) {
logger.error(`Invalid name of package.json`)
process.exit(1)
} else {
rollupConfig = configToRollupConfig(defaultConfig as any)
}
}

// function config > function(defaultRollupConfig)
if (typeof bundlerConfig === 'function') {
rollupConfig = bundlerConfig(configToRollupConfig(defaultConfig as any))
}

// array
if (Array.isArray(bundlerConfig)) {
rollupConfig = bundlerConfig.map((item) => {
return configToRollupConfig({ ...defaultConfig, ...item })
})
}

// simple config
const libundlerConfig = {
...defaultConfig,
...bundlerConfig,
} as LibundlerConfigObject
rollupConfig = configToRollupConfig(libundlerConfig)

if (libundlerConfig.verbose) {
// log config
br()
logger.log(`libundler config ↓\n`)
dir(libundlerConfig)
br()
logger.log(`rollup config ↓\n`)
dir(rollupConfig)
}

// log title
const bundlePrefix = Array.isArray(rollupConfig)
? `${rollupConfig.length} libraries`
: `library ${yellow(libundlerConfig.libName)}`

// start
logger.info(`${bundlePrefix} bundling...`)

// rollup bundle
const doBundle = async (options: RollupOptions) => {
const bundle = await rollup(options)
if (!options.output) {
return null
}

// rollup write
const write = async (outputOptions: OutputOptions) => {
// write the bundle to disk
const { output } = await bundle.write(outputOptions)
const outChunk = output[0]
// closes the bundle
await bundle.close()
br()
logger.success(`${bundlePrefix} - ${yellow(outChunk.fileName)} is bundled!`)
if (libundlerConfig.verbose) {
dir(outChunk)
}
return output
}

// write item or list
if (Array.isArray(options.output)) {
return Promise.all(options.output.map(write))
} else {
return write(options.output)
}
}

return (
Array.isArray(rollupConfig)
? Promise.all(rollupConfig.map(doBundle))
: doBundle(rollupConfig)
)
.then((result) => {
logger.success(`${bundlePrefix} bundle done!`)
return result
})
.catch((error) => {
logger.error(`bundle error!`, error)
throw error
})
}
103 changes: 0 additions & 103 deletions src/rollup.ts

This file was deleted.

Loading

0 comments on commit 08bf0cf

Please sign in to comment.