Skip to content

Commit

Permalink
Added import type replacing
Browse files Browse the repository at this point in the history
  • Loading branch information
xdan committed Jan 30, 2025
1 parent 30ae306 commit fc34322
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 0 deletions.
6 changes: 6 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ dts:
@$(NODE_MODULES_BIN)/replace "import .+.(less|svg)('|\");" '' ./build/types -r --include='*.d.ts' --silent

@$(TS_NODE_BASE) $(cwd)tools/utils/resolve-alias-imports.ts --rootDir=$(pwd) --cwd=./build/types --mode=dts --ver=$(version)
@make replace-import-types

@if [ -d ./build/esm ]; then \
echo "Copy types to esm folder ..."; \
Expand Down Expand Up @@ -323,3 +324,8 @@ append-config-types:
echo 'Resolve alias imports ...'
@$(TS_NODE_BASE) $(cwd)tools/utils/resolve-alias-imports.ts --rootDir=$(pwd) --cwd=$(pwd)/build/esm --filter=config.d.ts --mode=dts --ver=$(version)
cp ./build/esm/config.d.ts ./build/types/config.d.ts

.PHONY: replace-import-types
replace-import-types:
@echo 'Replace import types ...'
@$(TS_NODE_BASE) $(cwd)tools/utils/convert-imports-to-types.ts --cwd=./build/types
95 changes: 95 additions & 0 deletions tools/utils/convert-imports-to-types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
/*!
* Jodit Editor (https://xdsoft.net/jodit/)
* Released under MIT see LICENSE.txt in the project root for license information.
* Copyright (c) 2013-2025 Valeriy Chupurnov. All rights reserved. https://xdsoft.net
*/

import fs from 'node:fs';
import path from 'node:path';
import { hideBin } from 'yargs/helpers';
import yargs from 'yargs/yargs';

const argv = yargs(hideBin(process.argv))
.option('cwd', {
type: 'string',
demandOption: true,
description: 'Work directory'
})
.parseSync();

const cwd = path.resolve(argv.cwd);

convertImportsToTypes(cwd);

function convertImportsToTypes(dir: string): void {
fs.readdirSync(dir, {
withFileTypes: true
}).forEach((file): void => {
const sourcePath = path.join(dir, file.name);

if (file.isDirectory()) {
return convertImportsToTypes(sourcePath);
}

const content = fs.readFileSync(sourcePath, 'utf-8');

const newContent = [
replaceNamedImport,
replaceDefaultImport,
replaceStarImport,
replaceStarExport,
replaceNamedExport
].reduce((acc, fn) => fn(acc), content);

if (newContent === content) {
return;
}

fs.writeFileSync(sourcePath, newContent);
});
}

function replaceNamedImport(content: string): string {
const regImport = /import \{([^}]+)} from (['"])(.*)\2;/g;
if (!regImport.test(content)) {
return content;
}

return content.replace(regImport, 'import type { $1 } from $2$3$2;');
}

function replaceDefaultImport(content: string): string {
const regImport = /import (\w+) from (['"])(.*)\2;/g;
if (!regImport.test(content)) {
return content;
}

return content.replace(regImport, 'import type $1 from $2$3$2;');
}

function replaceStarImport(content: string): string {
const regImport = /import \* as (\w+) from (['"])(.*)\2;/g;
if (!regImport.test(content)) {
return content;
}

return content.replace(regImport, 'import type * as $1 from $2$3$2;');
}

function replaceStarExport(content: string): string {
const reg = /export \* from (['"])(.*)\1;/g;
if (!reg.test(content)) {
return content;
}

return content.replace(reg, 'export type * from $1$2$1;');
}

function replaceNamedExport(content: string): string {
const reg = /export \{([^}]+)} from (['"])(.*)\2;/g;
if (!reg.test(content)) {
return content;
}

return content.replace(reg, 'export type {$1} from $2$3$2;');
}

0 comments on commit fc34322

Please sign in to comment.