UNPKG

zoro-cli

Version:

https://github.com/vuejs/vue-cli

114 lines (102 loc) 3.17 kB
const path = require('path') const hash = require('hash-sum') class PluginAPI { constructor(id, service) { this.id = id this.service = service } /** * Resolve path for a project. * * @param {string} _path - Relative path from project root * @return {string} The resolved absolute path. */ resolve(_path) { return path.resolve(this.service.context, _path) } /** * Register a function that will receive a chainable webpack config * the function is lazy and won't be called until `resolveWebpackConfig` is * called * * @param {function} fn */ chainWebpack(fn) { this.service.webpackChainFns.push(fn) } /** * Register * - a webpack configuration object that will be merged into the config * OR * - a function that will receive the raw webpack config. * the function can either mutate the config directly or return an object * that will be merged into the config. * * @param {object | function} fn */ configureWebpack(fn) { this.service.webpackRawConfigFns.push(fn) } /** * Resolve the final raw webpack config, that will be passed to webpack. * * @param {ChainableWebpackConfig} [chainableConfig] * @return {object} Raw webpack config. */ resolveWebpackConfig(chainableConfig) { return this.service.resolveWebpackConfig(chainableConfig) } /** * Resolve an intermediate chainable webpack config instance, which can be * further tweaked before generating the final raw webpack config. * You can call this multiple times to generate different branches of the * base webpack config. * See https://github.com/mozilla-neutrino/webpack-chain * * @return {ChainableWebpackConfig} */ resolveChainableWebpackConfig() { return this.service.resolveChainableWebpackConfig() } /** * Generate a cache identifier from a number of variables */ genCacheConfig(id, partialIdentifier, configFiles) { const fs = require('fs') const cacheDirectory = this.resolve(`node_modules/.cache/${id}`) const variables = { partialIdentifier, // 'cli-service': require('../package.json').version, 'cache-loader': require('cache-loader/package.json').version, env: process.env.NODE_ENV, config: [ this.service.projectOptions.chainWebpack, this.service.projectOptions.configureWebpack, ], } if (configFiles) { const readConfig = file => { const absolutePath = this.resolve(file) if (fs.existsSync(absolutePath)) { return fs.readFileSync(absolutePath, 'utf-8') } return null } if (!Array.isArray(configFiles)) { configFiles = [configFiles] } /* eslint-disable no-restricted-syntax, no-await-in-loop */ for (const file of configFiles) { const content = readConfig(file) if (content) { variables.configFiles = content break } } /* eslint-enable no-restricted-syntax, no-await-in-loop */ } const cacheIdentifier = hash(variables) return { cacheDirectory, cacheIdentifier } } } module.exports = PluginAPI