1
1
/* eslint-disable import/no-extraneous-dependencies */
2
- import { join } from "path" ;
2
+ import { resolve , join } from "path" ;
3
3
4
4
/* covered by nuxt */
5
- import { move } from "fs-extra" ;
5
+ import { copy } from "fs-extra" ;
6
6
import _ from "lodash" ;
7
- import { Utils } from "nuxt" ;
7
+ import { r , urlJoin } from "@nuxt/common" ;
8
+ import consola from "consola" ;
8
9
import chokidar from "chokidar" ;
10
+ import env from "std-env" ;
9
11
import pify from "pify" ;
10
12
import webpack from "webpack" ;
11
13
import webpackDevMiddleware from "webpack-dev-middleware" ;
12
14
import webpackHotMiddleware from "webpack-hot-middleware" ;
15
+ import WebpackBar from "webpackbar" ;
13
16
import serveStatic from "serve-static" ;
14
- import Debug from "debug" ;
15
17
16
18
import pkg from "../package.json" ;
17
19
18
20
import ConfigManager from "./configManager" ;
19
21
import getWebpackNetlifyConfig from "./webpack.config" ;
20
22
import { toYAML } from "./utils/yaml" ;
21
23
22
- const debug = Debug ( "nuxt:netlify-cms" ) ;
24
+ const logger = consola . withScope ( "nuxt:netlify-cms" ) ;
23
25
24
26
const WEBPACK_CLIENT_COMPILER_NAME = "client" ;
25
27
const WEBPACK_NETLIFY_COMPILER_NAME = "netlify-cms" ;
@@ -29,63 +31,115 @@ export default function NetlifyCmsModule(moduleOptions) {
29
31
const configManager = new ConfigManager ( this . options , moduleOptions ) ;
30
32
const config = configManager . config ;
31
33
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
+
32
42
// This will be called once when builder started
33
43
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
+
34
100
// This will be run just before webpack compiler starts
35
101
this . nuxt . hook ( "build:compile" , ( { name } ) => {
36
102
if ( name !== WEBPACK_CLIENT_COMPILER_NAME ) {
37
103
return ;
38
104
}
39
- const webpackConfig = getWebpackNetlifyConfig (
40
- WEBPACK_NETLIFY_COMPILER_NAME ,
41
- this . options ,
42
- config
43
- ) ;
44
105
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" ) ;
59
107
60
108
// 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 ( ) ;
66
123
}
67
- debug ( `Bundle built!` ) ;
68
- } ) ;
124
+ ) ;
69
125
70
126
// in development
71
127
if ( this . options . dev ) {
72
128
// 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 ;
78
130
79
131
// Create webpack dev middleware
80
132
const netlifyWebpackDevMiddleware = pify (
81
133
webpackDevMiddleware ( netlifyCompiler , {
82
134
publicPath : "/" ,
83
- stats : builder . webpackStats ,
84
- noInfo : true ,
85
- quiet : true ,
135
+ stats : false ,
136
+ logLevel : "silent" ,
86
137
watchOptions : this . options . watchers . webpack
87
138
} )
88
139
) ;
140
+ netlifyWebpackDevMiddleware . close = pify (
141
+ netlifyWebpackDevMiddleware . close
142
+ ) ;
89
143
90
144
// Create webpack hot middleware
91
145
const netlifyWebpackHotMiddleware = pify (
@@ -137,7 +191,10 @@ export default function NetlifyCmsModule(moduleOptions) {
137
191
path : config . adminPath ,
138
192
handler : async ( req , res ) => {
139
193
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
+
141
198
await this . nuxt . renderer . netlifyWebpackDevMiddleware ( req , res ) ;
142
199
}
143
200
if ( this . nuxt . renderer . netlifyWebpackHotMiddleware ) {
@@ -147,7 +204,7 @@ export default function NetlifyCmsModule(moduleOptions) {
147
204
} ) ;
148
205
149
206
// Start watching config file
150
- const patterns = [ Utils . r ( configManager . cmsConfigFileName ) ] ;
207
+ const patterns = [ r ( configManager . cmsConfigFileName ) ] ;
151
208
152
209
const options = {
153
210
...this . options . watchers . chokidar ,
@@ -160,6 +217,8 @@ export default function NetlifyCmsModule(moduleOptions) {
160
217
this . nuxt . renderer . netlifyWebpackHotMiddleware . publish ( {
161
218
action : "reload"
162
219
} ) ;
220
+
221
+ logger . info ( "Netlify-cms files refreshed" ) ;
163
222
} , 200 ) ;
164
223
165
224
// Watch for src Files
@@ -185,13 +244,17 @@ export default function NetlifyCmsModule(moduleOptions) {
185
244
} ) ;
186
245
}
187
246
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 ( / \/ $ / , "" )
193
255
) ;
194
- debug ( "Netlify CMS files copied" ) ;
256
+
257
+ logger . success ( "Netlify-cms files copied" ) ;
195
258
} ) ;
196
259
}
197
260
0 commit comments