vbuild
Version:
Refined webpack development experience for Vue.js
68 lines (57 loc) • 1.52 kB
JavaScript
const fs = require('fs')
const path = require('path')
const chalk = require('chalk')
const AppError = require('./app-error')
exports.cwd = function (...args) {
return path.resolve(...args)
}
exports.ownDir = function (...args) {
return path.join(__dirname, '../', ...args)
}
exports.getConfigFile = function (config) {
return exports.cwd(typeof config === 'string' ? config : 'vbuild.config.js')
}
exports.getPublicPath = function (homepage, dev) {
if (!dev && homepage) {
return /\/$/.test(homepage) ? homepage : (homepage + '/')
}
return '/'
}
exports.ensureEntry = function (obj) {
for (const key in obj) {
if (!Array.isArray(obj[key])) {
obj[key] = [obj[key]]
}
}
return obj
}
exports.readPkg = function () {
try {
return require(exports.cwd('package.json'))
} catch (err) {
if (err.code === 'MODULE_NOT_FOUND') {
return {}
}
throw new AppError(err.message)
}
}
exports.inferHTML = function ({minimize}) {
const result = {
title: 'VBuild App',
template: exports.ownDir('lib/index.html'),
minify: {
collapseWhitespace: minimize,
minifyCSS: minimize,
minifyJS: minimize
}
}
const pkg = exports.readPkg()
result.pkg = pkg
result.title = pkg.productionName || pkg.title || pkg.name
result.description = pkg.description
if (fs.existsSync('index.html')) {
console.log(chalk.bold(`> Using HTML template file at ./index.html`))
result.template = exports.cwd('index.html')
}
return result
}