Created
February 20, 2020 00:37
-
-
Save shotasenga/e120a7f6419301b2b047eaddd02a73b1 to your computer and use it in GitHub Desktop.
next.config.js which enables built-in css and sass support with "includePaths" option
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const path = require("path"); | |
const withImages = require("next-images"); | |
let config = { | |
distDir: "../../dist/client", | |
experimental: { | |
css: true, | |
scss: true | |
}, | |
webpack: (config, { buildId, dev, isServer, defaultLoaders, webpack }) => { | |
// add includePaths to Sass loaders | |
const sassLoaders = findLoaders( | |
config.module.rules, | |
x => x.loader && x.loader.indexOf("/sass-loader/") !== -1 | |
); | |
const sassIncludePaths = [path.join(__dirname, "./styles")]; | |
for (let i = 0; i < sassLoaders.length; i++) { | |
sassLoaders[i].options.sassOptions = { | |
includePaths: sassIncludePaths | |
}; | |
} | |
return config; | |
} | |
}; | |
config = withImages(config); | |
module.exports = config; | |
function findLoaders(rules, fn, matches = []) { | |
for (let i = 0; i < rules.length; i++) { | |
const loaders = rules[i].use || []; | |
for (let j = 0; j < loaders.length; j++) { | |
if (fn(loaders[j])) { | |
matches.push(loaders[j]); | |
} | |
} | |
if (!Array.isArray(rules[i].oneOf)) { | |
continue; | |
} | |
findLoaders(rules[i].oneOf, fn, matches); | |
} | |
return matches; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment