-
Notifications
You must be signed in to change notification settings - Fork 49
Description
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.jsonfile. - 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.jsonfile 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-typeis not provided, then the default behaviour will be to use the detection logic described above. --input-type=commonjswill force Barrelsby to create CommonJS modules.--input-type=modulewill 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:
- https://nodejs.org/api/packages.html#--input-type-flag
- https://nodejs.org/api/packages.html#packages_package_json_and_file_extensions
- https://www.typescriptlang.org/docs/handbook/release-notes/typescript-4-5.html#ecmascript-module-support-in-nodejs
- https://www.typescriptlang.org/docs/handbook/release-notes/typescript-4-5.html#new-file-extensions
Please let me know if you have any feedback/corrections/suggestions 😄