Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hotfix: Expose global types #2442

Merged
merged 10 commits into from
Oct 11, 2024
25 changes: 25 additions & 0 deletions devops/build/global-types.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { createImportMap } from '../importmap/index.js';
import { writeFileWithDir } from '../utils/index.js';

const tsPath = './dist-cms/packages/extension-types/index.d.ts';

const importmap = createImportMap({
rootDir: './src',
replaceModuleExtensions: true,
});

const paths = Object.keys(importmap.imports);

const content = `
${paths.map((path) => `import '${path}';`).join('\n')}
`;

writeFileWithDir(tsPath, content, (err) => {
if (err) {
// eslint-disable-next-line no-undef
console.log(err);
}

// eslint-disable-next-line no-undef
console.log(`global-types file generated`);
});
4 changes: 2 additions & 2 deletions devops/importmap/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ export const createImportMap = (args) => {
// Iterate over the exports in package.json
for (const [key, value] of Object.entries(packageJsonExports || {})) {
// remove leading ./
if (value) {
if (value && value.endsWith('.js')) {
const moduleName = key.replace(/^\.\//, '');

// replace ./dist-cms with src and remove /index.js
let modulePath = value;
if (typeof args.rootDir !== 'undefined') modulePath = modulePath.replace(/^\.\/dist-cms/, args.rootDir);
if (args.replaceModuleExtensions) modulePath = modulePath.replace('.js', '.ts');
console.log('replacing', value, 'with', modulePath)
console.log('replacing', value, 'with', modulePath);
const importAlias = `${packageJsonName}/${moduleName}`;

imports[importAlias] = modulePath;
Expand Down
13 changes: 13 additions & 0 deletions devops/utils/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { writeFile, mkdir } from 'fs';
import * as pathModule from 'path';

const path = pathModule.default;
const getDirName = path.dirname;

export const writeFileWithDir = (path, contents, cb) => {
mkdir(getDirName(path), { recursive: true }, function (err) {
if (err) return cb(err);

writeFile(path, contents, cb);
});
};
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"./element-api": "./dist-cms/libs/element-api/index.js",
"./embedded-media": "./dist-cms/packages/embedded-media/index.js",
"./extension-api": "./dist-cms/libs/extension-api/index.js",
"./extension-types": "./dist-cms/packages/extension-types/index.d.ts",
"./formatting-api": "./dist-cms/libs/formatting-api/index.js",
"./localization-api": "./dist-cms/libs/localization-api/index.js",
"./observable-api": "./dist-cms/libs/observable-api/index.js",
Expand Down Expand Up @@ -152,7 +153,7 @@
"build:vite": "tsc && vite build --mode staging",
"build:workspaces": "npm run build -ws --if-present",
"build": "tsc --project ./src/tsconfig.build.json",
"postbuild": "rollup -c ./src/rollup.config.js",
"postbuild": "rollup -c ./src/rollup.config.js && node ./devops/build/global-types.js",
"check": "npm run lint:errors && npm run compile && npm run build-storybook && npm run generate:jsonschema:dist",
"check:paths": "node ./devops/build/check-path-length.js dist-cms 120",
"check:circular": "madge --circular --warning --extensions ts ./src",
Expand Down
Loading