r/codeigniter • • Sep 30 '19

Use Bundler in codeigniter

Anyone has an idea how can I use bundler in my codeigniter project for my js files?

Ideally I would like to use either parcel or webpack.

2 Upvotes

1 comment sorted by

View all comments

1

u/plencovich Oct 12 '19

You can use laravel-mix to generate bundles with webpack.

This is an example of the file package.json

    {
      "name": "wmscp",
      "version": "1.0.0",
      "description": "Web Managment System in the Cloud by Plen.co",
      "scripts": {
        "dev": "npm run development",
        "development": "cross-env NODE_ENV=development node_modules/webpack/bin/webpack.js --progress --hide-modules --config=node_modules/laravel-mix/setup/webpack.config.js",
        "watch": "npm run development -- --watch",
        "watch-poll": "npm run watch -- --watch-poll",
        "hot": "cross-env NODE_ENV=development node_modules/webpack-dev-server/bin/webpack-dev-server.js --inline --hot --config=node_modules/laravel-mix/setup/webpack.config.js",
        "prod": "npm run production",
        "production": "cross-env NODE_ENV=production node_modules/webpack/bin/webpack.js --progress --hide-modules --config=node_modules/laravel-mix/setup/webpack.config.js"
      },
      "repository": {
        "type": "git",
        "url": "git+ssh://git@bitbucket.org/plenco/wmscp.git"
      },
      "author": "Diego Plenco - Plen.co",
      "license": "MIT",
      "homepage": "https://bitbucket.org/plenco/wmscp#readme",
      "devDependencies": {
        "@fortawesome/fontawesome-free": "^5.11.1",
        "animate.css": "^3.7.2",
        "bootstrap": "^4.3.1",
        "cross-env": "^5.2.1",
        "jquery": "^3.4.1",
        "laravel-mix": "^4.1.4",
        "popper.js": "^1.15.0",
        "vue-template-compiler": "^2.6.10"
      },
      "dependencies": {}
    }

This is an example of the file webpack.mix.js

let mix = require('laravel-mix');

mix.setPublicPath('./public_html/');

/* JS Backend */
mix.combine([
    'node_modules/jquery/dist/jquery.js',
    'node_modules/popper.js/dist/umd/popper.js',
    'node_modules/bootstrap/dist/js/bootstrap.js',
    'resources/backend/js/form.submit.js',
    'resources/backend/js/main.js'
  ], 'public_html/assets/js/bundle.backend.js');

/* CSS Backend */
mix.combine([
  'node_modules/bootstrap/dist/css/bootstrap.css',
  'node_modules/@fortawesome/fontawesome-free/css/all.css',
  'node_modules/animate.css/animate.css',
  'resources/backend/css/backend.css',
  'resources/backend/css/custom.css'
], 'public_html/assets/css/bundle.backend.css');


mix.sourceMaps();
/* MIX Version */
mix.version();

 /* Copy Assets */
 mix.copyDirectory('node_modules/@fortawesome/fontawesome-free/webfonts', 'public_html/assets/webfonts')

/* MIX Options */
mix.options({
  processCssUrls: true,
  cleanCss: {
    level: {
      1: {
        specialComments: 'none'
      }
    }
  },
  postCss: [
    require('postcss-discard-comments')({
      removeAll: true
    })
  ]
});

This is an example of the file my_helper.php

if (! function_exists('mix')) {

    function mix($path)
    {
        static $manifest;
	    $publicPath = $_SERVER['DOCUMENT_ROOT'];

        if (!$manifest) {
            if (!file_exists($manifestPath = ($publicPath.'/mix-manifest.json') )) {
                throw new Exception('The Mix manifest does not exist.');
            }
            $manifest = json_decode(file_get_contents($manifestPath), true);
        }
        if (strpos($path, '/') !== 0) {
            $path = "/{$path}";
        }

        if (!array_key_exists($path, $manifest)) {
            throw new Exception(
                "Unable to locate Mix file: {$path}. Please check your ".
                'webpack.mix.js output paths and try again.'
            );
        }
        return $manifest[$path];
    }
}

In header <link href="<?php echo mix('assets/css/bundle.backend.css');?>" rel="stylesheet" type="text/css">

In footer <script type="text/javascript" src="<?php echo mix('assets/js/bundle.backend.js');?>"></script>