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

CommonJs loader does not ignore requires() in comments, if they appear at the last index of comments #919

Closed
colmbrady opened this issue Nov 17, 2015 · 4 comments

Comments

@colmbrady
Copy link
Contributor

It seems that the CommonJS loader can erroneously detect a module dependency which is actually not a correct dependency and is infact a comment block.

Im not quite sure how I can submit a unit tests to prove this as i'm using SystemJS to load up a browserified Javascript bundle, which contains a combined list of dependencies, one of which is handlebars. (The bug manifests with handlebars)

Anyhow, the module 'amddefine' (a dependency of handlebars) has the following comment -> '//Synchronous, single module require('')'. It seems SystemJS is recursively walking the dependency tree for all modules looking for "requires" -> because this comment has a require('') at the end of the comment line a string match bug causes SystemJs to think that it must require(''). This is the bug.

The bug currently exists at this line in master: https://github.com/systemjs/systemjs/blob/master/lib/cjs.js#L27

The code should be as below, (notice the match[0].length -1)

function inLocation(locations, match, starts) {
      var inLocation = false;
      for (var i = 0; i < locations.length; i++)
        if (locations[i][0] < match.index && locations[i][1] > match.index + (!starts ? (match[0].length - 1) : 0))
          return true;
      return false;
    }

Im happy to submit this fix in a pull request if you can provide some guidance R.E how to unit test this in your codebase?

Ive attached the javascript file but as its browserified i'm not sure how useful it is.

@colmbrady
Copy link
Contributor Author

bundle.txt

Searching for require('') in this file shows where the issue is occurring.

@guybedford
Copy link
Member

Amazing thank you so much for the detailed report!

The test cases for this are in the file at https://github.com/systemjs/systemjs/blob/master/test/tests/commonjs-requires.js. If you're able to provide a PR that would certainly be welcome, feel free to add an additional line there.

colmbrady added a commit to colmbrady/systemjs that referenced this issue Nov 18, 2015
- Fix code in cjs.js to correctly match last index of comments.
- Add test module that includes the require('') at end of comment string
- Add unit test to prove the module can be loaded OK with the comment present
@colmbrady
Copy link
Contributor Author

master...colmbrady:comment-detection-issue

Ive got some test coverage on the issue.

colmbrady added a commit to colmbrady/systemjs that referenced this issue Nov 18, 2015
- Adding unit test to existing commonjs-requires.js file.
- Remove redundant file.
guybedford added a commit that referenced this issue Nov 18, 2015
@guybedford
Copy link
Member

Released in 0.19.7.

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

No branches or pull requests

2 participants