Skip to content

Commit 416737e

Browse files
committed
feat(module): compatibility with nuxt v1
BREAKING CHANGE: This module is no more compatible with nuxt versions older than v1.0.0 Update `nuxt` devDependency to v.1.4.0
1 parent d12e76c commit 416737e

File tree

5 files changed

+788
-342
lines changed

5 files changed

+788
-342
lines changed

Diff for: package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,9 @@
4949
"jest": "^22.0.4",
5050
"koa": "^2.3.0",
5151
"koa-static": "^4.0.1",
52-
"netlify-cms": "1.6.0",
5352
"lint-staged": "^7.0.1",
54-
"nuxt": "^1.0.0-rc11",
53+
"netlify-cms": "1.6.0",
54+
"nuxt": "^1.4.0",
5555
"prettier": "^1.7.0",
5656
"request-promise-native": "^1.0.4",
5757
"standard-version": "^4.2.0"

Diff for: src/module.js

+33-19
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,12 @@ export default function NetlifyCmsModule(moduleOptions) {
3030
const config = configManager.config;
3131

3232
// This will be called once when builder started
33-
this.nuxt.plugin("build", builder => {
33+
this.nuxt.hook("build:before", builder => {
3434
// This will be run just before webpack compiler starts
35-
builder.plugin("compile", ({ builder, compiler }) => {
35+
this.nuxt.hook("build:compile", ({ name }) => {
36+
if (name !== WEBPACK_CLIENT_COMPILER_NAME) {
37+
return;
38+
}
3639
const webpackConfig = getWebpackNetlifyConfig(
3740
WEBPACK_NETLIFY_COMPILER_NAME,
3841
this.options,
@@ -54,18 +57,6 @@ export default function NetlifyCmsModule(moduleOptions) {
5457

5558
const netlifyCompiler = webpack(webpackConfig);
5659

57-
// Only add the compiler in production,
58-
// in dev watch will be started by dev-middleware
59-
if (!this.options.dev) {
60-
compiler.compilers.push(netlifyCompiler);
61-
compiler[netlifyCompiler.name] = netlifyCompiler;
62-
}
63-
64-
// Use shared filesystem and cache
65-
const clientCompiler = compiler[WEBPACK_CLIENT_COMPILER_NAME];
66-
netlifyCompiler.outputFileSystem = clientCompiler.outputFileSystem;
67-
netlifyCompiler.cache = clientCompiler.cache;
68-
6960
// This will be run just after webpack compiler ends
7061
netlifyCompiler.plugin("done", async stats => {
7162
// Don't reload failed builds
@@ -77,8 +68,10 @@ export default function NetlifyCmsModule(moduleOptions) {
7768

7869
// in development
7970
if (this.options.dev) {
71+
// Use shared filesystem and cache
72+
netlifyCompiler.outputFileSystem = builder.mfs;
8073
// Show a message inside console when the build is ready
81-
builder.plugin("compiled", async () => {
74+
this.nuxt.hook("build:compiled", async () => {
8275
debug(`Serving on: ${config.adminPath}`);
8376
});
8477

@@ -108,9 +101,30 @@ export default function NetlifyCmsModule(moduleOptions) {
108101
}
109102

110103
// Stop webpack middleware on nuxt.close()
111-
this.nuxt.plugin("close", async () => {
104+
this.nuxt.hook("close", async () => {
112105
await this.nuxt.renderer.netlifyWebpackDevMiddleware.close();
113106
});
107+
} else {
108+
// Only run the compiler in production,
109+
// in dev build is started by dev-middleware hooked to client webpack compiler
110+
this.nuxt.hook("build:done", async () => {
111+
await new Promise((resolve, reject) => {
112+
netlifyCompiler.run((err, stats) => {
113+
/* istanbul ignore next */
114+
if (err) {
115+
return reject(err);
116+
} else if (stats.hasErrors()) {
117+
if (this.options.test) {
118+
err = stats.toString(this.options.build.stats);
119+
}
120+
121+
return reject(err);
122+
}
123+
124+
resolve();
125+
});
126+
});
127+
});
114128
}
115129
});
116130
});
@@ -157,7 +171,7 @@ export default function NetlifyCmsModule(moduleOptions) {
157171
this.nuxt.renderer.netlifyFileWatcher = fileWatcher;
158172

159173
// Stop watching on nuxt.close()
160-
this.nuxt.plugin("close", () => {
174+
this.nuxt.hook("close", () => {
161175
this.nuxt.renderer.netlifyFileWatcher.close();
162176
});
163177
} else {
@@ -171,8 +185,8 @@ export default function NetlifyCmsModule(moduleOptions) {
171185
}
172186

173187
// Move cms folder from `dist/_nuxt` folder to `dist/` after nuxt generate
174-
this.nuxt.plugin("generator", generator => {
175-
generator.plugin("generate", async () => {
188+
this.nuxt.hook("generate", generator => {
189+
generator.hook("generate", async () => {
176190
await move(
177191
join(generator.distNuxtPath, config.adminPath).replace(/\/$/, ""),
178192
join(generator.distPath, config.adminPath).replace(/\/$/, "")

Diff for: test/__snapshots__/module.test.js.snap

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ exports[`module admin 1`] = `
99
<title>Content Manager</title>
1010
</head>
1111
<body>
12-
<script type=\\"text/javascript\\" src=\\"/admin/bundle.0d29fdd2672e897fea4b.js\\"></script></body>
12+
<script type=\\"text/javascript\\" src=\\"/admin/bundle.76047b676266a7874982.js\\"></script></body>
1313
</html>
1414
"
1515
`;

Diff for: test/fixture/nuxt.config.js

+7-1
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,11 @@ export default {
1010
render: {
1111
resourceHints: false
1212
},
13-
modules: [module]
13+
modules: [
14+
{
15+
src: "@@/src/module.js",
16+
options: {},
17+
handler: module
18+
}
19+
]
1420
};

0 commit comments

Comments
 (0)