Skip to content

Commit 5e93d70

Browse files
authored
fix: normalize the file argument of transformScript, fix Windows compatibility (vuejs#5424)
1 parent 3c286d6 commit 5e93d70

File tree

1 file changed

+27
-3
lines changed

1 file changed

+27
-3
lines changed

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

+27-3
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ const mergeDeps = require('./util/mergeDeps')
88
const runCodemod = require('./util/runCodemod')
99
const stringifyJS = require('./util/stringifyJS')
1010
const ConfigTransform = require('./ConfigTransform')
11-
const { semver, getPluginLink, toShortPluginId, loadModule } = require('@vue/cli-shared-utils')
11+
const { semver, error, getPluginLink, toShortPluginId, loadModule } = require('@vue/cli-shared-utils')
1212

1313
const isString = val => typeof val === 'string'
1414
const isFunction = val => typeof val === 'function'
@@ -81,6 +81,20 @@ class GeneratorAPI {
8181
this.generator.fileMiddlewares.push(middleware)
8282
}
8383

84+
/**
85+
* Normalize absolute path, Windows-style path
86+
* to the relative path used as index in this.files
87+
* @param {string} p the path to normalize
88+
*/
89+
_normalizePath (p) {
90+
if (path.isAbsolute(p)) {
91+
p = path.relative(this.generator.context, p)
92+
}
93+
// The `files` tree always use `/` in its index.
94+
// So we need to normalize the path string in case the user passes a Windows path.
95+
return p.replace(/\\/g, '/')
96+
}
97+
8498
/**
8599
* Resolve path for a project.
86100
*
@@ -373,10 +387,20 @@ class GeneratorAPI {
373387
* @param {object} options additional options for the codemod
374388
*/
375389
transformScript (file, codemod, options) {
390+
const normalizedPath = this._normalizePath(file)
391+
376392
this._injectFileMiddleware(files => {
377-
files[file] = runCodemod(
393+
if (typeof files[normalizedPath] === 'undefined') {
394+
error(`Cannot find file ${normalizedPath}`)
395+
return
396+
}
397+
398+
files[normalizedPath] = runCodemod(
378399
codemod,
379-
{ path: this.resolve(file), source: files[file] },
400+
{
401+
path: this.resolve(normalizedPath),
402+
source: files[normalizedPath]
403+
},
380404
options
381405
)
382406
})

0 commit comments

Comments
 (0)