webpack-subresource-integrity
Version:
Webpack plugin for enabling Subresource Integrity
79 lines • 4.17 kB
JavaScript
/**
* Copyright (c) 2015-present, Waysact Pty Ltd
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const path_1 = require("path");
const tmp_promise_1 = __importDefault(require("tmp-promise"));
const __1 = require("..");
const test_utils_1 = require("./test-utils");
const merge_1 = __importDefault(require("lodash/merge"));
jest.unmock("html-webpack-plugin");
function runWebpackForSimpleProject(options = {}) {
return __awaiter(this, void 0, void 0, function* () {
const tmpDir = yield tmp_promise_1.default.dir({ unsafeCleanup: true });
return yield (0, test_utils_1.runWebpack)((0, merge_1.default)({
mode: "production",
output: { path: tmpDir.path, crossOriginLoading: "anonymous" },
entry: (0, path_1.resolve)(__dirname, "./__fixtures__/simple-project/src/."),
plugins: [new __1.SubresourceIntegrityPlugin()],
}, options));
});
}
test("enabled with webpack mode=production", () => __awaiter(void 0, void 0, void 0, function* () {
var _a;
const mainAsset = (_a = (yield runWebpackForSimpleProject())
.toJson()
.assets) === null || _a === void 0 ? void 0 : _a.find((asset) => asset.name === "main.js");
expect(mainAsset).toBeDefined();
expect(mainAsset === null || mainAsset === void 0 ? void 0 : mainAsset["integrity"]).toMatch(/^sha384-\S+$/);
}));
test("disabled with webpack mode=development", () => __awaiter(void 0, void 0, void 0, function* () {
var _b;
const mainAsset = (_b = (yield runWebpackForSimpleProject({ mode: "development" }))
.toJson()
.assets) === null || _b === void 0 ? void 0 : _b.find((asset) => asset.name === "main.js");
expect(mainAsset).toBeDefined();
expect(mainAsset === null || mainAsset === void 0 ? void 0 : mainAsset["integrity"]).toBeUndefined();
}));
const isHashWarning = (warning) => warning.message.match(/Use \[contenthash\] and ensure realContentHash/);
test("warns when [fullhash] is used", () => __awaiter(void 0, void 0, void 0, function* () {
const stats = yield runWebpackForSimpleProject({
output: { filename: "[fullhash].js" },
});
expect(stats.compilation.warnings.find(isHashWarning)).toBeDefined();
}));
test("warns when [contenthash] is used without realContentHash", () => __awaiter(void 0, void 0, void 0, function* () {
const stats = yield runWebpackForSimpleProject({
output: { filename: "[contenthash].js" },
optimization: { realContentHash: false },
});
expect(stats.compilation.warnings.find(isHashWarning)).toBeDefined();
}));
test("doesn't warn when [contenthash] is used with realContentHash", () => __awaiter(void 0, void 0, void 0, function* () {
const stats = yield runWebpackForSimpleProject({
output: { filename: "[contenthash].js" },
optimization: { realContentHash: true },
});
expect(stats.compilation.warnings).toHaveLength(0);
}));
test("doesn't warn with default options", () => __awaiter(void 0, void 0, void 0, function* () {
const stats = yield runWebpackForSimpleProject();
expect(stats.compilation.warnings).toHaveLength(0);
}));
//# sourceMappingURL=integration.test.js.map
;