Skip to content

Commit

Permalink
ci: checks for bundling
Browse files Browse the repository at this point in the history
  • Loading branch information
mingxuanzhangsfdx committed Oct 31, 2024
1 parent de26962 commit 151e7ce
Show file tree
Hide file tree
Showing 11 changed files with 318 additions and 0 deletions.
13 changes: 13 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,19 @@ jobs:
windows-unit-tests:
needs: linux-unit-tests
uses: salesforcecli/github-workflows/.github/workflows/unitTestsWindows.yml@main
test-bundle:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: lts/*
cache: yarn
- uses: salesforcecli/github-workflows/.github/actions/yarnInstallWithRetries@main
- name: Build the project
run: yarn build
- name: check if bundling runs into failures
run: node scripts/testEsbuild.js

nuts:
needs: linux-unit-tests
Expand Down
1 change: 1 addition & 0 deletions .husky/pre-commit
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
. "$(dirname "$0")/_/husky.sh"

yarn lint && yarn pretty-quick --staged
node ./scripts/scanTs.js
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,11 @@
"@types/mime": "2.0.3",
"@types/minimatch": "^5.1.2",
"deep-equal-in-any-order": "^1.1.19",
"esbuild": "^0.24.0",
"eslint-plugin-sf-plugin": "^1.20.8",
"mocha-junit-reporter": "^1.23.3",
"mocha-snap": "^5.0.0",
"ts-morph": "^24.0.0",
"ts-node": "^10.9.2",
"ts-patch": "^3.2.1",
"typescript": "^5.5.4"
Expand Down
29 changes: 29 additions & 0 deletions scripts/esbuild.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/**
* NOTE: This file does NOT really generate a bundle version of sdr
* sdr is bundled directly in salesforcedx-vscode
* The file is only used to detect any potential risks to esbuild.
**/
const { build } = require('esbuild');

const sharedConfig = {
bundle: true,
format: 'cjs',
platform: 'node',
external: [], // The allowlist of dependencies that are not bundle-able
keepNames: true,
plugins: [],
supported: {
'dynamic-import': false,
},
logOverride: {
'unsupported-dynamic-import': 'error',
},
};

(async () => {
await build({
...sharedConfig,
entryPoints: ['./lib/src/index.js'],
outdir: 'dist',
});
})();
51 changes: 51 additions & 0 deletions scripts/scanTs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
const fs = require('fs');
const path = require('path');
const { Project, CallExpression } = require('ts-morph');

const SRC_DIR = path.join(__dirname, '..', 'src');
const project = new Project({
tsConfigFilePath: path.join(__dirname, '..', 'tsconfig.json'),
});

let detected = false;

const scanDirectory = (dir) => {
const files = fs.readdirSync(dir);
files.forEach((file) => {
const fullPath = path.join(dir, file);
const stat = fs.statSync(fullPath);
if (stat.isDirectory()) {
scanDirectory(fullPath);
} else if (fullPath.endsWith('.ts')) {
analyzeFile(fullPath);
}
});
};

// This function will detect all the usages of fs.read* and send warnings with the location of the usage
const analyzeFile = (filePath) => {
const srcFile = project.addSourceFileAtPath(filePath);
const funcCalls = srcFile.getDescendantsOfKind(CallExpression);

funcCalls.forEach((callExpression) => {
const exp = callExpression.getExpression();
if (exp.getText().startsWith('fs.read')) {
detected = true;
console.warn(
`Warning: Usage of "${exp.getText()}" in file "${filePath}" at line ${callExpression.getStartLineNumber()}.\n`
);
}
});
};

scanDirectory(SRC_DIR);

if (detected) {
console.log('The warnings above do not mean the usages are wrong.');
console.log(`Avoid reading local artifacts with "fs.read*" since esbuild cannot bundle the artifacts together.`);
console.log('Consider using import instead or reach out to IDEx Foundations team');
} else {
console.log('No fs.read* usages detected.');
}

console.log('Scan complete');
20 changes: 20 additions & 0 deletions scripts/testEsbuild.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
const { exec } = require('child_process');

const commandToRun = 'node ./scripts/esbuild.config.js';

// Run the command
exec(commandToRun, (error, stdout, stderr) => {
// Combine stdout and stderr to check the entire output
const output = `${stdout}\n${stderr}`;
if (error) {
console.error(stderr);
process.exit(1); // Exit with an error code
}
// Check if the output contains the error string
if (output.includes('[require-resolve-not-external]')) {
console.error('Error: A dependency that has to be externalized in esbuild process is found. Please resolve it!');
process.exit(1); // Exit with an error code
} else {
process.exit(0); // Exit with success code
}
});
1 change: 1 addition & 0 deletions src/registry/presets/presetMap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import { MetadataRegistry } from '../types';

// we have to import all presets explicitly for VSCE's esbuild bundling process
// other read methods might make esbuild fail to bundle the json file
import * as decomposeCustomLabelsBeta from './decomposeCustomLabelsBeta.json';
import * as decomposeCustomLabelsBeta2 from './decomposeCustomLabelsBeta2.json';
import * as decomposePermissionSetBeta from './decomposePermissionSetBeta.json';
Expand Down
2 changes: 2 additions & 0 deletions src/registry/registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
* For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause
*/
import { deepFreeze } from '../utils/collections';
// The static import of json file should never be changed,
// other read methods might make esbuild fail to bundle the json file
import * as registryData from './metadataRegistry.json';
import { MetadataRegistry } from './types';

Expand Down
2 changes: 2 additions & 0 deletions src/registry/standardvalueset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
* For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause
*/
import { deepFreeze } from '../utils/collections';
// The static import of json file should never be changed,
// other read methods might make esbuild fail to bundle the json file
import * as standardValueSetData from './stdValueSetRegistry.json';

/**
Expand Down
2 changes: 2 additions & 0 deletions src/registry/variants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import { Logger, SfProject, SfProjectJson, Lifecycle, SfError } from '@salesforce/core';
import { deepFreeze } from '../utils/collections';
import { MetadataRegistry } from './types';
// The static import of json file should never be changed,
// other read methods might make esbuild fail to bundle the json file
import * as registryData from './metadataRegistry.json';
import { presetMap } from './presets/presetMap';

Expand Down
Loading

2 comments on commit 151e7ce

@svc-cli-bot
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Benchmark

Benchmark suite Current: 151e7ce Previous: e9f3a6a Ratio
eda-componentSetCreate-linux 241 ms 232 ms 1.04
eda-sourceToMdapi-linux 2260 ms 2395 ms 0.94
eda-sourceToZip-linux 1948 ms 1902 ms 1.02
eda-mdapiToSource-linux 2848 ms 2985 ms 0.95
lotsOfClasses-componentSetCreate-linux 429 ms 433 ms 0.99
lotsOfClasses-sourceToMdapi-linux 3563 ms 3683 ms 0.97
lotsOfClasses-sourceToZip-linux 3078 ms 3145 ms 0.98
lotsOfClasses-mdapiToSource-linux 3472 ms 3568 ms 0.97
lotsOfClassesOneDir-componentSetCreate-linux 733 ms 772 ms 0.95
lotsOfClassesOneDir-sourceToMdapi-linux 6349 ms 6541 ms 0.97
lotsOfClassesOneDir-sourceToZip-linux 5411 ms 5809 ms 0.93
lotsOfClassesOneDir-mdapiToSource-linux 6392 ms 6522 ms 0.98

This comment was automatically generated by workflow using github-action-benchmark.

@svc-cli-bot
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Benchmark

Benchmark suite Current: 151e7ce Previous: e9f3a6a Ratio
eda-componentSetCreate-win32 677 ms 733 ms 0.92
eda-sourceToMdapi-win32 4747 ms 4698 ms 1.01
eda-sourceToZip-win32 3152 ms 3200 ms 0.98
eda-mdapiToSource-win32 6280 ms 6404 ms 0.98
lotsOfClasses-componentSetCreate-win32 1301 ms 1326 ms 0.98
lotsOfClasses-sourceToMdapi-win32 8351 ms 8664 ms 0.96
lotsOfClasses-sourceToZip-win32 5469 ms 5282 ms 1.04
lotsOfClasses-mdapiToSource-win32 7789 ms 8012 ms 0.97
lotsOfClassesOneDir-componentSetCreate-win32 2116 ms 2155 ms 0.98
lotsOfClassesOneDir-sourceToMdapi-win32 13603 ms 14072 ms 0.97
lotsOfClassesOneDir-sourceToZip-win32 9202 ms 9483 ms 0.97
lotsOfClassesOneDir-mdapiToSource-win32 14017 ms 14338 ms 0.98

This comment was automatically generated by workflow using github-action-benchmark.

Please sign in to comment.