Skip to content

Support for ES modules #178

@bencoveney

Description

@bencoveney

Support for ES modules

Barrelsby is a stand-in for the developer (writing code) and should be able to write equivalent code to that which the user would write - this now includes code structured as ES modules.

Detecting CommonJS Modules or ES Modules

When building each barrel file, we should perform the following steps:

  • Starting at the directory where the barrel file would be placed, look for a package.json file.
  • If one is not found, repeat the process for each parent directory until one is found.

Once this has been done, we will be able to work out if we are using CommonJS os ES Modules:

  • If a package.json file was found and it contains a "type" field with the value "module", then we are using ES Modules.
  • Otherwise, we are using Common JS.

This matches both the TypeScript and Node logic for determining whether a file should be treated as CommonJS or ESM.

This needs to be done for each barrel file, as we cannot safely assume that all barrels are part of the same package, for example somebody may be using barrelsby in a monorepo containing multiple projects.

Overriding the module type

We can add an additional configuration option to barrelsby to allow the module type to be manually specified.

  • If --input-type is not provided, then the default behaviour will be to use the detection logic described above.
  • --input-type=commonjs will force Barrelsby to create CommonJS modules.
  • --input-type=module will force Barrelsby to create ES modules.

This argument aligns with how node have named their argument: https://nodejs.org/api/packages.html#--input-type-flag

It could be nice to align with how TypeScript names their argument, however TypeScript allows many more values that are probably overkill for what we want to support.
https://www.typescriptlang.org/tsconfig#module

Support for new file extensions

Barrelsby should be updated so that it also considers .mts and .cts files to be TypeScript files, and will include them when working out which files to add to barrels.

Transformation of file extensions

Barrelsby should perform the following manipulation of import paths when writing import statements in barrel files:

File extension CommonJS Modification ESM Modification
.ts Extension removed Extension replaced with .js
.tsx Extension removed Extension replaced with .js
.mts Extension removed Extension replaced with .mjs
.cts Extension removed Extension replaced with .cjs

For CommonJS modules, the extensions could optionally be preserved, however this would represent a change to existing barrelsby output so is perhaps best left for another time.

While barrelsby does not currently support arbitrary file extensions, it could do so in the future - A possible example might be .json or .css files. In any code that processes import paths it would be great to leave any unknown file extensions in place to support this in the future.

Reading material:

Please let me know if you have any feedback/corrections/suggestions 😄

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions