Skip to content

Commit

Permalink
- upgrade dependencies
Browse files Browse the repository at this point in the history
- build as esm module project
- add support to es2022
- add CI pipelines
  • Loading branch information
t83714 committed Dec 23, 2023
1 parent 66ae54d commit 720b046
Show file tree
Hide file tree
Showing 12 changed files with 4,460 additions and 3,426 deletions.
40 changes: 21 additions & 19 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -1,25 +1,27 @@
{
"extends": ["eslint:recommended"],
"env": {
"browser": true,
"node": true,
"es6": true,
"mocha": true
},
"parser": "babel-eslint",
"extends": [
"plugin:@typescript-eslint/recommended",
"plugin:prettier/recommended"
],
"plugins": ["@typescript-eslint", "prettier"],
"rules": {
"comma-dangle": "off",
"no-extra-semi": "off",
"prettier/prettier": "error"
},
"parserOptions": {
"ecmaVersion": 2018,
"sourceType": "module",
"ecmaFeatures": {
"jsx": true
}
"no-use-before-define": "off",
"prettier/prettier": "error",
"no-var": "warn",
"prefer-const": "warn",
"no-unused-expressions": "off",
"@typescript-eslint/no-non-null-assertion": 0,
"@typescript-eslint/explicit-function-return-type": 0,
"@typescript-eslint/no-use-before-define": 0,
"@typescript-eslint/no-var-requires": "warn",
"@typescript-eslint/no-this-alias": "warn",
"@typescript-eslint/no-inferrable-types": "off",
"@typescript-eslint/ban-types": "warn",
"@typescript-eslint/no-empty-function": "warn",
"@typescript-eslint/ban-ts-comment": "warn",
"@typescript-eslint/no-explicit-any": "off"
},
"plugins": ["prettier"],
"parser": "@typescript-eslint/parser",
"settings": {
"react": {
"version": "detect"
Expand Down
33 changes: 33 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
name: Main CI Workflow

on: push


jobs:
build-test-eslint-node-18:
name: Build, Test & Eslint
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Use Node.js 18
uses: actions/setup-node@v3
with:
node-version: 18
- run: yarn install
- run: yarn build
- run: yarn test
- run: yarn eslint

build-test-eslint-node-20:
name: Build, Test & Eslint
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Use Node.js 20
uses: actions/setup-node@v3
with:
node-version: 20
- run: yarn install
- run: yarn build
- run: yarn test
- run: yarn eslint
88 changes: 52 additions & 36 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,15 @@
"name": "ts-module-alias-transformer",
"version": "1.0.4",
"description": "A cli tool uses Babel to rewrite module name from typescript's build result including .d.ts files.",
"type": "module",
"exports": "./dist/index.js",
"engines": {
"node": ">=18.0.0"
},
"scripts": {
"run": "node ./bin/index.js",
"eslint": "eslint ./src/**/*.ts",
"test": "mocha --require ts-node/register \"./src/test/**/*.spec.ts\" ",
"test": "mocha",
"dev": "NODE_ENV=production;ts-node src/index.ts",
"prebuild": "rimraf lib",
"build": "webpack",
Expand All @@ -30,45 +35,56 @@
"url": "https://github.com/t83714/ts-module-alias-transformer/issues"
},
"homepage": "https://github.com/t83714/ts-module-alias-transformer#readme",
"dependencies": {},
"private": false,
"bin": {
"ts-module-alias-transformer": "./bin/index.js"
},
"devDependencies": {
"@types/babel__core": "^7.1.3",
"@types/chai": "^4.2.5",
"@types/chalk": "^2.2.0",
"@types/commander": "^2.12.2",
"@types/fs-extra": "^8.0.1",
"@types/lodash": "^4.14.149",
"@types/mocha": "^5.2.7",
"@types/node": "^12.12.14",
"@types/recursive-readdir": "^2.2.0",
"@types/slash": "^3.0.0",
"@types/tmp": "^0.1.0",
"babel-eslint": "^10.0.3",
"chai": "^4.2.0",
"eslint": "^6.7.2",
"eslint-plugin-prettier": "^3.1.1",
"mocha": "^6.2.2",
"prettier": "^1.19.1",
"rimraf": "^3.0.0",
"tmp": "^0.1.0",
"ts-loader": "^6.2.1",
"ts-node": "^8.5.4",
"typescript": "^3.7.2",
"webpack": "^4.41.2",
"webpack-cli": "^3.3.10",
"@babel/core": "^7.7.4",
"@babel/plugin-syntax-typescript": "^7.7.4",
"babel-plugin-module-resolver": "^3.2.0",
"chalk": "^3.0.0",
"commander": "^4.0.1",
"fs-extra": "^8.1.0",
"lodash": "^4.17.15",
"make-dir": "^3.0.0",
"recursive-readdir": "^2.2.2",
"slash": "^3.0.0"
"@babel/core": "^7.23.6",
"@babel/eslint-parser": "^7.23.3",
"@babel/plugin-syntax-typescript": "^7.23.3",
"@babel/plugin-transform-class-properties": "^7.23.3",
"@babel/preset-typescript": "^7.23.3",
"@types/babel__core": "^7.20.5",
"@types/chai": "^4.3.11",
"@types/fs-extra": "^11.0.4",
"@types/lodash": "^4.14.202",
"@types/mocha": "^10.0.6",
"@types/node": "^18.11.9",
"@types/recursive-readdir": "^2.2.4",
"@types/tmp": "^0.2.6",
"@typescript-eslint/eslint-plugin": "^6.15.0",
"@typescript-eslint/parser": "^6.15.0",
"babel-plugin-module-resolver": "^5.0.0",
"caller-path": "^4.0.0",
"chai": "^5.0.0-rc.0",
"chalk": "^5.3.0",
"commander": "^11.1.0",
"eslint": "^8.56.0",
"eslint-config-prettier": "^9.1.0",
"eslint-plugin-prettier": "^5.1.1",
"fs-extra": "^11.2.0",
"lodash": "^4.17.21",
"make-dir": "^4.0.0",
"mocha": "^10.2.0",
"prettier": "^3.1.1",
"recursive-readdir": "^2.2.3",
"rimraf": "^5.0.5",
"slash": "^5.1.0",
"tmp": "^0.2.1",
"ts-loader": "^9.5.1",
"ts-node": "^10.9.2",
"typescript": "^5.3.3",
"webpack": "^5.89.0",
"webpack-cli": "^5.1.4"
},
"mocha": {
"require": [
"ts-node/register"
],
"loader": "ts-node/esm",
"spec": [
"src/test/**/**.spec.ts"
]
}
}
File renamed without changes.
59 changes: 59 additions & 0 deletions src/esmUtils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import { fileURLToPath } from "node:url";
import { dirname } from "node:path";
import { argv } from "node:process";
import { createRequire } from "node:module";
import callerPath from "caller-path";

/**
* Allow to require modules relative to the caller file.
* require() is not support in ESM modules. This function provides a workaround when you have to use require() in ESM modules.
* e.g. use to load JSON files.
* Or use to load legacy commonjs modules where formal `import` syntax is not convenient.
* e.g. load legacy commonjs modules without type definition files.
*
* Please note: before use this function, you should try to use `import` syntax whenever possible.
*
* @param {string} id
* @return {*} {*}
*/
export function require(id: string): any {
const requireFrom = createRequire(callerPath({ depth: 1 }) as string);
return requireFrom(id);
}

export function requireResolve(id: string): string {
const requireFrom = createRequire(callerPath({ depth: 1 }) as string);
return requireFrom.resolve(id);
}

/**
* This is an ESM replacement for `__filename`.
*
* Use it like this: `__filename()`.
*/
export const __filename = (): string =>
fileURLToPath(callerPath({ depth: 1 }) as string);

/**
* This is an ESM replacement for `__dirname`.
*
* Use it like this: `__dirname()`.
*/
export const __dirname = (): string =>
dirname(fileURLToPath(callerPath({ depth: 1 }) as string));

/**
* Indicates that the script was run directly.
* This is an ESM replacement for `require.main === module`.
*
* Use it like this: `isMain()`.
*/
export const isMain = (): boolean => {
if (!argv[1]) return false;
const currentPath = callerPath({ depth: 1 }) as string;
const require = createRequire(currentPath);
const scriptPath = require.resolve(argv[1]);
// get file path of caller module path
const modulePath = fileURLToPath(currentPath);
return scriptPath === modulePath;
};
14 changes: 8 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import program from "commander";
import pkg from "../package.json";
import transform from "./transform";
import { program } from "commander";
import { require } from "./esmUtils.js";
import transform from "./transform.js";
import chalk from "chalk";

const pkg = require("../package.json");

program
.name("ts-module-alias-transformer")
.version(pkg.version)
Expand All @@ -14,9 +16,9 @@ program
.arguments("<src> [dst]")
.option(
"-p, --mappingConfigPath <packageJsonPath>",
"Optional. Specify the mapping config json file location. \n"+
"By default, it will read the `_moduleMappings` field of package.json in current working directory to determine the module paths to replace. \n"+
"See https://github.com/t83714/ts-module-alias-transformer for more details."
"Optional. Specify the mapping config json file location. \n" +
"By default, it will read the `_moduleMappings` field of package.json in current working directory to determine the module paths to replace. \n" +
"See https://github.com/t83714/ts-module-alias-transformer for more details."
)
.action(async (src, dst, program) => {
try {
Expand Down
27 changes: 18 additions & 9 deletions src/test/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@ import {} from "mocha";
import { expect } from "chai";
import childProcess from "child_process";
import recursiveReadDir from "recursive-readdir";
import { MappingConfigType } from "../transform";
import { MappingConfigType } from "../transform.js";
import tmp from "tmp";
import fs from "fs-extra";
import path from "path";
import _ from "lodash";
import { __dirname as getCurrentDirName, requireResolve } from "../esmUtils.js";

const __dirname = getCurrentDirName();

const DIR_CREATE_OPTIONS = {
unsafeCleanup: true
Expand Down Expand Up @@ -34,10 +37,7 @@ async function testDir(dirPath: string, mappingOptions: MappingConfigType) {
(filePath, stats) => {
if (!stats.isDirectory()) {
const fileName = path.basename(filePath);
const ext = fileName
.split(".")
.slice(1)
.join(".");
const ext = fileName.split(".").slice(1).join(".");
if (testFileExtList.indexOf(ext) !== -1) {
return false;
} else {
Expand All @@ -49,27 +49,27 @@ async function testDir(dirPath: string, mappingOptions: MappingConfigType) {
]);

testFileList = _.uniq(
testFileList.map(filePath => {
testFileList.map((filePath) => {
const fileName = path.basename(filePath);
const fileParts = fileName.split(".");
const basename = fileParts.slice(0, -2).join(".");
return path.join(path.dirname(filePath), basename);
})
);

const resultFileList = testFileList.map(filePath => {
const resultFileList = testFileList.map((filePath) => {
const basename = path.basename(filePath);
const targetFile = path.join(srcDir.name, basename);
fs.copyFileSync(`${filePath}.from.txt`, targetFile);
return [`${filePath}.to.txt`, path.join(dstDir.name, basename)];
});

const tsconfigPath = path.resolve("tsconfig.json");
const tsNodeExec = require.resolve("ts-node/dist/bin.js");
const tsNodeExec = requireResolve("ts-node/dist/bin.js");
const entryPoint = path.resolve(__dirname, "../index.ts");

childProcess.execSync(
`${tsNodeExec} ${entryPoint} -p ${configFile.name} ${srcDir.name} ${dstDir.name}`,
`${tsNodeExec} --esm ${entryPoint} -p ${configFile.name} ${srcDir.name} ${dstDir.name}`,
{
stdio: "inherit",
cwd: path.dirname(tsconfigPath),
Expand All @@ -95,6 +95,15 @@ async function testDir(dirPath: string, mappingOptions: MappingConfigType) {
describe("Test Process Test Files", async () => {
process.env.NODE_ENV = "production";

it("es2022-esm target code should be processed correctly", async () => {
await testDir(
path.resolve(__dirname, "./testFiles/es2022-esm"),
{
"magda-typescript-common/src": "@magda/typescript-common/dist"
}
);
}).timeout(30000);

it("es5 target code should be processed correctly", async () => {
await testDir(
path.resolve(__dirname, "./testFiles/es5"),
Expand Down
Loading

0 comments on commit 720b046

Please sign in to comment.