Last active
March 8, 2020 09:52
-
-
Save levipadre/e3a87682ed2cf7db98f089cba24d75f3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Generate HTML files from Nunjucks | |
*/ | |
const mix = require('laravel-mix'), | |
path = require('path'), | |
fs = require('fs'), | |
paths = require('./paths'), | |
HTMLWebpackPlugin = require('html-webpack-plugin'); | |
var templateParameters = require('./data.json'); | |
let env = 'development'; | |
if (mix.inProduction()) { | |
env = 'production' | |
} | |
const directories = [] | |
function walkDir(dir, parent = '') { | |
var files = fs.readdirSync(dir); | |
for(var x in files){ | |
var next = path.join(dir,files[x]); | |
if(fs.lstatSync(next).isDirectory()==true){ | |
walkDir(next, parent+'/'+files[x]); | |
} else { | |
directories.push([path.parse(files[x]).name, (parent+'/').slice(1)]); | |
} | |
} | |
return directories | |
} | |
const pages = { | |
generatePages: function generatePages(pagesPath) { | |
return walkDir(pagesPath).map( | |
name => | |
new HTMLWebpackPlugin({ | |
template: `${pagesPath}/${name[1]}${name[0]}.njk`, | |
filename: paths.pages.base + `/${name[1]}${name[0]}.html`, | |
templateParameters: templateParameters | |
}) | |
); | |
} | |
}; | |
module.exports = pages; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"base": { | |
"dir": "./pages", | |
"node": "node_modules", | |
"start": "index.html" | |
}, | |
"src": { | |
"base": "./src", | |
"scss": "./src/scss", | |
"templates": "./src/templates", | |
"js": "./src/js", | |
"fonts": "./src/fonts", | |
"images": "./src/images" | |
}, | |
"pages": { | |
"base": "./pages", | |
"assets": "./pages/assets", | |
"layouts": "./pages/layouts", | |
"block": "./pages/blocks", | |
"js": "./pages/assets/js", | |
"css": "./pages/assets/css", | |
"fonts": "./pages/assets/fonts", | |
"images": "./pages/assets/images", | |
"vendor": "./pages/assets/vendor" | |
}, | |
"dist": { | |
"base": "./dist", | |
"css": "./dist/assets/css", | |
"js": "./dist/assets/js", | |
"pages": "./dist/pages", | |
"layouts": "./dist/pages/layouts", | |
"blocks": "./dist/pages/blocks", | |
"vendor": "./dist/assets/vendor", | |
"fonts": "./dist/assets/fonts", | |
"images": "./dist/assets/images" | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const paths = require('./paths'), | |
mix = require('laravel-mix'), | |
RemoveWebpackPlugin = require('remove-webpack-plugin'), | |
nunjucksConfig = require('./nunjucks.config'), | |
path = require('path'), | |
WebpackOnBuildPlugin = require('on-build-webpack'); | |
mix.webpackConfig({ | |
module: { | |
rules: [ | |
{ | |
test: /\.njk$/, | |
use: [ | |
{ | |
loader: 'simple-nunjucks-loader' | |
} | |
] | |
} | |
] | |
}, | |
plugins: [ | |
...nunjucksConfig.generatePages(path.resolve(__dirname, paths.src.templates)), | |
] | |
}) |
I think it's relate to how webpack config works. Webpack trying to use obsolete config.
I'm afraid webpack doesn't reload config, when entry point files changes. There definitely should be some solution.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Do you think is that also a html-webpack-plugin issue?
When I create a new .njk file under templates, nothing happens, I need to restart the server to make generate the .html file.
If I have the file already and I rename it, it crashes instead of renaming the html file and I got this error:
And finally when I delete a file, also crashes.