Skip to content

Commit 757a4b0

Browse files
authored
Merge pull request #1030 from forcedotcom/mz/esbuild-workflow
chore: esbuild artifacts
2 parents 9d5a5dd + 33cac99 commit 757a4b0

File tree

4 files changed

+177
-0
lines changed

4 files changed

+177
-0
lines changed

.github/workflows/esbuild-publish.yml

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
name: esbuild Compilation & npm Publish Workflow
2+
3+
on:
4+
workflow_dispatch:
5+
inputs:
6+
branch:
7+
description: 'Set the branch to use for automation tests'
8+
type: string
9+
required: false
10+
default: 'main'
11+
nodeVersion:
12+
description: version of node to use. It's better to specify latest, lts/* or lts/-1 than to hardode numbers
13+
type: string
14+
default: lts/*
15+
required: false
16+
17+
jobs:
18+
build:
19+
runs-on: ubuntu-latest
20+
steps:
21+
- uses: actions/checkout@v4
22+
with:
23+
ref: ${{ inputs.branch }}
24+
- uses: actions/setup-node@v4
25+
with:
26+
node-version: ${{ inputs.nodeVersion }}
27+
registry-url: 'https://registry.npmjs.org'
28+
cache: yarn
29+
- name: Install esbuild Dependencies
30+
run: |
31+
yarn add -D esbuild@^0.19.5 esbuild-plugin-pino@^2.1.0 npm-dts@^1.3.12 esbuild-plugin-tsc@^0.4.0
32+
- uses: salesforcecli/github-workflows/.github/actions/yarnInstallWithRetries@main
33+
- name: Update for Bundling
34+
run: |
35+
node scripts/updateForBundling.js
36+
- name: Generate Bundle
37+
run: |
38+
node scripts/build.js
39+
- name: Publish a Package
40+
run: |
41+
npm config set //registry.npmjs.org/:_authToken=$NODE_AUTH_TOKEN
42+
npm publish
43+
env:
44+
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}

scripts/build.js

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* Copyright (c) 2023, salesforce.com, inc.
3+
* All rights reserved.
4+
* Licensed under the BSD 3-Clause license.
5+
* For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause
6+
*/
7+
const { build } = require('esbuild');
8+
const esbuildPluginPino = require('esbuild-plugin-pino');
9+
const esbuildPluginTsc = require('esbuild-plugin-tsc');
10+
const { Generator } = require('npm-dts');
11+
const fs = require('fs');
12+
13+
new Generator({
14+
output: 'lib/exported.d.ts',
15+
}).generate();
16+
17+
const sharedConfig = {
18+
entryPoints: ['src/exported.ts'],
19+
bundle: true,
20+
// minify: true,
21+
plugins: [
22+
esbuildPluginPino({ transports: ['pino-pretty'] }),
23+
esbuildPluginTsc({
24+
tsconfigPath: './tsconfig.json',
25+
}),
26+
],
27+
};
28+
29+
(async () => {
30+
const result = await build({
31+
...sharedConfig,
32+
// external: ['src/logger/transformStream.ts'],
33+
platform: 'node', // for CJS
34+
outdir: 'lib',
35+
});
36+
const filePath = 'lib/exported.js';
37+
let bundledEntryPoint = fs.readFileSync(filePath, 'utf8');
38+
39+
const searchString = /\$\{process\.cwd\(\)\}\$\{require\("path"\)\.sep\}lib/g;
40+
const replacementString = `\${__dirname}\${require("path").sep}`;
41+
42+
bundledEntryPoint = bundledEntryPoint.replace(searchString, replacementString);
43+
fs.writeFileSync(filePath, bundledEntryPoint, 'utf8');
44+
45+
await build({
46+
entryPoints: ['src/logger/transformStream.ts'],
47+
bundle: true,
48+
minify: true,
49+
outdir: 'lib',
50+
platform: 'node', // for CJS
51+
plugins: [
52+
// esbuildPluginPino({ transports: ['pino-pretty'] }),
53+
],
54+
});
55+
})();

scripts/tsconfig.json

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"extends": "@salesforce/dev-config/tsconfig-strict",
3+
"compilerOptions": {
4+
"outDir": "../lib",
5+
"resolveJsonModule": true,
6+
"esModuleInterop": true,
7+
"rootDir": "../src",
8+
"plugins": [{ "transform": "../src/messageTransformer.ts" }]
9+
},
10+
"include": ["../src/**/*.ts"]
11+
}

scripts/updateForBundling.js

+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
const fs = require('fs');
2+
3+
// Function to update package.json
4+
function updatePackageJson() {
5+
const packagePath = './package.json';
6+
7+
fs.readFile(packagePath, 'utf8', (err, data) => {
8+
if (err) {
9+
console.error(`Error reading package.json: ${err}`);
10+
return;
11+
}
12+
13+
try {
14+
const packageJson = JSON.parse(data);
15+
16+
// Update package name if necessary
17+
if (packageJson.name && packageJson.name === '@salesforce/core') {
18+
packageJson.name = '@salesforce/core-bundle';
19+
}
20+
21+
// Remove 'prepack' and 'prepare' scripts
22+
if (packageJson.scripts) {
23+
delete packageJson.scripts.prepack;
24+
delete packageJson.scripts.prepare;
25+
}
26+
27+
fs.writeFile(packagePath, JSON.stringify(packageJson, null, 2), 'utf8', (writeErr) => {
28+
if (writeErr) {
29+
console.error(`Error writing to package.json: ${writeErr}`);
30+
} else {
31+
console.log('package.json updated successfully.');
32+
}
33+
});
34+
} catch (parseErr) {
35+
console.error(`Error parsing JSON in package.json: ${parseErr}`);
36+
}
37+
});
38+
}
39+
40+
// Function to update logger.ts
41+
function updateLoggerTs() {
42+
const loggerPath = './src/logger/logger.ts';
43+
44+
fs.readFile(loggerPath, 'utf8', (err, data) => {
45+
if (err) {
46+
console.error(`Error reading logger.ts: ${err}`);
47+
return;
48+
}
49+
50+
let updatedData = data.replace(
51+
"target: path.join('..', '..', 'lib', 'logger', 'transformStream')",
52+
"target: './transformStream'"
53+
);
54+
55+
fs.writeFile(loggerPath, updatedData, 'utf8', (writeErr) => {
56+
if (writeErr) {
57+
console.error(`Error writing to logger.ts: ${writeErr}`);
58+
} else {
59+
console.log('Logger.ts updated successfully.');
60+
}
61+
});
62+
});
63+
}
64+
65+
// Run the update functions
66+
updatePackageJson();
67+
updateLoggerTs();

0 commit comments

Comments
 (0)