This repository has been archived by the owner on Aug 11, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ed931c2
commit 08bf0cf
Showing
10 changed files
with
1,219 additions
and
2,659 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}) | ||
} |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.