Coder Social home page Coder Social logo

Comments (8)

mrjoelkemp avatar mrjoelkemp commented on July 28, 2024

Hey @choxnox! Thanks for contributing.

This is similar to the following request: #42.

I'm totally open to a PR that enhances the functionality for legal module syntax. I don't have time to do this myself, but happy to answer questions and review PRs.

from node-precinct.

choxnox avatar choxnox commented on July 28, 2024

Because this issue bothers me quite a lot (currently I have to manually include modules inside Serverless config file which is super awkward) I will actually try to "fix" this and send PR. So if I understood referenced issue correctly it's node-detective-cjs module that should be modified to support syntax from this issue?

from node-precinct.

mrjoelkemp avatar mrjoelkemp commented on July 28, 2024

I appreciate the help. Thanks in advance for the contribution.

So if I understood referenced issue correctly it's node-detective-cjs module that should be modified to support syntax from this issue?

Yes. My assumption here is that require.main.require is a commonjs/nodejs module resolution feature. If that's the case, then node-detective-cjs contains all of the commonjs-specific logic for how to find dependencies within those types of modules.

from node-precinct.

choxnox avatar choxnox commented on July 28, 2024

I took a quick look at the node-detective-cjs code and it seemed the problem is not there but in ast-module-types module and its isRequire function which is called from node-detective-cjs module.

So I went there and eventually made it work (in tests) by modifying that function (AST explorer has been of great help). Modules required with require.main.require get detected properly. Should I submit PR there instead?

Anyway, the only difference is this before:

// Whether or not the node represents a require function call
module.exports.isRequire = function (node) {
  if (!node) return false;

  var c = node.callee;

  return c &&
        node.type  === 'CallExpression' &&
        c.type     === 'Identifier' &&
        c.name     === 'require';
};

and after:

// Whether or not the node represents a require function call
module.exports.isRequire = function (node) {
  if (!node) return false;

  var c = node.callee;

  return c &&
        node.type  === 'CallExpression' && ((
            c.type     === 'Identifier' &&
            c.name     === 'require') || (
            c.type     === 'MemberExpression' &&
            c.object.type === 'MemberExpression' &&
            c.object.object.type === 'Identifier' &&
            c.object.object.name === 'require' &&
            c.object.property.type === 'Identifier' &&
            c.object.property.name === 'main' &&
            c.property.type === 'Identifier' &&
            c.property.name === 'require'
        ));
};

from node-precinct.

mrjoelkemp avatar mrjoelkemp commented on July 28, 2024

Great work tracking down a fix. That's actually a better place for it since module-definition will also properly correlate that special require with commonjs.

Consider using extracting the addition to a method like:

return isPlainRequire(node) || isMainScopedRequire(node);

where isMainScopedRequire contains your new logic extracted to a method. If you could extract the original logic for sniffing require to isPlainRequire, that might help the readability of the method.

from node-precinct.

choxnox avatar choxnox commented on July 28, 2024

Yeah, this was just a "hotfix" to see if idea sticks (tests passed). I'll go with your suggestions and submit the PR (most probably tomorrow).

from node-precinct.

choxnox avatar choxnox commented on July 28, 2024

I finally managed to find some free time today to do this. I've just sent PR for this.

from node-precinct.

mrjoelkemp avatar mrjoelkemp commented on July 28, 2024

@choxnox thanks again for your contribution. I pushed the support all the way up to precinct.

There's another missing piece to this though. We now have the ability to extract dependencies from main scoped require statements, but we don't yet know how to resolve them to a file on the filesystem. This is the module and code that's used to handle a commonjs style of module resolution: https://github.com/dependents/node-filing-cabinet/blob/master/index.js#L195.

Since require.main.require influences the behavior of how the module/partial is resolved, we either get it for free with our use of https://github.com/browserify/resolve here: https://github.com/dependents/node-filing-cabinet/blob/a11f774a6ef95cce8db1e700aebf45e4ee54affb/index.js#L221, or we'll have to either get that supported in that library or within filing-cabinet directly.

Does the fix in precinct completely satisfy your need, or are you trying to generate a dependency tree from your project (at which point, you'll need the mapping of module to file)? Are you open to a PR that adds main scoped commonjs module resolution support to filing cabinet?

from node-precinct.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.