From 02593fd02d2302c09b85664ed077c7499ee2b092 Mon Sep 17 00:00:00 2001 From: ByungJoon Lee Date: Mon, 19 Aug 2024 02:02:55 +0900 Subject: [PATCH 1/2] fix(#153): fixed the issue with @ctix-declaration statement generation - fixed the issue with @ctix-declaration statement generation - fixed the problem where the fileExt option was not working --- src/modules/commands/bundling.ts | 30 +---- ...get.inline.declaration.render.data.test.ts | 112 ++++++++++++++++++ .../modules/getInlineDeclarationRenderData.ts | 40 +++++++ .../templates/declarationFileTemplate.ts | 2 +- 4 files changed, 155 insertions(+), 29 deletions(-) create mode 100644 src/templates/modules/__tests__/get.inline.declaration.render.data.test.ts create mode 100644 src/templates/modules/getInlineDeclarationRenderData.ts diff --git a/src/modules/commands/bundling.ts b/src/modules/commands/bundling.ts index ba79cd8..d3cbc6f 100644 --- a/src/modules/commands/bundling.ts +++ b/src/modules/commands/bundling.ts @@ -16,9 +16,7 @@ import { ProjectContainer } from '#/modules/file/ProjectContainer'; import { checkOutputFile } from '#/modules/file/checkOutputFile'; import { getTsExcludeFiles } from '#/modules/file/getTsExcludeFiles'; import { getTsIncludeFiles } from '#/modules/file/getTsIncludeFiles'; -import { addCurrentDirPrefix } from '#/modules/path/addCurrentDirPrefix'; import { posixJoin } from '#/modules/path/modules/posixJoin'; -import { posixRelative } from '#/modules/path/modules/posixRelative'; import { posixResolve } from '#/modules/path/modules/posixResolve'; import { ExcludeContainer } from '#/modules/scope/ExcludeContainer'; import { IncludeContainer } from '#/modules/scope/IncludeContainer'; @@ -28,12 +26,11 @@ import { CE_TEMPLATE_NAME } from '#/templates/const-enum/CE_TEMPLATE_NAME'; import type { IIndexFileWriteParams } from '#/templates/interfaces/IIndexFileWriteParams'; import type { IIndexRenderData } from '#/templates/interfaces/IIndexRenderData'; import { TemplateContainer } from '#/templates/modules/TemplateContainer'; +import { getInlineDeclarationRenderData } from '#/templates/modules/getInlineDeclarationRenderData'; import { getRenderData } from '#/templates/modules/getRenderData'; import { getSelectStyle } from '#/templates/modules/getSelectStyle'; import chalk from 'chalk'; import dayjs from 'dayjs'; -import { replaceSepToPosix } from 'my-node-fp'; -import path from 'node:path'; import type * as tsm from 'ts-morph'; export async function bundling(buildOptions: TCommandBuildOptions, bundleOption: TBundleOptions) { @@ -194,30 +191,7 @@ export async function bundling(buildOptions: TCommandBuildOptions, bundleOption: CE_TEMPLATE_NAME.DECLARATION_FILE_TEMPLATE, { options: { quote: bundleOption.quote }, - declarations: await Promise.all( - inlineDeclarations - .filter((inlineDeclaration) => statementMap.get(inlineDeclaration.filePath) == null) - .map((declaration) => { - const relativePath = - bundleOption.output != null - ? addCurrentDirPrefix( - posixRelative( - bundleOption.output, - path.join( - path.dirname(declaration.filePath), - path.basename(declaration.filePath, path.extname(declaration.filePath)), - ), - ), - ) - : replaceSepToPosix( - `.${path.posix.sep}${path.join( - path.dirname(declaration.filePath), - path.basename(declaration.filePath, path.extname(declaration.filePath)), - )}`, - ); - return { ...declaration, relativePath }; - }), - ), + declarations: getInlineDeclarationRenderData(inlineDeclarations, bundleOption), }, ); diff --git a/src/templates/modules/__tests__/get.inline.declaration.render.data.test.ts b/src/templates/modules/__tests__/get.inline.declaration.render.data.test.ts new file mode 100644 index 0000000..d368eb9 --- /dev/null +++ b/src/templates/modules/__tests__/get.inline.declaration.render.data.test.ts @@ -0,0 +1,112 @@ +import { CE_INLINE_COMMENT_KEYWORD } from '#/comments/const-enum/CE_INLINE_COMMENT_KEYWORD'; +import type { getInlineCommentedFiles } from '#/comments/getInlineCommentedFiles'; +import { getInlineDeclarationRenderData } from '#/templates/modules/getInlineDeclarationRenderData'; +import pathe from 'pathe'; +import { describe, expect, it } from 'vitest'; + +describe('getInlineDeclarationRenderData', () => { + it('successfully generate render data, with output directory', () => { + const declarations: ReturnType = [ + { + filePath: pathe.join(process.cwd(), '/a/b/c/d/a.ts'), + commentCode: `// ${CE_INLINE_COMMENT_KEYWORD.FILE_DECLARATION_KEYWORD}`, + tag: CE_INLINE_COMMENT_KEYWORD.FILE_DECLARATION_KEYWORD, + pos: { + line: 0, + column: 0, + start: 0, + }, + workspaces: undefined, + }, + { + filePath: pathe.join(process.cwd(), '/a/b/c/e/b.ts'), + commentCode: `// ${CE_INLINE_COMMENT_KEYWORD.FILE_DECLARATION_KEYWORD}`, + tag: CE_INLINE_COMMENT_KEYWORD.FILE_DECLARATION_KEYWORD, + pos: { + line: 0, + column: 0, + start: 0, + }, + workspaces: undefined, + }, + ]; + + const data = getInlineDeclarationRenderData(declarations, { + output: pathe.join(process.cwd(), '/a/b/e'), + fileExt: 'to-js', + }); + + expect(data).toMatchObject([ + { + filePath: pathe.join(process.cwd(), '/a/b/c/d/a.ts'), + commentCode: '// @ctix-declaration', + tag: '@ctix-declaration', + pos: { line: 0, column: 0, start: 0 }, + workspaces: undefined, + relativePath: '../c/d/a.ts', + extname: { origin: '.ts', render: '.js' }, + }, + { + filePath: pathe.join(process.cwd(), '/a/b/c/e/b.ts'), + commentCode: '// @ctix-declaration', + tag: '@ctix-declaration', + pos: { line: 0, column: 0, start: 0 }, + workspaces: undefined, + relativePath: '../c/e/b.ts', + extname: { origin: '.ts', render: '.js' }, + }, + ]); + }); + + it('successfully generate render data, without output directory', () => { + const declarations: ReturnType = [ + { + filePath: pathe.join(process.cwd(), '/a/b/c/d/a.ts'), + commentCode: `// ${CE_INLINE_COMMENT_KEYWORD.FILE_DECLARATION_KEYWORD}`, + tag: CE_INLINE_COMMENT_KEYWORD.FILE_DECLARATION_KEYWORD, + pos: { + line: 0, + column: 0, + start: 0, + }, + workspaces: undefined, + }, + { + filePath: pathe.join(process.cwd(), '/a/b/c/e/b.ts'), + commentCode: `// ${CE_INLINE_COMMENT_KEYWORD.FILE_DECLARATION_KEYWORD}`, + tag: CE_INLINE_COMMENT_KEYWORD.FILE_DECLARATION_KEYWORD, + pos: { + line: 0, + column: 0, + start: 0, + }, + workspaces: undefined, + }, + ]; + + const data = getInlineDeclarationRenderData(declarations, { + fileExt: 'to-js', + }); + + expect(data).toMatchObject([ + { + filePath: pathe.join(process.cwd(), '/a/b/c/d/a.ts'), + commentCode: '// @ctix-declaration', + tag: '@ctix-declaration', + pos: { line: 0, column: 0, start: 0 }, + workspaces: undefined, + relativePath: `./${pathe.join(process.cwd(), '/a/b/c/d/a.ts')}`, + extname: { origin: '.ts', render: '.js' }, + }, + { + filePath: pathe.join(process.cwd(), '/a/b/c/e/b.ts'), + commentCode: '// @ctix-declaration', + tag: '@ctix-declaration', + pos: { line: 0, column: 0, start: 0 }, + workspaces: undefined, + relativePath: `./${pathe.join(process.cwd(), '/a/b/c/e/b.ts')}`, + extname: { origin: '.ts', render: '.js' }, + }, + ]); + }); +}); diff --git a/src/templates/modules/getInlineDeclarationRenderData.ts b/src/templates/modules/getInlineDeclarationRenderData.ts new file mode 100644 index 0000000..c61988c --- /dev/null +++ b/src/templates/modules/getInlineDeclarationRenderData.ts @@ -0,0 +1,40 @@ +import type { getInlineCommentedFiles } from '#/comments/getInlineCommentedFiles'; +import type { TBundleOptions } from '#/configs/interfaces/TBundleOptions'; +import { addCurrentDirPrefix } from '#/modules/path/addCurrentDirPrefix'; +import { getExtname } from '#/modules/path/getExtname'; +import { getImportStatementExtname } from '#/modules/path/getImportStatementExtname'; +import { posixRelative } from '#/modules/path/modules/posixRelative'; +import { replaceSepToPosix } from 'my-node-fp'; +import path from 'path'; +import pathe from 'pathe'; +import type { SetOptional } from 'type-fest'; + +export function getInlineDeclarationRenderData( + declarations: ReturnType, + options: SetOptional, 'output'>, +) { + const renderDatas = declarations.map((declaration) => { + const extname = getExtname(declaration.filePath); + const renderExtname = getImportStatementExtname(options.fileExt, extname); + const dirname = pathe.dirname(declaration.filePath); + const basename = pathe.basename(declaration.filePath, extname); + + const relativePath = + options.output != null + ? addCurrentDirPrefix( + posixRelative(options.output, pathe.join(dirname, `${basename}${extname}`)), + ) + : replaceSepToPosix(`.${path.posix.sep}${pathe.join(dirname, `${basename}${extname}`)}`); + + return { + ...declaration, + relativePath, + extname: { + origin: extname, + render: renderExtname, + }, + }; + }); + + return renderDatas; +} diff --git a/src/templates/templates/declarationFileTemplate.ts b/src/templates/templates/declarationFileTemplate.ts index 1c63d9b..25b444d 100644 --- a/src/templates/templates/declarationFileTemplate.ts +++ b/src/templates/templates/declarationFileTemplate.ts @@ -1,6 +1,6 @@ export const declarationFileTemplate = ` <%- it.declarations.forEach((declaration) => { -%> -import <%-= it.options.quote %><%= declaration.relativePath %><%= it.options.quote -%> +import <%-= it.options.quote %><%= declaration.relativePath %><%= declaration.extname.render %><%= it.options.quote -%> <%- }) %> `; From c19c68d3b482256a6bb2b2e9af2e96e220e687ed Mon Sep 17 00:00:00 2001 From: ByungJoon Lee Date: Mon, 19 Aug 2024 16:22:48 +0900 Subject: [PATCH 2/2] fix: changed to have exclude options take precedence over comment directives --- src/modules/commands/bundling.ts | 16 +++++++++------- .../get.inline.declaration.render.data.test.ts | 8 ++++---- .../modules/getInlineDeclarationRenderData.ts | 6 ++---- 3 files changed, 15 insertions(+), 15 deletions(-) diff --git a/src/modules/commands/bundling.ts b/src/modules/commands/bundling.ts index d3cbc6f..066d47d 100644 --- a/src/modules/commands/bundling.ts +++ b/src/modules/commands/bundling.ts @@ -85,14 +85,16 @@ export async function bundling(buildOptions: TCommandBuildOptions, bundleOption: project, filePaths, keyword: CE_INLINE_COMMENT_KEYWORD.FILE_DECLARATION_KEYWORD, - }).filter((declaration) => { - const sourceFile = project.getSourceFile(declaration.filePath); - if (sourceFile == null) { - return false; - } + }) + .filter((declaration) => !exclude.isExclude(declaration.filePath)) + .filter((declaration) => { + const sourceFile = project.getSourceFile(declaration.filePath); + if (sourceFile == null) { + return false; + } - return isDeclarationFile(sourceFile); - }); + return isDeclarationFile(sourceFile); + }); const filenames = filePaths .filter((filename) => include.isInclude(filename)) diff --git a/src/templates/modules/__tests__/get.inline.declaration.render.data.test.ts b/src/templates/modules/__tests__/get.inline.declaration.render.data.test.ts index d368eb9..a726ef2 100644 --- a/src/templates/modules/__tests__/get.inline.declaration.render.data.test.ts +++ b/src/templates/modules/__tests__/get.inline.declaration.render.data.test.ts @@ -43,7 +43,7 @@ describe('getInlineDeclarationRenderData', () => { tag: '@ctix-declaration', pos: { line: 0, column: 0, start: 0 }, workspaces: undefined, - relativePath: '../c/d/a.ts', + relativePath: '../c/d/a', extname: { origin: '.ts', render: '.js' }, }, { @@ -52,7 +52,7 @@ describe('getInlineDeclarationRenderData', () => { tag: '@ctix-declaration', pos: { line: 0, column: 0, start: 0 }, workspaces: undefined, - relativePath: '../c/e/b.ts', + relativePath: '../c/e/b', extname: { origin: '.ts', render: '.js' }, }, ]); @@ -95,7 +95,7 @@ describe('getInlineDeclarationRenderData', () => { tag: '@ctix-declaration', pos: { line: 0, column: 0, start: 0 }, workspaces: undefined, - relativePath: `./${pathe.join(process.cwd(), '/a/b/c/d/a.ts')}`, + relativePath: `./${pathe.join(process.cwd(), '/a/b/c/d/a')}`, extname: { origin: '.ts', render: '.js' }, }, { @@ -104,7 +104,7 @@ describe('getInlineDeclarationRenderData', () => { tag: '@ctix-declaration', pos: { line: 0, column: 0, start: 0 }, workspaces: undefined, - relativePath: `./${pathe.join(process.cwd(), '/a/b/c/e/b.ts')}`, + relativePath: `./${pathe.join(process.cwd(), '/a/b/c/e/b')}`, extname: { origin: '.ts', render: '.js' }, }, ]); diff --git a/src/templates/modules/getInlineDeclarationRenderData.ts b/src/templates/modules/getInlineDeclarationRenderData.ts index c61988c..91f5b2d 100644 --- a/src/templates/modules/getInlineDeclarationRenderData.ts +++ b/src/templates/modules/getInlineDeclarationRenderData.ts @@ -21,10 +21,8 @@ export function getInlineDeclarationRenderData( const relativePath = options.output != null - ? addCurrentDirPrefix( - posixRelative(options.output, pathe.join(dirname, `${basename}${extname}`)), - ) - : replaceSepToPosix(`.${path.posix.sep}${pathe.join(dirname, `${basename}${extname}`)}`); + ? addCurrentDirPrefix(posixRelative(options.output, pathe.join(dirname, basename))) + : replaceSepToPosix(`.${path.posix.sep}${pathe.join(dirname, basename)}`); return { ...declaration,