Skip to content

Commit 41c450e

Browse files
authored
feat(module): compatibility with nuxt v2 (#111)
BREAKING CHANGE: This module is no more compatible with nuxt versions older than v2.0.0 Update `nuxt` devDependency to v.2.0.0 Compatibility with webpack v4 Add `FriendlyErrorsWebpackPlugin` Add `@nuxt/friendly-errors-webpack-plugin`, `extract-css-chunks-webpack-plugin` Use `consola` instead of debug for logging, improve messages Add `webpackbar` in production, improve logging Upgrade husky, move config to its own key Remove `.yarnrc` engine compatibility fix Replace `nuxtent` by `@dinamomx/nuxtent` dependency, temporary fix, see nuxt-community/nuxtent-module#190 Fix `nuxt-netlify-cms` dependency to `file` protocol
1 parent 69f6102 commit 41c450e

11 files changed

+7837
-4538
lines changed

Diff for: .yarnrc

-1
This file was deleted.

Diff for: example/nuxt.config.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
module.exports = {
2-
modules: ["nuxtent", "nuxt-netlify-cms"],
2+
modules: ["@dinamomx/nuxtent", "nuxt-netlify-cms"],
33
nuxtent: {
44
content: {
55
page: "/_post",

Diff for: example/package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
"dependencies": {
66
"@nuxtjs/axios": "^5.3.1",
77
"nuxt": "latest",
8-
"nuxtent": "latest",
9-
"nuxt-netlify-cms": "../"
8+
"@dinamomx/nuxtent": "latest",
9+
"nuxt-netlify-cms": "file:../"
1010
},
1111
"scripts": {
1212
"dev": "nuxt",

Diff for: example/yarn.lock

+4,636-2,987
Large diffs are not rendered by default.

Diff for: package.json

+9-4
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,6 @@
77
"author": "Mehdi Lahlou <[email protected]>",
88
"repository": "https://github.com/medfreeman/nuxt-netlify-cms-module.git",
99
"scripts": {
10-
"#<git hooks>": "handled by husky",
11-
"precommit": "lint-staged",
12-
"#</git hooks>": "handled by husky",
1310
"lint": "eslint --ignore-path .gitignore --fix \"**/*.js\"",
1411
"test": "cross-env NODE_ENV=test DEBUG=nuxt:netlify-cms jest --runInBand --forceExit",
1512
"pretest": "yarn lint",
@@ -23,6 +20,8 @@
2320
"dist"
2421
],
2522
"dependencies": {
23+
"@nuxt/friendly-errors-webpack-plugin": "^2.4.0",
24+
"extract-css-chunks-webpack-plugin": "^3.3.2",
2625
"js-yaml": "^3.10.0",
2726
"needlepoint": "^1.0.5",
2827
"style-loader": "^0.23.0"
@@ -31,6 +30,7 @@
3130
"netlify-cms": ">=0.4.0"
3231
},
3332
"devDependencies": {
33+
"@nuxt/common": "^2.3.4",
3434
"babel-cli": "^6.26.0",
3535
"babel-eslint": "^10.0.1",
3636
"babel-plugin-transform-decorators-legacy": "^1.3.4",
@@ -52,11 +52,16 @@
5252
"koa-static": "^5.0.0",
5353
"lint-staged": "^8.0.4",
5454
"netlify-cms": "2.4.0",
55-
"nuxt": "^1.4.2",
55+
"nuxt": "^2.0.0",
5656
"prettier": "^1.7.0",
5757
"request-promise-native": "^1.0.4",
5858
"standard-version": "^5.0.0"
5959
},
60+
"husky": {
61+
"hooks": {
62+
"precommit": "lint-staged"
63+
}
64+
},
6065
"lint-staged": {
6166
"*.js": [
6267
"git-exec-and-restage prettier --write --"

Diff for: src/module.js

+110-47
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,27 @@
11
/* eslint-disable import/no-extraneous-dependencies */
2-
import { join } from "path";
2+
import { resolve, join } from "path";
33

44
/* covered by nuxt */
5-
import { move } from "fs-extra";
5+
import { copy } from "fs-extra";
66
import _ from "lodash";
7-
import { Utils } from "nuxt";
7+
import { r, urlJoin } from "@nuxt/common";
8+
import consola from "consola";
89
import chokidar from "chokidar";
10+
import env from "std-env";
911
import pify from "pify";
1012
import webpack from "webpack";
1113
import webpackDevMiddleware from "webpack-dev-middleware";
1214
import webpackHotMiddleware from "webpack-hot-middleware";
15+
import WebpackBar from "webpackbar";
1316
import serveStatic from "serve-static";
14-
import Debug from "debug";
1517

1618
import pkg from "../package.json";
1719

1820
import ConfigManager from "./configManager";
1921
import getWebpackNetlifyConfig from "./webpack.config";
2022
import { toYAML } from "./utils/yaml";
2123

22-
const debug = Debug("nuxt:netlify-cms");
24+
const logger = consola.withScope("nuxt:netlify-cms");
2325

2426
const WEBPACK_CLIENT_COMPILER_NAME = "client";
2527
const WEBPACK_NETLIFY_COMPILER_NAME = "netlify-cms";
@@ -29,63 +31,115 @@ export default function NetlifyCmsModule(moduleOptions) {
2931
const configManager = new ConfigManager(this.options, moduleOptions);
3032
const config = configManager.config;
3133

34+
const emitNetlifyConfig = compilation => {
35+
const netlifyConfigYAML = toYAML(configManager.cmsConfig);
36+
compilation.assets[NETLIFY_CONFIG_FILE_NAME] = {
37+
source: () => netlifyConfigYAML,
38+
size: () => netlifyConfigYAML.length
39+
};
40+
};
41+
3242
// This will be called once when builder started
3343
this.nuxt.hook("build:before", builder => {
44+
const bundleBuilder = builder.bundleBuilder;
45+
46+
const webpackConfig = getWebpackNetlifyConfig(
47+
WEBPACK_NETLIFY_COMPILER_NAME,
48+
this.options,
49+
config
50+
);
51+
52+
webpackConfig.plugins.push({
53+
apply(compiler) {
54+
compiler.hooks.emit.tapAsync("NetlifyCMSPlugin", (compilation, cb) => {
55+
compilation.hooks.additionalAssets.tapAsync(
56+
"NetlifyCMSPlugin",
57+
callback => {
58+
emitNetlifyConfig(compilation);
59+
callback();
60+
}
61+
);
62+
63+
emitNetlifyConfig(compilation);
64+
cb();
65+
});
66+
}
67+
});
68+
69+
!this.options.dev &&
70+
webpackConfig.plugins.push(
71+
new WebpackBar({
72+
name: WEBPACK_NETLIFY_COMPILER_NAME,
73+
color: "red",
74+
reporters: ["basic", "fancy", "profile", "stats"],
75+
basic: !this.options.build.quiet && env.minimalCLI,
76+
fancy: !this.options.build.quiet && !env.minimalCLI,
77+
profile: !this.options.build.quiet && this.options.build.profile,
78+
stats:
79+
!this.options.build.quiet &&
80+
!this.options.dev &&
81+
this.options.build.stats,
82+
reporter: {
83+
change: (_, { shortPath }) => {
84+
this.nuxt.callHook("bundler:change", shortPath);
85+
},
86+
done: context => {
87+
if (context.hasErrors) {
88+
this.nuxt.callHook("bundler:error");
89+
}
90+
},
91+
allDone: () => {
92+
this.nuxt.callHook("bundler:done");
93+
}
94+
}
95+
})
96+
)
97+
98+
const netlifyCompiler = webpack(webpackConfig);
99+
34100
// This will be run just before webpack compiler starts
35101
this.nuxt.hook("build:compile", ({ name }) => {
36102
if (name !== WEBPACK_CLIENT_COMPILER_NAME) {
37103
return;
38104
}
39-
const webpackConfig = getWebpackNetlifyConfig(
40-
WEBPACK_NETLIFY_COMPILER_NAME,
41-
this.options,
42-
config
43-
);
44105

45-
webpackConfig.plugins.push({
46-
apply(compiler) {
47-
compiler.plugin("emit", (compilation, cb) => {
48-
const netlifyConfigYAML = toYAML(configManager.cmsConfig);
49-
compilation.assets[NETLIFY_CONFIG_FILE_NAME] = {
50-
source: () => netlifyConfigYAML,
51-
size: () => netlifyConfigYAML.length
52-
};
53-
cb();
54-
});
55-
}
56-
});
57-
58-
const netlifyCompiler = webpack(webpackConfig);
106+
logger.success("Netlify-cms builder initialized");
59107

60108
// This will be run just after webpack compiler ends
61-
netlifyCompiler.plugin("done", async stats => {
62-
// Don't reload failed builds
63-
if (stats.hasErrors()) {
64-
/* istanbul ignore next */
65-
return;
109+
netlifyCompiler.hooks.done.tapAsync(
110+
"NetlifyCMSPlugin",
111+
async (stats, cb) => {
112+
// Don't reload failed builds
113+
if (stats.hasErrors()) {
114+
/* istanbul ignore next */
115+
return;
116+
}
117+
118+
// Show a message inside console when the build is ready
119+
this.options.dev &&
120+
logger.info(`Netlify-cms served on: ${config.adminPath}`);
121+
122+
cb();
66123
}
67-
debug(`Bundle built!`);
68-
});
124+
);
69125

70126
// in development
71127
if (this.options.dev) {
72128
// Use shared filesystem and cache
73-
netlifyCompiler.outputFileSystem = builder.mfs;
74-
// Show a message inside console when the build is ready
75-
this.nuxt.hook("build:compiled", async () => {
76-
debug(`Serving on: ${config.adminPath}`);
77-
});
129+
netlifyCompiler.outputFileSystem = bundleBuilder.mfs;
78130

79131
// Create webpack dev middleware
80132
const netlifyWebpackDevMiddleware = pify(
81133
webpackDevMiddleware(netlifyCompiler, {
82134
publicPath: "/",
83-
stats: builder.webpackStats,
84-
noInfo: true,
85-
quiet: true,
135+
stats: false,
136+
logLevel: "silent",
86137
watchOptions: this.options.watchers.webpack
87138
})
88139
);
140+
netlifyWebpackDevMiddleware.close = pify(
141+
netlifyWebpackDevMiddleware.close
142+
);
89143

90144
// Create webpack hot middleware
91145
const netlifyWebpackHotMiddleware = pify(
@@ -137,7 +191,10 @@ export default function NetlifyCmsModule(moduleOptions) {
137191
path: config.adminPath,
138192
handler: async (req, res) => {
139193
if (this.nuxt.renderer.netlifyWebpackDevMiddleware) {
140-
debug(`requesting url: ${Utils.urlJoin(config.adminPath, req.url)}`);
194+
logger.info(
195+
`Netlify-cms requested url: ${urlJoin(config.adminPath, req.url)}`
196+
);
197+
141198
await this.nuxt.renderer.netlifyWebpackDevMiddleware(req, res);
142199
}
143200
if (this.nuxt.renderer.netlifyWebpackHotMiddleware) {
@@ -147,7 +204,7 @@ export default function NetlifyCmsModule(moduleOptions) {
147204
});
148205

149206
// Start watching config file
150-
const patterns = [Utils.r(configManager.cmsConfigFileName)];
207+
const patterns = [r(configManager.cmsConfigFileName)];
151208

152209
const options = {
153210
...this.options.watchers.chokidar,
@@ -160,6 +217,8 @@ export default function NetlifyCmsModule(moduleOptions) {
160217
this.nuxt.renderer.netlifyWebpackHotMiddleware.publish({
161218
action: "reload"
162219
});
220+
221+
logger.info("Netlify-cms files refreshed");
163222
}, 200);
164223

165224
// Watch for src Files
@@ -185,13 +244,17 @@ export default function NetlifyCmsModule(moduleOptions) {
185244
});
186245
}
187246

188-
// Move cms folder from `dist/_nuxt` folder to `dist/` after nuxt generate
189-
this.nuxt.hook("generate:distCopied", async generator => {
190-
await move(
191-
join(generator.distNuxtPath, config.adminPath).replace(/\/$/, ""),
192-
join(generator.distPath, config.adminPath).replace(/\/$/, "")
247+
// Move cms folder from `.nuxt/dist/admin` folder to `dist/` after nuxt generate
248+
this.nuxt.hook("generate:distCopied", async nuxt => {
249+
await copy(
250+
resolve(nuxt.options.buildDir, "dist", config.adminPath).replace(
251+
/\/$/,
252+
""
253+
),
254+
join(nuxt.distPath, config.adminPath).replace(/\/$/, "")
193255
);
194-
debug("Netlify CMS files copied");
256+
257+
logger.success("Netlify-cms files copied");
195258
});
196259
}
197260

0 commit comments

Comments
 (0)