google / google/closure-compiler

Allow ESM imports of externs files

Open
#3,740 4 comments 4 reactions 0 assignees View on GitHub
feat
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

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.