Skip to content

Commit

Permalink
feat(compiler): enhance support for declare statements
Browse files Browse the repository at this point in the history
  - fix #145
  - support declare statements even when other statements are present in the file
    - except when there are export statements
  - change peer dependencies version
  • Loading branch information
imjuni committed Aug 12, 2024
1 parent 8159292 commit a6a82b7
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 25 deletions.
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -151,9 +151,9 @@
"yargs": "^17.7.2"
},
"peerDependencies": {
"prettier": "^3",
"prettier-plugin-organize-imports": "^3",
"typescript": "^5"
"prettier": ">=3",
"prettier-plugin-organize-imports": ">=3",
"typescript": ">=5"
},
"lint-staged": {
"*.{js,jsx,ts,tsx}": [
Expand Down
1 change: 0 additions & 1 deletion src/compilers/isDeclaration.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import * as tsm from 'ts-morph';

const declarationKindMap: Map<tsm.SyntaxKind, boolean> = new Map([
[tsm.SyntaxKind.ImportDeclaration, true],
[tsm.SyntaxKind.ModuleDeclaration, true],
]);

Expand Down
2 changes: 1 addition & 1 deletion src/compilers/isDeclarationFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@ export function isDeclarationFile(sourceFile: tsm.SourceFile) {
};
});

return statements.every((statement) => statement.isDeclaration);
return statements.some((statement) => statement.isDeclaration);
}
42 changes: 22 additions & 20 deletions src/modules/commands/bundling.ts
Original file line number Diff line number Diff line change
Expand Up @@ -190,31 +190,33 @@ export async function bundling(buildOptions: TCommandBuildOptions, bundleOption:
return { renderData, ...style };
});

const importsRendered = await TemplateContainer.evaluate(
const inlineDeclarationsRendered = await TemplateContainer.evaluate(
CE_TEMPLATE_NAME.DECLARATION_FILE_TEMPLATE,
{
options: { quote: bundleOption.quote },
declarations: await Promise.all(
inlineDeclarations.map((declaration) => {
const relativePath =
bundleOption.output != null
? addCurrentDirPrefix(
posixRelative(
bundleOption.output,
path.join(
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)),
),
),
)
: replaceSepToPosix(
`.${path.posix.sep}${path.join(
path.dirname(declaration.filePath),
path.basename(declaration.filePath, path.extname(declaration.filePath)),
)}`,
);
return { ...declaration, relativePath };
}),
)}`,
);
return { ...declaration, relativePath };
}),
),
},
);
Expand All @@ -232,7 +234,7 @@ export async function bundling(buildOptions: TCommandBuildOptions, bundleOption:
Spinner.it.start('output file exists check, ...');

const outputMap = new Map<string, string>();
outputMap.set(output, [importsRendered, ...exportsRendered].join('\n'));
outputMap.set(output, [inlineDeclarationsRendered, ...exportsRendered].join('\n'));
const fileExistReason = await checkOutputFile(outputMap);

if (!bundleOption.overwrite && !bundleOption.backup && fileExistReason.length > 0) {
Expand Down

0 comments on commit a6a82b7

Please sign in to comment.