Skip to content

Instantly share code, notes, and snippets.

@danawoodman
Last active December 22, 2016 18:19
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 danawoodman/65d967528a6a410f52606575df6aca33 to your computer and use it in GitHub Desktop.
Save danawoodman/65d967528a6a410f52606575df6aca33 to your computer and use it in GitHub Desktop.
Example of using WebPack to create frontend assets for a non-SPA (Single Page App) project where you selectively load React components within specific templates.
import MyStuff from 'frontend/components/my-stuff'
module.exports = {
renderStuff(node, props) {
// Wrap in <Provider/> here too if using Redux
ReactDOM.render(<MyStuff {...props} />, node)
}
}
script(src='/assets/frontend.js')
script.
var node = document.getElementById('foo')
MyLibrary.renderStuff(node, { extra: 'props', go: 'here' })
const webpack = require('webpack')
const path = require('path')
module.exports = {
entry: [
'babel-polyfill',
'./frontend/index.js'
],
output: {
path: path.join(__dirname, 'public'),
filename: 'frontend.js',
library: 'MyLibrary', // important bit, exposes global `MyLibrary` variable
libraryTarget: 'var',
publicPath: '/assets',
},
module: {
loaders: [
{
test: /\.js$/,
cacheDirectory: true,
exclude: /node_modules/,
loader: 'babel',
},
],
},
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment