ts-ext-fix
Fixing import extensions for ES6 purpose on javascript files after being generated by typescript.
Usage
Install:
npm install ts-ext-fix
Within Javascript
import {recompile} from "ts-ext-fix";
const rootFolderToJS = "./js";
const baseDirectory = "./";
const filter = "js";
recompile(rootFolder, filter, baseDirectory);
The above function will iterate through all files metnioned in filter option and convert import ... from "someFile"
to import ... from "someFile.js"
(or whatever the correct extension value is).
The function ensures gathers the correct extension values from the files that was created, and only if it could find the proper file within "rootFolder" or it's subdirectories will the extensions be added.
Please note that filter option can be an array of strings or a string value.
If no rootFolder is specified ./ is used.
Folder resolution occurs through:
- path.resolve(rootFolder, fromValue).
- If value is a proper file within rootFolder (or it's subfolders) the extension of the file will be added.
- If not: path.resolve(baseFolder, fromValue).
- If value is a proper file within rootFolder (or it's subfolders) the extension of the file will be added.
The same can also be called via cli
extfix myroot/folder/location --filter js --f=jsx --base my/base/folder
will result in filters being an array of [js, jsx], wil baseDirectory of my/base/folder and rootDirectory of myroot/folder/location.
Options
name | type | cli | alias | default | description |
---|---|---|---|---|---|
root | string | - | - | ./ | The root folder where all javascript files can be found. It is first value in array in cli. |
base | string | --base | -b | ./ | The base address where files are referenced from. Mostly used when files are refered to with / instead of ./. |
filters | string / string[] | --filter | -f | [ 'js' ] | Filter the files to fix in javascript files. |
Note
Unfortunately I could not spend enough time on this project to do proper testing. But this solution is working for my current project and if someone else needs something similar, feel free to use.
Contributions are welcome.
Ideas: expand files to include for extension fixes to include all files from base directory, but only change files within rootDirectory (i.e. a file in root directory imports file outside of rootDirectory, but inside of baseDirectory.. this can be fixed). However we will ahve to exclude node_modules folder in that case to save processing time.