Skip to content

Commit

Permalink
* (bluefox) Added the build support for vite and typescript
Browse files Browse the repository at this point in the history
  • Loading branch information
GermanBluefox committed Sep 20, 2024
1 parent 36c3ccf commit 4a816be
Show file tree
Hide file tree
Showing 5 changed files with 157 additions and 5 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ node node_modules/@iobroker/build-tools/convertI18n.js path/to/i18n
### **WORK IN PROGRESS**
-->
## Changelog
### **WORK IN PROGRESS**
* (bluefox) Added the build support for vite and typescript

### 1.0.3 (2024-09-19)
* (bluefox) Added DANGEROUSLY_DISABLE_HOST_CHECK for buildReact

Expand Down
10 changes: 10 additions & 0 deletions build/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@ options?: {
/** Set to false if you want to execute without `--force` flag */
force?: boolean;
}): Promise<void>;
export declare function tsc(
/** React directory to build */
src: string, options?: {
/** Root directory to copy the version from */
rootDir?: string;
}): Promise<void>;
export declare function buildReact(
/** React directory to build */
src: string,
Expand All @@ -49,6 +55,10 @@ options?: {
exec?: boolean;
/** Max memory size for exec */
ramSize?: number;
/** Use vite for build */
vite?: boolean;
/** execute tsc before building ReactJS */
tsc?: boolean;
}): Promise<void>;
/** @deprecated use buildReact with the craco flag */
export declare function buildCraco(
Expand Down
67 changes: 66 additions & 1 deletion build/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion build/index.js.map

Large diffs are not rendered by default.

80 changes: 77 additions & 3 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,59 @@ export function npmInstall(
});
}

export function tsc(
/** React directory to build */
src: string,
options?: {
/** Root directory to copy the version from */
rootDir?: string,
},
): Promise<void> {
if (src.endsWith('/')) {
src = src.substring(0, src.length - 1);
}
let rootDir: string | undefined;if (options?.rootDir) {
rootDir = options.rootDir;
if (rootDir.endsWith('/')) {
rootDir = rootDir.substring(0, options.rootDir.length - 1);
}
}

return new Promise((resolve, reject) => {
const cpOptions: CommonSpawnOptions = {
stdio: 'pipe' as IOType,
cwd: src,
};

let script;
script = `${src}/node_modules/typescript/bin/tsc`;
if (rootDir && !existsSync(script)) {
script = `${rootDir}/node_modules/typescript/bin/tsc`;
if (!existsSync(script)) {
// admin could have another structure
script = `${rootDir}/../node_modules/typescript/bin/tsc`;
if (!existsSync(script)) {
script = `${rootDir}/../../node_modules/typescript/bin/tsc`;
}
}
}

if (!existsSync(script)) {
console.error(`Cannot find execution file: ${script}`);
reject(`Cannot find execution file: ${script}`);
} else {
const child: ChildProcess = fork(script, [], cpOptions);
child?.stdout?.on('data', data => console.log(data.toString()));
child?.stderr?.on('data', data => console.log(data.toString()));

child.on('close', code => {
console.log(`child process exited with code ${code}`);
code ? reject(`Exit code: ${code}`) : resolve();
});
}
});
}

export function buildReact(
/** React directory to build */
src: string,
Expand All @@ -232,6 +285,10 @@ export function buildReact(
exec?: boolean,
/** Max memory size for exec */
ramSize?: number,
/** Use vite for build */
vite?: boolean,
/** execute tsc before building ReactJS */
tsc?: boolean,
},
): Promise<void> {
if (src.endsWith('/')) {
Expand All @@ -252,8 +309,7 @@ export function buildReact(

writeFileSync(`${src}/package.json`, JSON.stringify(data, null, 4));
}

return new Promise((resolve, reject) => {
const reactPromise: Promise<void> = new Promise((resolve, reject) => {
const cpOptions: CommonSpawnOptions = {
stdio: 'pipe' as IOType,
cwd: src,
Expand All @@ -276,7 +332,19 @@ export function buildReact(
}
}
}
} else {
} else if (options?.vite) {
script = `${src}/node_modules/vite/bin/vite.js`;
if (rootDir && !existsSync(script)) {
script = `${rootDir}/node_modules/vite/bin/vite.js`;
if (!existsSync(script)) {
// admin could have another structure
script = `${rootDir}/../node_modules/vite/bin/vite.js`;
if (!existsSync(script)) {
script = `${rootDir}/../../node_modules/vite/bin/vite.js`;
}
}
}
}else {
script = `${src}/node_modules/react-scripts/scripts/build.js`;
if (rootDir && !existsSync(script)) {
script = `${rootDir}/node_modules/react-scripts/scripts/build.js`;
Expand Down Expand Up @@ -310,6 +378,12 @@ export function buildReact(
});
}
});

if (options?.tsc) {
return tsc(src, options)
.then(() => reactPromise);
}
return reactPromise;
}

/** @deprecated use buildReact with the craco flag */
Expand Down

0 comments on commit 4a816be

Please sign in to comment.