Skip to content

Commit

Permalink
fix: work around npm6/postcss8 hoisting issue (#6358)
Browse files Browse the repository at this point in the history
Closes #6342
  • Loading branch information
haoqunjiang committed Mar 24, 2021
1 parent 2035543 commit 27b4263
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 0 deletions.
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({
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

0 comments on commit 27b4263

Please sign in to comment.