From bfe5471e13a58a68cbdb8cc66d6594a1addaa96d Mon Sep 17 00:00:00 2001 From: William Godbe Date: Mon, 22 Jul 2024 09:32:48 -0700 Subject: [PATCH] Use minified .js files from submodule in source-build (#56864) * Use minified .js files from submodule in source-build * Fixup * Fixup again * Add check for changes to minified .js files * Fix typo * Add doc * Fix md error * Update Microsoft.AspNetCore.Components.Endpoints.csproj * Fix script * Update submodule * Print it * Upload the .js files as part of the build * Update submodule --- .azure/pipelines/ci-public.yml | 7 +++++ .gitmodules | 4 +++ docs/UpdatingMinifiedJsFiles.md | 16 ++++++++++++ eng/Common.props | 2 ++ eng/build.ps1 | 5 ++-- eng/build.sh | 3 ++- eng/scripts/CompareMinifiedJsFiles.ps1 | 26 +++++++++++++++++++ ...oft.AspNetCore.Components.Endpoints.csproj | 1 + ...rosoft.AspNetCore.Components.Server.csproj | 1 + ...osoft.AspNetCore.Components.WebView.csproj | 1 + src/submodules/BlazorMinifiedJs | 1 + 11 files changed, 64 insertions(+), 3 deletions(-) create mode 100644 docs/UpdatingMinifiedJsFiles.md create mode 100644 eng/scripts/CompareMinifiedJsFiles.ps1 create mode 160000 src/submodules/BlazorMinifiedJs diff --git a/.azure/pipelines/ci-public.yml b/.azure/pipelines/ci-public.yml index 9438143ad7d2..124806b65edd 100644 --- a/.azure/pipelines/ci-public.yml +++ b/.azure/pipelines/ci-public.yml @@ -556,6 +556,9 @@ stages: beforeBuild: - powershell: "& ./src/Servers/IIS/tools/UpdateIISExpressCertificate.ps1; & ./src/Servers/IIS/tools/update_schema.ps1" displayName: Setup IISExpress test certificates and schema + afterBuild: + - powershell: ./eng/scripts/CompareMinifiedJsFiles.ps1 + displayName: Check for changes in generated minified .js files artifacts: - name: Windows_Test_Logs_Attempt_$(System.JobAttempt) path: artifacts/log/ @@ -565,6 +568,10 @@ stages: path: artifacts/TestResults/ publishOnError: true includeForks: true + - name: Minified_JS_Files + path: src/Components/Web.JS/dist/Release/ + publishOnError: true + includeForks: true - template: jobs/default-build.yml parameters: diff --git a/.gitmodules b/.gitmodules index 8dfbbd322bd3..2180904ec871 100644 --- a/.gitmodules +++ b/.gitmodules @@ -11,3 +11,7 @@ url = https://github.com/dotnet/Node-Externals branch = main depth = 1 +[submodule "src/submodules/BlazorMinifiedJs"] + path = src/submodules/BlazorMinifiedJs + url = https://github.com/dotnet/BlazorMinifiedJs + branch = main diff --git a/docs/UpdatingMinifiedJsFiles.md b/docs/UpdatingMinifiedJsFiles.md new file mode 100644 index 000000000000..ddbc8af582c4 --- /dev/null +++ b/docs/UpdatingMinifiedJsFiles.md @@ -0,0 +1,16 @@ +# Updating minified .js files + +Building our `src/Components` projects will produce minified.js files under `src/Components/Web.JS/dist/Release`. In order to avoid constant merge conflicts, and to avoid having to restore NPM components over the network during offline source-build, we keep the latest versions of those files in a submodule repo, https://github.com/dotnet/blazorminifiedjs. If you are prepping a PR that is going to change the contents of those files, please follow the steps in this doc. + +1. Build the node components of the repo + 1. Running `npm run build` from the repo root should be sufficient, assuming you have already installed the prereqs listed in our [Building from source doc](https://github.com/dotnet/aspnetcore/edit/main/docs/BuildFromSource.md). +2. In a separate folder, clone the [BlazorMinifiedJs repo](https://github.com/dotnet/blazorminifiedjs). +3. Check out a new branch in your clone, based off of `main`. +4. Replace the files in `BlazorMinifiedJs/src` with the minified .js files you just generated in aspnetcore (these can be found at `aspnetcore/src/Components/Web.JS/dist/Release`). +5. Push your `BlazorMinifiedJs` branch and open a PR in that repo. +6. Once that PR has been merged, return to your aspnetcore PR, navigate to `src/submodules/BlazorMinifiedJs`, and checkout the commit you just pushed. +7. Push the submodule update to your aspnetcore PR. + +Alternatively, you can find the generated .js files in the artifacts of your PR build, under the artifact named "Minified_JS_Files". This may be more reliable than building the node components locally. + +Following these steps should remediate any build errors related to `BlazorMinifiedJs` in your PR. \ No newline at end of file diff --git a/eng/Common.props b/eng/Common.props index 1ef8fa3c188e..7ae71770751b 100644 --- a/eng/Common.props +++ b/eng/Common.props @@ -18,6 +18,8 @@ true true + $(BuildNodeJSUnlessSourcebuild) + false true true diff --git a/eng/build.ps1 b/eng/build.ps1 index 9d59a817208e..3696fe11e0df 100644 --- a/eng/build.ps1 +++ b/eng/build.ps1 @@ -235,6 +235,7 @@ if ($BuildManaged -or ($All -and (-not $NoBuildManaged))) { if ($node) { $nodeHome = Split-Path -Parent (Split-Path -Parent $node.Path) Write-Host -f Magenta "Building of C# project is enabled and has dependencies on NodeJS projects. Building of NodeJS projects is enabled since node is detected in $nodeHome." + Write-Host -f Magenta "Note that if you are running Source Build, building NodeJS projects will be disabled later on." $BuildNodeJS = $true } else { @@ -310,8 +311,8 @@ if ($NoBuildJava) { $dotnetBuildArguments += "/p:BuildJava=false"; $BuildJava = if ($BuildJava) { $dotnetBuildArguments += "/p:BuildJava=true" } if ($NoBuildManaged) { $dotnetBuildArguments += "/p:BuildManaged=false"; $BuildManaged = $false } if ($BuildManaged) { $dotnetBuildArguments += "/p:BuildManaged=true" } -if ($NoBuildNodeJS) { $dotnetBuildArguments += "/p:BuildNodeJS=false"; $BuildNodeJS = $false } -if ($BuildNodeJS) { $dotnetBuildArguments += "/p:BuildNodeJS=true" } +if ($NoBuildNodeJS) { $dotnetBuildArguments += "/p:BuildNodeJSUnlessSourcebuild=false"; $BuildNodeJS = $false } +if ($BuildNodeJS) { $dotnetBuildArguments += "/p:BuildNodeJSUnlessSourcebuild=true" } # Don't bother with two builds if just one will build everything. Ignore super-weird cases like # "-Projects ... -NoBuildJava -NoBuildManaged -NoBuildNodeJS". An empty `./build.ps1` command will build both diff --git a/eng/build.sh b/eng/build.sh index da590f940ff1..1275859dfaf0 100755 --- a/eng/build.sh +++ b/eng/build.sh @@ -266,6 +266,7 @@ if [ "$build_managed" = true ] || ([ "$build_all" = true ] && [ "$build_managed" if [ -z "$build_nodejs" ]; then if [ -x "$(command -v node)" ]; then __warn "Building of C# project is enabled and has dependencies on NodeJS projects. Building of NodeJS projects is enabled since node is detected on PATH." + __warn "Note that if you are running Source Build, building NodeJS projects will be disabled later on." build_nodejs=true else __warn "Building of NodeJS projects is disabled since node is not detected on Path and no BuildNodeJs or NoBuildNodeJs setting is set explicitly." @@ -281,7 +282,7 @@ fi # Only set these MSBuild properties if they were explicitly set by build parameters. [ ! -z "$build_java" ] && msbuild_args[${#msbuild_args[*]}]="-p:BuildJava=$build_java" [ ! -z "$build_native" ] && msbuild_args[${#msbuild_args[*]}]="-p:BuildNative=$build_native" -[ ! -z "$build_nodejs" ] && msbuild_args[${#msbuild_args[*]}]="-p:BuildNodeJS=$build_nodejs" +[ ! -z "$build_nodejs" ] && msbuild_args[${#msbuild_args[*]}]="-p:BuildNodeJSUnlessSourcebuild=$build_nodejs" [ ! -z "$build_managed" ] && msbuild_args[${#msbuild_args[*]}]="-p:BuildManaged=$build_managed" [ ! -z "$build_installers" ] && msbuild_args[${#msbuild_args[*]}]="-p:BuildInstallers=$build_installers" diff --git a/eng/scripts/CompareMinifiedJsFiles.ps1 b/eng/scripts/CompareMinifiedJsFiles.ps1 new file mode 100644 index 000000000000..6681a1380bf1 --- /dev/null +++ b/eng/scripts/CompareMinifiedJsFiles.ps1 @@ -0,0 +1,26 @@ +[string[]] $errors = @() + +function _compareFiles($fileName) { + Write-Host "Comparing contents for $fileName" + $repoRoot = Resolve-Path "$PSScriptRoot/../.." + $localFile = "$repoRoot/src/Components/Web.JS/dist/Release/$fileName" + $submoduleFile = "$repoRoot/src/submodules/BlazorMinifiedJs/src/$fileName" + $delta = Compare-Object -ReferenceObject ((Get-Content -Path $submoduleFile).trim()) -DifferenceObject ((Get-Content -Path $localFile).trim()) + if (![string]::IsNullOrEmpty($delta)) { + $script:errors += "Diff found in $fileName, please see https://github.com/dotnet/aspnetcore/blob/main/docs/UpdatingMinifiedJsFiles.md for remediation steps" + } +} + +$MinifiedJsFiles = "blazor.web.js","blazor.server.js","blazor.webview.js" + +foreach ($JsFile in $MinifiedJsFiles) { + _compareFiles -fileName $JsFile +} + +foreach ($err in $errors) { + Write-Host -f Red $err +} + +if ($errors) { + exit 1 +} \ No newline at end of file diff --git a/src/Components/Endpoints/src/Microsoft.AspNetCore.Components.Endpoints.csproj b/src/Components/Endpoints/src/Microsoft.AspNetCore.Components.Endpoints.csproj index 327f717d4af1..daa616946866 100644 --- a/src/Components/Endpoints/src/Microsoft.AspNetCore.Components.Endpoints.csproj +++ b/src/Components/Endpoints/src/Microsoft.AspNetCore.Components.Endpoints.csproj @@ -65,6 +65,7 @@ blazor.web.js $(MSBuildThisFileDirectory)..\..\Web.JS\dist\Debug\$(BlazorWebJSFilename) $(MSBuildThisFileDirectory)..\..\Web.JS\dist\Release\$(BlazorWebJSFilename) + $(RepoRoot)src\submodules\BlazorMinifiedJs\src\$(BlazorWebJSFilename) diff --git a/src/Components/Server/src/Microsoft.AspNetCore.Components.Server.csproj b/src/Components/Server/src/Microsoft.AspNetCore.Components.Server.csproj index 260e60ac5efb..0a2334909b33 100644 --- a/src/Components/Server/src/Microsoft.AspNetCore.Components.Server.csproj +++ b/src/Components/Server/src/Microsoft.AspNetCore.Components.Server.csproj @@ -103,6 +103,7 @@ blazor.server.js $(MSBuildThisFileDirectory)..\..\Web.JS\dist\Debug\$(BlazorServerJSFilename) $(MSBuildThisFileDirectory)..\..\Web.JS\dist\Release\$(BlazorServerJSFilename) + $(RepoRoot)src\submodules\BlazorMinifiedJs\src\$(BlazorServerJSFilename) diff --git a/src/Components/WebView/WebView/src/Microsoft.AspNetCore.Components.WebView.csproj b/src/Components/WebView/WebView/src/Microsoft.AspNetCore.Components.WebView.csproj index c669218e36f3..dd06bc9a0c83 100644 --- a/src/Components/WebView/WebView/src/Microsoft.AspNetCore.Components.WebView.csproj +++ b/src/Components/WebView/WebView/src/Microsoft.AspNetCore.Components.WebView.csproj @@ -62,6 +62,7 @@ blazor.webview.js $(MSBuildThisFileDirectory)..\..\..\Web.JS\dist\Debug\$(BlazorWebViewJSFilename) $(MSBuildThisFileDirectory)..\..\..\Web.JS\dist\Release\$(BlazorWebViewJSFilename) + $(RepoRoot)src\submodules\BlazorMinifiedJs\src\$(BlazorWebViewJSFilename) diff --git a/src/submodules/BlazorMinifiedJs b/src/submodules/BlazorMinifiedJs new file mode 160000 index 000000000000..42f116d7c126 --- /dev/null +++ b/src/submodules/BlazorMinifiedJs @@ -0,0 +1 @@ +Subproject commit 42f116d7c126fe087db17b15c8365c27616e1acb