Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 16 additions & 40 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -217,10 +217,23 @@ export class OxcMinifyPlugin {
// Detect ecma target from webpack output environment
const ecmaTarget = getEcmaTarget(compiler.options.output?.environment as Record<string, boolean | undefined> | undefined);

// Configure legal comments handling
const commentsFile = `${name}.LICENSE.txt`;
const codegenOptions = typeof this.options.minifyOptions.codegen === "object"
? { ...this.options.minifyOptions.codegen }
: {};
if (this.options.extractComments) {
codegenOptions.legalComments = "linked";
codegenOptions.legalCommentsPath = commentsFile;
} else {
codegenOptions.legalComments = "none";
}

const minifyOptions: MinifyOptions = {
...this.options.minifyOptions,
module: isModule,
sourcemap: sourcemap,
codegen: codegenOptions,
compress:
this.options.minifyOptions.compress !== false
? {
Expand Down Expand Up @@ -264,20 +277,9 @@ export class OxcMinifyPlugin {

// Handle extracted comments
let extractedCommentsSource: webpackSources.Source | undefined;
if (this.options.extractComments) {
const { code, comments } = extractLicenseComments(outputCode);
if (comments.length > 0) {
outputCode = code;
const commentsFile = `${name}.LICENSE.txt`;
const commentsText = comments.join("\n\n");
extractedCommentsSource = new RawSource(
`${commentsText}\n`,
);

// Add banner pointing to license file
const banner = `/*! For license information please see ${commentsFile} */`;
outputCode = `${banner}\n${outputCode}`;
}
if (this.options.extractComments && result.legalComments.length > 0) {
const commentsText = result.legalComments.join("\n\n");
extractedCommentsSource = new RawSource(`${commentsText}\n`);
}

let outputSource: webpackSources.Source;
Expand Down Expand Up @@ -344,32 +346,6 @@ export class OxcMinifyPlugin {
}
}

function extractLicenseComments(code: string): {
code: string;
comments: string[];
} {
const comments: string[] = [];
const resultCode = code.replace(
/\/\*[*!][\s\S]*?\*\//g,
(match) => {
if (isLicenseComment(match)) {
comments.push(match);
return "";
}
return match;
},
);
return { code: resultCode, comments };
}

function isLicenseComment(comment: string): boolean {
// Match comments that contain @license, @preserve, or start with /*!
return (
/(@license|@preserve)/i.test(comment) ||
comment.startsWith("/*!")
);
}

function getEcmaTarget(
environment?: Record<string, boolean | undefined>,
): string {
Expand Down