UNPKG

1.77 kBJavaScriptView Raw
1"use strict";
2var __importDefault = (this && this.__importDefault) || function (mod) {
3 return (mod && mod.__esModule) ? mod : { "default": mod };
4};
5Object.defineProperty(exports, "__esModule", { value: true });
6exports.noCollisionNamespace = exports.packageNamespaceFactory = void 0;
7const murmurhash_1 = __importDefault(require("murmurhash"));
8function packageNamespaceFactory(findConfig, loadConfig, { dirname, relative }, hashSalt = '', prefix = '', normalizeVersion = (semver) => semver) {
9 return (namespace, stylesheetPath) => {
10 const configPath = findConfig('package.json', { cwd: dirname(stylesheetPath) });
11 if (!configPath) {
12 throw new Error(`Could not find package.json for ${stylesheetPath}`);
13 }
14 const config = loadConfig(configPath);
15 const fromRoot = relative(dirname(configPath), stylesheetPath).replace(/\\/g, '/');
16 return (prefix +
17 namespace +
18 murmurhash_1.default.v3(hashSalt + config.name + '@' + normalizeVersion(config.version) + '/' + fromRoot));
19 };
20}
21exports.packageNamespaceFactory = packageNamespaceFactory;
22function noCollisionNamespace({ prefix = '', used: usedNamespaces = new Map(), } = {}) {
23 return (namespace, stylesheetPath) => {
24 const ns = prefix + namespace;
25 const used = usedNamespaces.get(ns);
26 if (used) {
27 if (used.stylesheetPath !== stylesheetPath) {
28 throw new Error(`namespace (${ns} of ${stylesheetPath}) is already in use`);
29 }
30 }
31 else {
32 usedNamespaces.set(ns, { prefix, namespace, stylesheetPath });
33 }
34 return ns;
35 };
36}
37exports.noCollisionNamespace = noCollisionNamespace;
38//# sourceMappingURL=resolve-namespace-factories.js.map
\No newline at end of file