From 4c1831cf64fdcde7039a0dc1fe2fc0c51f17422c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=87=E7=BA=AF?= Date: Wed, 3 Feb 2021 18:59:24 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8DhardSoureWebpackPlugin?= =?UTF-8?q?=E5=9C=A8webpack=E7=9A=84define=E6=8F=92=E4=BB=B6=E5=8F=98?= =?UTF-8?q?=E5=8C=96=E6=97=B6=E4=BE=9D=E7=84=B6=E4=BD=BF=E7=94=A8cache?= =?UTF-8?q?=E7=9A=84bug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 默认使用node-object-hash对webpackConfig处理计算hash,插件配置解析为[Object, object],所以define插件配置发生变化,产生的hash一样 --- .../src/plugins/features/hardSource.js | 24 +++++++++++++++---- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/packages/fes-preset-built-in/src/plugins/features/hardSource.js b/packages/fes-preset-built-in/src/plugins/features/hardSource.js index 7622f399..59e1d132 100644 --- a/packages/fes-preset-built-in/src/plugins/features/hardSource.js +++ b/packages/fes-preset-built-in/src/plugins/features/hardSource.js @@ -1,8 +1,3 @@ -import { - winPath -} from '@umijs/utils'; -import HardSourceWebpackPlugin from 'hard-source-webpack-plugin'; - export default (api) => { api.describe({ key: 'hardSource', @@ -14,12 +9,31 @@ export default (api) => { }); api.chainWebpack((webpackConfig) => { + const HardSourceWebpackPlugin = require('hard-source-webpack-plugin'); + const { winPath } = require('@umijs/utils'); + const crypto = require('crypto'); + const path = require('path'); + const { toString } = require('webpack-chain'); const cwd = api.cwd; if (api.env === 'development') { webpackConfig .plugin('hardSource') .use(HardSourceWebpackPlugin, [{ cacheDirectory: winPath(`${cwd}/.cache/hard-source/[confighash]`), + configHash: (config) => { + const hardSourcePlugin = config.plugins.find( + ({ constructor }) => constructor.name === 'HardSourceWebpackPlugin' + ); + const cacheDir = hardSourcePlugin.getCachePath(); + const context = path.resolve(process.cwd(), config.context); + const clone = Object.assign({}, config, { + context: path.relative(cacheDir, context) + }); + return crypto + .createHash('sha256') + .update(toString(clone)) + .digest('hex'); + }, ...api.config.hardSource || {} }]); webpackConfig