Skip to content

Commit

Permalink
exclude actually need to be instance of RegExp
Browse files Browse the repository at this point in the history
  • Loading branch information
pieh committed Feb 25, 2021
1 parent b49c458 commit ec4b6d2
Showing 1 changed file with 20 additions and 17 deletions.
37 changes: 20 additions & 17 deletions packages/gatsby/src/utils/webpack-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -769,26 +769,29 @@ export const createWebpackUtils = (
}
): CssMinimizerPlugin => new CssMinimizerPlugin(options)

plugins.fastRefresh = ({ modulesThatUseGatsby }): Plugin =>
new ReactRefreshWebpackPlugin({
plugins.fastRefresh = ({ modulesThatUseGatsby }): Plugin => {
const regExpToHack = /node_modules/
regExpToHack.test = (modulePath: string): boolean => {
// when it's not coming from node_modules we treat it as a source file.
if (!vendorRegex.test(modulePath)) {
return false
}

// If the module uses Gatsby as a dependency
// we want to treat it as src because of shadowing
return !modulesThatUseGatsby.some(module =>
modulePath.includes(module.path)
)
}

return new ReactRefreshWebpackPlugin({
overlay: false,
// this is a bit hacky - exclude expect string or regexp or array of those
// so this is tricking ReactRefreshWebpackPlugin that we provide RegExp
exclude: {
test: modulePath => {
// when it's not coming from node_modules we treat it as a source file.
if (!vendorRegex.test(modulePath)) {
return false
}

// If the module uses Gatsby as a dependency
// we want to treat it as src because of shadowing
return !modulesThatUseGatsby.some(module =>
modulePath.includes(module.path)
)
},
} as RegExp,
// so this is tricking ReactRefreshWebpackPlugin with providing regexp with
// overwritten .test method
exclude: regExpToHack,
})
}

plugins.extractText = (options: any): Plugin =>
new MiniCssExtractPlugin({
Expand Down

0 comments on commit ec4b6d2

Please sign in to comment.