Package: lektor-webpack-support
lektor-webpack-supportThis is a plugin for Lektor that adds support for webpack to projects. When
enabled it can build a webpack project from thewebpack/folder into the
asset folder automatically when the server (or build process) is run with
the-f webpackflag.Enabling the PluginTo enable the plugin add this to your project file, run this command while
sitting in your Lektor project directory:lektorpluginsaddlektor-webpack-supportCreating a Webpack ProjectNext you need to create a webpack project. Create awebpack/folder and
inside that folder createpackage.jsonand awebpack.config.jswebpack/package.jsonThis file instructsnpmwhich packages we will need. All we need for a
start is to create an almost empty file (name and version fields are mandatory
but not important for functionality, change them to suit your own needs):{"name":"lektor-webpack","version":"1.0.0","private":true}Now we cannpm install(oryarn add) the rest:$ cd </path/to/your/lektor/project>/webpack
$ npm install --save-dev webpack webpack-cli @babel/core sass babel-loader sass-loader css-loader url-loader file-loader mini-css-extract-pluginThis will install webpack itself together with babel and sass as well as
a bunch of loaders we need for getting all that configured. Because we
created apackage.jsonbefore and we use--save-devthe dependencies
will be remembered in thepackage.jsonfile.webpack/webpack.config.jsNext up is the webpack config file. Here we will go with a very basic
setup that's good enough to cover most things you will encounter. The
idea is to build the files fromwebpack/scssandwebpack/jsintoassets/static/genso that we can use it even if we do not have webpack
installed for as long as someone else ran it before.constpath=require("path");constMiniCssExtractPlugin=require("mini-css-extract-plugin");module.exports=={entry:{app:"./js/main.js",styles:"./scss/main.scss",},output:{path:path.join(path.dirname(__dirname),"assets","static","gen"),filename:"[name].js",},devtool:"source-map",mode:"production",module:{rules:[{test:/\.js$/,exclude:/node_modules/,use:["babel-loader"],},{test:/\.scss$/,use:[MiniCssExtractPlugin.loader,"css-loader","sass-loader"],},{test:/\.css$/,use:[MiniCssExtractPlugin.loader,"css-loader"],},{test:/\.(woff2?|ttf|eot|svg|png|jpe?g|gif)$/,use:["file"],},],},plugins:[newMiniCssExtractPlugin({filename:"styles.css"})],};Creating the AppNow we can start building our app. We configured at least two files
in webpack:js/main.jsandscss/main.scss. Those are the entry
points we need to have. You can create them as empty files inwebpack/js/main.jsandwebpack/scss/main.scss.Running the ServerNow you're ready to go. When you runlektor serverwebpack will not
run, instead you need to now run it aslektor server -f webpackwhich
will enable the webpack build. Webpack automatically builds your files
intoassets/static/genand this is where Lektor will then pick up the
files. This is done so that you can ship the webpack generated assets
to others that do not have webpack installed which simplifies using a
Lektor website that uses webpack.Manual BuildsTo manually trigger a build that also invokes webpack you can uselektor build -f webpack.Including The FilesNow you need to include the files in your template. This will do it:<linkrel="stylesheet"href="{{ '/static/gen/styles.css'|asseturl }}"><scripttype=text/javascriptsrc="{{ '/static/gen/app.js'|asseturl }}"charset="utf-8"></script>