77 * Bundles the extension with esbuild. Injects a shim for import.meta.url so
88 * SDK code that uses createRequire(import.meta.url) works in CJS output.
99 */
10- import esbuild from " esbuild" ;
11- import fs from " node:fs" ;
12- import path from " path" ;
13- import { fileURLToPath } from " url" ;
10+ import esbuild from ' esbuild' ;
11+ import fs from ' node:fs' ;
12+ import path from ' path' ;
13+ import { fileURLToPath } from ' url' ;
1414
1515const __filename = fileURLToPath ( import . meta. url ) ;
1616const __dirname = path . dirname ( __filename ) ;
1717
1818// scripts/ -> package root
19- const pkgRoot = path . resolve ( __dirname , ".." ) ;
19+ const pkgRoot = path . resolve ( __dirname , '..' ) ;
2020
2121// In CJS there is no import.meta; SDK's version.js uses createRequire(import.meta.url). Shim it.
2222// Use globalThis so the value is visible inside all module wrappers in the bundle.
@@ -25,19 +25,19 @@ const IMPORT_META_URL_SHIM =
2525
2626function loaderFor ( filePath ) {
2727 const ext = path . extname ( filePath ) ;
28- if ( ext === " .ts" || filePath . endsWith ( " .tsx" ) ) return "ts" ;
29- return "js" ;
28+ if ( ext === ' .ts' || filePath . endsWith ( ' .tsx' ) ) return 'ts' ;
29+ return 'js' ;
3030}
3131
3232const importMetaUrlPlugin = {
33- name : " import-meta-url-shim" ,
33+ name : ' import-meta-url-shim' ,
3434 setup ( build ) {
35- build . onLoad ( { filter : / \. ( t s | t s x | j s | m j s | c j s ) $ / } , ( args ) => {
36- const contents = fs . readFileSync ( args . path , " utf-8" ) ;
37- const replaced = contents . includes ( " import.meta.url" )
38- ? contents . replace ( / i m p o r t \. m e t a \. u r l / g, " globalThis.__import_meta_url" )
35+ build . onLoad ( { filter : / \. ( t s | t s x | j s | m j s | c j s ) $ / } , ( args ) => {
36+ const contents = fs . readFileSync ( args . path , ' utf-8' ) ;
37+ const replaced = contents . includes ( ' import.meta.url' )
38+ ? contents . replace ( / i m p o r t \. m e t a \. u r l / g, ' globalThis.__import_meta_url' )
3939 : contents ;
40- return { contents : replaced , loader : loaderFor ( args . path ) } ;
40+ return { contents : replaced , loader : loaderFor ( args . path ) } ;
4141 } ) ;
4242 } ,
4343} ;
@@ -47,69 +47,67 @@ const importMetaUrlPlugin = {
4747// we replace that require in the bundle output with the actual JSON (post-build).
4848// Also replace require.resolve('@salesforce/b2c-tooling-sdk/package.json') so it doesn't throw when
4949// the extension runs from a VSIX (no node_modules). We use __dirname so path.dirname(...) is the extension dist.
50- const sdkPkgJsonPath = path . join ( pkgRoot , ".." , "b2c-tooling-sdk" , "package.json" ) ;
51- const REQUIRE_RESOLVE_PACKAGE_JSON_RE = / r e q u i r e \d * \. r e s o l v e \s * \( \s * [ " ' ] @ s a l e s f o r c e \/ b 2 c - t o o l i n g - s d k \/ p a c k a g e \. j s o n [ " ' ] \s * \) / g;
50+ const sdkPkgJsonPath = path . join ( pkgRoot , '..' , 'b2c-tooling-sdk' , 'package.json' ) ;
51+ const REQUIRE_RESOLVE_PACKAGE_JSON_RE =
52+ / r e q u i r e \d * \. r e s o l v e \s * \( \s * [ " ' ] @ s a l e s f o r c e \/ b 2 c - t o o l i n g - s d k \/ p a c k a g e \. j s o n [ " ' ] \s * \) / g;
5253const REQUIRE_RESOLVE_REPLACEMENT = "require('path').join(__dirname, 'package.json')" ;
5354
5455function inlineSdkPackageJson ( ) {
55- const outPath = path . join ( pkgRoot , "dist" , "extension.js" ) ;
56- let str = fs . readFileSync ( outPath , "utf8" ) ;
57- const sdkPkg = JSON . stringify ( JSON . parse ( fs . readFileSync ( sdkPkgJsonPath , "utf8" ) ) ) ;
58- str = str . replace (
59- / r e q u i r e \d * \s * \( \s * [ " ' ] @ s a l e s f o r c e \/ b 2 c - t o o l i n g - s d k \/ p a c k a g e \. j s o n [ " ' ] \s * \) / g,
60- sdkPkg
61- ) ;
56+ const outPath = path . join ( pkgRoot , 'dist' , 'extension.js' ) ;
57+ let str = fs . readFileSync ( outPath , 'utf8' ) ;
58+ const sdkPkg = JSON . stringify ( JSON . parse ( fs . readFileSync ( sdkPkgJsonPath , 'utf8' ) ) ) ;
59+ str = str . replace ( / r e q u i r e \d * \s * \( \s * [ " ' ] @ s a l e s f o r c e \/ b 2 c - t o o l i n g - s d k \/ p a c k a g e \. j s o n [ " ' ] \s * \) / g, sdkPkg ) ;
6260 str = str . replace ( REQUIRE_RESOLVE_PACKAGE_JSON_RE , REQUIRE_RESOLVE_REPLACEMENT ) ;
63- fs . writeFileSync ( outPath , str , " utf8" ) ;
61+ fs . writeFileSync ( outPath , str , ' utf8' ) ;
6462}
6563
66- const watchMode = process . argv . includes ( " --watch" ) ;
64+ const watchMode = process . argv . includes ( ' --watch' ) ;
6765
6866const buildOptions = {
69- entryPoints : [ path . join ( pkgRoot , " src" , " extension.ts" ) ] ,
67+ entryPoints : [ path . join ( pkgRoot , ' src' , ' extension.ts' ) ] ,
7068 bundle : true ,
71- platform : " node" ,
72- format : " cjs" ,
73- target : " node18" ,
74- outfile : path . join ( pkgRoot , " dist" , " extension.js" ) ,
69+ platform : ' node' ,
70+ format : ' cjs' ,
71+ target : ' node18' ,
72+ outfile : path . join ( pkgRoot , ' dist' , ' extension.js' ) ,
7573 sourcemap : true ,
7674 metafile : true ,
77- external : [ " vscode" ] ,
75+ external : [ ' vscode' ] ,
7876 // In watch mode, include "development" so esbuild resolves the SDK's exports to .ts source files
7977 // directly (no SDK rebuild needed). Production builds use the built dist/ artifacts.
80- conditions : watchMode
81- ? [ "development" , "require" , "node" , "default" ]
82- : [ "require" , "node" , "default" ] ,
83- mainFields : [ "main" , "module" ] ,
84- banner : { js : IMPORT_META_URL_SHIM } ,
78+ conditions : watchMode ? [ 'development' , 'require' , 'node' , 'default' ] : [ 'require' , 'node' , 'default' ] ,
79+ mainFields : [ 'main' , 'module' ] ,
80+ banner : { js : IMPORT_META_URL_SHIM } ,
8581 plugins : [ importMetaUrlPlugin ] ,
86- logLevel : " info" ,
82+ logLevel : ' info' ,
8783} ;
8884
8985if ( watchMode ) {
9086 const ctx = await esbuild . context ( buildOptions ) ;
9187 await ctx . watch ( ) ;
92- console . log ( " [esbuild] watching for changes..." ) ;
88+ console . log ( ' [esbuild] watching for changes...' ) ;
9389} else {
9490 const result = await esbuild . build ( buildOptions ) ;
9591
9692 inlineSdkPackageJson ( ) ;
9793
9894 if ( result . metafile && process . env . ANALYZE_BUNDLE ) {
99- const metaPath = path . join ( pkgRoot , " dist" , " meta.json" ) ;
100- fs . writeFileSync ( metaPath , JSON . stringify ( result . metafile , null , 2 ) , " utf-8" ) ;
95+ const metaPath = path . join ( pkgRoot , ' dist' , ' meta.json' ) ;
96+ fs . writeFileSync ( metaPath , JSON . stringify ( result . metafile , null , 2 ) , ' utf-8' ) ;
10197 const inputs = Object . entries ( result . metafile . inputs ) . map ( ( [ file , info ] ) => ( {
10298 file : path . relative ( pkgRoot , file ) ,
10399 bytes : info . bytes ,
104100 } ) ) ;
105101 inputs . sort ( ( a , b ) => b . bytes - a . bytes ) ;
106102 const total = inputs . reduce ( ( s , i ) => s + i . bytes , 0 ) ;
107- console . log ( " \n--- Bundle analysis (top 40 by input size) ---" ) ;
103+ console . log ( ' \n--- Bundle analysis (top 40 by input size) ---' ) ;
108104 console . log ( `Total inputs: ${ ( total / 1024 / 1024 ) . toFixed ( 2 ) } MB\n` ) ;
109- inputs . slice ( 0 , 40 ) . forEach ( ( { file, bytes } , i ) => {
105+ inputs . slice ( 0 , 40 ) . forEach ( ( { file, bytes} , i ) => {
110106 const pct = ( ( bytes / total ) * 100 ) . toFixed ( 1 ) ;
111- console . log ( `${ String ( i + 1 ) . padStart ( 2 ) } ${ ( bytes / 1024 ) . toFixed ( 1 ) . padStart ( 8 ) } KB ${ pct . padStart ( 5 ) } % ${ file } ` ) ;
107+ console . log (
108+ `${ String ( i + 1 ) . padStart ( 2 ) } ${ ( bytes / 1024 ) . toFixed ( 1 ) . padStart ( 8 ) } KB ${ pct . padStart ( 5 ) } % ${ file } ` ,
109+ ) ;
112110 } ) ;
113- console . log ( " \nWrote" , metaPath ) ;
111+ console . log ( ' \nWrote' , metaPath ) ;
114112 }
115113}
0 commit comments