question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How do I bundle with webpack?

See original GitHub issue

I have a site that already have jquery 1.7 loaded.

I want to bundle my dependencies with webpack.

I’m tried

import “select2” require “select2”

my webpack config looks like this:

module.exports = {
    mode: "development",
    entry: {
        frb: "./src/app/loader.js"
    },
    output: {
        path: path.resolve(__dirname, "./src/lib"),
        filename: "bundle.js"
    },
    module: {
        rules: [
            // { parser: { amd: false } },
            {
                test: /\.(js|jsx)$/,
                exclude: /(node_modules)/,
                loader: "babel-loader",
                query: {
                    presets: [
                        [
                            "@babel/preset-env",
                            {
                                targets: {
                                    chrome: "58",
                                    ie: "9"
                                }
                            }
                        ]
                    ]
                }
            }
        ]
    },
    externals: {
        $: "jquery",
        jQuery: "jquery"
    },

    plugins: [
        // Ignore all locale files of moment.js
        new webpack.IgnorePlugin(/^\.\/locale$/, /moment$/),
        new webpack.ProvidePlugin({
            $: "jquery",
            jQuery: "jquery"
        })
    ]
};

I just keep getting : TypeError: $(...).select2 is not a function

Am I missing something?

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:10 (2 by maintainers)

github_iconTop GitHub Comments

8reactions
chlorophyllkidcommented, Aug 5, 2019

Maybe it would be an improvement to the documentation to add an up to date version of the installation. People will answer this question a lot, since there is no common example.

5reactions
gricey432commented, Nov 1, 2018

I managed to get this to work in Typescript + webpack kind of (though I’m not sure if it’s the right approach).

dropdown.ts

/// <reference path="@types/select2-module.d.ts" />
import * as $ from 'jquery';
// TODO this works but I'm not sure how to get the types right
// @ts-ignore
import * as select2 from "select2";

import "select2/dist/css/select2.css";
import "select2-bootstrap-theme/dist/select2-bootstrap.css";

// Hook up select2 to jquery
select2($);

@types/select2-module.d.ts

declare module 'select2' {
    function select2($: JQueryStatic): void;
    export = select2;
}

I also had to remove the dependency on requirejs from the DefinitelyTyped types because it conflicted with another require type, but that’s kind of specific to my project I think.

Hopefully a similar approach helps with other non-typescript webpack bundling. I think the important part might be to explicitly bind with the select2($).

Read more comments on GitHub >

github_iconTop Results From Across the Web

Getting Started - webpack
Learn how to bundle a JavaScript application with webpack 5. ... First let's create a directory, initialize npm, install webpack locally, ...
Read more >
How to Bundle a Simple Static Site Using Webpack - SitePoint
Add the following to app.js : require('./main.js');. And change the webpack config thus: entry: './src/js/app.js',. Run npm run build again to ...
Read more >
How to Bundle an Application with Webpack - Sweetcode.io
Webpack takes your Javascript files, dependencies as input, bundles them, and then outputs compressed optimised files. It then constructs a dependency graph for ......
Read more >
Webpack: How It Generates the Bundle - Encora
Bundle. File that contains all the modules your application needs. In other words, it is how the application is packaged. Dependency Graph. When ......
Read more >
Module Bundling with Webpack: Getting Started Guide
Module bundlers are just what they are called, they bundle up JavaScript modules into one file. This way, when the client makes a...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found