Skip to content

Commit

Permalink
feat: make templates bundle-able
Browse files Browse the repository at this point in the history
  • Loading branch information
mingxuanzhangsfdx committed Aug 1, 2024
1 parent b5c8aa3 commit a75c497
Show file tree
Hide file tree
Showing 4 changed files with 288 additions and 16 deletions.
8 changes: 5 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
{
"name": "@salesforce/templates",
"name": "@salesforce/templates-bundle",
"version": "61.4.0",
"salesforceApiVersion": "61",
"description": "Salesforce JS library for templates",
"bugs": "https://github.com/forcedotcom/salesforcedx-templates/issues",
"main": "lib/index.js",
"main": "dist/src/index.js",
"author": "Salesforce",
"homepage": "https://github.com/forcedotcom/salesforcedx-templates",
"license": "BSD-3-Clause",
"files": [
"/lib",
"/dist",
"/messages"
],
"dependencies": {
Expand Down Expand Up @@ -50,6 +50,7 @@
"eslint-plugin-prettier": "^3.1.3",
"husky": "^8.0.3",
"mocha": "^10.4.0",
"npm-dts": "^1.3.12",
"nyc": "^15.1.0",
"prettier": "^2.0.5",
"pretty-quick": "^3.1.0",
Expand All @@ -69,6 +70,7 @@
"scripts": {
"build": "yarn run clean:lib && yarn build:templates && yarn compile",
"build:templates": "node scripts/build-templates",
"bundle": "yarn build && node scripts/esbuild.config.js",
"clean:lib": "shx rm -rf lib && shx rm -rf coverage && shx rm -rf .nyc_output",
"commit": "git-cz",
"compile": "tsc -b",
Expand Down
12 changes: 7 additions & 5 deletions scripts/build-templates.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@ const fs = require('fs');
const path = require('path');
const shell = require('shelljs');

// Determine the directory based on the 'esbuild' argument
const isEsbuild = process.argv.includes('--esbuild');
const currDir = process.cwd();
const libDir = path.join(currDir, 'lib');
const libTemplatesDir = path.join(libDir, 'templates');
const outputDir = isEsbuild ? 'dist' : 'lib';
const outputTemplatesDir = path.join(currDir, outputDir, 'templates');
const srcTemplatesDir = path.join(currDir, 'src', 'templates');

if (!fs.existsSync(libDir)) {
shell.mkdir(libDir);
if (!fs.existsSync(outputDir)) {
shell.mkdir(outputDir);
}

shell.cp('-rf', srcTemplatesDir, libTemplatesDir);
shell.cp('-rf', srcTemplatesDir, outputTemplatesDir);
41 changes: 41 additions & 0 deletions scripts/esbuild.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
const { build } = require('esbuild');
const { exec } = require('child_process');
const { Generator } = require('npm-dts');
new Generator({
output: `dist/src/index.d.ts`,
}).generate();

const sharedConfig = {
bundle: true,
format: 'cjs',
platform: 'node',
external: [],
minify: false,
keepNames: true,
plugins: [],
};

(async () => {
await build({
...sharedConfig,
entryPoints: ['./lib/index.js'],
outdir: 'dist/src',
});
})()
.then(() => {
exec(
'node ./scripts/build-templates.js --esbuild',
(error, stdout, stderr) => {
if (error) {
console.error(`Error executing command: ${error}`);
return;
}
if (stderr) {
console.error(`Error output: ${stderr}`);
return;
}
console.log(`Output: ${stdout}`);
}
);
})
.catch(() => process.exit(1));
Loading

0 comments on commit a75c497

Please sign in to comment.