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" ;
66import _ from "lodash" ;
7- import { Utils } from "nuxt" ;
7+ import { r , urlJoin } from "@nuxt/common" ;
8+ import consola from "consola" ;
89import chokidar from "chokidar" ;
10+ import env from "std-env" ;
911import pify from "pify" ;
1012import webpack from "webpack" ;
1113import webpackDevMiddleware from "webpack-dev-middleware" ;
1214import webpackHotMiddleware from "webpack-hot-middleware" ;
15+ import WebpackBar from "webpackbar" ;
1316import serveStatic from "serve-static" ;
14- import Debug from "debug" ;
1517
1618import pkg from "../package.json" ;
1719
1820import ConfigManager from "./configManager" ;
1921import getWebpackNetlifyConfig from "./webpack.config" ;
2022import { toYAML } from "./utils/yaml" ;
2123
22- const debug = Debug ( "nuxt:netlify-cms" ) ;
24+ const logger = consola . withScope ( "nuxt:netlify-cms" ) ;
2325
2426const WEBPACK_CLIENT_COMPILER_NAME = "client" ;
2527const 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