Skip to content

Commit 4a889dc

Browse files
jkowalleckmerceyz
andauthored
feat(builder): add --metafile flag (#6212)
**What's the problem this PR addresses?** <!-- Describe the rationale of your PR. --> <!-- Link all issues that it closes. (Closes/Resolves #xxxx.) --> resolves #6211 ... **How did you fix it?** - added a boolean CLI switch to `builder build {bundle,plugin}` called `--metafile`, defaults to `false` - if CLI switch - is `false`, then the `esbuild` in instructed to NOT emit `metafile` - is `true`, then ... - the `esbuild` in instructed to emit `metafile` - the `metafile` data is written to target file `bundles/${name}.meta.json` - the target file is announced in the summary example ourput: ```shellSession $ builder build plugin --metafile ➤ YN0000: ┌ Building @yarnpkg/plugin-cyclonedx ➤ YN0000: └ Completed in 8s 619ms ➤ YN0000: ✓ Done building @yarnpkg/plugin-cyclonedx! ➤ YN0000: ? Bundle path: /.../cyclonedx-node-yarn/bundles/@yarnpkg/plugin-cyclonedx.js ➤ YN0000: ? Bundle size: 771.52 KiB ➤ YN0000: ? Bundle meta: /.../cyclonedx-node-yarn/bundles/@yarnpkg/plugin-cyclonedx.meta.json ``` <!-- A detailed description of your implementation. --> ... **Checklist** <!--- Don't worry if you miss something, chores are automatically tested. --> <!--- This checklist exists to help you remember doing the chores when you submit a PR. --> <!--- Put an `x` in all the boxes that apply. --> - [x] I have read the [Contributing Guide](https://yarnpkg.com/advanced/contributing). <!-- See https://yarnpkg.com/advanced/contributing#preparing-your-pr-to-be-released for more details. --> <!-- Check with `yarn version check` and fix with `yarn version check -i` --> - [x] I have set the packages that need to be released for my changes to be effective. <!-- The "Testing chores" workflow validates that your PR follows our guidelines. --> <!-- If it doesn't pass, click on it to see details as to what your PR might be missing. --> - [x] I will check that all automated PR checks pass before the PR gets reviewed. --------- Signed-off-by: Jan Kowalleck <[email protected]> Co-authored-by: merceyz <[email protected]>
1 parent 17de52a commit 4a889dc

File tree

3 files changed

+37
-0
lines changed

3 files changed

+37
-0
lines changed

.yarn/versions/7ec9c2c1.yml

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
releases:
2+
"@yarnpkg/builder": minor
3+
4+
declined:
5+
- "@yarnpkg/plugin-constraints"
6+
- "@yarnpkg/plugin-exec"
7+
- "@yarnpkg/plugin-interactive-tools"
8+
- "@yarnpkg/plugin-stage"
9+
- "@yarnpkg/plugin-typescript"
10+
- "@yarnpkg/plugin-version"
11+
- "@yarnpkg/plugin-workspace-tools"
12+
- "@yarnpkg/cli"

packages/yarnpkg-builder/sources/commands/build/bundle.ts

+12
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,10 @@ export default class BuildBundleCommand extends Command {
7070
description: `Includes a source map in the bundle`,
7171
});
7272

73+
metafile = Option.Boolean(`--metafile`, false, {
74+
description: `Emit a metafile next to the bundle`,
75+
});
76+
7377
async execute() {
7478
const basedir = process.cwd();
7579
const portableBaseDir = npath.toPortablePath(basedir);
@@ -79,6 +83,7 @@ export default class BuildBundleCommand extends Command {
7983
const plugins = findPlugins({basedir, profile: this.profile, plugins: this.plugins.map(plugin => path.resolve(plugin))});
8084
const modules = [...getDynamicLibs().keys()].concat(plugins);
8185
const output = ppath.join(portableBaseDir, `bundles/yarn.js`);
86+
const metafile = this.metafile ? ppath.join(portableBaseDir, `bundles/yarn.meta.json`) : false;
8287

8388
let version = pkgJsonVersion(basedir);
8489

@@ -132,6 +137,7 @@ export default class BuildBundleCommand extends Command {
132137
}),
133138
},
134139
outfile: npath.fromPortablePath(output),
140+
metafile: metafile !== false,
135141
// Default extensions + .mjs
136142
resolveExtensions: [`.tsx`, `.ts`, `.jsx`, `.mjs`, `.js`, `.css`, `.json`],
137143
logLevel: `silent`,
@@ -160,6 +166,10 @@ export default class BuildBundleCommand extends Command {
160166
}
161167

162168
await xfs.chmodPromise(output, 0o755);
169+
170+
if (metafile) {
171+
await xfs.writeFilePromise(metafile, JSON.stringify(res.metafile));
172+
}
163173
});
164174
});
165175

@@ -174,6 +184,8 @@ export default class BuildBundleCommand extends Command {
174184
report.reportInfo(null, `${Mark.Question} Bundle path: ${formatUtils.pretty(configuration, output, formatUtils.Type.PATH)}`);
175185
report.reportInfo(null, `${Mark.Question} Bundle size: ${formatUtils.pretty(configuration, (await xfs.statPromise(output)).size, formatUtils.Type.SIZE)}`);
176186
report.reportInfo(null, `${Mark.Question} Bundle version: ${formatUtils.pretty(configuration, version, formatUtils.Type.REFERENCE)}`);
187+
if (metafile)
188+
report.reportInfo(null, `${Mark.Question} Bundle meta: ${formatUtils.pretty(configuration, metafile, formatUtils.Type.PATH)}`);
177189

178190
report.reportSeparator();
179191

packages/yarnpkg-builder/sources/commands/build/plugin.ts

+13
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,10 @@ export default class BuildPluginCommand extends Command {
5454
description: `Includes a source map in the bundle`,
5555
});
5656

57+
metafile = Option.Boolean(`--metafile`, false, {
58+
description: `Emit a metafile next to the bundle`,
59+
});
60+
5761
async execute() {
5862
const basedir = process.cwd();
5963
const portableBaseDir = npath.toPortablePath(basedir);
@@ -63,6 +67,7 @@ export default class BuildPluginCommand extends Command {
6367
const name = getNormalizedName(rawName);
6468
const prettyName = structUtils.prettyIdent(configuration, structUtils.parseIdent(name));
6569
const output = ppath.join(portableBaseDir, `bundles/${name}.js`);
70+
const metafile = this.metafile ? ppath.join(portableBaseDir, `bundles/${name}.meta.json`) : false;
6671

6772
await xfs.mkdirPromise(ppath.dirname(output), {recursive: true});
6873

@@ -113,6 +118,7 @@ export default class BuildPluginCommand extends Command {
113118
entryPoints: [path.resolve(basedir, main ?? `sources/index`)],
114119
bundle: true,
115120
outfile: npath.fromPortablePath(output),
121+
metafile: metafile !== false,
116122
// Default extensions + .mjs
117123
resolveExtensions: [`.tsx`, `.ts`, `.jsx`, `.mjs`, `.js`, `.css`, `.json`],
118124
logLevel: `silent`,
@@ -139,6 +145,10 @@ export default class BuildPluginCommand extends Command {
139145
report.reportWarning(MessageName.UNNAMED, `${warning.location.file}:${warning.location.line}:${warning.location.column}`);
140146
report.reportWarning(MessageName.UNNAMED, ` ↳ ${warning.text}`);
141147
}
148+
149+
if (metafile) {
150+
await xfs.writeFilePromise(metafile, JSON.stringify(res.metafile));
151+
}
142152
});
143153
});
144154

@@ -152,6 +162,9 @@ export default class BuildPluginCommand extends Command {
152162
report.reportInfo(null, `${Mark.Check} Done building ${prettyName}!`);
153163
report.reportInfo(null, `${Mark.Question} Bundle path: ${formatUtils.pretty(configuration, output, formatUtils.Type.PATH)}`);
154164
report.reportInfo(null, `${Mark.Question} Bundle size: ${formatUtils.pretty(configuration, (await xfs.statPromise(output)).size, formatUtils.Type.SIZE)}`);
165+
if (metafile) {
166+
report.reportInfo(null, `${Mark.Question} Bundle meta: ${formatUtils.pretty(configuration, metafile, formatUtils.Type.PATH)}`);
167+
}
155168
}
156169

157170
return report.exitCode();

0 commit comments

Comments
 (0)