Skip to content

Commit

Permalink
Merge pull request #1030 from forcedotcom/mz/esbuild-workflow
Browse files Browse the repository at this point in the history
chore: esbuild artifacts
  • Loading branch information
mshanemc authored Feb 22, 2024
2 parents 9d5a5dd + 33cac99 commit 757a4b0
Show file tree
Hide file tree
Showing 4 changed files with 177 additions and 0 deletions.
44 changes: 44 additions & 0 deletions .github/workflows/esbuild-publish.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
name: esbuild Compilation & npm Publish Workflow

on:
workflow_dispatch:
inputs:
branch:
description: 'Set the branch to use for automation tests'
type: string
required: false
default: 'main'
nodeVersion:
description: version of node to use. It's better to specify latest, lts/* or lts/-1 than to hardode numbers
type: string
default: lts/*
required: false

jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
ref: ${{ inputs.branch }}
- uses: actions/setup-node@v4
with:
node-version: ${{ inputs.nodeVersion }}
registry-url: 'https://registry.npmjs.org'
cache: yarn
- name: Install esbuild Dependencies
run: |
yarn add -D esbuild@^0.19.5 esbuild-plugin-pino@^2.1.0 npm-dts@^1.3.12 esbuild-plugin-tsc@^0.4.0
- uses: salesforcecli/github-workflows/.github/actions/yarnInstallWithRetries@main
- name: Update for Bundling
run: |
node scripts/updateForBundling.js
- name: Generate Bundle
run: |
node scripts/build.js
- name: Publish a Package
run: |
npm config set //registry.npmjs.org/:_authToken=$NODE_AUTH_TOKEN
npm publish
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
55 changes: 55 additions & 0 deletions scripts/build.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* Copyright (c) 2023, salesforce.com, inc.
* All rights reserved.
* Licensed under the BSD 3-Clause license.
* For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause
*/
const { build } = require('esbuild');
const esbuildPluginPino = require('esbuild-plugin-pino');
const esbuildPluginTsc = require('esbuild-plugin-tsc');
const { Generator } = require('npm-dts');
const fs = require('fs');

new Generator({
output: 'lib/exported.d.ts',
}).generate();

const sharedConfig = {
entryPoints: ['src/exported.ts'],
bundle: true,
// minify: true,
plugins: [
esbuildPluginPino({ transports: ['pino-pretty'] }),
esbuildPluginTsc({
tsconfigPath: './tsconfig.json',
}),
],
};

(async () => {
const result = await build({
...sharedConfig,
// external: ['src/logger/transformStream.ts'],
platform: 'node', // for CJS
outdir: 'lib',
});
const filePath = 'lib/exported.js';
let bundledEntryPoint = fs.readFileSync(filePath, 'utf8');

const searchString = /\$\{process\.cwd\(\)\}\$\{require\("path"\)\.sep\}lib/g;
const replacementString = `\${__dirname}\${require("path").sep}`;

bundledEntryPoint = bundledEntryPoint.replace(searchString, replacementString);
fs.writeFileSync(filePath, bundledEntryPoint, 'utf8');

await build({
entryPoints: ['src/logger/transformStream.ts'],
bundle: true,
minify: true,
outdir: 'lib',
platform: 'node', // for CJS
plugins: [
// esbuildPluginPino({ transports: ['pino-pretty'] }),
],
});
})();
11 changes: 11 additions & 0 deletions scripts/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"extends": "@salesforce/dev-config/tsconfig-strict",
"compilerOptions": {
"outDir": "../lib",
"resolveJsonModule": true,
"esModuleInterop": true,
"rootDir": "../src",
"plugins": [{ "transform": "../src/messageTransformer.ts" }]
},
"include": ["../src/**/*.ts"]
}
67 changes: 67 additions & 0 deletions scripts/updateForBundling.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
const fs = require('fs');

// Function to update package.json
function updatePackageJson() {
const packagePath = './package.json';

fs.readFile(packagePath, 'utf8', (err, data) => {
if (err) {
console.error(`Error reading package.json: ${err}`);
return;
}

try {
const packageJson = JSON.parse(data);

// Update package name if necessary
if (packageJson.name && packageJson.name === '@salesforce/core') {
packageJson.name = '@salesforce/core-bundle';
}

// Remove 'prepack' and 'prepare' scripts
if (packageJson.scripts) {
delete packageJson.scripts.prepack;
delete packageJson.scripts.prepare;
}

fs.writeFile(packagePath, JSON.stringify(packageJson, null, 2), 'utf8', (writeErr) => {
if (writeErr) {
console.error(`Error writing to package.json: ${writeErr}`);
} else {
console.log('package.json updated successfully.');
}
});
} catch (parseErr) {
console.error(`Error parsing JSON in package.json: ${parseErr}`);
}
});
}

// Function to update logger.ts
function updateLoggerTs() {
const loggerPath = './src/logger/logger.ts';

fs.readFile(loggerPath, 'utf8', (err, data) => {
if (err) {
console.error(`Error reading logger.ts: ${err}`);
return;
}

let updatedData = data.replace(
"target: path.join('..', '..', 'lib', 'logger', 'transformStream')",
"target: './transformStream'"
);

fs.writeFile(loggerPath, updatedData, 'utf8', (writeErr) => {
if (writeErr) {
console.error(`Error writing to logger.ts: ${writeErr}`);
} else {
console.log('Logger.ts updated successfully.');
}
});
});
}

// Run the update functions
updatePackageJson();
updateLoggerTs();

0 comments on commit 757a4b0

Please sign in to comment.