Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[v2] fix sass and less variants of css modules in build-html stage #5340

Merged
merged 1 commit into from
May 9, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 10 additions & 28 deletions packages/gatsby-plugin-less/src/gatsby-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ exports.onCreateWebpackConfig = (
) => {
const { setWebpackConfig } = actions
const PRODUCTION = stage !== `develop`
const isSSR = stage.includes(`html`)

const lessLoader = {
loader: resolve(`less-loader`),
Expand All @@ -17,12 +18,14 @@ exports.onCreateWebpackConfig = (

const lessRule = {
test: /\.less$/,
use: [
loaders.miniCssExtract(),
loaders.css({ importLoaders: 1 }),
loaders.postcss({ plugins: postCssPlugins }),
lessLoader,
],
use: isSSR
? [loaders.null()]
: [
loaders.miniCssExtract(),
loaders.css({ importLoaders: 1 }),
loaders.postcss({ plugins: postCssPlugins }),
lessLoader,
],
}
const lessRuleModules = {
test: /\.module\.less$/,
Expand All @@ -33,38 +36,17 @@ exports.onCreateWebpackConfig = (
lessLoader,
],
}
const lessRuleModulesSSR = {
test: /\.module\.less$/,
use: [
loaders.css({ modules: true, importLoaders: 1 }),
loaders.postcss({ plugins: postCssPlugins }),
lessLoader,
],
}

let configRules = []

switch (stage) {
case `develop`:
case `build-javascript`:
configRules = configRules.concat([
{
oneOf: [lessRuleModules, lessRule],
},
])
break

case `build-html`:
case `develop-html`:
configRules = configRules.concat([
{
oneOf: [
lessRuleModulesSSR,
{
...lessRule,
use: [loaders.null()],
},
],
oneOf: [lessRuleModules, lessRule],
},
])
break
Expand Down
38 changes: 10 additions & 28 deletions packages/gatsby-plugin-sass/src/gatsby-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ exports.onCreateWebpackConfig = (
) => {
const { setWebpackConfig } = actions
const PRODUCTION = stage !== `develop`
const isSSR = stage.includes(`html`)

const sassLoader = {
loader: resolve(`sass-loader`),
Expand All @@ -17,12 +18,14 @@ exports.onCreateWebpackConfig = (

const sassRule = {
test: /\.s(a|c)ss$/,
use: [
loaders.miniCssExtract(),
loaders.css({ importLoaders: 1 }),
loaders.postcss({ plugins: postCssPlugins }),
sassLoader,
],
use: isSSR
? [loaders.null()]
: [
loaders.miniCssExtract(),
loaders.css({ importLoaders: 1 }),
loaders.postcss({ plugins: postCssPlugins }),
sassLoader,
],
}
const sassRuleModules = {
test: /\.module\.s(a|c)ss$/,
Expand All @@ -33,38 +36,17 @@ exports.onCreateWebpackConfig = (
sassLoader,
],
}
const sassRuleModulesSSR = {
test: /\.module\.s(a|c)ss$/,
use: [
loaders.css({ modules: true, importLoaders: 1 }),
loaders.postcss({ plugins: postCssPlugins }),
sassLoader,
],
}

let configRules = []

switch (stage) {
case `develop`:
case `build-javascript`:
configRules = configRules.concat([
{
oneOf: [sassRuleModules, sassRule],
},
])
break

case `build-html`:
case `develop-html`:
configRules = configRules.concat([
{
oneOf: [
sassRuleModulesSSR,
{
...sassRule,
use: [loaders.null()],
},
],
oneOf: [sassRuleModules, sassRule],
},
])
break
Expand Down
19 changes: 19 additions & 0 deletions packages/gatsby/src/utils/webpack-extract-css-modules-map.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
const Module = require(`module`)
const loaderUtils = require(`loader-utils`)
const path = require(`path`)

module.exports = function(content) {
const [filename] = loaderUtils
.getRemainingRequest(this)
.split(`!`)
.slice(-1)

const dir = path.dirname(filename)

var m = new Module(filename, this)
m.filename = filename
m.paths = Module._nodeModulePaths(dir)
m._compile(content, filename)

return `module.exports = ${JSON.stringify(m.exports.locals)}`
}
4 changes: 3 additions & 1 deletion packages/gatsby/src/utils/webpack-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,9 @@ module.exports = async ({
options,
// use MiniCssExtractPlugin only on production builds
loader: PRODUCTION
? MiniCssExtractPlugin.loader
? stage === `build-html`
? require.resolve(`./webpack-extract-css-modules-map`)
: MiniCssExtractPlugin.loader
: require.resolve(`style-loader`),
}
},
Expand Down
14 changes: 5 additions & 9 deletions packages/gatsby/src/utils/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -150,20 +150,15 @@ module.exports = async (
__PREFIX_PATHS__: program.prefixPaths,
__PATH_PREFIX__: JSON.stringify(store.getState().config.pathPrefix),
}),

plugins.extractText(
stage === `develop`
? {
filename: `[name].css`,
chunkFilename: `[name].css`,
}
: {}
),
]

switch (stage) {
case `develop`:
configPlugins = configPlugins.concat([
plugins.extractText({
filename: `[name].css`,
chunkFilename: `[name].css`,
}),
plugins.hotModuleReplacement(),
plugins.noEmitOnErrors(),

Expand All @@ -184,6 +179,7 @@ module.exports = async (
break
case `build-javascript`: {
configPlugins = configPlugins.concat([
plugins.extractText(),
// Minify Javascript.
plugins.uglify({
uglifyOptions: {
Expand Down