Skip to content

Commit 27b4263

Browse files
authored
fix: work around npm6/postcss8 hoisting issue (#6358)
Closes #6342
1 parent 2035543 commit 27b4263

File tree

4 files changed

+49
-0
lines changed

4 files changed

+49
-0
lines changed

Diff for: packages/@vue/cli-service/bin/postinstall.js

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
const fs = require('fs')
2+
const path = require('path')
3+
4+
const { semver } = require('@vue/cli-shared-utils')
5+
6+
const cwd = process.cwd()
7+
8+
const userAgent = process.env.npm_config_user_agent
9+
const usesNpm6 = userAgent && userAgent.startsWith('npm/6.')
10+
if (!usesNpm6) {
11+
process.exit()
12+
}
13+
14+
const pkgPath = path.resolve(cwd, './package.json')
15+
const pkg = fs.existsSync(pkgPath) ? require(pkgPath) : {}
16+
const deps = {
17+
...pkg.dependencies,
18+
...pkg.devDependencies,
19+
...pkg.optionalDependencies
20+
}
21+
22+
let hasPostCSS8 = false
23+
if (deps.postcss) {
24+
hasPostCSS8 = semver.intersects(deps.postcss, '8.x')
25+
}
26+
27+
if (!hasPostCSS8) {
28+
const hotfixPath = path.resolve(__dirname, '../generator/hotfix-npm6only.js')
29+
const targetPath = path.resolve(__dirname, '../generator/hotfix.js')
30+
fs.renameSync(hotfixPath, targetPath)
31+
}
+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// this file will be renamed to hotfix.js if the package is installed by npm 6
2+
module.exports = (api) => {
3+
api.extendPackage({
4+
dependencies: {
5+
postcss: '^8.2.6'
6+
}
7+
})
8+
}

Diff for: packages/@vue/cli-service/generator/index.js

+7
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
const fs = require('fs-extra')
2+
const path = require('path')
3+
14
module.exports = (api, options) => {
25
api.render('./template', {
36
doesCompile: api.hasPlugin('babel') || api.hasPlugin('typescript')
@@ -74,4 +77,8 @@ module.exports = (api, options) => {
7477
if (options.configs) {
7578
api.extendPackage(options.configs)
7679
}
80+
81+
if (fs.existsSync(path.resolve(__dirname, './hotfix.js'))) {
82+
require('./hotfix')(api, options, options)
83+
}
7784
}

Diff for: packages/@vue/cli-service/package.json

+3
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
"bin": {
88
"vue-cli-service": "bin/vue-cli-service.js"
99
},
10+
"scripts": {
11+
"postinstall": "node bin/postinstall.js"
12+
},
1013
"repository": {
1114
"type": "git",
1215
"url": "git+https://github.com/vuejs/vue-cli.git",

0 commit comments

Comments
 (0)