Skip to content

Allow ESM imports of externs files #3740

Open
@ctjlewis

Description

@ctjlewis

Sample source:

/** moduleTest.js */
import fs from 'fs';
console.log(fs);

Command:

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):

import fs from 'fs';console.log(fs);

Actual output:

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:

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:

console.log(require('fs'));

Which produces the expected output.

Metadata

Metadata

Assignees

No one assigned

    Labels

    featFeature Request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions