Skip to content

Commit 3e816ae

Browse files
zyzyzyryxyPiotr Paulski
andauthored
chore: list bundled packages before publication (#861)
List bundled packages and their versions for automated vulnerability reporting. --------- Co-authored-by: Piotr Paulski <piotrpaulski@chromium.org>
1 parent d491d87 commit 3e816ae

1 file changed

Lines changed: 85 additions & 11 deletions

File tree

rollup.config.mjs

Lines changed: 85 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
*/
1717

1818
/**
19-
* @fileoverview take from {@link https://github.com/GoogleChromeLabs/chromium-bidi/blob/main/rollup.config.mjs | chromium-bidi}
19+
* @fileoverview taken from {@link https://github.com/GoogleChromeLabs/chromium-bidi/blob/main/rollup.config.mjs | chromium-bidi}
2020
* and modified to specific requirement.
2121
*/
2222

@@ -41,23 +41,99 @@ const allowedLicenses = [
4141
'0BSD',
4242
];
4343

44+
const thirdPartyDir = './build/src/third_party';
45+
46+
const {devDependencies = {}} = JSON.parse(
47+
fs.readFileSync(path.join(process.cwd(), 'package.json'), 'utf-8'),
48+
);
49+
50+
// special case for puppeteer, from which we only bundle puppeteer-core
51+
devDependencies['puppeteer-core'] = devDependencies['puppeteer'];
52+
53+
const aggregatedStats = {
54+
bundlesProcessed: 0,
55+
totalBundles: 0,
56+
bundledPackages: new Set(),
57+
};
58+
59+
const projectNodeModulesPath =
60+
path.join(process.cwd(), 'node_modules') + path.sep;
61+
62+
function getPackageName(modulePath) {
63+
// Handle rollup's virtual module paths (paths starting with 0x00)
64+
const absolutePathStart = modulePath.indexOf(projectNodeModulesPath);
65+
if (absolutePathStart < 0) {
66+
return null;
67+
}
68+
69+
const relativePath = modulePath.slice(
70+
projectNodeModulesPath.length + absolutePathStart,
71+
);
72+
const segments = relativePath.split(path.sep);
73+
74+
// handle scoped packages
75+
if (segments[0].startsWith('@') && segments[1]) {
76+
return `${segments[0]}/${segments[1]}`;
77+
}
78+
return segments[0];
79+
}
80+
81+
/**
82+
* @returns {import('rollup').Plugin}
83+
*/
84+
function listBundledDeps() {
85+
aggregatedStats.totalBundles++;
86+
return {
87+
name: 'gather-bundled-dependencies',
88+
generateBundle(options, bundle) {
89+
for (const chunk of Object.values(bundle)) {
90+
if (chunk.type === 'chunk' && chunk.modules) {
91+
// chunk.modules is an object where keys are the absolute file paths
92+
Object.keys(chunk.modules).forEach(modulePath => {
93+
const packageName = getPackageName(modulePath);
94+
if (packageName) {
95+
aggregatedStats.bundledPackages.add(packageName);
96+
}
97+
});
98+
}
99+
}
100+
aggregatedStats.bundlesProcessed++;
101+
102+
// Only write the file when the last bundle is finished
103+
if (aggregatedStats.bundlesProcessed === aggregatedStats.totalBundles) {
104+
const outputPath = path.join(thirdPartyDir, 'bundled-packages.json');
105+
106+
const bundledDevDeps = Object.fromEntries(
107+
Object.entries(devDependencies).filter(
108+
([name]) =>
109+
aggregatedStats.bundledPackages.has(name) ||
110+
name === 'chrome-devtools-frontend',
111+
),
112+
);
113+
114+
fs.writeFileSync(outputPath, JSON.stringify(bundledDevDeps, null, 2));
115+
}
116+
},
117+
};
118+
}
119+
44120
const seenDependencies = new Map();
45121

46122
/**
47-
* @param {string} wrapperIndexPath
123+
* @param {string} wrapperIndexName
48124
* @param {import('rollup').OutputOptions} [extraOutputOptions={}]
49125
* @param {import('rollup').ExternalOption} [external=[]]
50126
* @returns {import('rollup').RollupOptions}
51127
*/
52128
const bundleDependency = (
53-
wrapperIndexPath,
129+
wrapperIndexName,
54130
extraOutputOptions = {},
55131
external = [],
56132
) => ({
57-
input: wrapperIndexPath,
133+
input: path.join(thirdPartyDir, wrapperIndexName),
58134
output: {
59135
...extraOutputOptions,
60-
file: wrapperIndexPath,
136+
file: path.join(thirdPartyDir, wrapperIndexName),
61137
sourcemap: !isProduction,
62138
format: 'esm',
63139
},
@@ -78,10 +154,7 @@ const bundleDependency = (
78154
failOnViolation: true,
79155
},
80156
output: {
81-
file: path.join(
82-
path.dirname(wrapperIndexPath),
83-
'THIRD_PARTY_NOTICES',
84-
),
157+
file: path.join(thirdPartyDir, 'THIRD_PARTY_NOTICES'),
85158
template(dependencies) {
86159
for (const dependency of dependencies) {
87160
const key = `${dependency.name}:${dependency.version}`;
@@ -164,6 +237,7 @@ const bundleDependency = (
164237
},
165238
},
166239
}),
240+
listBundledDeps(),
167241
commonjs(),
168242
json(),
169243
nodeResolve(),
@@ -173,7 +247,7 @@ const bundleDependency = (
173247

174248
export default [
175249
bundleDependency(
176-
'./build/src/third_party/index.js',
250+
'index.js',
177251
{
178252
inlineDynamicImports: true,
179253
},
@@ -195,7 +269,7 @@ export default [
195269
},
196270
),
197271
bundleDependency(
198-
'./build/src/third_party/devtools-formatter-worker.js',
272+
'devtools-formatter-worker.js',
199273
{
200274
inlineDynamicImports: true,
201275
},

0 commit comments

Comments
 (0)