Skip to content

Commit d94e6b2

Browse files
author
Piotr Paulski
committed
chore: list bundled packages before publication
1 parent 7e279f1 commit d94e6b2

1 file changed

Lines changed: 82 additions & 8 deletions

File tree

rollup.config.mjs

Lines changed: 82 additions & 8 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,96 @@ 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 = path.join(process.cwd(), 'node_modules') + path.sep;
60+
61+
function getPackageName(modulePath) {
62+
// Handle rollup's virtual module paths (paths starting with 0x00)
63+
const absolutePathStart = modulePath.indexOf(projectNodeModulesPath);
64+
if (absolutePathStart < 0) {
65+
return null;
66+
}
67+
68+
const relativePath = modulePath.slice(projectNodeModulesPath.length + absolutePathStart);
69+
const segments = relativePath.split(path.sep);
70+
71+
// handle scoped packages
72+
if (segments[0].startsWith('@') && segments[1]) {
73+
return `${segments[0]}/${segments[1]}`;
74+
}
75+
return segments[0];
76+
}
77+
78+
/**
79+
* @returns {import('rollup').Plugin}
80+
*/
81+
function listBundledDeps() {
82+
aggregatedStats.totalBundles++;
83+
return {
84+
name: 'gather-bundled-dependencies',
85+
generateBundle(options, bundle) {
86+
for (const chunk of Object.values(bundle)) {
87+
if (chunk.type === 'chunk' && chunk.modules) {
88+
// chunk.modules is an object where keys are the absolute file paths
89+
Object.keys(chunk.modules).forEach(modulePath => {
90+
const packageName = getPackageName(modulePath);
91+
if (packageName) {
92+
aggregatedStats.bundledPackages.add(packageName);
93+
}
94+
});
95+
}
96+
}
97+
aggregatedStats.bundlesProcessed++;
98+
99+
// Only write the file when the last bundle is finished
100+
if (aggregatedStats.bundlesProcessed === aggregatedStats.totalBundles) {
101+
const outputPath = path.join(thirdPartyDir, 'bundled-packages.json');
102+
103+
const bundledDevDeps = Object.fromEntries(
104+
Object.entries(devDependencies)
105+
.filter(([name]) => aggregatedStats.bundledPackages.has(name) || name === 'chrome-devtools-frontend')
106+
);
107+
108+
fs.writeFileSync(
109+
outputPath,
110+
JSON.stringify(bundledDevDeps, null, 2)
111+
);
112+
}
113+
}
114+
};
115+
}
116+
44117
const seenDependencies = new Map();
45118

46119
/**
47-
* @param {string} wrapperIndexPath
120+
* @param {string} wrapperIndexName
48121
* @param {import('rollup').OutputOptions} [extraOutputOptions={}]
49122
* @param {import('rollup').ExternalOption} [external=[]]
50123
* @returns {import('rollup').RollupOptions}
51124
*/
52125
const bundleDependency = (
53-
wrapperIndexPath,
126+
wrapperIndexName,
54127
extraOutputOptions = {},
55128
external = [],
56129
) => ({
57-
input: wrapperIndexPath,
130+
input: path.join(thirdPartyDir, wrapperIndexName),
58131
output: {
59132
...extraOutputOptions,
60-
file: wrapperIndexPath,
133+
file: path.join(thirdPartyDir, wrapperIndexName),
61134
sourcemap: !isProduction,
62135
format: 'esm',
63136
},
@@ -79,7 +152,7 @@ const bundleDependency = (
79152
},
80153
output: {
81154
file: path.join(
82-
path.dirname(wrapperIndexPath),
155+
thirdPartyDir,
83156
'THIRD_PARTY_NOTICES',
84157
),
85158
template(dependencies) {
@@ -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)