Skip to content

Commit

Permalink
test: source maps (#372)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-akait authored Jan 8, 2021
1 parent 0633c9d commit e10b8b4
Show file tree
Hide file tree
Showing 4 changed files with 1,662 additions and 629 deletions.
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

0 comments on commit e10b8b4

Please sign in to comment.