|
1 | 1 | const HtmlWebpackPlugin = require(`html-webpack-plugin`) |
2 | 2 | const HtmlWebpackIncludeAssetsPlugin = require(`html-webpack-include-assets-plugin`) |
| 3 | +const ExtractTextPlugin = require(`extract-text-webpack-plugin`) |
| 4 | + |
| 5 | +function plugins(stage) { |
| 6 | + const commonPlugins = [ |
| 7 | + // Output /admin/index.html |
| 8 | + new HtmlWebpackPlugin({ |
| 9 | + title: `Content Manager`, |
| 10 | + filename: `admin/index.html`, |
| 11 | + chunks: [`cms`], |
| 12 | + }), |
| 13 | + |
| 14 | + // Include the identity widget script in the html file |
| 15 | + new HtmlWebpackIncludeAssetsPlugin({ |
| 16 | + assets: [`https://identity.netlify.com/v1/netlify-identity-widget.js`], |
| 17 | + append: false, |
| 18 | + publicPath: false, |
| 19 | + }), |
| 20 | + ] |
3 | 21 |
|
4 | | -exports.modifyWebpackConfig = ( |
5 | | - { config, stage }, |
6 | | - { modulePath = `${__dirname}/cms.js` } |
7 | | -) => { |
8 | 22 | switch (stage) { |
9 | 23 | case `develop`: |
| 24 | + return commonPlugins |
| 25 | + case `build-javascript`: |
| 26 | + return [...commonPlugins, new ExtractTextPlugin(`cms.css`)] |
| 27 | + default: |
| 28 | + return [] |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +function module(config, stage) { |
| 33 | + switch (stage) { |
10 | 34 | case `build-javascript`: |
11 | | - config.merge({ |
12 | | - entry: { |
13 | | - cms: modulePath, |
14 | | - }, |
15 | | - plugins: [ |
16 | | - new HtmlWebpackPlugin({ |
17 | | - title: `Content Manager`, |
18 | | - filename: `admin/index.html`, |
19 | | - chunks: [`cms`], |
20 | | - }), |
21 | | - new HtmlWebpackIncludeAssetsPlugin({ |
22 | | - assets: [ |
23 | | - `https://identity.netlify.com/v1/netlify-identity-widget.js`, |
24 | | - ], |
25 | | - append: false, |
26 | | - publicPath: false, |
27 | | - }), |
28 | | - ], |
| 35 | + // Exclude Netlify CMS styles from Gatsby CSS bundle. This relies on |
| 36 | + // Gatsby using webpack-configurator for webpack config extension, and |
| 37 | + // also on the target loader key being named "css" in Gatsby's webpack |
| 38 | + // config. |
| 39 | + config.loader(`css`, { |
| 40 | + exclude: [/\/node_modules\/netlify-cms\//], |
| 41 | + }) |
| 42 | + |
| 43 | + // Exclusively extract Netlify CMS styles to /cms.css (filename configured |
| 44 | + // above with plugin instantiation). |
| 45 | + config.loader(`cms-css`, { |
| 46 | + test: /\.css$/, |
| 47 | + include: [/\/node_modules\/netlify-cms\//], |
| 48 | + loader: ExtractTextPlugin.extract([`css`]), |
29 | 49 | }) |
30 | 50 | return config |
31 | 51 | default: |
32 | 52 | return config |
33 | 53 | } |
34 | 54 | } |
| 55 | + |
| 56 | +exports.modifyWebpackConfig = ( |
| 57 | + { config, stage }, |
| 58 | + { modulePath = `${__dirname}/cms.js` } |
| 59 | +) => { |
| 60 | + config.merge({ |
| 61 | + entry: { |
| 62 | + cms: modulePath, |
| 63 | + }, |
| 64 | + plugins: plugins(stage), |
| 65 | + }) |
| 66 | + |
| 67 | + module(config, stage) |
| 68 | + |
| 69 | + return config |
| 70 | +} |
0 commit comments