Skip to content

Commit

Permalink
feat(builder): add --metafile flag (#6212)
Browse files Browse the repository at this point in the history
**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 <jan.kowalleck@gmail.com>
Co-authored-by: merceyz <merceyz@users.noreply.github.com>
  • Loading branch information
jkowalleck and merceyz committed Apr 10, 2024
1 parent 17de52a commit 4a889dc
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 0 deletions.
12 changes: 12 additions & 0 deletions .yarn/versions/7ec9c2c1.yml
@@ -0,0 +1,12 @@
releases:
"@yarnpkg/builder": minor

declined:
- "@yarnpkg/plugin-constraints"
- "@yarnpkg/plugin-exec"
- "@yarnpkg/plugin-interactive-tools"
- "@yarnpkg/plugin-stage"
- "@yarnpkg/plugin-typescript"
- "@yarnpkg/plugin-version"
- "@yarnpkg/plugin-workspace-tools"
- "@yarnpkg/cli"
12 changes: 12 additions & 0 deletions packages/yarnpkg-builder/sources/commands/build/bundle.ts
Expand Up @@ -70,6 +70,10 @@ export default class BuildBundleCommand extends Command {
description: `Includes a source map in the bundle`,
});

metafile = Option.Boolean(`--metafile`, false, {
description: `Emit a metafile next to the bundle`,
});

async execute() {
const basedir = process.cwd();
const portableBaseDir = npath.toPortablePath(basedir);
Expand All @@ -79,6 +83,7 @@ export default class BuildBundleCommand extends Command {
const plugins = findPlugins({basedir, profile: this.profile, plugins: this.plugins.map(plugin => path.resolve(plugin))});
const modules = [...getDynamicLibs().keys()].concat(plugins);
const output = ppath.join(portableBaseDir, `bundles/yarn.js`);
const metafile = this.metafile ? ppath.join(portableBaseDir, `bundles/yarn.meta.json`) : false;

let version = pkgJsonVersion(basedir);

Expand Down Expand Up @@ -132,6 +137,7 @@ export default class BuildBundleCommand extends Command {
}),
},
outfile: npath.fromPortablePath(output),
metafile: metafile !== false,
// Default extensions + .mjs
resolveExtensions: [`.tsx`, `.ts`, `.jsx`, `.mjs`, `.js`, `.css`, `.json`],
logLevel: `silent`,
Expand Down Expand Up @@ -160,6 +166,10 @@ export default class BuildBundleCommand extends Command {
}

await xfs.chmodPromise(output, 0o755);

if (metafile) {
await xfs.writeFilePromise(metafile, JSON.stringify(res.metafile));
}
});
});

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

report.reportSeparator();

Expand Down
13 changes: 13 additions & 0 deletions packages/yarnpkg-builder/sources/commands/build/plugin.ts
Expand Up @@ -54,6 +54,10 @@ export default class BuildPluginCommand extends Command {
description: `Includes a source map in the bundle`,
});

metafile = Option.Boolean(`--metafile`, false, {
description: `Emit a metafile next to the bundle`,
});

async execute() {
const basedir = process.cwd();
const portableBaseDir = npath.toPortablePath(basedir);
Expand All @@ -63,6 +67,7 @@ export default class BuildPluginCommand extends Command {
const name = getNormalizedName(rawName);
const prettyName = structUtils.prettyIdent(configuration, structUtils.parseIdent(name));
const output = ppath.join(portableBaseDir, `bundles/${name}.js`);
const metafile = this.metafile ? ppath.join(portableBaseDir, `bundles/${name}.meta.json`) : false;

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

Expand Down Expand Up @@ -113,6 +118,7 @@ export default class BuildPluginCommand extends Command {
entryPoints: [path.resolve(basedir, main ?? `sources/index`)],
bundle: true,
outfile: npath.fromPortablePath(output),
metafile: metafile !== false,
// Default extensions + .mjs
resolveExtensions: [`.tsx`, `.ts`, `.jsx`, `.mjs`, `.js`, `.css`, `.json`],
logLevel: `silent`,
Expand All @@ -139,6 +145,10 @@ export default class BuildPluginCommand extends Command {
report.reportWarning(MessageName.UNNAMED, `${warning.location.file}:${warning.location.line}:${warning.location.column}`);
report.reportWarning(MessageName.UNNAMED, ` ↳ ${warning.text}`);
}

if (metafile) {
await xfs.writeFilePromise(metafile, JSON.stringify(res.metafile));
}
});
});

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

return report.exitCode();
Expand Down

0 comments on commit 4a889dc

Please sign in to comment.