1 | "use strict";
|
2 | var __importDefault = (this && this.__importDefault) || function (mod) {
|
3 | return (mod && mod.__esModule) ? mod : { "default": mod };
|
4 | };
|
5 | Object.defineProperty(exports, "__esModule", { value: true });
|
6 | exports.noCollisionNamespace = exports.packageNamespaceFactory = void 0;
|
7 | const murmurhash_1 = __importDefault(require("murmurhash"));
|
8 | function 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 | }
|
21 | exports.packageNamespaceFactory = packageNamespaceFactory;
|
22 | function 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 | }
|
37 | exports.noCollisionNamespace = noCollisionNamespace;
|
38 |
|
\ | No newline at end of file |