reuse import util for both tsconfig and test runner

This commit is contained in:
Mads Rasmussen
2024-02-11 22:16:03 +01:00
parent 125151d57b
commit cd66852eef
5 changed files with 139 additions and 99 deletions

View File

@@ -2,7 +2,7 @@ import { packageJsonExports, packageJsonName } from '../package/index.js';
export const createImportMap = (args) => {
const imports = {
...args.defaultImports,
...args.additionalImports,
};
// Iterate over the exports in package.json
@@ -12,7 +12,7 @@ export const createImportMap = (args) => {
const moduleName = key.replace(/^\.\//, '');
// replace ./dist-cms with src and remove /index.js
const modulePath = value.replace(/^\.\/dist-cms/, './src').replace('.js', '.ts');
const modulePath = value.replace(/^\.\/dist-cms/, args.rootDir).replace('.js', '.ts');
const importAlias = `${packageJsonName}/${moduleName}`;
imports[importAlias] = modulePath;

View File

@@ -1,6 +1,6 @@
import { writeFileSync } from 'fs';
import { format } from 'prettier';
import { packageJsonName, packageJsonExports } from '../package/index.js';
import { createImportMap } from '../importmap/index.js';
const tsconfigPath = 'tsconfig.json';
const tsconfigComment = `// Don't edit this file directly. It is generated by /devops/tsconfig/index.js\n\n`;
@@ -37,22 +37,18 @@ const tsConfigBase = {
],
};
const paths = {
'@umbraco-cms/internal/test-utils': ['utils/test-utils.ts'],
};
const importmap = createImportMap({
rootDir: './src',
additionalImports: {
'@umbraco-cms/internal/test-utils': './utils/test-utils.ts',
},
});
// Iterate over the exports in package.json
for (const [key, value] of Object.entries(packageJsonExports || {})) {
// remove leading ./
if (value) {
const moduleName = key.replace(/^\.\//, '');
const paths = {};
// replace ./dist-cms with src and remove /index.js
const modulePath = value.replace(/^\.\/dist-cms/, 'src').replace(/\/index.js$/, '');
const importAlias = `${packageJsonName}/${moduleName}`;
paths[importAlias] = [modulePath];
}
for (const [key, value] of Object.entries(importmap.imports)) {
const valueAsArray = [value];
paths[key] = valueAsArray;
}
tsConfigBase.compilerOptions.paths = paths;