Last active
December 22, 2016 18:19
-
-
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.
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
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) | |
} | |
} |
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
script(src='/assets/frontend.js') | |
script. | |
var node = document.getElementById('foo') | |
MyLibrary.renderStuff(node, { extra: 'props', go: 'here' }) |
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 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