Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug: Resultant bundles fail to find modules via require() as expected. Example code inside #1862

Open
JSMan2000 opened this issue Aug 15, 2018 · 1 comment

Comments

@JSMan2000
Copy link

Expected Result

  1. A valid bundle is included and all modules can be found via require()

Actual result

The bundle fails to require the "bluebird" module with the following error:

Uncaught Error: Cannot find module 'bluebird'

You can see in the reproduction code that bluebird does in fact exist. It is defined in main2.js/bundle2.js. As a result, it should be found, but it fails to be found.

The issue is that when a bundle uses "external" it fails to look for the module in another bundle (the "external" code that includes it), which defeats the point of externally including something.

Reproduction Code

Include bundle1.js and bundle2.js into a file and then call the functions:

window.main1();
window.main2();

You will receive the error that bluebird cannot be found even though it is clearly defined in main2/bundle2

It is the main1() function that fails to find the module.

Grunt file

module.exports = function(grunt) {
    grunt.initConfig({
        browserify: {
            main1: {
                src: ['src/main1.js'],
                dest: 'build/bundle1.js',
                options: {
                    external: ['bluebird']
                },
            },

            main2: {
                src: ['src/main2.js'],
                dest: 'build/bundle2.js',
            }
        }
    });

    grunt.loadNpmTasks('grunt-browserify');
    grunt.registerTask('default', ['browserify']);

};

Main1.js

window.main1 = function() {
    let pexec = require('pexec');
    console.log(pexec);
}

Main2.js

window.main2 = function() {
    let t = require('bluebird');
    console.log(t);
};
@casr
Copy link

casr commented Oct 29, 2018

I think you might need to add a option to the main2.js build options to expose bluebird if you would like to make it external. Then you must load bundle2.js before bundle1.js on the page as bundle1.js requires the module exposed to it from bundle2.js.

Something like:

module.exports = function(grunt) {
    grunt.initConfig({
        browserify: {
            main1: {
                src: ['src/main1.js'],
                dest: 'build/bundle1.js',
                options: {
                    external: ['bluebird']
                },
            },

            main2: {
                src: ['src/main2.js'],
                dest: 'build/bundle2.js',
                options: {
                  require: ['bluebird'],
                },
            }
        }
    });

    grunt.loadNpmTasks('grunt-browserify');
    grunt.registerTask('default', ['browserify']);

};

might work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants