Skip to content

Instantly share code, notes, and snippets.

@IngwiePhoenix
Created December 28, 2015 12:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save IngwiePhoenix/be95c34431eac727676c to your computer and use it in GitHub Desktop.
Save IngwiePhoenix/be95c34431eac727676c to your computer and use it in GitHub Desktop.
Webpack EJS widgets
  • Clone the gist.
  • Run: npm install ejs-compiled-loader
  • Run: webpack --config 02-webpack.config.js
  • Watch the magic happen.

This should split into two output JS files. One has the code fromt he entry, the other has the widget. Its loaded async.

module.exports = {
entry: "./ß3-basic.js",
output: {
path: "./out",
filename: "build.js",
chunkfilename: "chunk.[id].js"
},
module: {
loaders: [
{ test: /\.ejs$/, loader: "ejs-compiled" }
]
}
}
// ES6. Commented out since we dont use ES6 in this example
// import Widget from "./04-widget.ejs";
// CommonJS. Commented out to demonstrate code splitting.
// var Widget = require("./04-widget.ejs");
// Code splitting
require.ensure([
"./04-widget.ejs"
], function(require){
var Widget = require("./04-widget.ejs");
document.body.write(Widget({
heading: "Hello",
text: "world"
}))
})
<h1><%= heading %></h1>
<p><%= text %></p>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment