UNPKG

restrict-imports-loader

Version:

A Webpack loader to restrict imports in ES and TypeScript

66 lines 2.51 kB
"use strict"; var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k]; result["default"] = mod; return result; }; Object.defineProperty(exports, "__esModule", { value: true }); const path = __importStar(require("path")); const UP_ONE_LEVEL_LENGTH = 3; const REGEX_UP_LEVELS = new RegExp(String.raw `(?:\.\.\/)+`, "g"); function everythingInPackage(packageName) { return matchedBy(new RegExp(String.raw `^${packageName}(\/.*)?$`)); } exports.everythingInPackage = everythingInPackage; function matchedBy(r) { return importPath => Promise.resolve({ restricted: r.test(importPath) }); } exports.matchedBy = matchedBy; function climbingUpwardsMoreThan(levels) { return importPath => { const maxLength = lengthOfLongestMatch(normalize(importPath).match(REGEX_UP_LEVELS)); const maxClimbs = maxLength / UP_ONE_LEVEL_LENGTH; const s = maxClimbs === 1 ? "" : "s"; return Promise.resolve({ restricted: maxClimbs > levels, info: `(contains ${maxClimbs} consecutive occurrence${s} of "../"; max ${levels} allowed)`, }); }; } exports.climbingUpwardsMoreThan = climbingUpwardsMoreThan; function lengthOfLongestMatch(matches) { return (matches === null ? 0 : matches.reduce((acc, m) => Math.max(acc, m.length), 0)); } function normalize(importPath) { return importPath.replace(/\/+/g, "/").replace(/\/\.\//g, "/"); } exports.everythingInside = everything(true); exports.everythingOutside = everything(false); function everything(insideIsRestricted) { return dirs => { return (importPath, loaderContext) => new Promise((resolve, reject) => { loaderContext.resolve(loaderContext.context, importPath, (err, result) => { if (err === null) { resolve({ restricted: insideIsRestricted === dirs.some(contains(result)), info: `(resolved: ${result})`, }); } else { reject(err.message); } }); }); }; } function contains(contained) { return dir => { const relative = path.relative(dir, contained); return !relative.startsWith("..") && !path.isAbsolute(relative); }; } //# sourceMappingURL=deciders.js.map