sidewaysglance's avatar

Being dragged into the world of webpack

So in previous projects I used mrclay/minify which did a great job managing concatenation & caching of potentially ever changing js & css files.

To implement something similar or maybe better, it seems that I am going to be using node / webpack / laravel mix.

In terms of my app in it's current state, I have separate js & css files that are loaded as required in certain views.

It seems that to use webpack I need files that don't "clash" - or use entry points? Not sure!

For example on one page I might have the code that reads $(document).on('click', '.create', function(){doOneThing();}) and on another page I might have code that does $(document).on('click', '.create', function(){doSomethingElse();}).

Does anyone have an easy solution for this, or a tutorital I could read that will explain how I can add my (probably considered legacy) js to a webpack build system plz? Guess I am going to need to modularise my code in some way?

Thanks

0 likes
2 replies
LaryAI's avatar
Level 58

Webpack is a powerful tool for managing and bundling JavaScript and CSS files. To get started with webpack, you'll need to install it and configure it for your project. Here are the basic steps:

  1. Install webpack and webpack-cli:
npm install webpack webpack-cli --save-dev
  1. Create a webpack configuration file (webpack.config.js) in the root of your project:
const path = require('path');

module.exports = {
  entry: {
    app: './src/index.js',
  },
  output: {
    filename: '[name].bundle.js',
    path: path.resolve(__dirname, 'dist'),
  },
};

This configuration sets up an entry point (src/index.js) and an output file (dist/app.bundle.js).

  1. Install any necessary loaders and plugins for your project. For example, if you're using Sass, you'll need to install the sass-loader and node-sass packages:
npm install sass-loader node-sass --save-dev
  1. Update your JavaScript and CSS files to use ES6 modules and import/export statements. For example:
// src/create.js
export function doOneThing() {
  // ...
}

// src/other.js
export function doSomethingElse() {
  // ...
}
  1. Update your HTML files to include the bundled JavaScript and CSS files:
<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>My App</title>
    <link rel="stylesheet" href="dist/app.bundle.css">
  </head>
  <body>
    <script src="dist/app.bundle.js"></script>
  </body>
</html>
  1. Use the imported functions in your JavaScript files:
// src/index.js
import { doOneThing } from './create';
import { doSomethingElse } from './other';

$(document).on('click', '.create', function() {
  doOneThing();
});

$(document).on('click', '.create', function() {
  doSomethingElse();
});

This code imports the doOneThing and doSomethingElse functions from separate files and uses them in separate event listeners.

With these steps, you should be able to get started with webpack and modularize your JavaScript and CSS code. For more information, check out the webpack documentation and tutorials.

sidewaysglance's avatar

@LaryAI Thanks Lary but I'm not entirely sure that's helpful - I would like to use laravel mix & get some human opinions on the best way forward.

Please or to participate in this conversation.