Skip to content

Commit

Permalink
fix: use a postinstall script to fix npm6/postcss8 hoisting issue
Browse files Browse the repository at this point in the history
Closes vuejs#6342
  • Loading branch information
haoqunjiang committed Mar 17, 2021
1 parent df62363 commit 3390776
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
34 changes: 34 additions & 0 deletions packages/@vue/cli-service/bin/postinstall.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
const fs = require('fs')
const path = require('path')

const stripAnsi = require('strip-ansi')
const { execa, semver } = require('@vue/cli-shared-utils')

const cwd = process.cwd()

const usesNpm = fs.existsSync(path.resolve(cwd, './package-lock.json'))
if (!usesNpm) {
process.exit()
}

const npmVersion = stripAnsi(execa.sync('npm', ['--version']).stdout)
if (!semver.satisfies(npmVersion, '6.x')) {
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) {
execa.sync('npm', ['install', '--save-dev', '--loglevel', 'error', 'postcss@8'])
}
4 changes: 4 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 Expand Up @@ -66,6 +69,7 @@
"postcss": "^8.2.6",
"postcss-loader": "^5.0.0",
"ssri": "^8.0.1",
"strip-ansi": "^6.0.0",
"terser-webpack-plugin": "^5.1.1",
"thread-loader": "^3.0.0",
"url-loader": "^4.1.1",
Expand Down

0 comments on commit 3390776

Please sign in to comment.