Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: source maps #372

Merged
merged 1 commit into from
Jan 8, 2021
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
290 changes: 289 additions & 1 deletion test/TerserPlugin.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import path from "path";
import { SourceMapConsumer } from "source-map";
import CopyWebpackPlugin from "copy-webpack-plugin";
import RequestShortener from "webpack/lib/RequestShortener";
import { javascript } from "webpack";
import { javascript, SourceMapDevToolPlugin } from "webpack";

import TerserPlugin from "../src/index";

Expand All @@ -23,6 +23,22 @@ import {

jest.setTimeout(10000);

expect.addSnapshotSerializer({
test: (value) => {
// For string that are valid JSON
if (typeof value !== "string") {
return false;
}

try {
return typeof JSON.parse(value) === "object";
} catch (e) {
return false;
}
},
print: (value) => JSON.stringify(JSON.parse(value), null, 2),
});

describe("TerserPlugin", () => {
const rawSourceMap = {
version: 3,
Expand Down Expand Up @@ -1320,4 +1336,276 @@ describe("TerserPlugin", () => {
resolve();
});
});

it('should work with the "devtool" option and the "false" value', async () => {
const compiler = getCompiler({
entry: path.resolve(__dirname, "./fixtures/entry.js"),
devtool: false,
});

new TerserPlugin().apply(compiler);

const stats = await compile(compiler);

expect(readsAssets(compiler, stats)).toMatchSnapshot("assets");
expect(getErrors(stats)).toMatchSnapshot("errors");
expect(getWarnings(stats)).toMatchSnapshot("warnings");
});

it('should work with "devtool" option and the "source-map" value', async () => {
const compiler = getCompiler({
entry: path.resolve(__dirname, "./fixtures/entry.js"),
devtool: "source-map",
});

new TerserPlugin().apply(compiler);

const stats = await compile(compiler);

expect(readsAssets(compiler, stats)).toMatchSnapshot("assets");
expect(getErrors(stats)).toMatchSnapshot("errors");
expect(getWarnings(stats)).toMatchSnapshot("warnings");
});

it('should work with "devtool" option and the "inline-source-map" value', async () => {
const compiler = getCompiler({
entry: path.resolve(__dirname, "./fixtures/entry.js"),
devtool: "inline-source-map",
});

new TerserPlugin().apply(compiler);

const stats = await compile(compiler);

expect(readsAssets(compiler, stats)).toMatchSnapshot("assets");
expect(getErrors(stats)).toMatchSnapshot("errors");
expect(getWarnings(stats)).toMatchSnapshot("warnings");
});

it('should work with "devtool" option and the "hidden-source-map" value', async () => {
const compiler = getCompiler({
entry: path.resolve(__dirname, "./fixtures/entry.js"),
devtool: "hidden-source-map",
});

new TerserPlugin().apply(compiler);

const stats = await compile(compiler);

expect(readsAssets(compiler, stats)).toMatchSnapshot("assets");
expect(getErrors(stats)).toMatchSnapshot("errors");
expect(getWarnings(stats)).toMatchSnapshot("warnings");
});

it('should work with "devtool" option and the "nosources-source-map" value', async () => {
const compiler = getCompiler({
entry: path.resolve(__dirname, "./fixtures/entry.js"),
devtool: "nosources-source-map",
});

new TerserPlugin().apply(compiler);

const stats = await compile(compiler);

expect(readsAssets(compiler, stats)).toMatchSnapshot("assets");
expect(getErrors(stats)).toMatchSnapshot("errors");
expect(getWarnings(stats)).toMatchSnapshot("warnings");
});

it('should work with "devtool" option and the "eval" value', async () => {
const compiler = getCompiler({
entry: path.resolve(__dirname, "./fixtures/entry.js"),
devtool: "eval",
});

new TerserPlugin().apply(compiler);

const stats = await compile(compiler);

expect(readsAssets(compiler, stats)).toMatchSnapshot("assets");
expect(getErrors(stats)).toMatchSnapshot("errors");
expect(getWarnings(stats)).toMatchSnapshot("warnings");
});

it('should work with "devtool" option and the "cheap-source-map" value', async () => {
const compiler = getCompiler({
entry: path.resolve(__dirname, "./fixtures/entry.js"),
devtool: "cheap-source-map",
});

new TerserPlugin().apply(compiler);

const stats = await compile(compiler);

expect(readsAssets(compiler, stats)).toMatchSnapshot("assets");
expect(getErrors(stats)).toMatchSnapshot("errors");
expect(getWarnings(stats)).toMatchSnapshot("warnings");
});

it('should work with the "SourceMapDevToolPlugin" plugin (like "source-map")', async () => {
const compiler = getCompiler({
entry: path.resolve(__dirname, "./fixtures/entry.js"),
devtool: false,
plugins: [
new SourceMapDevToolPlugin({
filename: "[file].map[query]",
module: true,
columns: true,
}),
],
});

new TerserPlugin().apply(compiler);

const stats = await compile(compiler);

expect(readsAssets(compiler, stats)).toMatchSnapshot("assets");
expect(getErrors(stats)).toMatchSnapshot("errors");
expect(getWarnings(stats)).toMatchSnapshot("warnings");
});

it('should work with the "SourceMapDevToolPlugin" plugin (like "cheap-source-map")', async () => {
const compiler = getCompiler({
entry: path.resolve(__dirname, "./fixtures/entry.js"),
devtool: false,
plugins: [
new SourceMapDevToolPlugin({
filename: "[file].map[query]",
module: false,
columns: false,
}),
],
});

new TerserPlugin().apply(compiler);

const stats = await compile(compiler);

expect(readsAssets(compiler, stats)).toMatchSnapshot("assets");
expect(getErrors(stats)).toMatchSnapshot("errors");
expect(getWarnings(stats)).toMatchSnapshot("warnings");
});

it("should work with multi compiler mode with source maps", async () => {
const multiCompiler = getCompiler([
{
mode: "production",
devtool: "eval",
bail: true,
cache: { type: "memory" },
entry: path.resolve(__dirname, "./fixtures/entry.js"),
output: {
path: path.resolve(__dirname, "./dist"),
filename: "[name]-1.js",
chunkFilename: "[id]-1.[name].js",
},
optimization: {
minimize: false,
},
plugins: [new TerserPlugin()],
},
{
mode: "production",
devtool: "source-map",
bail: true,
cache: { type: "memory" },
entry: path.resolve(__dirname, "./fixtures/entry.js"),
output: {
path: path.resolve(__dirname, "./dist"),
filename: "[name]-2.js",
chunkFilename: "[id]-2.[name].js",
},
optimization: {
minimize: false,
},
plugins: [new TerserPlugin()],
},
{
mode: "production",
bail: true,
cache: { type: "memory" },
devtool: false,
entry: path.resolve(__dirname, "./fixtures/entry.js"),
output: {
path: path.resolve(__dirname, "./dist"),
filename: "[name]-3.js",
chunkFilename: "[id]-3.[name].js",
},
optimization: {
minimize: false,
},
plugins: [
new SourceMapDevToolPlugin({
filename: "[file].map[query]",
module: false,
columns: false,
}),
new TerserPlugin(),
],
},
{
mode: "production",
bail: true,
cache: { type: "memory" },
devtool: false,
entry: path.resolve(__dirname, "./fixtures/entry.js"),
output: {
path: path.resolve(__dirname, "./dist"),
filename: "[name]-4.js",
chunkFilename: "[id]-4.[name].js",
},
optimization: {
minimize: false,
},
plugins: [
new SourceMapDevToolPlugin({
filename: "[file].map[query]",
module: true,
columns: true,
}),
new TerserPlugin(),
],
},
]);

const multiStats = await compile(multiCompiler);

multiStats.stats.forEach((stats, index) => {
expect(
readsAssets(multiCompiler.compilers[index], stats)
).toMatchSnapshot("assets");
expect(getErrors(stats)).toMatchSnapshot("errors");
expect(getWarnings(stats)).toMatchSnapshot("warnings");
});
});

it('should work with "devtool" option and the "source-map" value (the "parallel" option is "false")', async () => {
const compiler = getCompiler({
entry: path.resolve(__dirname, "./fixtures/entry.js"),
devtool: "source-map",
});

new TerserPlugin({ parallel: false }).apply(compiler);

const stats = await compile(compiler);

expect(readsAssets(compiler, stats)).toMatchSnapshot("assets");
expect(getErrors(stats)).toMatchSnapshot("errors");
expect(getWarnings(stats)).toMatchSnapshot("warnings");
});

it('should work with "devtool" option and the "source-map" value (the "parallel" option is "true")', async () => {
const compiler = getCompiler({
entry: path.resolve(__dirname, "./fixtures/entry.js"),
devtool: "source-map",
});

new TerserPlugin({ parallel: true }).apply(compiler);

const stats = await compile(compiler);

expect(readsAssets(compiler, stats)).toMatchSnapshot("assets");
expect(getErrors(stats)).toMatchSnapshot("errors");
expect(getWarnings(stats)).toMatchSnapshot("warnings");
});
});
Loading