-
Notifications
You must be signed in to change notification settings - Fork 11
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
Using NodeJS builtins #439
Comments
I suppose you can do something like this: {
"external": {
"path": "path"
},
"compilerFlags": {
"output_wrapper": "(function(path){%output%})(require('path'))"
}
} |
Thanks so much, that's very close to a similar workaround I saw recently. This will be incredibly powerful if I can get it to work, but there are some snags here. With the simplest possible configuration:
{
"compilerOptions": {
"strict": true,
"esModuleInterop": true
}
}
{
"modules": {
"bundle": "test.ts"
},
"prefix": {
"rollup": "dev/",
"cc": "dist/"
},
"compilerFlags": {
"process_common_js_modules": true,
"module_resolution": "NODE",
"output_wrapper": "(function(path){%output%})(require('path'))"
},
"external": {
"path": "path"
}
} We get:
With my nontrivial TSCC config, the error disappears due to {
"modules": {
"bundle": "test.ts"
},
"prefix": {
"rollup": "dev/",
"cc": "dist/"
},
"compilerFlags": {
"process_common_js_modules": true,
"module_resolution": "NODE",
"output_wrapper": "(function(path){%output%})(require('path'))",
"dependency_mode": "PRUNE_LEGACY",
"strict_mode_input": true,
"assume_function_wrapper": true,
"compilation_level": "ADVANCED",
"language_in": "ES_NEXT",
"language_out": "ECMASCRIPT5_STRICT"
},
"external": {
"path": "path"
}
} But the output AST is empty:
(function(path){'use strict';})(require('path')) If we can find a way to achieve this, it becomes pretty straightforward to set up a wrapper and pass in all ~30 builtins, and handle all Node input without issue. Your help is extremely appreciated, and the work you have done here is extremely valuable. |
Is this due to the generated externs? Does the compiler think I believe I saw a
/**
* @externs
* @suppress {duplicate,checkTypes}
*/
// NOTE: generated by tsickle, do not edit.
/** Generated by TSCC */
/**
* @type{typeof path}
* @const
*/
var path = {}; |
As far as I know, tsickle does not support Most of time, you can replace default imports to namespace imports ( It'd be better to have |
@ctjlewis: with TSCC, you will only need to add the mock from the other workaround. |
Sorry if this should be obvious, but how could I use Node.js globals? I'm getting errors such as:
I think I've fixed the last error using:
... but still not sure how I can avoid the errors to do with Buffer. Actually I'm getting better results by adding import { Buffer } from "buffer";
import { stdin } from "process"; I'm still getting the warning about "type/symbol" conflict but no errors. However, the definitions don't survive ADVANCED_COMPILATION, for example |
@jscheid Most likely the content of |
I was extremely pleased to see my TS compile to perfect
-O ADVANCED
output after adding thetsconfig.json
, but when I add a simple builtin import:I receive the following error:
This is one of the larger pains with Closure Compiler in general - Node builtins should be automatically detected (opt-out with a flag) and supplied as externs, and left untouched in output. TSCC does a fantastic job of handling thousands of other problems, but if we can't compile programs containing Node builtins, we can't realize the full benefit.
Any thoughts on this or recommendations? The test program is below.
The text was updated successfully, but these errors were encountered: