-
Notifications
You must be signed in to change notification settings - Fork 10.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
WIP Update netlify-cms plugin to work in v2.
This currently works fine when using "gatsby develop" but fails in production when using "gatsy build". In a production site the cms code is affected by the webpackJsonP wrappings and dynamic loading. I added a template to specify the config file location and define a page context to make the script loader happy.
- Loading branch information
Showing
3 changed files
with
60 additions
and
53 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,74 +1,70 @@ | ||
const HtmlWebpackPlugin = require(`html-webpack-plugin`) | ||
const ExtractTextPlugin = require(`extract-text-webpack-plugin`) | ||
const ExtractTextPlugin = require(`mini-css-extract-plugin`) | ||
const path = require(`path`) | ||
|
||
const extractCmsCss = new ExtractTextPlugin(`cms.css`) | ||
const extractCmsCss = new ExtractTextPlugin({ | ||
filename: `cms.css`, | ||
}) | ||
|
||
function plugins(stage) { | ||
function plugins(stage, rules) { | ||
const commonPlugins = [ | ||
// Output /admin/index.html | ||
new HtmlWebpackPlugin({ | ||
template: path.resolve(__dirname, `./template.html`), | ||
title: `Content Manager`, | ||
filename: `admin/index.html`, | ||
chunks: [`cms`], | ||
entryPoint: `cms`, | ||
}), | ||
] | ||
|
||
switch (stage) { | ||
case `develop`: | ||
return commonPlugins | ||
return { | ||
plugins: commonPlugins, | ||
rules: [], | ||
} | ||
case `build-javascript`: | ||
return [...commonPlugins, extractCmsCss] | ||
return { | ||
plugins: [...commonPlugins, extractCmsCss], | ||
rules: [ | ||
{ | ||
test: /\.css$/, | ||
include: [/\/node_modules\/netlify-cms\//], | ||
loader: extractCmsCss.loader, | ||
}, | ||
], | ||
} | ||
|
||
default: | ||
return [] | ||
} | ||
} | ||
|
||
/** | ||
* Exclude Netlify CMS styles from Gatsby CSS bundle. This relies on Gatsby | ||
* using webpack-configurator for webpack config extension, and also on the | ||
* target loader key being named "css" in Gatsby's webpack config. | ||
*/ | ||
function excludeFromLoader(key, config) { | ||
config.loader(key, ({ exclude, ...configRest }) => { | ||
const regex = /\/node_modules\/netlify-cms\// | ||
if (!exclude) { | ||
return { ...configRest, exclude: regex } | ||
} | ||
if (Array.isArray(exclude)) { | ||
return { ...configRest, exclude: [...exclude, regex] } | ||
exports.onCreateWebpackConfig = ({ actions, stage, rules, getConfig }, { modulePath }) => { | ||
const info = plugins(stage, rules) | ||
const config = getConfig() | ||
let update = false | ||
config.module.rules.forEach((rule) => { | ||
if (rule.test && (rule.test.toString() === `.css$`)) { | ||
rule.exclude = rule.exclude || [] | ||
rule.exclude.push(/\/node_modules\/netlify-cms\//) | ||
update = true | ||
} | ||
return { ...configRest, exclude: [exclude, regex] } | ||
}) | ||
} | ||
|
||
function module(config, stage) { | ||
switch (stage) { | ||
case `build-css`: | ||
excludeFromLoader(`css`, config) | ||
return config | ||
case `build-javascript`: | ||
excludeFromLoader(`css`, config) | ||
|
||
// Exclusively extract Netlify CMS styles to /cms.css (filename configured | ||
// above with plugin instantiation). | ||
config.loader(`cms-css`, { | ||
test: /\.css$/, | ||
include: [/\/node_modules\/netlify-cms\//], | ||
loader: extractCmsCss.extract([`css`]), | ||
}) | ||
return config | ||
default: | ||
return config | ||
if (info.rules && info.rules.length) { | ||
info.rules.forEach((rule) => { | ||
config.module.rules.push(rule) | ||
update = true | ||
}) | ||
} | ||
if (info.plugins && info.plugins.length) { | ||
info.plugins.forEach((plugin) => { | ||
config.plugins.push(plugin) | ||
update = true | ||
}) | ||
} | ||
if (update) { | ||
config.entry.cms = [`${__dirname}/cms.js`, modulePath].filter((d) => d) | ||
actions.replaceWebpackConfig(config) | ||
} | ||
} | ||
|
||
exports.onCreateWebpackConfig = ({ actions, stage }, { modulePath }) => { | ||
const config = actions.setWebpackConfig({ | ||
entry: { | ||
cms: [`${__dirname}/cms.js`, modulePath].filter(p => p), | ||
}, | ||
plugins: plugins(stage), | ||
}) | ||
|
||
module(config, stage) | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<meta charset="UTF-8"> | ||
<title><%= htmlWebpackPlugin.options.title %></title> | ||
<link type="text/yaml" rel="cms-config-url" href="/admin/config.yml"/> | ||
<script type="text/javascript">window.page={jsonName:'cms'};</script> | ||
</head> | ||
<body> | ||
</body> | ||
</html> |