capaj / require-globify

transform for browserify, which allows to require files with globbing expressions
MIT License
70 stars 11 forks source link

Exception when used with factor-bundle with somewhat specific source files layout #28

Closed futpib closed 7 years ago

futpib commented 7 years ago

After building my project with require-globify and factor-bundle I get the following exception in the browser:

a.js:6 Uncaught TypeError: Cannot read property '0' of undefined
    at Object.require.4.dup (file:///home/futpib/tmp/require-globify-factor-bundle-issue-demo/dist/a.js:6:16)
    at s (file:///home/futpib/tmp/require-globify-factor-bundle-issue-demo/dist/a.js:1:262)
    at file:///home/futpib/tmp/require-globify-factor-bundle-issue-demo/dist/a.js:1:313
    at Object.require.3../lib/util.js (file:///home/futpib/tmp/require-globify-factor-bundle-issue-demo/dist/a.js:2:27)
    at s (file:///home/futpib/tmp/require-globify-factor-bundle-issue-demo/dist/a.js:1:262)
    at e (file:///home/futpib/tmp/require-globify-factor-bundle-issue-demo/dist/a.js:1:433)
    at file:///home/futpib/tmp/require-globify-factor-bundle-issue-demo/dist/a.js:1:451

Steps to reproduce:

  1. clone https://github.com/futpib/require-globify-factor-bundle-issue-demo.git
  2. npm install
  3. node index.js
  4. xdg-open index.html or open index.html in your browser
  5. uncaught exception in the browser console

I tried to come up with a shorter executable example, but this is the simplest I could get.

Here is the compiled code that leads to exception (prettified by chrome's debugger):

<!-- index.html -->
<script src="./dist/common.js"></script>
<script src="./dist/a.js"></script>
// common.js
require = (/* omitting boilerplate browser-pack/prelude.js */)({
    2: [function(require, module, exports) {}
    , {}]
}, {}, [])
// a.js
require = (/* omitting boilerplate browser-pack/prelude.js */)({
    3: [function(require, module, exports) {
        module.exports = {
            'util': require('./lib/util.js')
        };
    }
    , {
        "./lib/util.js": 4
    }],
    4: [function(require, module, exports) {
        arguments[4][2][0].apply(exports, arguments) // Uncaught TypeError: Cannot read property '0' of undefined
    }
    , {
        "dup": 2
    }]
}, {}, [3])

Note that naming of the src/* subdirectories is somehow significant (a and a-b). If I rename a-b to b or ab I am no longer able to reproduce this. (no idea)

Also, identical contents of src/a/lib/util.js and src/a-b/lib/util.js seems to be significant (they don't have to be empty, but they have to be identical, for this issue to reproduce). This makes me thing this may be related to dedupe part of browserify pipeline, but I'm not yet that deep into the whole build process to figure this one out for sure.

Workaround: don't glob-require identical files within directories that are prefixes of one another :).

I know this is a very specific edge case that no one probably cares about, I hope to at least get some pointers to what this could be related to.

futpib commented 7 years ago

This turned out to be unrelated to require-globify. Sorry for that.

futpib commented 7 years ago

This is a duplicate of substack/factor-bundle#51