Skip to content
This repository has been archived by the owner on Dec 6, 2021. It is now read-only.

Commit

Permalink
Expose JavaScript API
Browse files Browse the repository at this point in the history
  • Loading branch information
egoist committed Feb 17, 2017
1 parent efb9fca commit 171ef12
Show file tree
Hide file tree
Showing 9 changed files with 68 additions and 67 deletions.
30 changes: 30 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
+ [host](#host)
* [Custom server logic](#custom-server-logic)
* [Custom build process](#custom-build-process)
* [JavaScript API](#javascript-api)
- [Recipes](#recipes)
- [FAQ](#faq)
- [Contributing](#contributing)
Expand Down Expand Up @@ -420,6 +421,35 @@ module.exports = {

[⬆ back to top](#vbuild)

### JavaScript API

You can use vbuild as a Node.js module:

```js
const vbuild = require('vbuild')

const res = vbuild(cliOptions)
//=> console.log(res)
{
webpackConfig, // final webpack config
options, // final options
server // in dev mode, an instance of `http.Server`
}

// error catch
try {
vbuild(options)
} catch (err) {
if (err.name === 'AppError') {
// error occurs in starting the compilation
} else {
// other unknown error
}
}
```

[⬆ back to top](#vbuild)

## Recipes

- [Progressive web app](./docs/recipes/progressive-web-app.md)
Expand Down
9 changes: 5 additions & 4 deletions bin/vbuild-init
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ const kopy = require('kopy')
const $ = require('shelljs')
const tildify = require('tildify')
const install = require('yarn-install')
const logger = require('../lib/logger')
const _ = require('../lib/utils')

const cli = meow(`
Expand All @@ -28,12 +27,14 @@ const cli = meow(`
const name = cli.input[0]

if (!name) {
logger.fatal('A folder name is required to perform this action!')
console.error(chalk.red('> A folder name is required to perform this action!'))
process.exit(1)
}

const dest = _.cwd(name)
if (!cli.flags.force && fs.existsSync(dest)) {
logger.fatal(`${chalk.yellow(tildify(dest))} already exists, you can use \`--force\` option to override it`)
console.error(chalk.red(`${chalk.yellow(tildify(dest))} already exists, you can use \`--force\` option to override it`))
process.exit(1)
}

const data = Object.assign({
Expand Down Expand Up @@ -71,4 +72,4 @@ kopy(_.ownDir('template'), dest, {

console.log(chalk.bold('To build for production:'))
console.log('\n npm run build\n')
}).catch(logger.fatal)
}).catch(console.error)
12 changes: 11 additions & 1 deletion bin/vbuild-pack
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,14 @@ if (cli.input[0]) {
options.entry = options.entry || cli.input[0]
}

require('../lib')(options)
try {
require('../lib')(options)
} catch (err) {
console.error(`${chalk.bgRed.black(' ERROR ')} Something went wrong during the build:\n`)
if (err.name === 'AppError') {
console.error(chalk.red(err.message))
} else {
console.error(err.stack)
}
process.exit(1)
}
7 changes: 7 additions & 0 deletions lib/app-error.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module.exports = class AppError extends Error {
constructor(msg) {
super()
this.name = this.constructor.name
this.message = msg
}
}
8 changes: 5 additions & 3 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ const {cwd, ownDir, getConfigFile} = require('./utils')
const run = require('./run')
const loaders = require('./loaders')
const loadConfig = require('./load-config')
const logger = require('./logger')
const AppError = require('./app-error')

function start(cliOptions) { // eslint-disable-line complexity
function start(cliOptions = {}) { // eslint-disable-line complexity
console.log('> Starting...')
const userConfig = loadConfig(cliOptions)

Expand Down Expand Up @@ -80,7 +80,7 @@ function start(cliOptions) { // eslint-disable-line complexity
}

if (options.entry === 'index.js' && !fs.existsSync(options.entry)) {
logger.fatal(`Entry file ${chalk.yellow(options.entry)} does not exist, did you forget to create one?`)
throw new AppError(`Entry file ${chalk.yellow(options.entry)} does not exist, did you forget to create one?`)
}

if (options.dev) {
Expand Down Expand Up @@ -343,6 +343,8 @@ function start(cliOptions) { // eslint-disable-line complexity
})
}
}

return {webpackConfig, options, server}
}

function getFilenames(options) {
Expand Down
4 changes: 2 additions & 2 deletions lib/load-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const tildify = require('tildify')
const chalk = require('chalk')
const requireUncached = require('require-uncached')
const _ = require('./utils')
const AppError = require('./app-error')

module.exports = function (options) {
if (options.config) {
Expand All @@ -18,8 +19,7 @@ module.exports = function (options) {
delete userConfig.production
return Object.assign({}, userConfig, options.dev ? devConfig : prodConfig)
}
console.error(chalk.red(`> Config file does not exist at ${tildify(configPath)}`))
process.exit(1)
throw new AppError(chalk.red(`> Config file does not exist at ${tildify(configPath)}`))
}
return {}
}
48 changes: 0 additions & 48 deletions lib/logger.js

This file was deleted.

9 changes: 4 additions & 5 deletions lib/run.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const chalk = require('chalk')
const rm = require('rimraf').sync
const webpack = require('webpack')
const createServer = require('../lib/server')
const logger = require('./logger')
const AppError = require('./app-error')

module.exports = function (webpackConfig, options) {
process.stdout.write('\x1Bc')
Expand All @@ -21,7 +21,7 @@ module.exports = function (webpackConfig, options) {
compiler = webpack(webpackConfig)
} catch (err) {
if (err.name === 'WebpackOptionsValidationError') {
logger.fatal(err.message)
throw new AppError(err.message)
} else {
throw err
}
Expand All @@ -48,12 +48,11 @@ module.exports = function (webpackConfig, options) {

function handleBuild(err, stats, watch) {
if (err) {
logger.fatal(err.stack)
throw new AppError(err.stack)
}
if (stats.hasErrors() || stats.hasWarnings()) {
const failureMessage = `\n\n${chalk.bgRed.black(' ERROR ')} Compiling failed!\n`
console.log(stats.toString('errors-only') + failureMessage)
process.exit(1)
throw new AppError(stats.toString('errors-only') + failureMessage)
}
console.log(stats.toString(options.stats))
console.log(`\n${chalk.bgGreen.black(' DONE ')} Compiled successfully!\n`)
Expand Down
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"url": "egoist/vbuild",
"type": "git"
},
"main": "lib/index.js",
"files": [
"bin",
"lib",
Expand All @@ -17,7 +18,7 @@
"bin": "bin/vbuild",
"scripts": {
"test": "jest && npm run lint",
"lint": "xo --fix",
"lint": "xo",
"toc": "markdown-toc -i README.md",
"precommit": "npm test"
},
Expand Down Expand Up @@ -45,8 +46,7 @@
],
"rules": {
"guard-for-in": 0,
"import/no-dynamic-require": 0,
"unicorn/no-process-exit": 0
"import/no-dynamic-require": 0
}
},
"dependencies": {
Expand Down Expand Up @@ -91,4 +91,4 @@
"webpack-merge": "^2.6.1",
"yarn-install": "^0.2.1"
}
}
}

0 comments on commit 171ef12

Please sign in to comment.