Skip to content

Producing CommonJS modules #2406

@wiktortomczak

Description

@wiktortomczak

Is it possible to use Closure compiler to produce a standalone CommonJS module, for use by dependent external (not managed by the compiler) JS code? That is, to produce a single file that exports its public symbols via CommonJS special exports variable?

The problem I'm running into is that:

  • with --process_common_js_modules, the compiler renames exports to module$path$to$source, making it impossible to import the compiled source as a CommonJS module
  • without --process_common_js_modules, the compiler does not resolve CommonJS require imports, so other CommonJS modules can't be used as the target module's building blocks.

I believe I understand the motivation for renaming exports (prevent name clashes between multiple CommonJS modules concatenated in the single compiled file), but this is not enough if the compiled file is meant to be imported by some other code. Ideally, the exports variable would not be renamed in the top-level module that defines the module's public symbols (it still needs to be renamed, as is already the case, in implementation modules the top-level module imports itself).

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions