@@ -44,29 +44,56 @@ function hasAbsolutePaths(content: string): {
4444}
4545
4646/**
47- * Check if content contains bundled code that should be external .
48- * Looks for signs that dependencies were bundled inline instead of kept external .
47+ * Check if bundle contains inlined dependencies .
48+ * Reads package.json dependencies and ensures they are NOT bundled inline .
4949 */
50- function checkForBundledDependencies ( content : string ) : {
50+ async function checkBundledDependencies ( content : string ) : Promise < {
5151 bundledDeps : string [ ]
5252 hasNoBundledDeps : boolean
53- } {
54- // Dependencies that should remain external (not bundled inline).
55- // We check if their package code is bundled by looking for their exports.
56- const externalDeps = [
57- {
58- name : '@socketsecurity/registry' ,
59- // Look for characteristic exports from this package.
60- pattern : / \/ \/ @ s o c k e t s e c u r i t y \/ r e g i s t r y / ,
61- } ,
62- ]
53+ } > {
54+ // Read package.json to get runtime dependencies.
55+ const pkgJsonPath = path . join ( packagePath , 'package.json' )
56+ const pkgJson = JSON . parse ( await fs . readFile ( pkgJsonPath , 'utf8' ) )
57+ const dependencies = pkgJson . dependencies || { }
6358
6459 const bundledDeps : string [ ] = [ ]
6560
66- for ( const dep of externalDeps ) {
67- // If we find evidence that the package's code is bundled inline.
68- if ( dep . pattern . test ( content ) ) {
69- bundledDeps . push ( dep . name )
61+ // If we have NO dependencies, check that no external packages are bundled.
62+ if ( Object . keys ( dependencies ) . length === 0 ) {
63+ // Look for signs of bundled npm packages.
64+ // Bundled packages often have characteristic patterns like:
65+ // - var xxx_exports = {};
66+ // - __toCommonJS(package_name_exports)
67+ // - Multiple functions from same package bundled together.
68+ const bundledPackagePatterns = [
69+ // Socket packages that should always be external.
70+ / @ s o c k e t s e c u r i t y \/ r e g i s t r y / ,
71+ ]
72+
73+ for ( const pattern of bundledPackagePatterns ) {
74+ // Check if package name appears in context that suggests bundling.
75+ // Look for: var import_package = require("package") without the actual require call.
76+ // This would indicate the package code is bundled inline.
77+ const bundlePattern = new RegExp (
78+ `var\\s+\\w+\\s*=\\s*__toCommonJS\\([^)]*${ pattern . source } ` ,
79+ )
80+
81+ if ( bundlePattern . test ( content ) ) {
82+ bundledDeps . push ( pattern . source )
83+ }
84+ }
85+ } else {
86+ // If we have dependencies, check that they remain external (not bundled).
87+ for ( const dep of Object . keys ( dependencies ) ) {
88+ const escapedDep = dep . replace ( / [ / \\ ^ $ * + ? . ( ) | [ \] { } ] / g, '\\$&' )
89+ // Check if dependency code is bundled by looking for __toCommonJS pattern.
90+ const bundlePattern = new RegExp (
91+ `var\\s+\\w+\\s*=\\s*__toCommonJS\\([^)]*${ escapedDep } ` ,
92+ )
93+
94+ if ( bundlePattern . test ( content ) ) {
95+ bundledDeps . push ( dep )
96+ }
7097 }
7198 }
7299
@@ -96,22 +123,22 @@ describe('Bundle validation', () => {
96123 ) . toBe ( false )
97124 } )
98125
99- it ( 'should not bundle external dependencies inline' , async ( ) => {
126+ it ( 'should not bundle dependencies inline (validate against package.json dependencies) ' , async ( ) => {
100127 const cliPath = path . join ( buildPath , 'cli.js' )
101128 const content = await fs . readFile ( cliPath , 'utf8' )
102129
103- const result = checkForBundledDependencies ( content )
130+ const result = await checkBundledDependencies ( content )
104131
105132 if ( ! result . hasNoBundledDeps ) {
106- console . error ( 'Found bundled code from external dependencies :' )
133+ console . error ( 'Found bundled dependencies (should be external) :' )
107134 for ( const dep of result . bundledDeps ) {
108135 console . error ( ` - ${ dep } ` )
109136 }
110137 }
111138
112139 expect (
113140 result . hasNoBundledDeps ,
114- 'External dependencies should not be bundled inline' ,
141+ 'Dependencies from package.json should be external, not bundled inline' ,
115142 ) . toBe ( true )
116143 } )
117144} )
0 commit comments