|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const path = require('path'); |
| 4 | +const fs = require('fs'); |
| 5 | +const _ = require('lodash'); |
| 6 | +const chalk = require('chalk'); |
| 7 | + |
| 8 | +const { defaults, validate } = require('@vue/cli-service/lib/options'); |
| 9 | + |
| 10 | +module.exports = function(api, opts = {}) { |
| 11 | + |
| 12 | + const logger = api.logger; |
| 13 | + |
| 14 | + // load user config |
| 15 | + const userOptions = loadUserOptions(api, opts); |
| 16 | + const defaultsDeep = require('lodash.defaultsdeep'); |
| 17 | + const projectOptions = defaultsDeep(userOptions, defaults()); |
| 18 | + |
| 19 | + logger.debug('[vue:project-config]', projectOptions); |
| 20 | + |
| 21 | + /** |
| 22 | + * Generate a cache identifier from a number of variables |
| 23 | + */ |
| 24 | + api.extendMethod('genCacheConfig', (id, partialIdentifier, configFiles = []) => { |
| 25 | + const fs = require('fs'); |
| 26 | + const cacheDirectory = api.resolve(`node_modules/.cache/${id}`); |
| 27 | + |
| 28 | + // replace \r\n to \n generate consistent hash |
| 29 | + const fmtFunc = conf => { |
| 30 | + if (typeof conf === 'function') { |
| 31 | + return conf.toString().replace(/\r\n?/g, '\n'); |
| 32 | + } |
| 33 | + return conf; |
| 34 | + }; |
| 35 | + |
| 36 | + const variables = { |
| 37 | + partialIdentifier, |
| 38 | + 'cli-service': require('../package.json').version, |
| 39 | + 'cache-loader': require('cache-loader/package.json').version, |
| 40 | + env: process.env.NODE_ENV, |
| 41 | + test: !!process.env.VUE_CLI_TEST, |
| 42 | + config: [ |
| 43 | + fmtFunc(projectOptions.chainWebpack), |
| 44 | + fmtFunc(projectOptions.configureWebpack), |
| 45 | + ], |
| 46 | + }; |
| 47 | + |
| 48 | + if (!Array.isArray(configFiles)) { |
| 49 | + configFiles = [ configFiles ]; |
| 50 | + } |
| 51 | + configFiles = configFiles.concat([ |
| 52 | + 'package-lock.json', |
| 53 | + 'yarn.lock', |
| 54 | + 'pnpm-lock.yaml', |
| 55 | + ]); |
| 56 | + |
| 57 | + const readConfig = file => { |
| 58 | + const absolutePath = api.resolve(file); |
| 59 | + if (!fs.existsSync(absolutePath)) { |
| 60 | + return; |
| 61 | + } |
| 62 | + |
| 63 | + if (absolutePath.endsWith('.js')) { |
| 64 | + // should evaluate config scripts to reflect environment variable changes |
| 65 | + try { |
| 66 | + return JSON.stringify(require(absolutePath)); |
| 67 | + } catch (e) { |
| 68 | + return fs.readFileSync(absolutePath, 'utf-8'); |
| 69 | + } |
| 70 | + } else { |
| 71 | + return fs.readFileSync(absolutePath, 'utf-8'); |
| 72 | + } |
| 73 | + }; |
| 74 | + |
| 75 | + for (const file of configFiles) { |
| 76 | + const content = readConfig(file); |
| 77 | + if (content) { |
| 78 | + variables.configFiles = content.replace(/\r\n?/g, '\n'); |
| 79 | + break; |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + const hash = require('hash-sum'); |
| 84 | + const cacheIdentifier = hash(variables); |
| 85 | + return { cacheDirectory, cacheIdentifier }; |
| 86 | + }, { |
| 87 | + description: 'Generate a cache identifier from a number of variables', |
| 88 | + }); |
| 89 | + |
| 90 | + require('./methods.js')(api, projectOptions); |
| 91 | + |
| 92 | + const vueRoot = path.dirname(require.resolve('@vue/cli-service')); |
| 93 | + |
| 94 | + require('./plugins/commandPlugins.js')(api, vueRoot, projectOptions); |
| 95 | + require('./plugins/configPlugins.js')(api, vueRoot, projectOptions); |
| 96 | + require('./plugins/projectPlugins.js')(api, vueRoot, projectOptions); |
| 97 | +}; |
| 98 | + |
| 99 | +function loadUserOptions(api, opts) { |
| 100 | + const logger = api.logger; |
| 101 | + |
| 102 | + // vue.config.js |
| 103 | + let fileConfig, |
| 104 | + pkgConfig, |
| 105 | + resolved, |
| 106 | + resolvedFrom; |
| 107 | + |
| 108 | + const configPath = ( |
| 109 | + process.env.VUE_CLI_SERVICE_CONFIG_PATH || path.resolve(api.getCwd(), 'vue.config.js') |
| 110 | + ); |
| 111 | + if (fs.existsSync(configPath)) { |
| 112 | + try { |
| 113 | + fileConfig = require(configPath); |
| 114 | + |
| 115 | + if (typeof fileConfig === 'function') { |
| 116 | + fileConfig = fileConfig(); |
| 117 | + } |
| 118 | + |
| 119 | + if (!fileConfig || typeof fileConfig !== 'object') { |
| 120 | + logger.error( |
| 121 | + `Error loading ${chalk.bold('vue.config.js')}: should export an object or a function that returns object.` |
| 122 | + ); |
| 123 | + fileConfig = null; |
| 124 | + } |
| 125 | + } catch (e) { |
| 126 | + logger.error(`Error loading ${chalk.bold('vue.config.js')}:`); |
| 127 | + throw e; |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + // package.vue |
| 132 | + pkgConfig = api.pkg && api.pkg.vue; |
| 133 | + if (pkgConfig && typeof pkgConfig !== 'object') { |
| 134 | + logger.error( |
| 135 | + `Error loading vue-cli config in ${chalk.bold('package.json')}: ` + |
| 136 | + 'the "vue" field should be an object.' |
| 137 | + ); |
| 138 | + pkgConfig = null; |
| 139 | + } |
| 140 | + |
| 141 | + if (fileConfig) { |
| 142 | + if (pkgConfig) { |
| 143 | + logger.warn( |
| 144 | + '"vue" field in package.json ignored ' + |
| 145 | + `due to presence of ${chalk.bold('vue.config.js')}.` |
| 146 | + ); |
| 147 | + logger.warn( |
| 148 | + `You should migrate it into ${chalk.bold('vue.config.js')} ` + |
| 149 | + 'and remove it from package.json.' |
| 150 | + ); |
| 151 | + } |
| 152 | + resolved = fileConfig; |
| 153 | + resolvedFrom = 'vue.config.js'; |
| 154 | + } else if (pkgConfig) { |
| 155 | + resolved = pkgConfig; |
| 156 | + resolvedFrom = '"vue" field in package.json'; |
| 157 | + } else { |
| 158 | + resolved = opts.inlineOptions || {}; |
| 159 | + resolvedFrom = 'inline options'; |
| 160 | + } |
| 161 | + |
| 162 | + if (resolved.css && typeof resolved.css.modules !== 'undefined') { |
| 163 | + if (typeof resolved.css.requireModuleExtension !== 'undefined') { |
| 164 | + logger.warn( |
| 165 | + `You have set both "css.modules" and "css.requireModuleExtension" in ${chalk.bold('vue.config.js')}, ` + |
| 166 | + '"css.modules" will be ignored in favor of "css.requireModuleExtension".' |
| 167 | + ); |
| 168 | + } else { |
| 169 | + logger.warn( |
| 170 | + `"css.modules" option in ${chalk.bold('vue.config.js')} ` + |
| 171 | + 'is deprecated now, please use "css.requireModuleExtension" instead.' |
| 172 | + ); |
| 173 | + resolved.css.requireModuleExtension = !resolved.css.modules; |
| 174 | + } |
| 175 | + } |
| 176 | + |
| 177 | + // normalize some options |
| 178 | + ensureSlash(resolved, 'publicPath'); |
| 179 | + if (typeof resolved.publicPath === 'string') { |
| 180 | + resolved.publicPath = resolved.publicPath.replace(/^\.\//, ''); |
| 181 | + } |
| 182 | + removeSlash(resolved, 'outputDir'); |
| 183 | + |
| 184 | + // validate options |
| 185 | + validate(resolved, msg => { |
| 186 | + logger.error( |
| 187 | + `Invalid options in ${chalk.bold(resolvedFrom)}: ${msg}` |
| 188 | + ); |
| 189 | + }); |
| 190 | + |
| 191 | + return resolved; |
| 192 | +} |
| 193 | + |
| 194 | +function ensureSlash(config, key) { |
| 195 | + let val = config[key]; |
| 196 | + if (typeof val === 'string') { |
| 197 | + if (!/^https?:/.test(val)) { |
| 198 | + val = val.replace(/^([^/.])/, '/$1'); |
| 199 | + } |
| 200 | + config[key] = val.replace(/([^/])$/, '$1/'); |
| 201 | + } |
| 202 | +} |
| 203 | + |
| 204 | +function removeSlash(config, key) { |
| 205 | + if (typeof config[key] === 'string') { |
| 206 | + config[key] = config[key].replace(/\/$/g, ''); |
| 207 | + } |
| 208 | +} |
0 commit comments