From 96161f51994411e98eb280e4404497510053a4fa Mon Sep 17 00:00:00 2001 From: Cyril Wanner Date: Wed, 1 Apr 2020 12:41:58 +0200 Subject: [PATCH] Only apply nextjs css fix for affected versions --- lib/loaders/img-loader.js | 21 ++++++++++++++++++++- package-lock.json | 7 +++---- package.json | 1 + 3 files changed, 24 insertions(+), 5 deletions(-) diff --git a/lib/loaders/img-loader.js b/lib/loaders/img-loader.js index 7600170..1572c5b 100644 --- a/lib/loaders/img-loader.js +++ b/lib/loaders/img-loader.js @@ -1,3 +1,4 @@ +const compareVersions = require('compare-versions'); const { getResourceQueries } = require('../resource-queries'); const { getWebpResourceQuery } = require('./webp-loader'); const { getUrlLoaderOptions } = require('./url-loader'); @@ -74,6 +75,24 @@ const getHandledFilesRegex = (handledImageTypes) => { return new RegExp(`\\.(${handledFiles.filter(Boolean).join('|')})$`, 'i'); }; +/** + * Checks if the plugin should get applied in CSS or not. + * Since next.js version 9.2, they already handle images out of the box so it should temporarily + * get disabled in that case to prevent breaking projects until a better solution is found. + * + * @return {boolean} + */ +const applyInCss = () => { + try { + const nextJsVersion = require('next/package.json').version; // eslint-disable-line import/no-unresolved + + return compareVersions(nextJsVersion, '9.2.0') === -1; + } catch (e) { + // in case of any error, just assume the newest nextjs version is installed + return false; + } +}; + /** * Apply the img loader to the webpack configuration * @@ -97,7 +116,7 @@ const applyImgLoader = ( webpackConfig.module.rules.push({ test: getHandledFilesRegex(handledImageTypes), - issuer: { + issuer: applyInCss() ? undefined : { // Next.js already handles url() in css/sass/scss files test: /\.\w+(?