Require Outside Libraries

When you use Webpack, the hardest thing is that you need to start thinking about your JavaScript differently. You need to stop thinking about global variables, and start thinking about how you can code correctly. It's not as easy as it sounds: we've been using global variables in JavaScript... well... forever!

For example, in RepLogApp.js, we created this self-executing function to give our code a little bit of isolation:

... lines 1 - 4
(function(window, $, Routing, swal) {
... lines 6 - 213
})(window, jQuery, Routing, swal);

That part isn't too important. But at the bottom, we are relying on there to be a global jQuery variable. It just must exist, or else everything will explode! On top, this becomes a $ variable in the function.

Open the base layout file - base.html.twig. The only reason our code works is that, at the bottom, yep! We have a script tag for jQuery, which adds a global jQuery variable:

... lines 1 - 98
{% block javascripts %}
<script src="https://code.jquery.com/jquery-3.1.1.min.js" integrity="sha256-hVVnYaiADRTO2PzUGmuLJr8BLUSjGIZsDYGmIJLv2b8=" crossorigin="anonymous"></script>
... lines 101 - 105
{% endblock %}
... lines 107 - 110

And this is the process we've used for years: add a script tag for a JS library, then reference its global variable everywhere else.

I hate this! In RepLogApp.js, I just have to hope that jQuery was included correctly. That's madness, and it needs to stop. So, from now on, we have a new philosophy: if we need a variable in a file - like $ - then we need to require it in the same way that we are requiring Helper.

The only difference is that jQuery is a third-party library. Well... in PHP, we would use Composer to install third-party libraries. And... yea! In JavaScript, we can use Yarn to do the same thing!

Installing jQuery via Yarn

Check this out: open a third terminal tab - we're getting greedy! Then run:

yarn add jquery --dev

Yep! We can use yarn to download front-end libraries! Oh, and you can search for package names on npmjs.com or npms.io.

This downloads jquery into the node_modules/ directory and adds it to package.json:

8 lines package.json
{
"devDependencies": {
... line 3
"jquery": "^3.3.1",
... line 5
}
}

Requiring jQuery

So... how do we require it? Oh, it's awesome: const $ = require('jquery'):

... lines 1 - 2
const Helper = require('./RepLogHelper');
const $ = require('jquery');
... lines 5 - 216

That's it! When a require path does not start with a ., Webpack knows to look for a package in node_modules/ with that name.

And now that we are properly importing the $ variable - yay us - remove $ and jQuery from the self-executing function:

... lines 1 - 3
const $ = require('jquery');
(function(window, Routing, swal) {
... lines 7 - 214
})(window, Routing, swal);

Yep, when we use the $ variable below, it is no longer dependent on any global jQuery variable! Responsible coding for the win!

But... does it work? Try it! Go back to our site and refresh! It does! That's because, back on the terminal, if you run:

ls -la public/build

... yep! Our rep_log.js file now has jQuery inside of it - you know because it's now 300kb! Don't worry, we'll talk about optimizations later.

But the point is this: all we need to do is require the libraries we need, and Webpack takes care of the rest!

Installing & Using SweetAlert2

Let's require one more outside package. Search for "swal". We're using a really cool library called SweetAlert to bring up the delete dialog. But... the only reason this works is that, in the template, we're including a script tag for it:

... lines 1 - 47
{% block stylesheets %}
... lines 49 - 50
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/sweetalert2@7.11.0/dist/sweetalert2.min.css" />
{% endblock %}
... line 53
{% block javascripts %}
... lines 55 - 56
<script src="https://cdn.jsdelivr.net/npm/sweetalert2@7.11.0/dist/sweetalert2.all.min.js"></script>
... lines 58 - 65
{% endblock %}

Boo! Let's refactor this to require that library properly.

If you search for this package, you'll find out that it's called sweetalert2. Let's install it:

yarn add sweetalert2 --dev

This time, delete the script tag entirely. We can't remove the jQuery script tag yet because we're still using the global variable in a few places. But, we'll fix that soon.

Then, in RepLogApp.js, remove the argument from the self-executing function: that global variable doesn't even exist anymore!

... lines 1 - 6
(function(window, Routing) {
... lines 8 - 215
})(window, Routing);

To prove it, refresh! Awesome!

swal is not defined

To get it back, add const swal = require('sweetalert2');:

... lines 1 - 4
const swal = require('sweetalert2');
(function(window, Routing) {
... lines 8 - 215
})(window, Routing);

As soon as we save this, Webpack recompiles, we refresh and... it works! Yes! We can use any outside library by running one command and adding one require line.

Let's use our new unstoppable skills to refactor our code into re-usable components.

Leave a comment!

  • 2018-07-12 weaverryan

    Hey Bertin van den Ham!

    Woohoo! I'm so happy it's working! This is exactly the "workflow" I was hoping people would have to enable this feature :).

    > 1. Now i need to add the postcss.config.js file to the root but is there a way to change the path, eq to /assets/configs/

    Hmm. The commented-out config you have looks perfect. When you try this, do you get an error? Or does it just not see the file in the new location? If you completely move the file to the new location (and use that config), is there an error or does it just not work correctly? I'm not sure what's going wrong here.

    > 2. Some other question, in the video about Copy image you mentioned that encore would have an copy feature in the future. Is this already being implemented?

    Booo. No, not yet. Here's the PR: https://github.com/symfony/.... It's blocked due to some problems in an upstream library. I'm not sure if we'll be able to work around that, or if we'll just need to implement something ourself.

    Cheers!

  • 2018-07-11 Bertin van den Ham

    Hey weaverryan
    It works.
    I just enabled enablePostCss() in webpack.config.js
    When running yarn run encore dev --watch, i get the message package 'postcss-loader' missing.

    After adding the package and run the comment again, it says: FIX create a postcss.config.js file.

    I've created this file at the root


    # at config, postcss.config.js
    module.exports = {
    plugins: {
    'autoprefixer': {},
    }
    };


    Now it works

    And when i look in my compiled css
    position: sticky; has now also position: -webkit-sticky; (created by autoPrefixer).

    But i've some questions:
    1. Now i need to add the postcss.config.js file to the root but is there a way to change the path, eq to /assets/configs/

    I've seen some options on git, webpack-encore pull 130

    But don't now how to use it within webpack.encore.js.
    See my webpack.config.js below with an example but not working


    var Encore = require('@symfony/webpack-encore');
    const CopyWebpackPlugin = require('copy-webpack-plugin');

    Encore
    .setOutputPath('public/build/')
    .setPublicPath('/build')
    .cleanupOutputBeforeBuild()
    .autoProvidejQuery()
    .enableSourceMaps(!Encore.isProduction())
    .enableVersioning()

    .createSharedEntry('js/common', ['jquery'])
    .addEntry('js/backend/backend', './assets/js/backend/backend.js')
    .addStyleEntry('css/backend/backend', ['./assets/scss/backend/backend.scss'])

    .addEntry('js/frontend/app', './assets/js/frontend/app.js')
    .addStyleEntry('css/frontend/app', ['./assets/scss/frontend/app.scss'])
    .addStyleEntry('css/frontend/login', './assets/scss/frontend/login.scss')

    .enableSassLoader()
    .enablePostCssLoader()

    .addPlugin(new CopyWebpackPlugin([
    // copies to {output}/static
    { from: './assets/static', to: 'img' }
    ]))
    ;

    // example but not working
    // Encore.enablePostCssLoader((options) => {
    // options.config = {
    // path: 'assets/config/'
    // };
    // });

    module.exports = Encore.getWebpackConfig();

    Some other question, in the video about Copy image you mentioned that encore would have an copy feature in the future. Is this already being implemented?

  • 2018-07-11 weaverryan

    Hey Bertin van den Ham!

    ... not to ignore your question... but I might have a better solution for doing this. Encore already supports PostCss (just call enablePostCss) - I see you added this manually in your code, but this isn't necessary. PostCss (as I think you know) can be used to integrate with autoprefixer. In fact, if I remember correctly, when you call enablePostCss(), Encore will give you an error that you need to create a config file, and the config contents it suggests include the code needed to enable autoprefixer (you likely already have this config file). So basically, you add one line to webpack.config.js, add one config file, install a few packages (which Encore will tell you) and... that should be it!

    Let me know if this helps!

  • 2018-07-10 Bertin van den Ham

    I'am trying to install a yarn plugin called: autoprefixer .
    Autoprefixer auto fills css for different browsers.

    In my webpack.config.js i've added:

     
    #webpack.config.js
    .addLoader({
    test: /\.css$/,
    use: ["style-loader", "css-loader", "postcss-loader"]
    })

    In my backend.js i've added

     
    #assets/js/backend/backend.js
    'use strict';

    import $ from 'jquery';
    import 'bootstrap';
    import 'autoprefixer';

    // rest of js code here
    $(document).ready(function(){

    });

    But when i run yarn run encore dev --watch i get errors. Hope someone could help me out.

     
    Webpack is watching the files…

    ERROR Failed to compile with 2 errors 19:58:37

    error in ./assets/scss/backend/backend.scss

    Module build failed:
    'use strict';
    ^
    Invalid CSS after "'": expected 1 selector or at-rule, was "'use strict';"
    in /node_modules/autoprefixer/lib/autoprefixer.js (line 1, column 1)

    @ ./assets/scss/backend/backend.scss 4:14-211
    @ multi ./assets/scss/backend/backend.scss

    error in ./assets/scss/backend/backend.scss

    Module build failed: ModuleBuildError: Module build failed:
    'use strict';
    ^
    Invalid CSS after "'": expected 1 selector or at-rule, was "'use strict';"
    in /node_modules/autoprefixer/lib/autoprefixer.js (line 1, column 1)
    at runLoaders (/node_modules/webpack/lib/NormalModule.js:195:19)
    at newwayhoogeveen/node_modules/loader-runner/lib/LoaderRunner.js:364:11
    at /node_modules/loader-runner/lib/LoaderRunner.js:230:18
    at context.callback (/node_modules/loader-runner/lib/LoaderRunner.js:111:13)
    at Object.asyncSassJobQueue.push [as callback] (/node_modules/sass-loader/lib/loader.js:76:13)
    at Object.done [as callback] node_modules/neo-async/async.js:7974:18)
    at options.error (node_modules/node-sass/lib/index.js:294:32)

  • 2018-04-23 Diego Aguiar

    Hey JAVroegop

    Nice tip, thanks for sharing it :)
    Cheers!

  • 2018-04-23 JAVroegop

    If you ever get a warning from Yarn like this:

    An unexpected error occurred: "EPERM: operation not permitted, unlink '.....\\node_modules\\.bin\\encore'"

    Before you try to install jquery, please stop encore. I spent quite a while figuring out why yarn couldn't delete node_modules/.bin/encore... on my Windows 10 machine. *Doh!*