Skip to content

Commit 5214cce

Browse files
committed
feat: generate types on build, start
1 parent 978f7c7 commit 5214cce

File tree

2 files changed

+54
-66
lines changed

2 files changed

+54
-66
lines changed

src/commands/build/web/index.ts

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,28 @@ import getWidgetName from '../../../utils/getWidgetName';
1212
import getWidgetPackageJson from '../../../utils/getWidgetPackageJson';
1313
import getMendixWidgetDirectory from '../../../utils/getMendixWidgetDirectory';
1414
import getViteUserConfiguration from '../../../utils/getViteUserConfiguration';
15+
import { generateTypesFromFile } from '../../../type-generator';
1516

1617
const buildWebCommand = async (isProduction: boolean = false) => {
17-
// try {
18+
try {
19+
showMessage('Generate types');
20+
21+
const widgetName = await getWidgetName();
22+
const originWidgetXmlPath = path.join(PROJECT_DIRECTORY, `src/${widgetName}.xml`);
23+
const typingsPath = path.join(PROJECT_DIRECTORY, 'typings');
24+
const typingsDirExists = await pathIsExists(typingsPath);
25+
26+
if (typingsDirExists) {
27+
await fs.rm(typingsPath, { recursive: true, force: true });
28+
}
29+
30+
await fs.mkdir(typingsPath);
31+
32+
const newTypingsFilePath = path.join(typingsPath, `${widgetName}Props.d.ts`);
33+
const typingContents = await generateTypesFromFile(originWidgetXmlPath, 'web');
34+
35+
await fs.writeFile(newTypingsFilePath, typingContents);
36+
1837
showMessage('Remove previous builds');
1938

2039
const distDir = path.join(PROJECT_DIRECTORY, DIST_DIRECTORY_NAME);
@@ -46,10 +65,8 @@ const buildWebCommand = async (isProduction: boolean = false) => {
4665
resultViteConfig = await getViteDefaultConfig(false);
4766
}
4867

49-
const widgetName = await getWidgetName();
5068
const originPackageXmlPath = path.join(PROJECT_DIRECTORY, 'src/package.xml');
5169
const destPackageXmlPath = path.join(WEB_OUTPUT_DIRECTORY, 'package.xml');
52-
const originWidgetXmlPath = path.join(PROJECT_DIRECTORY, `src/${widgetName}.xml`);
5370
const destWidgetXmlPath = path.join(WEB_OUTPUT_DIRECTORY, `${widgetName}.xml`);
5471

5572
await fs.copyFile(originPackageXmlPath, destPackageXmlPath);
@@ -94,9 +111,9 @@ const buildWebCommand = async (isProduction: boolean = false) => {
94111
await fs.copyFile(mpkFileDestPath, mendixMpkFileDestPath);
95112

96113
showMessage(`${COLOR_GREEN('Build complete.')}`);
97-
// } catch (error) {
98-
// showMessage(`${COLOR_ERROR('Build failed.')}\nError occurred: ${COLOR_ERROR((error as Error).stack)}`);
99-
// }
114+
} catch (error) {
115+
showMessage(`${COLOR_ERROR('Build failed.')}\nError occurred: ${COLOR_ERROR((error as Error).stack)}`);
116+
}
100117
};
101118

102119
export default buildWebCommand;

src/commands/start/web/index.ts

Lines changed: 31 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,32 @@ import pathIsExists from "../../../utils/pathIsExists";
1010
import { getViteDefaultConfig } from '../../../configurations/vite';
1111
import getWidgetName from '../../../utils/getWidgetName';
1212
import getViteUserConfiguration from '../../../utils/getViteUserConfiguration';
13+
import { generateTypesFromFile } from '../../../type-generator';
14+
15+
const generateTyping = async () => {
16+
const widgetName = await getWidgetName();
17+
const originWidgetXmlPath = path.join(PROJECT_DIRECTORY, `src/${widgetName}.xml`);
18+
const typingsPath = path.join(PROJECT_DIRECTORY, 'typings');
19+
const typingsDirExists = await pathIsExists(typingsPath);
20+
21+
if (typingsDirExists) {
22+
await fs.rm(typingsPath, { recursive: true, force: true });
23+
}
24+
25+
await fs.mkdir(typingsPath);
26+
27+
const newTypingsFilePath = path.join(typingsPath, `${widgetName}Props.d.ts`);
28+
const typingContents = await generateTypesFromFile(originWidgetXmlPath, 'web');
29+
30+
await fs.writeFile(newTypingsFilePath, typingContents);
31+
};
1332

1433
const startWebCommand = async () => {
1534
try {
1635
showMessage('Start widget server');
1736

37+
await generateTyping();
38+
1839
const customViteConfigPath = path.join(PROJECT_DIRECTORY, VITE_CONFIGURATION_FILENAME);
1940
const viteConfigIsExists = await pathIsExists(customViteConfigPath);
2041
let resultViteConfig: UserConfig;
@@ -362,66 +383,16 @@ const startWebCommand = async () => {
362383
}
363384
}
364385
},
365-
// {
366-
// name: 'mendix-hotreload-react',
367-
// enforce: 'pre',
368-
// transform(code, id) {
369-
// if (!id.includes('node_modules') && /\.(tsx?|jsx?)$/.test(id)) {
370-
// let transformedCode = code;
371-
372-
// transformedCode = transformedCode.replace(
373-
// /import\s+(\w+)\s+from\s+['"]react['"]/g,
374-
// 'const $1 = window.React'
375-
// );
376-
377-
// transformedCode = transformedCode.replace(
378-
// /import\s+\*\s+as\s+(\w+)\s+from\s+['"]react['"]/g,
379-
// 'const $1 = window.React'
380-
// );
381-
382-
// transformedCode = transformedCode.replace(
383-
// /import\s+{([^}]+)}\s+from\s+['"]react['"]/g,
384-
// (match, imports) => {
385-
// const cleanImports = imports.replace(/\s+/g, ' ').trim();
386-
// return `const { ${cleanImports} } = window.React`;
387-
// }
388-
// );
389-
390-
// transformedCode = transformedCode.replace(
391-
// /import\s+(\w+)\s*,\s*{([^}]+)}\s+from\s+['"]react['"]/g,
392-
// (match, defaultImport, namedImports) => {
393-
// const cleanImports = namedImports.replace(/\s+/g, ' ').trim();
394-
// return `const ${defaultImport} = window.React;\nconst { ${cleanImports} } = window.React`;
395-
// }
396-
// );
397-
398-
// transformedCode = transformedCode.replace(
399-
// /import\s+(\w+)\s+from\s+['"]react-dom['"]/g,
400-
// 'const $1 = window.ReactDOM'
401-
// );
402-
403-
// transformedCode = transformedCode.replace(
404-
// /import\s+{([^}]+)}\s+from\s+['"]react-dom['"]/g,
405-
// 'const { $1 } = window.ReactDOM'
406-
// );
407-
408-
// transformedCode = transformedCode.replace(
409-
// /import\s+{([^}]+)}\s+from\s+['"]react-dom\/client['"]/g,
410-
// 'const { $1 } = window.ReactDOM'
411-
// );
412-
413-
// transformedCode = transformedCode.replace(
414-
// /import\s+type\s+{([^}]+)}\s+from\s+['"]react['"]/g,
415-
// '// Type import removed: $1'
416-
// );
417-
418-
// return {
419-
// code: transformedCode,
420-
// map: null
421-
// };
422-
// }
423-
// },
424-
// },
386+
{
387+
name: 'mendix-xml-watch-plugin',
388+
configureServer(server) {
389+
server.watcher.on('change', (file) => {
390+
if (file.endsWith('xml')) {
391+
generateTyping();
392+
}
393+
});
394+
}
395+
}
425396
]
426397
});
427398

0 commit comments

Comments
 (0)