X Tutup
The Wayback Machine - https://web.archive.org/web/20230419211332/https://github.com/nodejs/node/issues/40098
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

Feature Request: make Module.prototype._compile() public #40098

Open
mscdex opened this issue Sep 13, 2021 · 13 comments
Open

Feature Request: make Module.prototype._compile() public #40098

mscdex opened this issue Sep 13, 2021 · 13 comments
Labels
feature request Issues that request new features to be added to Node.js. module Issues and PRs related to the module subsystem.

Comments

@mscdex
Copy link
Contributor

mscdex commented Sep 13, 2021

Already the Module constructor is exported from require('module'), which gets userland halfway to being able to compile and evaluate a module from a string. Right now we can call myModule._compile(...) and then get at the exports, but I would much rather have and use a public (non-underscore-prefixed) version of this method.

My particular use case for this is I have many different modules in separate files that all export some data, functions, etc.. As part of a build step to simplify accessing these modules, I combine all of the their sources into a single module, exporting each submodule. In order to achieve this, I need to be able to compile, run, and get at each submodule's exports. As far as I can tell there is no (public) way to do that currently.

@mscdex mscdex added module Issues and PRs related to the module subsystem. feature request Issues that request new features to be added to Node.js. labels Sep 13, 2021
@mscdex mscdex changed the title Feature Request: make module._compile() public Feature Request: make Module.prototype._compile() public Sep 13, 2021
@aduh95
Copy link
Contributor

aduh95 commented Sep 13, 2021

That sounds like a job for cjs-module-lexer, which is vendored in node already, maybe we could consider making it publicly available?

@mscdex
Copy link
Contributor Author

mscdex commented Sep 14, 2021

I don't think cjs-module-lexer would help my use case as it appears to merely parse source code to find out what's attached to module.exports. I need my "super module" to be able to export the actual values, which requires evaluation of the source code.

Here's a simple example of more or less what I'm currently having to do:

'use strict';

const Module = require('module');

function load(filename, content) {
  const mod = new Module(filename, module);

  // This is my "problem". I'd much rather have `mod.compile(content, filename);`
  mod._compile(content, filename);

  return mod.exports;
}

module.exports = {
  foo: load('foo.js', '/* foo.js raw contents here */'),
  bar: load('bar.js', '/* bar.js raw contents here */'),
  // ...
};

@targos
Copy link
Member

targos commented Sep 14, 2021

If we want to expose this publicly, maybe we should design a slightly better API?

@mscdex
Copy link
Contributor Author

mscdex commented Sep 14, 2021

Personally I'm perfectly fine with the API as-is, it's flexible enough and isn't complicated.

@github-actions
Copy link
Contributor

github-actions bot commented Apr 4, 2022

There has been no activity on this feature request for 5 months and it is unlikely to be implemented. It will be closed 6 months after the last non-automated comment.

For more information on how the project manages feature requests, please consult the feature request management document.

@github-actions github-actions bot added the stale label Apr 4, 2022
@mscdex
Copy link
Contributor Author

mscdex commented Apr 4, 2022

Would anyone be opposed to me just renaming ._compile to .compile and making ._compile an alias?

@mscdex mscdex removed the stale label Apr 4, 2022
@targos
Copy link
Member

targos commented Apr 4, 2022

I would be opposed. I'd much prefer a single new function exported from 'module'.

@mscdex
Copy link
Contributor Author

mscdex commented Apr 4, 2022

@targos Suggestions then?

@targos
Copy link
Member

targos commented Apr 5, 2022

To be improved:

const { createCommonJSModule } = require('module');

const myModule = createCommonJSModule({
  filename: 'myModule.js',
  parent: module,
  code: `
    exports.value = 42;
  `
});

console.log(myModule.exports.value) // 42

@github-actions
Copy link
Contributor

github-actions bot commented Oct 3, 2022

There has been no activity on this feature request for 5 months and it is unlikely to be implemented. It will be closed 6 months after the last non-automated comment.

For more information on how the project manages feature requests, please consult the feature request management document.

@github-actions github-actions bot added the stale label Oct 3, 2022
@mscdex
Copy link
Contributor Author

mscdex commented Oct 3, 2022

unstale por favor

@mscdex mscdex removed the stale label Oct 3, 2022
@github-actions
Copy link
Contributor

github-actions bot commented Apr 2, 2023

There has been no activity on this feature request for 5 months and it is unlikely to be implemented. It will be closed 6 months after the last non-automated comment.

For more information on how the project manages feature requests, please consult the feature request management document.

@github-actions github-actions bot added the stale label Apr 2, 2023
@mscdex
Copy link
Contributor Author

mscdex commented Apr 2, 2023

unstale por favor

@mscdex mscdex removed the stale label Apr 2, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature request Issues that request new features to be added to Node.js. module Issues and PRs related to the module subsystem.
Projects
Status: Pending Triage
Development

No branches or pull requests

3 participants
X Tutup