From b508781ea5e1fa81c0e6e41214184cd8e8bdbc57 Mon Sep 17 00:00:00 2001 From: Haoqun Jiang Date: Wed, 24 Mar 2021 16:09:02 +0800 Subject: [PATCH 1/2] Revert "fix: work around npm6/postcss8 hoisting issue (#6358)" This reverts commit 27b4263ed299e219aa5fbe7b024c226e5775c549. --- packages/@vue/cli-service/bin/postinstall.js | 31 ------------------- .../cli-service/generator/hotfix-npm6only.js | 8 ----- packages/@vue/cli-service/generator/index.js | 7 ----- packages/@vue/cli-service/package.json | 3 -- 4 files changed, 49 deletions(-) delete mode 100644 packages/@vue/cli-service/bin/postinstall.js delete mode 100644 packages/@vue/cli-service/generator/hotfix-npm6only.js diff --git a/packages/@vue/cli-service/bin/postinstall.js b/packages/@vue/cli-service/bin/postinstall.js deleted file mode 100644 index b134954d1c..0000000000 --- a/packages/@vue/cli-service/bin/postinstall.js +++ /dev/null @@ -1,31 +0,0 @@ -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) -} diff --git a/packages/@vue/cli-service/generator/hotfix-npm6only.js b/packages/@vue/cli-service/generator/hotfix-npm6only.js deleted file mode 100644 index 6141487f06..0000000000 --- a/packages/@vue/cli-service/generator/hotfix-npm6only.js +++ /dev/null @@ -1,8 +0,0 @@ -// 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' - } - }) -} diff --git a/packages/@vue/cli-service/generator/index.js b/packages/@vue/cli-service/generator/index.js index 643f4abe0c..d5b1c8ae58 100644 --- a/packages/@vue/cli-service/generator/index.js +++ b/packages/@vue/cli-service/generator/index.js @@ -1,6 +1,3 @@ -const fs = require('fs-extra') -const path = require('path') - module.exports = (api, options) => { api.render('./template', { doesCompile: api.hasPlugin('babel') || api.hasPlugin('typescript') @@ -77,8 +74,4 @@ module.exports = (api, options) => { if (options.configs) { api.extendPackage(options.configs) } - - if (fs.existsSync(path.resolve(__dirname, './hotfix.js'))) { - require('./hotfix')(api, options, options) - } } diff --git a/packages/@vue/cli-service/package.json b/packages/@vue/cli-service/package.json index 47e8a2ecb3..986a0f05c4 100644 --- a/packages/@vue/cli-service/package.json +++ b/packages/@vue/cli-service/package.json @@ -7,9 +7,6 @@ "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", From fc91fb7e07f9109f7b1ccf27b1af07d45a7d79ae Mon Sep 17 00:00:00 2001 From: Haoqun Jiang Date: Wed, 24 Mar 2021 17:53:06 +0800 Subject: [PATCH 2/2] fix: check hoisted postcss version and prompt user on how to fix the bug --- packages/@vue/cli-service/lib/config/css.js | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/packages/@vue/cli-service/lib/config/css.js b/packages/@vue/cli-service/lib/config/css.js index acfa1f7a1c..e8b5061356 100644 --- a/packages/@vue/cli-service/lib/config/css.js +++ b/packages/@vue/cli-service/lib/config/css.js @@ -1,5 +1,6 @@ const fs = require('fs') const path = require('path') +const { chalk, semver, loadModule } = require('@vue/cli-shared-utils') const isAbsoluteUrl = require('../util/isAbsoluteUrl') const findExisting = (context, files) => { @@ -58,6 +59,19 @@ module.exports = (api, rootOptions) => { ])) if (!hasPostCSSConfig) { + // #6342 + // NPM 6 may incorrectly hoist postcss 7 to the same level of autoprefixer + // So we have to run a preflight check to tell the users how to fix it + const autoprefixerDirectory = path.dirname(require.resolve('autoprefixer/package.json')) + const postcssPkg = loadModule('postcss/package.json', autoprefixerDirectory) + const postcssVersion = postcssPkg.version + if (!semver.satisfies(postcssVersion, '8.x')) { + throw new Error( + `The package manager has hoisted a wrong version of ${chalk.cyan('postcss')}, ` + + `please run ${chalk.cyan('npm i postcss@8 -D')} to fix it.` + ) + } + loaderOptions.postcss = { postcssOptions: { plugins: [