Skip to content

Commit

Permalink
fix: register styles in development mode
Browse files Browse the repository at this point in the history
  • Loading branch information
erezrokah committed Nov 26, 2019
1 parent d2f44ff commit 1459b05
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 20 deletions.
21 changes: 17 additions & 4 deletions packages/gatsby-plugin-netlify-cms/src/cms.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,20 @@ if (!CMS_MANUAL_INIT) {
)
}

/**
* The stylesheet output from the modules at `modulePath` will be at `cms.css`.
*/
CMS.registerPreviewStyle(`cms.css`)
// eslint-disable-next-line no-undef
if (PRODUCTION) {
/**
* The stylesheet output from the modules at `modulePath` will be at `cms.css`.
*/
CMS.registerPreviewStyle(`cms.css`)
} else {
/**
* In development styles are injected dynamically via the style-loader plugin
*/
window.addEventListener(`DOMContentLoaded`, event => {
const list = document.querySelectorAll(`link[rel='stylesheet']`)
list.forEach(({ href }) => {
CMS.registerPreviewStyle(href)
})
})
}
21 changes: 5 additions & 16 deletions packages/gatsby-plugin-netlify-cms/src/gatsby-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,19 +49,6 @@ function replaceRule(value) {
return null
}

// Manually swap `style-loader` for `MiniCssExtractPlugin.loader`.
// `style-loader` is only used in development, and doesn't allow us to pass
// the `styles` entry css path to Netlify CMS.
if (
typeof value.loader === `string` &&
value.loader.includes(`style-loader`)
) {
return {
...value,
loader: MiniCssExtractPlugin.loader,
}
}

return value
}

Expand Down Expand Up @@ -187,9 +174,10 @@ exports.onCreateWebpackConfig = (

// Use a simple filename with no hash so we can access from source by
// path.
new MiniCssExtractPlugin({
filename: `[name].css`,
}),
stage !== `develop` &&
new MiniCssExtractPlugin({
filename: `[name].css`,
}),

// Auto generate CMS index.html page.
new HtmlWebpackPlugin({
Expand Down Expand Up @@ -238,6 +226,7 @@ exports.onCreateWebpackConfig = (

new webpack.DefinePlugin({
CMS_MANUAL_INIT: JSON.stringify(manualInit),
PRODUCTION: JSON.stringify(stage !== `develop`),
}),
].filter(p => p),

Expand Down

0 comments on commit 1459b05

Please sign in to comment.