mirror of
https://github.com/vuejs/vue-cli.git
synced 2026-04-23 21:40:00 -05:00
refactor: generator internal tweaks
- avoid hitting the disk again when extracting / extending config files - normalize paths when reading files before invoking
This commit is contained in:
@@ -1,10 +1,10 @@
|
||||
const ejs = require('ejs')
|
||||
const slash = require('slash')
|
||||
const debug = require('debug')
|
||||
const GeneratorAPI = require('./GeneratorAPI')
|
||||
const sortObject = require('./util/sortObject')
|
||||
const writeFileTree = require('./util/writeFileTree')
|
||||
const configTransforms = require('./util/configTransforms')
|
||||
const normalizeFilePaths = require('./util/normalizeFilePaths')
|
||||
const injectImportsAndOptions = require('./util/injectImportsAndOptions')
|
||||
const { toShortPluginId, matchesPluginId } = require('@vue/cli-shared-utils')
|
||||
|
||||
@@ -82,7 +82,7 @@ module.exports = class Generator {
|
||||
const res = transform(
|
||||
value,
|
||||
checkExisting,
|
||||
this.context
|
||||
this.files
|
||||
)
|
||||
const { content, filename } = res
|
||||
this.files[filename] = content
|
||||
@@ -141,20 +141,20 @@ module.exports = class Generator {
|
||||
for (const middleware of this.fileMiddlewares) {
|
||||
await middleware(files, ejs.render)
|
||||
}
|
||||
|
||||
// normalize file paths on windows
|
||||
// all paths are converted to use / instead of \
|
||||
normalizeFilePaths(files)
|
||||
|
||||
// handle imports and root option injections
|
||||
Object.keys(files).forEach(file => {
|
||||
// normalize paths
|
||||
const normalized = slash(file)
|
||||
if (file !== normalized) {
|
||||
files[normalized] = files[file]
|
||||
delete files[file]
|
||||
}
|
||||
// handle imports and root option injections
|
||||
files[normalized] = injectImportsAndOptions(
|
||||
files[normalized],
|
||||
this.imports[normalized],
|
||||
this.rootOptions[normalized]
|
||||
files[file] = injectImportsAndOptions(
|
||||
files[file],
|
||||
this.imports[file],
|
||||
this.rootOptions[file]
|
||||
)
|
||||
})
|
||||
|
||||
for (const postProcess of this.postProcessFilesCbs) {
|
||||
await postProcess(files)
|
||||
}
|
||||
|
||||
@@ -7,8 +7,9 @@ const inquirer = require('inquirer')
|
||||
const isBinary = require('isbinaryfile')
|
||||
const Generator = require('./Generator')
|
||||
const { loadOptions } = require('./options')
|
||||
const { installDeps } = require('./util/installDeps')
|
||||
const { loadModule } = require('./util/module')
|
||||
const { installDeps } = require('./util/installDeps')
|
||||
const normalizeFilePaths = require('./util/normalizeFilePaths')
|
||||
const {
|
||||
log,
|
||||
error,
|
||||
@@ -33,7 +34,7 @@ async function readFiles (context) {
|
||||
? fs.readFileSync(name)
|
||||
: fs.readFileSync(name, 'utf-8')
|
||||
}
|
||||
return res
|
||||
return normalizeFilePaths(res)
|
||||
}
|
||||
|
||||
function getPkg (context) {
|
||||
|
||||
@@ -1,15 +1,12 @@
|
||||
const fs = require('fs')
|
||||
const path = require('path')
|
||||
const extendJSConfig = require('./extendJSConfig')
|
||||
const stringifyJS = require('./stringifyJS')
|
||||
|
||||
function makeJSTransform (filename) {
|
||||
return function transformToJS (value, checkExisting, context) {
|
||||
const absolutePath = path.resolve(context, filename)
|
||||
if (checkExisting && fs.existsSync(absolutePath)) {
|
||||
return function transformToJS (value, checkExisting, files) {
|
||||
if (checkExisting && files[filename]) {
|
||||
return {
|
||||
filename,
|
||||
content: extendJSConfig(value, fs.readFileSync(absolutePath, 'utf-8'))
|
||||
content: extendJSConfig(value, files[filename])
|
||||
}
|
||||
} else {
|
||||
return {
|
||||
@@ -21,11 +18,10 @@ function makeJSTransform (filename) {
|
||||
}
|
||||
|
||||
function makeJSONTransform (filename) {
|
||||
return function transformToJSON (value, checkExisting, context) {
|
||||
return function transformToJSON (value, checkExisting, files) {
|
||||
let existing = {}
|
||||
const absolutePath = path.resolve(context, filename)
|
||||
if (checkExisting && fs.existsSync(absolutePath)) {
|
||||
existing = JSON.parse(fs.readFileSync(absolutePath, 'utf-8'))
|
||||
if (checkExisting && files[filename]) {
|
||||
existing = JSON.parse(files[filename])
|
||||
}
|
||||
value = Object.assign(existing, value)
|
||||
return {
|
||||
@@ -36,12 +32,12 @@ function makeJSONTransform (filename) {
|
||||
}
|
||||
|
||||
function makeMutliExtensionJSONTransform (filename, preferJS) {
|
||||
return function transformToMultiExtensions (value, checkExisting, context) {
|
||||
return function transformToMultiExtensions (value, checkExisting, files) {
|
||||
function defaultTransform () {
|
||||
if (preferJS) {
|
||||
return makeJSTransform(`${filename}.js`)(value, false, context)
|
||||
return makeJSTransform(`${filename}.js`)(value, false, files)
|
||||
} else {
|
||||
return makeJSONTransform(filename)(value, false, context)
|
||||
return makeJSONTransform(filename)(value, false, files)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -49,17 +45,16 @@ function makeMutliExtensionJSONTransform (filename, preferJS) {
|
||||
return defaultTransform()
|
||||
}
|
||||
|
||||
const absolutePath = path.resolve(context, filename)
|
||||
if (fs.existsSync(absolutePath)) {
|
||||
return makeJSONTransform(filename)(value, checkExisting, context)
|
||||
} else if (fs.existsSync(`${absolutePath}.json`)) {
|
||||
return makeJSONTransform(`${filename}.json`)(value, checkExisting, context)
|
||||
} else if (fs.existsSync(`${absolutePath}.js`)) {
|
||||
return makeJSTransform(`${filename}.js`)(value, checkExisting, context)
|
||||
} else if (fs.existsSync(`${absolutePath}.yaml`)) {
|
||||
return transformYAML(value, `${filename}.yaml`, fs.readFileSync(`${absolutePath}.yaml`, 'utf-8'))
|
||||
} else if (fs.existsSync(`${absolutePath}.yml`)) {
|
||||
return transformYAML(value, `${filename}.yml`, fs.readFileSync(`${absolutePath}.yml`, 'utf-8'))
|
||||
if (files[filename]) {
|
||||
return makeJSONTransform(filename)(value, checkExisting, files)
|
||||
} else if (files[`${filename}.json`]) {
|
||||
return makeJSONTransform(`${filename}.json`)(value, checkExisting, files)
|
||||
} else if (files[`${filename}.js`]) {
|
||||
return makeJSTransform(`${filename}.js`)(value, checkExisting, files)
|
||||
} else if (files[`${filename}.yaml`]) {
|
||||
return transformYAML(value, `${filename}.yaml`, files[`${filename}.yaml`])
|
||||
} else if (files[`${filename}.yml`]) {
|
||||
return transformYAML(value, `${filename}.yml`, files[`${filename}.yml`])
|
||||
} else {
|
||||
return defaultTransform()
|
||||
}
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
const slash = require('slash')
|
||||
|
||||
module.exports = function normalizeFilePaths (files) {
|
||||
Object.keys(files).forEach(file => {
|
||||
const normalized = slash(file)
|
||||
if (file !== normalized) {
|
||||
files[normalized] = files[file]
|
||||
delete files[file]
|
||||
}
|
||||
})
|
||||
return files
|
||||
}
|
||||
Reference in New Issue
Block a user