google / google/closure-compiler
Allow ESM imports of externs files
- Dominant language
- JavaScript
- Stars
- 7.7k
- Forks
- 1.2k
- Avg merge
- 2d 12h
- Merged PRs (30d)
- 6
Description
Sample source:
```javascript
/** moduleTest.js */
import fs from 'fs';
console.log(fs);
```
Command:
```none
google-closure-compiler \
-O ADVANCED \
--module_resolution NODE \
--process_common_js_modules \
--externs ./node_modules/google-closure-compiler/contrib/nodejs/fs.js \
moduleTest.js
```
The compiler should not try to import an extern. Expected output should be something like (leaving `import` statement intact):
```js
import fs from 'fs';console.log(fs);
```
Actual output:
```none
moduleTest.js:1: ERROR - [JSC_JS_MODULE_LOAD_WARNING] Failed to load module "fs"
1| import fs from 'fs';
^
1 error(s), 0 warning(s)
```
I recognize that this is a low-priority issue, but I want to file it anyway so it's documented. It is highly valuable to be able to declare imports as external, especially NodeJS builtins.
Ideally, `--jscomp_off moduleLoad --jscomp_off undefinedVars` would produce useful output, but it generates:
```javascript
console.log(default$$module$fs);
```
Where `default$$module$fs` is naturally `undefined`. This is a hacky suggestion, but for `-O ADVANCED`, it might be appropriate to replace `default$$module$fs` with `require('fs')`, such that the generated output is:
```javascript
console.log(require('fs'));
```
Which produces the expected output.
Contributor guide
Assessment
This issue has not been assessed yet.