@@ -6,11 +6,12 @@ import * as ESBuild from 'esbuild'
66import * as Zod from 'zod'
77import PackageJson from '@npmcli/package-json'
88import * as Semver from 'semver'
9+ import * as TsMorph from 'ts-morph'
910import * as Fs from 'node:fs'
1011import * as NodeHttps from 'node:https'
1112import * as Process from 'node:process'
1213
13- let BuildType : 'production' | 'development' = Zod . string ( ) . refine ( BT => BT === 'production' || BT === 'development' ) . default ( 'production' ) . parse ( Process . argv [ 4 ] ?? 'production' )
14+ let BuildType : 'production' | 'development' = Zod . string ( ) . refine ( BT => BT === 'production' || BT === 'development' ) . default ( 'production' ) . parse ( Process . argv [ 3 ] ?? 'production' )
1415console . log ( 'Build type set to:' , BuildType )
1516
1617let Version : string = ( await PackageJson . load ( './' ) ) . content . version
@@ -78,40 +79,48 @@ for (const SellerEntry of IABSellersJsonData.sellers) {
7879}
7980console . log ( 'Collected' , DomainsList . size , 'unique domains from IAB Sellers.json' )
8081
82+ const IndexFilePath = `./sources/src/#generated-${ crypto . randomUUID ( ) } .ts`
83+ Fs . copyFileSync ( './sources/src/index.ts' , IndexFilePath )
84+ const Project = new TsMorph . Project ( { tsConfigFilePath : './tsconfig.json' } )
85+ const IndexFile = Project . getSourceFileOrThrow ( IndexFilePath )
86+ const TargetedDomainsArray = IndexFile . getVariableDeclaration ( 'TargetedDomains' ) . getInitializerIfKindOrThrow ( TsMorph . SyntaxKind . ArrayLiteralExpression )
87+ for ( const Domain of DomainsList ) {
88+ TargetedDomainsArray . addElement ( `'${ Domain } '` )
89+ }
90+ await Project . save ( )
91+ console . log ( 'Updated targeted domains in' , IndexFilePath , '; total domains:' , DomainsList . size )
92+
8193const HeaderLocation = './sources/banner.txt'
8294let ConvertedHeader : string = ''
8395for ( const Line of Fs . readFileSync ( HeaderLocation , 'utf-8' ) . split ( '\n' ) ) {
8496 if ( Line . includes ( '%%VERSION_VALUE%%' ) ) {
8597 ConvertedHeader += Line . replaceAll ( '%%VERSION_VALUE%%' , Version ) + '\n'
8698 } else if ( Line . includes ( '%%NAME%%' ) ) {
8799 ConvertedHeader += Line . replaceAll ( '%%NAME%%' , BuildType === 'production' ? 'tinyShield' : 'tinyShield (Development)' ) + '\n'
88- } else if ( Line === '%%DOMAIN_INJECTION%%' ) {
89- for ( const DomainEntry of DomainsList ) {
90- ConvertedHeader += `// @match *://${ DomainEntry } /*\n`
91- ConvertedHeader += `// @match *://*.${ DomainEntry } /*\n`
92- }
93100 } else {
94101 ConvertedHeader += Line + '\n'
95102 }
96103}
97- console . log ( 'Generated header with domain injections and processing ' )
104+ console . log ( 'Generated header with userscript name and version ' )
98105let AttachHeaderPath = `/tmp/${ crypto . randomUUID ( ) } `
99106Fs . writeFileSync ( AttachHeaderPath , ConvertedHeader , 'utf-8' )
100107console . log ( 'Written temporary header file to:' , AttachHeaderPath )
101108await ESBuild . build ( {
102- entryPoints : [ './sources/src/index.ts' ] ,
109+ entryPoints : [ IndexFilePath ] ,
103110 bundle : true ,
104- minify : true ,
111+ minify : BuildType === 'production' ,
105112 define : {
106113 global : 'window'
107114 } ,
108115 inject : [ './sources/esbuild.inject.ts' ] ,
109116 banner : {
110117 js : Fs . readFileSync ( AttachHeaderPath , 'utf-8' )
111118 } ,
112- target : [ 'es2024' , 'chrome119' , 'firefox117 ' , 'safari121 ' ] ,
119+ target : [ 'es2024' , 'chrome119' , 'firefox142 ' , 'safari26 ' ] ,
113120 outfile : BuildType === 'production' ? './dist/tinyShield.user.js' : './dist/tinyShield.dev.user.js' ,
114121} )
115122console . log ( 'Build completed' )
116123Fs . rmSync ( AttachHeaderPath )
117- console . log ( 'Temporary header file removed' )
124+ console . log ( 'Temporary header file removed' )
125+ Fs . rmSync ( IndexFilePath )
126+ console . log ( 'Temporary source file removed' )
0 commit comments