For a long time, I had the notion that webpack could only be ran for set ups that are independent from the back end. For example SPAs that only communicate with AJAX.
webpack -w
and that's it. It will watch for changes, and then do what it does, and throw it to the output directory you specified. After that, you just point to the output file in your template like <script src="./build/index.js" />
. This project does just that, as simple as possible.
You won't benefit from HMR, but at least you CAN use webpack for these connected set ups which are much more common in the professional world.
Webpack can be simple too.
Example
I went ahead and created a very simple example for you. Click here to download it through github. Install dependencies:npm i
run webpack:
npm run webpack
this will bundle what's in the entry file and its imports and output it in the /build directory.
Check out the changes by navigating to the index.html file, or start a server in the project's root directory with:
python -m SimpleHTTPServer 3001
Some info on the project
The javascript is simple, it just imports a module and calls it. This is to demonstrate what the core of webpack is all about: bundling.import sayHi from './sayHi'; sayHi();So to get that code above to work, webpack needs to be set up:
const path = require('path'); module.exports = { entry: './src/index.js', output: { path: path.join(__dirname, "build"), filename: 'index.js', }, };This is all webpack needs to do its job. An entry place to your app, and where to output the generated bundle. That's it. All the other stuff, loaders, plugins, etc. are to enhance webpack, and accommodate to your needs of using babel, SASS, css modules, etc...
Leave a Reply