Skip to content

Commit 02b9735

Browse files
committed
Setup CDS extractor esbuild JS bundle
Changes the build process for the CDS extractor to produce `dist/cds-extractor.bundle.js` and `dist/cds-extractor.bundle.js.map` files instead of a pair of `.js` and `.js.map` files for every compiled `.ts` file. Allows for pre-build of the JavaScript code used to run the CDS extractor while committing the bare minimum number of files for the "distribution" of the build.
1 parent 9b941c4 commit 02b9735

11 files changed

+669
-110
lines changed

extractors/cds/tools/.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Override .gitignore configs from parent directories in order to
2+
# include the bundled JS code for the CDS extractor.
3+
!dist/
4+

extractors/cds/tools/dist/cds-extractor.bundle.js

Lines changed: 23 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

extractors/cds/tools/dist/cds-extractor.bundle.js.map

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
import { statSync } from 'fs';
2+
3+
import { build as esbuildFunc } from 'esbuild';
4+
5+
const buildOptions = {
6+
entryPoints: ['cds-extractor.ts'],
7+
bundle: true,
8+
platform: 'node',
9+
target: 'node18',
10+
outfile: 'dist/cds-extractor.bundle.js',
11+
external: [
12+
// Node.js built-in modules
13+
'fs',
14+
'path',
15+
'os',
16+
'child_process',
17+
'util',
18+
'events',
19+
'stream',
20+
'url',
21+
'crypto',
22+
'process',
23+
'buffer',
24+
'assert',
25+
'module',
26+
'net',
27+
'tls',
28+
'http',
29+
'https',
30+
'zlib',
31+
'readline',
32+
'worker_threads',
33+
],
34+
minify: true,
35+
sourcemap: true,
36+
format: 'cjs',
37+
banner: {
38+
js: '#!/usr/bin/env node',
39+
},
40+
logLevel: 'info',
41+
// Handle TypeScript files
42+
loader: {
43+
'.ts': 'ts',
44+
},
45+
// Resolve TypeScript paths
46+
resolveExtensions: ['.ts', '.js'],
47+
// Ensure proper module resolution
48+
mainFields: ['main', 'module'],
49+
conditions: ['node'],
50+
};
51+
52+
async function build() {
53+
try {
54+
console.log('🚀 Building CDS extractor bundle...');
55+
56+
const result = await esbuildFunc(buildOptions);
57+
58+
if (result.errors.length > 0) {
59+
console.error('❌ Build errors:', result.errors);
60+
process.exit(1);
61+
}
62+
63+
if (result.warnings.length > 0) {
64+
console.warn('⚠️ Build warnings:', result.warnings);
65+
}
66+
67+
console.log('✅ Bundle created successfully at dist/cds-extractor.bundle.js');
68+
69+
// Check bundle size
70+
const stats = statSync('dist/cds-extractor.bundle.js');
71+
// Convert bytes to MB
72+
const sizeInMB = stats.size / (1024 * 1024);
73+
console.log(`📦 Created CDS extractor JS bundle: total bundle size: ${sizeInMB.toFixed(2)} MB`);
74+
} catch (error) {
75+
console.error('❌ Build failed:', error);
76+
process.exit(1);
77+
}
78+
}
79+
80+
void build();

extractors/cds/tools/eslint.config.mjs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -173,12 +173,18 @@ export default defineConfig([
173173
},
174174
// Add JavaScript-specific configuration that doesn't use TypeScript parser
175175
{
176-
files: ['**/*.js', '**/.prettierrc.js', '**/jest.config.js'],
176+
files: [
177+
'**/*.js',
178+
'**/.prettierrc.js',
179+
'**/jest.config.js',
180+
'esbuild.config.mjs',
181+
'validate-bundle.js',
182+
],
177183
languageOptions: {
178184
// Use default parser for JS files (removes TS parser requirement)
179185
parser: undefined,
180186
ecmaVersion: 2018,
181-
sourceType: 'module',
187+
sourceType: 'script', // Allow CommonJS
182188
},
183189
rules: {
184190
// Disable TypeScript-specific rules for JS files
@@ -190,6 +196,8 @@ export default defineConfig([
190196
'@typescript-eslint/restrict-template-expressions': 'off',
191197
'@typescript-eslint/no-unsafe-argument': 'off',
192198
'@typescript-eslint/unbound-method': 'off',
199+
// Allow CommonJS for build scripts
200+
'import/no-commonjs': 'off',
193201
},
194202
},
195203
{

extractors/cds/tools/index-files.cmd

Lines changed: 15 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -6,65 +6,27 @@ REM - assumes it is run from the root of the project source directory;
66

77
where node >nul 2>nul
88
if %ERRORLEVEL% neq 0 (
9-
echo node executable is required (in PATH) to run the 'cds-extractor.js' script. Please install Node.js and try again.
9+
echo node executable is required (in PATH) to run the 'cds-extractor.bundle.js' script. Please install Node.js and try again.
1010
exit /b 2
1111
)
1212

13-
where npm >nul 2>nul
14-
if %ERRORLEVEL% neq 0 (
15-
echo npm executable is required (in PATH) to install the dependencies for the 'index-files.js' script.
16-
exit /b 3
17-
)
18-
1913
REM Set the _cwd variable to the present working directory as the directory
2014
REM from which this script was called, which we assume is the "source root" directory
2115
REM of the project that to be scanned / indexed.
2216
set "_cwd=%CD%"
2317
set "_script_dir=%~dp0"
24-
set "_cds_extractor_js_path=%_script_dir%dist\cds-extractor.js"
25-
set "_cds_extractor_node_modules_dir=%_script_dir%node_modules"
26-
27-
REM Change to the directory of this batch script to ensure that npm looks up the
28-
REM package.json file in the correct directory and installs the dependencies
29-
REM (i.e. node_modules) relative to this directory. This is technically a
30-
REM violation of the assumption that extractor scripts will be run with the
31-
REM current working directory set to the root of the project source, but we
32-
REM also need node_modules to be installed here and not in the project source
33-
REM root, so we make a compromise of:
34-
REM 1. changing to this batch script's directory;
35-
REM 2. passing the original working directory as a parameter to the
36-
REM cds-extractor.js script;
37-
REM 3. expecting the cds-extractor.js script to immediately change back to
38-
REM original working (aka the project source root) directory.
39-
40-
cd /d "%_script_dir%" && ^
41-
42-
REM Check if the 'node_modules' directory exists in the current script's directory.
43-
REM This is a highly imperfect check that CDS extractor dependencies have been installed.
44-
if not exist "%_cds_extractor_node_modules_dir%" (
45-
echo Installing dependencies for the CDS extractor script in '%_cds_extractor_node_modules_dir%' directory.
46-
npm install --quiet --no-audit --no-fund
47-
if %ERRORLEVEL% equ 0 (
48-
echo CDS extractor dependencies installed successfully.
49-
) else (
50-
echo Error: Failed to install CDS extractor dependencies.
51-
echo Please ensure that the dependencies have been installed by running 'npm install' in the 'extractors\cds\tools' directory.
52-
exit /b 4
53-
)
54-
)
55-
56-
REM Check if the 'cds-extractor.js' script exists at the expected path.
57-
if not exist "%_cds_extractor_js_path%" (
58-
echo Building the 'cds-extractor.js' script from TypeScript source.
59-
npm run build --silent
60-
if %ERRORLEVEL% equ 0 (
61-
echo CDS extractor script built successfully.
62-
) else (
63-
echo Error: Failed to build the CDS extractor script.
64-
echo Please ensure that the TypeScript source has been compiled to JavaScript in the 'dist' directory.
65-
exit /b 5
66-
)
18+
set "_cds_extractor_bundle_path=%_script_dir%dist\cds-extractor.bundle.js"
19+
20+
REM Change to the directory of this batch script to maintain consistency with
21+
REM the original approach for path resolution.
22+
cd /d "%_script_dir%"
23+
24+
REM Check if the pre-built bundle exists
25+
if exist "%_cds_extractor_bundle_path%" (
26+
echo Running the 'cds-extractor.bundle.js' script
27+
node "%_cds_extractor_bundle_path%" "%_cwd%"
28+
) else (
29+
echo Error: CDS extractor bundle not found at '%_cds_extractor_bundle_path%'
30+
echo Please ensure that the bundle has been built by running 'npm run bundle' in the 'extractors\cds\tools' directory.
31+
exit /b 6
6732
)
68-
69-
echo Running the 'cds-extractor.js' script && ^
70-
node "%_cds_extractor_js_path%" "%_cwd%"

extractors/cds/tools/index-files.sh

Lines changed: 15 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -9,64 +9,27 @@ set -eu
99

1010
if ! command -v node > /dev/null
1111
then
12-
echo "node executable is required (in PATH) to run the 'cds-extractor.js' script. Please install Node.js and try again."
12+
echo "node executable is required (in PATH) to run the 'cds-extractor.bundle.js' script. Please install Node.js and try again."
1313
exit 2
1414
fi
1515

16-
if ! command -v npm > /dev/null
17-
then
18-
echo "npm executable is required (in PATH) to install the dependencies for the 'index-files.js' script."
19-
exit 3
20-
fi
21-
2216
# Set the _cwd variable to the present working directory (PWD) as the directory
2317
# from which this script was called, which we assume is the "source root" directory
2418
# of the project that to be scanned / indexed.
2519
_cwd="$PWD"
2620
_script_dir=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
27-
_cds_extractor_js_path="${_script_dir}/dist/cds-extractor.js"
28-
_cds_extractor_node_modules_dir="${_script_dir}/node_modules"
29-
30-
# Change to the directory of this shell script to ensure that npm looks up the
31-
# package.json file in the correct directory and installs the dependencies
32-
# (i.e. node_modules) relative to this directory. This is technically a violation
33-
# of the assumption that extractor scripts will be run with the current working
34-
# directory set to the root of the project source, but we also need node_modules
35-
# to be installed here and not in the project source root, so we make the following
36-
# compromise:
37-
#
38-
# 1. change to this shell script's directory;
39-
# 2. pass the original working directory as a parameter to the
40-
# cds-extractor.js script;
41-
# 3. expect the cds-extractor.js script to immediately change back to
42-
# original working (aka the project source root) directory.
43-
44-
cd "$_script_dir" && \
45-
46-
# Check if the 'node_modules' directory exists in the current script's directory.
47-
# This is a highly imperfect check that CDS extractor dependencies have been installed.
48-
if [ ! -d "${_cds_extractor_node_modules_dir}" ]; then
49-
echo "Installing dependencies for the CDS extractor script in '${_cds_extractor_node_modules_dir}' directory."
50-
if npm install --quiet --no-audit --no-fund ; then
51-
echo "CDS extractor dependencies installed successfully."
52-
else
53-
echo "Error: Failed to install CDS extractor dependencies."
54-
echo "Please ensure that the dependencies have been installed by running 'npm install' in the 'extractors/cds/tools' directory."
55-
exit 4
56-
fi
57-
fi
58-
59-
# Check if the 'cds-extractor.js' script exists at the expected path.
60-
if [ ! -f "${_cds_extractor_js_path}" ]; then
61-
echo "Building the 'cds-extractor.js' script from TypeScript source."
62-
if npm run build --silent; then
63-
echo "CDS extractor script built successfully."
64-
else
65-
echo "Error: Failed to build the CDS extractor script."
66-
echo "Please ensure that the TypeScript source has been compiled to JavaScript in the 'dist' directory."
67-
exit 5
68-
fi
21+
_cds_extractor_bundle_path="${_script_dir}/dist/cds-extractor.bundle.js"
22+
23+
# Change to the directory of this shell script to maintain consistency with
24+
# the original approach for path resolution.
25+
cd "$_script_dir"
26+
27+
# Check if the pre-built bundle exists
28+
if [ -f "${_cds_extractor_bundle_path}" ]; then
29+
echo "Running the 'cds-extractor.bundle.js' script"
30+
node "${_cds_extractor_bundle_path}" "$_cwd"
31+
else
32+
echo "Error: CDS extractor bundle not found at '${_cds_extractor_bundle_path}'"
33+
echo "Please ensure that the bundle has been built by running 'npm run bundle' in the 'extractors/cds/tools' directory."
34+
exit 6
6935
fi
70-
71-
echo "Running the 'cds-extractor.js' script" && \
72-
node "${_cds_extractor_js_path}" "$_cwd"

0 commit comments

Comments
 (0)