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

fix: work around npm6/postcss8 hoisting issue #6358

Merged
merged 3 commits into from
Mar 24, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
31 changes: 31 additions & 0 deletions packages/@vue/cli-service/bin/postinstall.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
const fs = require('fs')
const path = require('path')

const { semver } = require('@vue/cli-shared-utils')

const cwd = process.cwd()

const userAgent = process.env.npm_config_user_agent
const usesNpm6 = userAgent && userAgent.startsWith('npm/6.')
if (!usesNpm6) {
process.exit()
}

const pkgPath = path.resolve(cwd, './package.json')
const pkg = fs.existsSync(pkgPath) ? require(pkgPath) : {}
const deps = {
...pkg.dependencies,
...pkg.devDependencies,
...pkg.optionalDependencies
}

let hasPostCSS8 = false
if (deps.postcss) {
hasPostCSS8 = semver.intersects(deps.postcss, '8.x')
}

if (!hasPostCSS8) {
const hotfixPath = path.resolve(__dirname, '../generator/hotfix-npm6only.js')
const targetPath = path.resolve(__dirname, '../generator/hotfix.js')
fs.renameSync(hotfixPath, targetPath)
}
8 changes: 8 additions & 0 deletions packages/@vue/cli-service/generator/hotfix-npm6only.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// this file will be renamed to hotfix.js if the package is installed by npm 6
module.exports = (api) => {
api.extendPackage({
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can check the package manager version here and avoid the postinstall

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The generator is controlled by @vue/cli itself, so npm is not involved during the execution, therefore the package manager version is not available.

I don't feel like adding an ad hoc API just for this fix, so I chose to keep the postinstall script.

dependencies: {
postcss: '^8.2.6'
}
})
}
7 changes: 7 additions & 0 deletions packages/@vue/cli-service/generator/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
const fs = require('fs-extra')
const path = require('path')

module.exports = (api, options) => {
api.render('./template', {
doesCompile: api.hasPlugin('babel') || api.hasPlugin('typescript')
Expand Down Expand Up @@ -74,4 +77,8 @@ module.exports = (api, options) => {
if (options.configs) {
api.extendPackage(options.configs)
}

if (fs.existsSync(path.resolve(__dirname, './hotfix.js'))) {
require('./hotfix')(api, options, options)
}
}
3 changes: 3 additions & 0 deletions packages/@vue/cli-service/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
"bin": {
"vue-cli-service": "bin/vue-cli-service.js"
},
"scripts": {
"postinstall": "node bin/postinstall.js"
},
"repository": {
"type": "git",
"url": "git+https://github.com/vuejs/vue-cli.git",
Expand Down