Unverified Commit 955ea422 authored by Joe Haddad's avatar Joe Haddad Committed by GitHub

Monkey patch `next-sass` and `next-less` (#8944)

* Monkey patch `next-sass` and `next-less`
These two Next.js plugins use poor webpack practices: they define plugins without resolving themselves.

As a result, the resolution root is unknown by webpack and you get into package hoisting bugs.

To fix this, we emulate the logic these packages should've performed themselves on a best-effort basis.

* Add comments explaining the monkey patch

* Fix comment

* Update packages/next/build/webpack-config.ts
Co-Authored-By: 's avatarTim Neutkens <tim@timneutkens.nl>
parent 3d5af360
......@@ -892,6 +892,88 @@ export default async function getBaseWebpackConfig(
}
}
// Patch `@zeit/next-sass` and `@zeit/next-less` compatibility
if (
!isServer &&
webpackConfig.module &&
Array.isArray(webpackConfig.module.rules)
) {
;[].forEach.call(webpackConfig.module.rules, function(
rule: webpack.RuleSetRule
) {
if (!(rule.test instanceof RegExp && Array.isArray(rule.use))) {
return
}
// Check if the rule we're iterating over applies to Sass or Less
if (
!(
rule.test.source === '\\.scss$' ||
rule.test.source === '\\.sass$' ||
rule.test.source === '\\.less$'
)
) {
return
}
;[].forEach.call(rule.use, function(use: webpack.RuleSetUseItem) {
if (
!(
use &&
typeof use === 'object' &&
// Identify use statements only pertaining to `css-loader`
use.loader === 'css-loader' &&
use.options &&
typeof use.options === 'object' &&
// The `minimize` property is a good heuristic that we need to
// perform this hack. The `minimize` property was only valid on
// old `css-loader` versions. Custom setups (that aren't next-sass
// or next-less) likely have the newer version.
// We still handle this gracefully below.
Object.prototype.hasOwnProperty.call(use.options, 'minimize')
)
) {
return
}
// Try to monkey patch within a try-catch. We shouldn't fail the build
// if we cannot pull this off.
// The user may not even be using the `next-sass` or `next-less`
// plugins.
// If it does work, great!
try {
// Resolve the version of `@zeit/next-css` as depended on by the Sass
// or Less plugin.
const correctNextCss = resolveRequest(
'@zeit/next-css',
require.resolve(
(rule.test as RegExp).source === '\\.less$'
? '@zeit/next-less'
: '@zeit/next-sass'
)
)
// If we found `@zeit/next-css` ...
if (correctNextCss) {
// ... resolve the version of `css-loader` shipped with that
// package instead of whichever was hoisted highest in your
// `node_modules` tree.
const correctCssLoader = resolveRequest(
'css-loader',
correctNextCss
)
if (correctCssLoader) {
// We saved the user from a failed build!
use.loader = correctCssLoader
}
}
} catch (_) {
// The error is not required to be handled.
}
})
})
}
// Backwards compat for `main.js` entry key
const originalEntry: any = webpackConfig.entry
if (typeof originalEntry !== 'undefined') {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment