Skip to content
Merged
Show file tree
Hide file tree
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
167 changes: 87 additions & 80 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -559,8 +559,6 @@ class TerserPlugin {
`${name} from Terser plugin\nMinimizer doesn't return result`,
),
);

return;
}

if (output.warnings && output.warnings.length > 0) {
Expand Down Expand Up @@ -598,99 +596,104 @@ class TerserPlugin {
);
}

let shebang;

if (
/** @type {ExtractCommentsObject} */
(this.options.extractComments).banner !== false &&
output.extractedComments &&
output.extractedComments.length > 0 &&
output.code.startsWith("#!")
) {
const firstNewlinePosition = output.code.indexOf("\n");

shebang = output.code.slice(0, Math.max(0, firstNewlinePosition));
output.code = output.code.slice(
Math.max(0, firstNewlinePosition + 1),
);
}
if (output.code) {
let shebang;

if (output.map) {
output.source = new SourceMapSource(
output.code,
name,
output.map,
input,
/** @type {RawSourceMap} */
(inputSourceMap),
true,
);
} else {
output.source = new RawSource(output.code);
}

if (output.extractedComments && output.extractedComments.length > 0) {
const commentsFilename =
if (
/** @type {ExtractCommentsObject} */
(this.options.extractComments).filename ||
"[file].LICENSE.txt[query]";

let query = "";
let filename = name;

const querySplit = filename.indexOf("?");
(this.options.extractComments).banner !== false &&
output.extractedComments &&
output.extractedComments.length > 0 &&
output.code.startsWith("#!")
) {
const firstNewlinePosition = output.code.indexOf("\n");

if (querySplit >= 0) {
query = filename.slice(querySplit);
filename = filename.slice(0, querySplit);
shebang = output.code.slice(0, Math.max(0, firstNewlinePosition));
output.code = output.code.slice(
Math.max(0, firstNewlinePosition + 1),
);
}

const lastSlashIndex = filename.lastIndexOf("/");
const basename =
lastSlashIndex === -1
? filename
: filename.slice(lastSlashIndex + 1);
const data = { filename, basename, query };

output.commentsFilename = compilation.getPath(
commentsFilename,
data,
);

let banner;
if (output.map) {
output.source = new SourceMapSource(
output.code,
name,
output.map,
input,
/** @type {RawSourceMap} */
(inputSourceMap),
true,
);
} else {
output.source = new RawSource(output.code);
}

// Add a banner to the original file
if (
/** @type {ExtractCommentsObject} */
(this.options.extractComments).banner !== false
output.extractedComments &&
output.extractedComments.length > 0
) {
banner =
const commentsFilename =
/** @type {ExtractCommentsObject} */
(this.options.extractComments).banner ||
`For license information please see ${path
.relative(path.dirname(name), output.commentsFilename)
.replace(/\\/g, "/")}`;
(this.options.extractComments).filename ||
"[file].LICENSE.txt[query]";

if (typeof banner === "function") {
banner = banner(output.commentsFilename);
let query = "";
let filename = name;

const querySplit = filename.indexOf("?");

if (querySplit >= 0) {
query = filename.slice(querySplit);
filename = filename.slice(0, querySplit);
}

if (banner) {
output.source = new ConcatSource(
shebang ? `${shebang}\n` : "",
`/*! ${banner} */\n`,
output.source,
);
const lastSlashIndex = filename.lastIndexOf("/");
const basename =
lastSlashIndex === -1
? filename
: filename.slice(lastSlashIndex + 1);
const data = { filename, basename, query };

output.commentsFilename = compilation.getPath(
commentsFilename,
data,
);

let banner;

// Add a banner to the original file
if (
/** @type {ExtractCommentsObject} */
(this.options.extractComments).banner !== false
) {
banner =
/** @type {ExtractCommentsObject} */
(this.options.extractComments).banner ||
`For license information please see ${path
.relative(path.dirname(name), output.commentsFilename)
.replace(/\\/g, "/")}`;

if (typeof banner === "function") {
banner = banner(output.commentsFilename);
}

if (banner) {
output.source = new ConcatSource(
shebang ? `${shebang}\n` : "",
`/*! ${banner} */\n`,
output.source,
);
}
}
}

const extractedCommentsString = output.extractedComments
.sort()
.join("\n\n");
const extractedCommentsString = output.extractedComments
.sort()
.join("\n\n");

output.extractedCommentsSource = new RawSource(
`${extractedCommentsString}\n`,
);
output.extractedCommentsSource = new RawSource(
`${extractedCommentsString}\n`,
);
}
}

await cacheItem.storePromise({
Expand All @@ -714,6 +717,10 @@ class TerserPlugin {
}
}

if (!output.source) {
return;
}

/** @type {AssetInfo} */
const newInfo = { minimized: true };
const { source, extractedCommentsSource } = output;
Expand Down
1 change: 0 additions & 1 deletion src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -523,7 +523,6 @@ async function uglifyJsMinify(

return {
code: result.code,

map: result.map ? JSON.parse(result.map) : undefined,
errors: result.error ? [result.error] : [],
warnings: result.warnings || [],
Expand Down
20 changes: 16 additions & 4 deletions test/__snapshots__/TerserPlugin.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -3377,8 +3377,14 @@ exports[`TerserPlugin should work, extract comments in one file and use memory c

exports[`TerserPlugin should write stdout and stderr of workers to stdout and stderr of main process in not parallel mode: assets 1`] = `
Object {
"one.js": "",
"two.js": "",
"one.js": "/******/ (() => { // webpackBootstrap

/******/ })()
;",
"two.js": "/******/ (() => { // webpackBootstrap

/******/ })()
;",
}
`;

Expand All @@ -3400,8 +3406,14 @@ exports[`TerserPlugin should write stdout and stderr of workers to stdout and st

exports[`TerserPlugin should write stdout and stderr of workers to stdout and stderr of main process in parallel mode: assets 1`] = `
Object {
"one.js": "",
"two.js": "",
"one.js": "/******/ (() => { // webpackBootstrap

/******/ })()
;",
"two.js": "/******/ (() => { // webpackBootstrap

/******/ })()
;",
}
`;

Expand Down
17 changes: 17 additions & 0 deletions test/__snapshots__/minify-option.test.js.snap
Original file line number Diff line number Diff line change
@@ -1,5 +1,20 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`minify option should output errors and warning: errors 1`] = `
Array [
"Error: main.js from Terser plugin
Minimizer doesn't return result",
"Error: main.js from Terser plugin
error",
]
`;

exports[`minify option should output errors and warning: warnings 1`] = `
Array [
"Warning: warning",
]
`;

exports[`minify option should snapshot with extracting comments: assets 1`] = `
Object {
"main.js": "/*! For license information please see main.js.LICENSE.txt */
Expand Down Expand Up @@ -400,6 +415,8 @@ exports[`minify option should work using when the \`minify\` option is \`uglifyJ
Array [
"Error: broken.js from Terser plugin
Minimizer doesn't return result",
"Error: broken.js from Terser plugin
Unterminated template literal [broken.js:1,undefined]",
]
`;

Expand Down
16 changes: 16 additions & 0 deletions test/minify-option.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,22 @@ describe("minify option", () => {
expect(getWarnings(stats)).toMatchSnapshot("warnings");
});

it("should output errors and warning", async () => {
const compiler = getCompiler();

new TerserPlugin({
minify: () => ({
errors: ["error"],
warnings: ["warning"],
}),
}).apply(compiler);

const stats = await compile(compiler);

expect(getErrors(stats)).toMatchSnapshot("errors");
expect(getWarnings(stats)).toMatchSnapshot("warnings");
});

it("should snapshot with extracting comments", async () => {
const compiler = getCompiler({
entry: path.resolve(__dirname, "./fixtures/minify/es5.js"),
Expand Down
Loading