Skip to content

Commit 79ee90b

Browse files
committed
refactor: generator internal tweaks
- avoid hitting the disk again when extracting / extending config files - normalize paths when reading files before invoking
1 parent 65d5d36 commit 79ee90b

File tree

4 files changed

+47
-39
lines changed

4 files changed

+47
-39
lines changed

packages/@vue/cli/lib/Generator.js

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
const ejs = require('ejs')
2-
const slash = require('slash')
32
const debug = require('debug')
43
const GeneratorAPI = require('./GeneratorAPI')
54
const sortObject = require('./util/sortObject')
65
const writeFileTree = require('./util/writeFileTree')
76
const configTransforms = require('./util/configTransforms')
7+
const normalizeFilePaths = require('./util/normalizeFilePaths')
88
const injectImportsAndOptions = require('./util/injectImportsAndOptions')
99
const { toShortPluginId, matchesPluginId } = require('@vue/cli-shared-utils')
1010

@@ -82,7 +82,7 @@ module.exports = class Generator {
8282
const res = transform(
8383
value,
8484
checkExisting,
85-
this.context
85+
this.files
8686
)
8787
const { content, filename } = res
8888
this.files[filename] = content
@@ -141,20 +141,20 @@ module.exports = class Generator {
141141
for (const middleware of this.fileMiddlewares) {
142142
await middleware(files, ejs.render)
143143
}
144+
145+
// normalize file paths on windows
146+
// all paths are converted to use / instead of \
147+
normalizeFilePaths(files)
148+
149+
// handle imports and root option injections
144150
Object.keys(files).forEach(file => {
145-
// normalize paths
146-
const normalized = slash(file)
147-
if (file !== normalized) {
148-
files[normalized] = files[file]
149-
delete files[file]
150-
}
151-
// handle imports and root option injections
152-
files[normalized] = injectImportsAndOptions(
153-
files[normalized],
154-
this.imports[normalized],
155-
this.rootOptions[normalized]
151+
files[file] = injectImportsAndOptions(
152+
files[file],
153+
this.imports[file],
154+
this.rootOptions[file]
156155
)
157156
})
157+
158158
for (const postProcess of this.postProcessFilesCbs) {
159159
await postProcess(files)
160160
}

packages/@vue/cli/lib/invoke.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@ const inquirer = require('inquirer')
77
const isBinary = require('isbinaryfile')
88
const Generator = require('./Generator')
99
const { loadOptions } = require('./options')
10-
const { installDeps } = require('./util/installDeps')
1110
const { loadModule } = require('./util/module')
11+
const { installDeps } = require('./util/installDeps')
12+
const normalizeFilePaths = require('./util/normalizeFilePaths')
1213
const {
1314
log,
1415
error,
@@ -33,7 +34,7 @@ async function readFiles (context) {
3334
? fs.readFileSync(name)
3435
: fs.readFileSync(name, 'utf-8')
3536
}
36-
return res
37+
return normalizeFilePaths(res)
3738
}
3839

3940
function getPkg (context) {

packages/@vue/cli/lib/util/configTransforms.js

Lines changed: 19 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
1-
const fs = require('fs')
2-
const path = require('path')
31
const extendJSConfig = require('./extendJSConfig')
42
const stringifyJS = require('./stringifyJS')
53

64
function makeJSTransform (filename) {
7-
return function transformToJS (value, checkExisting, context) {
8-
const absolutePath = path.resolve(context, filename)
9-
if (checkExisting && fs.existsSync(absolutePath)) {
5+
return function transformToJS (value, checkExisting, files) {
6+
if (checkExisting && files[filename]) {
107
return {
118
filename,
12-
content: extendJSConfig(value, fs.readFileSync(absolutePath, 'utf-8'))
9+
content: extendJSConfig(value, files[filename])
1310
}
1411
} else {
1512
return {
@@ -21,11 +18,10 @@ function makeJSTransform (filename) {
2118
}
2219

2320
function makeJSONTransform (filename) {
24-
return function transformToJSON (value, checkExisting, context) {
21+
return function transformToJSON (value, checkExisting, files) {
2522
let existing = {}
26-
const absolutePath = path.resolve(context, filename)
27-
if (checkExisting && fs.existsSync(absolutePath)) {
28-
existing = JSON.parse(fs.readFileSync(absolutePath, 'utf-8'))
23+
if (checkExisting && files[filename]) {
24+
existing = JSON.parse(files[filename])
2925
}
3026
value = Object.assign(existing, value)
3127
return {
@@ -36,30 +32,29 @@ function makeJSONTransform (filename) {
3632
}
3733

3834
function makeMutliExtensionJSONTransform (filename, preferJS) {
39-
return function transformToMultiExtensions (value, checkExisting, context) {
35+
return function transformToMultiExtensions (value, checkExisting, files) {
4036
function defaultTransform () {
4137
if (preferJS) {
42-
return makeJSTransform(`${filename}.js`)(value, false, context)
38+
return makeJSTransform(`${filename}.js`)(value, false, files)
4339
} else {
44-
return makeJSONTransform(filename)(value, false, context)
40+
return makeJSONTransform(filename)(value, false, files)
4541
}
4642
}
4743

4844
if (!checkExisting) {
4945
return defaultTransform()
5046
}
5147

52-
const absolutePath = path.resolve(context, filename)
53-
if (fs.existsSync(absolutePath)) {
54-
return makeJSONTransform(filename)(value, checkExisting, context)
55-
} else if (fs.existsSync(`${absolutePath}.json`)) {
56-
return makeJSONTransform(`${filename}.json`)(value, checkExisting, context)
57-
} else if (fs.existsSync(`${absolutePath}.js`)) {
58-
return makeJSTransform(`${filename}.js`)(value, checkExisting, context)
59-
} else if (fs.existsSync(`${absolutePath}.yaml`)) {
60-
return transformYAML(value, `${filename}.yaml`, fs.readFileSync(`${absolutePath}.yaml`, 'utf-8'))
61-
} else if (fs.existsSync(`${absolutePath}.yml`)) {
62-
return transformYAML(value, `${filename}.yml`, fs.readFileSync(`${absolutePath}.yml`, 'utf-8'))
48+
if (files[filename]) {
49+
return makeJSONTransform(filename)(value, checkExisting, files)
50+
} else if (files[`${filename}.json`]) {
51+
return makeJSONTransform(`${filename}.json`)(value, checkExisting, files)
52+
} else if (files[`${filename}.js`]) {
53+
return makeJSTransform(`${filename}.js`)(value, checkExisting, files)
54+
} else if (files[`${filename}.yaml`]) {
55+
return transformYAML(value, `${filename}.yaml`, files[`${filename}.yaml`])
56+
} else if (files[`${filename}.yml`]) {
57+
return transformYAML(value, `${filename}.yml`, files[`${filename}.yml`])
6358
} else {
6459
return defaultTransform()
6560
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
const slash = require('slash')
2+
3+
module.exports = function normalizeFilePaths (files) {
4+
Object.keys(files).forEach(file => {
5+
const normalized = slash(file)
6+
if (file !== normalized) {
7+
files[normalized] = files[file]
8+
delete files[file]
9+
}
10+
})
11+
return files
12+
}

0 commit comments

Comments
 (0)