Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(GeneratorAPI): forceOverwrite option for extendPackage #6307

Merged
merged 2 commits into from
Feb 23, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion packages/@vue/cli-plugin-webpack-4/generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ module.exports = (api) => {
return toMerge
},
{
warnIncompatibleVersions: false
warnIncompatibleVersions: false,
forceOverwrite: true
}
)
}
35 changes: 35 additions & 0 deletions packages/@vue/cli/__tests__/Generator.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -523,6 +523,41 @@ test('api: extendPackage + { warnIncompatibleVersions: false }', async () => {
})
})

test('api: extendPackage + { forceOverwrite: true }', async () => {
const generator = new Generator('/', {
pkg: {
devDependencies: {
'sass-loader': '^11.0.0'
}
},
plugins: [{
id: 'test',
apply: api => {
api.extendPackage(
{
devDependencies: {
'sass-loader': '^10.0.0'
}
},
{ warnIncompatibleVersions: false, forceOverwrite: true }
)
}
}]
})

await generator.generate()
const pkg = JSON.parse(fs.readFileSync('/package.json', 'utf-8'))

// should not warn about the version conflicts
expect(logs.warn.length).toBe(0)
// should use the newer version
expect(pkg).toEqual({
devDependencies: {
'sass-loader': '^10.0.0'
}
})
})

test('api: render fs directory', async () => {
const generator = new Generator('/', {
plugins: [
Expand Down
5 changes: 4 additions & 1 deletion packages/@vue/cli/lib/GeneratorAPI.js
Original file line number Diff line number Diff line change
Expand Up @@ -219,12 +219,15 @@ class GeneratorAPI {
* that dependency fields are always deep merged regardless of this option.
* @param {boolean} [options.warnIncompatibleVersions=true] Output warning
* if two dependency version ranges don't intersect.
* @param {boolean} [options.forceOverwrite=false] force using the dependency
* version provided in the first argument, instead of trying to get the newer ones
*/
extendPackage (fields, options = {}) {
const extendOptions = {
prune: false,
merge: true,
warnIncompatibleVersions: true
warnIncompatibleVersions: true,
forceOverwrite: false
}

// this condition statement is added for compatibility reason, because
Expand Down
19 changes: 12 additions & 7 deletions packages/@vue/cli/lib/util/mergeDeps.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ module.exports = function mergeDeps (
sources,
{
prune,
warnIncompatibleVersions
warnIncompatibleVersions,
forceOverwrite
}
) {
const result = Object.assign({}, sourceDeps)
Expand Down Expand Up @@ -63,11 +64,15 @@ module.exports = function mergeDeps (
const r = tryGetNewerRange(sourceRangeSemver, injectingRangeSemver)
const didGetNewer = !!r

// if failed to infer newer version, use existing one because it's likely
// built-in
result[depName] = didGetNewer
? injectSemver(injectingRange, r)
: sourceRange
if (forceOverwrite) {
result[depName] = injectingRange
} else if (didGetNewer) {
result[depName] = injectSemver(injectingRange, r)
} else {
// if failed to infer newer version, use existing one because it's likely
// built-in
result[depName] = sourceRange
}

// if changed, update source
if (result[depName] === injectingRange) {
Expand All @@ -85,7 +90,7 @@ module.exports = function mergeDeps (
`conflicting versions for project dependency "${depName}":\n\n` +
`- ${sourceRange} injected by generator "${sourceGeneratorId}"\n` +
`- ${injectingRange} injected by generator "${generatorId}"\n\n` +
`Using ${didGetNewer ? `newer ` : ``}version (${
`Using ${(!forceOverwrite && didGetNewer) ? `newer ` : ``}version (${
result[depName]
}), but this may cause build errors.`
)
Expand Down
3 changes: 2 additions & 1 deletion packages/@vue/cli/types/cli-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,8 @@ const generator: GeneratorPlugin = (api, options, rootOptions, invoking) => {
{
merge: true,
prune: true,
warnIncompatibleVersions: true
warnIncompatibleVersions: true,
forceOverwrite: true
}
)

Expand Down
3 changes: 3 additions & 0 deletions packages/@vue/cli/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ type ExtendPackageOptions =
prune?: boolean
merge?: boolean
warnIncompatibleVersions?: boolean
forceOverwrite?: boolean
}
| boolean

Expand Down Expand Up @@ -121,6 +122,8 @@ declare class GeneratorAPI {
* that dependency fields are always deep merged regardless of this option.
* @param [options.warnIncompatibleVersions=true] Output warning
* if two dependency version ranges don't intersect.
* @param [options.forceOverwrite=false] force using the dependency
* version provided in the first argument, instead of trying to get the newer ones
*/
extendPackage(
fields: (pkg: Record<string, any>) => object,
Expand Down