Build: Improve license creation (#16435)
This commit is contained in:
1 parent
3861dbc9fd
commit
092b9f72a5
4 files changed
+57
-67
No files matched your search
@@ -9,7 +9,6 @@ import createEsmUtils from "esm-utils";
|
||||
import { DIST_DIR, PROJECT_ROOT } from "../utils/index.js";
|
||||
import esbuildPluginAddDefaultExport from "./esbuild-plugins/add-default-export.js";
|
||||
import esbuildPluginEvaluate from "./esbuild-plugins/evaluate.js";
|
||||
import esbuildPluginLicense from "./esbuild-plugins/license.js";
|
||||
import esbuildPluginPrimitiveDefine from "./esbuild-plugins/primitive-define.js";
|
||||
import esbuildPluginReplaceModule from "./esbuild-plugins/replace-module.js";
|
||||
import esbuildPluginShimCommonjsObjects from "./esbuild-plugins/shim-commonjs-objects.js";
|
||||
@@ -38,7 +37,7 @@ const getRelativePath = (from, to) => {
|
||||
return relativePath;
|
||||
};
|
||||
|
||||
function getEsbuildOptions({ file, files, shouldCollectLicenses, cliOptions }) {
|
||||
function getEsbuildOptions({ file, files, cliOptions }) {
|
||||
// Save dependencies to file
|
||||
file.dependencies = [];
|
||||
|
||||
@@ -231,14 +230,6 @@ function getEsbuildOptions({ file, files, shouldCollectLicenses, cliOptions }) {
|
||||
replacements: [...replaceModule, ...(buildOptions.replaceModule ?? [])],
|
||||
}),
|
||||
file.platform === "universal" && esbuildPluginNodeModulePolyfills(),
|
||||
shouldCollectLicenses &&
|
||||
esbuildPluginLicense({
|
||||
cwd: PROJECT_ROOT,
|
||||
thirdParty: {
|
||||
includePrivate: true,
|
||||
output: (dependencies) => file.dependencies.push(...dependencies),
|
||||
},
|
||||
}),
|
||||
cliOptions.reports &&
|
||||
esbuildPluginVisualizer({ formats: cliOptions.reports }),
|
||||
esbuildPluginThrowWarnings({
|
||||
@@ -291,7 +282,7 @@ function getEsbuildOptions({ file, files, shouldCollectLicenses, cliOptions }) {
|
||||
|
||||
async function runEsbuild(options) {
|
||||
const esbuildOptions = getEsbuildOptions(options);
|
||||
await esbuild.build(esbuildOptions);
|
||||
return { esbuildResult: await esbuild.build(esbuildOptions) };
|
||||
}
|
||||
|
||||
export default runEsbuild;
|
||||
@@ -2,6 +2,7 @@ import fs from "node:fs/promises";
|
||||
import path from "node:path";
|
||||
|
||||
import { outdent } from "outdent";
|
||||
import rollupPluginLicense from "rollup-plugin-license";
|
||||
|
||||
import { DIST_DIR, PROJECT_ROOT } from "../utils/index.js";
|
||||
|
||||
@@ -17,9 +18,35 @@ function toBlockQuote(text) {
|
||||
.join("\n");
|
||||
}
|
||||
|
||||
async function getLicenseText(files) {
|
||||
let dependencies = files.flatMap((file) => file.dependencies);
|
||||
function getDependencies(results) {
|
||||
// A fake rollup chunk
|
||||
const chunk = {
|
||||
modules: Object.fromEntries(
|
||||
results
|
||||
.flatMap((result) =>
|
||||
Object.keys(result.esbuildResult?.metafile.inputs ?? {}),
|
||||
)
|
||||
.map((file) => [file, { renderedLength: 1 }]),
|
||||
),
|
||||
};
|
||||
|
||||
let dependencies;
|
||||
const plugin = rollupPluginLicense({
|
||||
cwd: PROJECT_ROOT,
|
||||
thirdParty: {
|
||||
includePrivate: true,
|
||||
output(_dependencies) {
|
||||
dependencies = _dependencies;
|
||||
},
|
||||
},
|
||||
});
|
||||
plugin.renderChunk("", chunk);
|
||||
plugin.generateBundle();
|
||||
|
||||
return dependencies;
|
||||
}
|
||||
|
||||
async function getLicenseText(dependencies) {
|
||||
dependencies = dependencies.filter(
|
||||
(dependency, index) =>
|
||||
// Exclude ourself
|
||||
@@ -121,21 +148,27 @@ async function getLicenseText(files) {
|
||||
].join("\n\n");
|
||||
}
|
||||
|
||||
async function buildLicense({ file, files, shouldCollectLicenses }) {
|
||||
if (files.indexOf(file) !== files.length - 1) {
|
||||
async function buildLicense({ file, files, results, cliOptions }) {
|
||||
if (files.at(-1) !== file) {
|
||||
throw new Error("license should be last file to build.");
|
||||
}
|
||||
|
||||
if (!shouldCollectLicenses) {
|
||||
const shouldBuildLicense =
|
||||
!cliOptions.playground &&
|
||||
!cliOptions.files &&
|
||||
typeof cliOptions.minify !== "boolean";
|
||||
|
||||
if (!shouldBuildLicense) {
|
||||
return;
|
||||
}
|
||||
|
||||
const javascriptFiles = files.filter((file) => file.kind === "javascript");
|
||||
if (javascriptFiles.some((file) => !Array.isArray(file.dependencies))) {
|
||||
return { skipped: true };
|
||||
const dependencies = getDependencies(results);
|
||||
|
||||
if (dependencies.length === 0) {
|
||||
throw new Error("Fail to collect dependencies.");
|
||||
}
|
||||
|
||||
const text = await getLicenseText(javascriptFiles);
|
||||
const text = await getLicenseText(dependencies);
|
||||
|
||||
await fs.writeFile(LICENSE_FILE, text);
|
||||
}
|
||||
|
||||
+13
-19
@@ -46,7 +46,7 @@ const clear = () => {
|
||||
readline.cursorTo(process.stdout, 0, null);
|
||||
};
|
||||
|
||||
async function buildFile({ file, files, shouldCollectLicenses, cliOptions }) {
|
||||
async function buildFile({ file, files, cliOptions, results }) {
|
||||
let displayName = file.output.file;
|
||||
if (
|
||||
(file.platform === "universal" && file.output.format !== "esm") ||
|
||||
@@ -69,37 +69,32 @@ async function buildFile({ file, files, shouldCollectLicenses, cliOptions }) {
|
||||
|
||||
let result;
|
||||
try {
|
||||
result = await file.build({
|
||||
file,
|
||||
files,
|
||||
shouldCollectLicenses,
|
||||
cliOptions,
|
||||
});
|
||||
result = await file.build({ file, files, cliOptions, results });
|
||||
} catch (error) {
|
||||
console.log(status.FAIL + "\n");
|
||||
console.error(error);
|
||||
throw error;
|
||||
}
|
||||
|
||||
result ??= {
|
||||
file: cliOptions.saveAs ?? file.output.file,
|
||||
};
|
||||
result ??= {};
|
||||
|
||||
if (result.skipped) {
|
||||
console.log(status.SKIPPED);
|
||||
return;
|
||||
}
|
||||
|
||||
const outputFile = cliOptions.saveAs ?? file.output.file;
|
||||
|
||||
const sizeMessages = [];
|
||||
if (cliOptions.printSize) {
|
||||
const { size } = await fs.stat(path.join(DIST_DIR, result.file));
|
||||
const { size } = await fs.stat(path.join(DIST_DIR, outputFile));
|
||||
sizeMessages.push(prettyBytes(size));
|
||||
}
|
||||
|
||||
if (cliOptions.compareSize) {
|
||||
// TODO: Use `import.meta.resolve` when Node.js support
|
||||
const stablePrettierDirectory = path.dirname(require.resolve("prettier"));
|
||||
const stableVersionFile = path.join(stablePrettierDirectory, result.file);
|
||||
const stableVersionFile = path.join(stablePrettierDirectory, outputFile);
|
||||
let stableSize;
|
||||
try {
|
||||
({ size: stableSize } = await fs.stat(stableVersionFile));
|
||||
@@ -108,7 +103,7 @@ async function buildFile({ file, files, shouldCollectLicenses, cliOptions }) {
|
||||
}
|
||||
|
||||
if (stableSize) {
|
||||
const { size } = await fs.stat(path.join(DIST_DIR, result.file));
|
||||
const { size } = await fs.stat(path.join(DIST_DIR, outputFile));
|
||||
const sizeDiff = size - stableSize;
|
||||
const message = chalk[sizeDiff > 0 ? "yellow" : "green"](
|
||||
prettyBytes(sizeDiff),
|
||||
@@ -129,6 +124,8 @@ async function buildFile({ file, files, shouldCollectLicenses, cliOptions }) {
|
||||
}
|
||||
|
||||
console.log(status.DONE);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
async function run() {
|
||||
@@ -151,15 +148,12 @@ async function run() {
|
||||
}
|
||||
}
|
||||
|
||||
const shouldCollectLicenses =
|
||||
!cliOptions.playground &&
|
||||
!cliOptions.files &&
|
||||
typeof cliOptions.minify !== "boolean";
|
||||
|
||||
console.log(chalk.inverse(" Building packages "));
|
||||
|
||||
const results = [];
|
||||
for (const file of files) {
|
||||
await buildFile({ file, files, shouldCollectLicenses, cliOptions });
|
||||
const result = await buildFile({ file, files, cliOptions, results });
|
||||
results.push(result);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,28 +0,0 @@
|
||||
import rollupPluginLicense from "rollup-plugin-license";
|
||||
|
||||
export default function esbuildPluginLicense(options) {
|
||||
const plugin = rollupPluginLicense(options);
|
||||
|
||||
return {
|
||||
name: "license",
|
||||
|
||||
setup(build) {
|
||||
build.initialOptions.metafile = true;
|
||||
|
||||
build.onEnd((result) => {
|
||||
if (result.errors.length > 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
const files = Object.keys(result.metafile.inputs);
|
||||
const chunk = {
|
||||
modules: Object.fromEntries(
|
||||
files.map((file) => [file, { renderedLength: 1 }]),
|
||||
),
|
||||
};
|
||||
plugin.renderChunk("", chunk);
|
||||
plugin.generateBundle();
|
||||
});
|
||||
},
|
||||
};
|
||||
}
|
||||
Reference in new issue
Block a user