-
-
Notifications
You must be signed in to change notification settings - Fork 673
Add node resolution for transformer #940
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
Conversation
How about use |
@MaxGraey is |
This actually node (commonjs) mechanics. Don't think we need this in current time until we load everything ahead of time |
@MaxGraey To use resolve we would still have to check for an error: > require.resolve("a")
Thrown:
Error: Cannot find module 'a'
Require stack:
- <repl>
at Function.Module._resolveFilename (internal/modules/cjs/loader.js:625:15)
at Function.resolve (internal/modules/cjs/helpers.js:21:19) {
code: 'MODULE_NOT_FOUND',
requireStack: [ '<repl>' ]
} |
@willemneal Yes, but you got only MODULE_NOT_FOUND error and don't need |
function tryResolvePath(path) {
try { return require.resolve(path) }
catch (e) { return null }
} |
So something like this: function resolve(m) {
try {
return require.resolve(m);
} catch (e) {
return null;
}
} and const classOrModule = (resolve(filename) == null) ? require(transformArgs[i]) : require(filename); |
Just tested it with my transformer and it works. |
great |
cli/asc.js
Outdated
@@ -220,7 +229,7 @@ exports.main = function main(argv, options, callback) { | |||
: path.join(process.cwd(), filename); | |||
if (/\.ts$/.test(filename)) require("ts-node").register({ transpileOnly: true, skipProject: true }); | |||
try { | |||
const classOrModule = require(filename); | |||
const classOrModule = require(pathResolves(filename) ? filename : transformArgs[i]); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here, transfromArgs[i]
is not trimmed, which filename
is guaranteed to be, and trying to require that is unnecessary if the path is absolute anyway. Also, require(transformArgs[i])
is relative to node_modules/assemblyscript/cli/asc.js
which appears problematic. A cleaner variant might be
- if absolute, require as-is
- if relative, use node-resolve relative to cwd? (not sure about --baseDir)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
resolve can take an array of paths to use instead of the default, so we could use cwd and baseDir there.
Also why is the filename not guaranteed to be trimmed? Shouldn't all cli arguments be trimmed?
Now we have function _require(m, paths) {
const _path = require.resolve(m, {paths});
return require(_path)
}
//and
let filename = transformArgs[i].trim();
if (/\.ts$/.test(filename)) require("ts-node").register({ transpileOnly: true, skipProject: true });
try {
const classOrModule = _require(filename, [baseDir, process.cwd()]); If the path is absolute that it doesn't matter for the resolution paths, otherwise you try to look starting in both
|
Okay testing shows that this resolves currently in both situations where asc is locally in "node_modules" or from a different location, e.g. globally. However, when asc is non-local I get the following:
This is because my transformer requires "assemblyscript" and asc requires "../dist/assemblyscript". So when when everything is local this ends up being the same file and assemblyscript is not loaded the second time. One solution could be to pass the compiler and then the transformer adds the types file to its config. Alternatively we can just keep the requirement that you must keep them together. |
I've tested this and it works for my transformer, but again only if they are both local. |
Looks good, thanks! |
Try relative path first and then with normal node resolution.