diff --git a/src/index.ts b/src/index.ts index fb9b272..ce9ca04 100644 --- a/src/index.ts +++ b/src/index.ts @@ -217,10 +217,23 @@ export class OxcMinifyPlugin { // Detect ecma target from webpack output environment const ecmaTarget = getEcmaTarget(compiler.options.output?.environment as Record | 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 ? { @@ -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; @@ -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 {