I have exactly the same setup and ended up with a working solution.
Directories setup
_config.yml
in the root folder:
destination: dist
source: src
keep_files:
- webpack
A webpack
folder for everything "webpackable" (JS, CSS, LESS, .vue
, ...), the webpack.config.js
I use for initial setups:
const path = require('path');
const webpack = require('webpack')
const WebpackShellPlugin = require('webpack-shell-plugin')
module.exports = {
watch: true,
entry: path.join(__dirname, 'entry.js'),
output: {
path: path.join(__dirname, '..', 'dist', 'webpack'),
filename: 'bundle.js',
publicPath: '/webpack/'
},
module: {
rules: [{
test: /\.js$/,
loader: 'babel-loader',
exclude: /node_modules/
},
{
test: /\.css$/,
use: ['style-loader', 'css-loader']
},
{
test: /\.less$/,
use: ['style-loader', 'css-loader', 'less-loader']
},
{
test: /\.woff(2)?(\?v=[0-9]\.[0-9]\.[0-9])?$/,
loader: "url-loader?limit=10000&mimetype=application/font-woff"
},
{
test: /\.(ttf|eot|svg)(\?v=[0-9]\.[0-9]\.[0-9])?$/,
loader: "file-loader"
},
]
},
plugins: [
// https://stackoverflow.com/a/39283602/903011
new webpack.ProvidePlugin({
$: "jquery",
jQuery: "jquery"
}),
new WebpackShellPlugin({onBuildStart:['cd .. && jekyll b --watch']})
]
}
// start live server
var liveServer = require("live-server");
var params = {
port: 8080, // Set the server port. Defaults to 8080.
root: "../dist", // Set root directory that's being served. Defaults to cwd.
open: true, // When false, it won't load your browser by default.
logLevel: 1, // 0 = errors only, 1 = some, 2 = lots
};
liveServer.start(params);
A src
folder (at root level, same level as webpack
) where I keep the whole Jekyll stuff, and .html
files. My footer.html
file included with all files imports webpack/bundle.js
.
A dist
folder with the compiled files, it can be used directly (on a web server, a CDN, ...)
How it works
- I run
webpack
in the root folder (it watches the folder)
- it builds
bundle.js
and places it in /dist/webpack
. A subfolder is used so that it is not overwritten by Jekyll upon compilation (see Jekyll config above). It could also be a file, but I have other webpack-emitted files there
- it starts a Jekyll build and watch process - on
src
, outputting to dist
- it starts a livereload server which serves
dist
When I modify a file in the webpack
folder, webpack
rebuilds bundle.js
. The livereload server sees a change and updates the browser.
When I modify a file in src
, Jekyll sees the change and rebuilds the .html
files. The livereload server sees a change and updates the browser.
Hope this helps.