Skip to content

Commit

Permalink
Use writeIfChanged
Browse files Browse the repository at this point in the history
  • Loading branch information
volkanceylan committed Dec 25, 2024
1 parent f4cea49 commit 4ea9203
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 25 deletions.
5 changes: 4 additions & 1 deletion common-features/build/Feature.Build.targets
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
<Project>
<PropertyGroup>
<PrepareForBuildDependsOn>CompileProjectTSConfig;$(PrepareForBuildDependsOn)</PrepareForBuildDependsOn>
</PropertyGroup>
<ItemGroup>
<TypeScriptCompile Remove="node_modules\**;obj\**;out\**;dist\**"></TypeScriptCompile>
<ProjectCapability Include="TypeScriptNuGet" />
Expand All @@ -8,7 +11,7 @@
</ItemGroup>
<Import Project="$(SerenitySrc)Serenity.Net.Web\Serenity.Net.Web.targets" Condition="'$(AddAutoRefs)' != 'false'" />
<Import Project="$(SerenitySrc)RestoreNpmPackages.targets" />
<Target Name="CompileProjectTSConfig" AfterTargets="AfterBuild" DependsOnTargets="NpmInstall;RestoreNodeTypes"
<Target Name="CompileProjectTSConfig" DependsOnTargets="NpmInstall;RestoreNodeTypes"
Condition="Exists('tsconfig.json') And '$(BuildingProject)' != 'false' And '$(DesignTimeBuild)' != 'true'">
<Message Text="Executing TypeScript compiler for $(ProjectName)..." Importance="high"></Message>
<Exec Command="node &quot;$(TSJavaScriptFile.Replace('build\\..\tools\', 'tools\'))&quot; -p ./tsconfig.json" Condition="!Exists('tsbuild.js')" ContinueOnError="true" />
Expand Down
3 changes: 2 additions & 1 deletion packages/corelib/Serenity.Corelib.csproj
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
<Project Sdk="Microsoft.NET.Sdk.Razor">
<PropertyGroup>
<Description>NuGet version of @serenity-is/corelib NPM package</Description>
<PrepareForBuildDependsOn>TSBuild;$(PrepareForBuildDependsOn)</PrepareForBuildDependsOn>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\sleekgrid\Serenity.SleekGrid.csproj" />
</ItemGroup>
<Target Name="TSBuild" Condition="'$(SkipNodeScripts)' != 'true'" AfterTargets="AfterBuild" DependsOnTargets="NpmInstall">
<Target Name="TSBuild" Condition="'$(SkipNodeScripts)' != 'true'" DependsOnTargets="NpmInstall">
<Exec Command="pnpm build" />
</Target>
</Project>
40 changes: 33 additions & 7 deletions packages/corelib/build/build.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import esbuild from "esbuild";
import { join, resolve } from "path";
import { existsSync, mkdirSync, readFileSync, writeFileSync } from "fs";
import { dirname, join, resolve } from "path";
import { fileURLToPath } from 'url';

const root = resolve(join(fileURLToPath(new URL('.', import.meta.url)), '../'));
Expand Down Expand Up @@ -44,6 +45,27 @@ export function importAsGlobalsPlugin(mapping) {
};
}

export function writeIfChanged() {
return {
name: "write-if-changed",
setup(build) {
const write = build.initialOptions.write;
build.initialOptions.write = false;
build.onEnd(result => {
if (write === undefined || write) {
result.outputFiles?.forEach(file => {
if (!existsSync(file.path) ||
readFileSync(file.path, 'utf8') !== file.text) {
mkdirSync(dirname(file.path), { recursive: true });
writeFileSync(file.path, file.text);
}
});
}
});
}
};
}

await esbuild.build({
...coreLibBase,
external: ['@serenity-is/sleekgrid'],
Expand Down Expand Up @@ -82,21 +104,25 @@ const corelibGlobalBase = {
},
format: 'iife',
globalName: 'Serenity',
outfile: 'wwwroot/index.global.js',
plugins: [importAsGlobalsPlugin({
"@serenity-is/sleekgrid": "Slick"
})]
outfile: 'wwwroot/index.global.js'
};

await esbuild.build({
...corelibGlobalBase,
minify: false,
outfile: 'wwwroot/index.global.js'
outfile: 'wwwroot/index.global.js',
plugins: [importAsGlobalsPlugin({
"@serenity-is/sleekgrid": "Slick"
}), writeIfChanged()]
});

await esbuild.build({
...corelibGlobalBase,
lineLimit: 1000,
minify: true,
outfile: 'wwwroot/index.global.min.js'
outfile: 'wwwroot/index.global.min.js',
plugins: [importAsGlobalsPlugin({
"@serenity-is/sleekgrid": "Slick"
}), writeIfChanged()]

});
2 changes: 1 addition & 1 deletion packages/sleekgrid
37 changes: 22 additions & 15 deletions packages/tsbuild/dist/index.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,10 @@
import esbuild from "esbuild";
import { existsSync, readdirSync, statSync, mkdirSync, writeFileSync, rmSync, readFileSync } from "fs";
import { join, relative, resolve } from "path";
import { exit } from "process";
import { dirname, join, relative, resolve } from "path";
import { globSync } from "glob";

export const defaultEntryPointGlobs = ['Modules/**/*Page.ts', 'Modules/**/*Page.tsx', 'Modules/**/ScriptInit.ts'];

export function checkIfTrigger() {
if (process.argv.slice(2).some(x => x == "--trigger")) {
if (existsSync('typings/serenity.corelib/_trigger.ts'))
rmSync('typings/serenity.corelib/_trigger.ts')
else {
if (!existsSync('typings/serenity.corelib/'))
mkdirSync('typings/serenity.corelib/');
writeFileSync('typings/serenity.corelib/_trigger.ts', '// for triggering build');
}
exit(0);
}
}

export const importAsGlobalsMapping = {
"@serenity-is/base": "Serenity",
"@serenity-is/corelib": "Serenity",
Expand Down Expand Up @@ -109,6 +95,7 @@ export const esbuildOptions = (opt) => {

delete opt.clean;
delete opt.importAsGlobals;
delete opt.writeIfChanged;

return Object.assign({
absWorkingDir: resolve('./'),
Expand Down Expand Up @@ -208,4 +195,24 @@ export function cleanPlugin() {
});
}
}
}

export function writeIfChanged() {
return {
name: "write-if-changed",
setup(build) {
build.onEnd(result => {
result.outputFiles?.forEach(file => {
if (existsSync(file.path)) {
const old = readFileSync(file.path);
if (old.equals(file.contents))
return;
console.log("overwriting " + file.path);
mkdirSync(dirname(file.path), { recursive: true });
writeFileSync(file.path, file.text);
}
});
});
}
};
}

0 comments on commit 4ea9203

Please sign in to comment.