@roots/bud-cache 5.0.0-next.6 → 5.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,41 +1,25 @@
1
1
  <p align="center">
2
- <img alt="Bud" src="https://cdn.roots.io/app/uploads/logo-bud.svg" height="100" />
2
+ <img src="https://cdn.roots.io/app/uploads/logo-bud.svg" height="100" alt="Bud" />
3
3
  </p>
4
4
 
5
5
  <p align="center">
6
- <img
7
- alt="MIT License"
8
- src="https://img.shields.io/github/license/roots/bud?color=%23525ddc&style=flat-square"
9
- />
10
- <a href="https://app.fossa.com/projects/git%2Bgithub.com%2Froots%2Fbud?ref=badge_small" alt="FOSSA Status">
11
- <img src="https://app.fossa.com/api/projects/git%2Bgithub.com%2Froots%2Fbud.svg?type=small"/>
12
- </a>
13
- <a href="https://www.npmjs.com/package/@roots/bud">
14
- <img src="https://img.shields.io/npm/v/@roots/bud.svg?color=%23525ddc&style=flat-square" />
15
- </a>
16
- <a href="https://codecov.io/gh/roots/bud">
17
- <img src="https://codecov.io/gh/roots/bud/branch/next/graph/badge.svg?token=DRJ28OD8XD"/>
18
- </a>
19
- <a href="https://twitter.com/rootswp">
20
- <img
21
- alt="Follow Roots"
22
- src="https://img.shields.io/twitter/follow/rootswp.svg?color=%23525ddc&style=flat-square"
23
- />
24
- </a>
6
+ <img alt="MIT License" src="https://img.shields.io/github/license/roots/bud?color=%23525ddc&style=flat-square" /> <img alt="npm" src="https://img.shields.io/npm/v/@roots/bud.svg?color=%23525ddc&style=flat-square" /> <img alt="Follow Roots" src="https://img.shields.io/twitter/follow/rootswp.svg?color=%23525ddc&style=flat-square" />
25
7
  </p>
26
8
 
27
-
28
9
  <h1 align="center">
29
10
  <strong>@roots/bud-cache</strong>
30
11
  </h1>
31
12
 
32
- > Config caching
13
+ <p align="center">
14
+ Config caching
15
+ </p>
16
+
33
17
  ## Installation
34
18
 
35
19
  Install **@roots/bud-cache** to your project.
36
20
 
37
21
  ```shell
38
- yarn add @roots/bud-cache
22
+ yarn add @roots/bud-cache --dev
39
23
  ```
40
24
 
41
25
  ## Documentation
@@ -67,8 +51,4 @@ Help support our open-source development efforts by [becoming a patron](https://
67
51
  <a href="https://carrot.com/"><img src="https://cdn.roots.io/app/uploads/carrot.svg" alt="Carrot" width="200" height="150"/></a>
68
52
  <a href="https://www.c21redwood.com/"><img src="https://cdn.roots.io/app/uploads/c21redwood.svg" alt="C21 Redwood Realty" width="200" height="150"/></a>
69
53
  <a href="https://wordpress.com/"><img src="https://cdn.roots.io/app/uploads/wordpress.svg" alt="WordPress.com" width="200" height="150"/></a>
70
- <a href="https://icons8.com/"><img src="https://cdn.roots.io/app/uploads/icons8.svg" alt="Icons8" width="200" height="150"/></a>
71
- <a href="https://www.harnessup.com/"><img src="https://cdn.roots.io/app/uploads/harness-software.svg" alt="Harness Software" width="200" height="150"/></a>
72
- <a href="https://www.codersclan.com/"><img src="https://cdn.roots.io/app/uploads/coders-clan.svg" alt="Coders Clan" width="200" height="150"/></a>
73
- <a href="https://generodigital.com/"><img src="https://cdn.roots.io/app/uploads/genero.svg" alt="Genero" width="200" height="150"/></a>
74
- <a href="https://motto.ca/roots"><img src="https://cdn.roots.io/app/uploads/motto.svg" alt="Motto" width="200" height="150"/></a>
54
+ <a href="https://pantheon.io/"><img src="https://cdn.roots.io/app/uploads/pantheon.svg" alt="Pantheon" width="200" height="150"/></a>
@@ -1,13 +1,14 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.readFileSync = exports.createHash = exports.globby = exports.bind = exports.Bud = void 0;
3
+ exports.Bud = exports.writeFile = exports.writeJson = exports.remove = exports.readJson = exports.readFile = exports.ensureFile = exports.createHash = exports.globby = exports.bind = void 0;
4
4
  const tslib_1 = require("tslib");
5
- exports.Bud = (0, tslib_1.__importStar)(require("@roots/bud-framework"));
5
+ const Bud = (0, tslib_1.__importStar)(require("@roots/bud-framework"));
6
+ exports.Bud = Bud;
6
7
  var bud_support_1 = require("@roots/bud-support");
7
8
  Object.defineProperty(exports, "bind", { enumerable: true, get: function () { return bud_support_1.bind; } });
8
9
  Object.defineProperty(exports, "globby", { enumerable: true, get: function () { return bud_support_1.globby; } });
9
10
  var crypto_1 = require("crypto");
10
11
  Object.defineProperty(exports, "createHash", { enumerable: true, get: function () { return crypto_1.createHash; } });
11
12
  const bud_support_2 = require("@roots/bud-support");
12
- exports.readFileSync = bud_support_2.fs.readFileSync;
13
+ exports.ensureFile = bud_support_2.fs.ensureFile, exports.readFile = bud_support_2.fs.readFile, exports.readJson = bud_support_2.fs.readJson, exports.remove = bud_support_2.fs.remove, exports.writeJson = bud_support_2.fs.writeJson, exports.writeFile = bud_support_2.fs.writeFile;
13
14
  //# sourceMappingURL=cache.dependencies.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"cache.dependencies.js","sourceRoot":"","sources":["../../../src/Cache/cache.dependencies.ts"],"names":[],"mappings":";;;;AAAA,yEAA2C;AAC3C,kDAA+C;AAAvC,mGAAA,IAAI,OAAA;AAAE,qGAAA,MAAM,OAAA;AACpB,iCAAiC;AAAzB,oGAAA,UAAU,OAAA;AAElB,oDAAqC;AACvB,oBAAY,GAAI,gBAAE,cAAA"}
1
+ {"version":3,"file":"cache.dependencies.js","sourceRoot":"","sources":["../../../src/Cache/cache.dependencies.ts"],"names":[],"mappings":";;;;AAAA,uEAA2C;AAanC,kBAAG;AAZX,kDAA+C;AAAvC,mGAAA,IAAI,OAAA;AAAE,qGAAA,MAAM,OAAA;AACpB,iCAAiC;AAAzB,oGAAA,UAAU,OAAA;AAElB,oDAAqC;AAEnC,kBAAU,GAMR,gBAAE,aALJ,gBAAQ,GAKN,gBAAE,WAJJ,gBAAQ,GAIN,gBAAE,WAHJ,cAAM,GAGJ,gBAAE,SAFJ,iBAAS,GAEP,gBAAE,YADJ,iBAAS,GACP,gBAAE,WAAA"}
@@ -2,108 +2,104 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.Cache = void 0;
4
4
  const tslib_1 = require("tslib");
5
+ const fs_extra_1 = require("fs-extra");
6
+ const helpful_decorators_1 = require("helpful-decorators");
7
+ const lodash_1 = require("lodash");
5
8
  const cache_dependencies_1 = require("./cache.dependencies");
6
9
  /**
7
10
  * Cache service class
8
11
  *
9
- * @remarks
10
- * Interfaces with:
11
- *
12
- * - {@link @roots/bud-framework#Project} to determine project dependencies for snapshotting/validation.
13
- *
14
- * - {@link @roots/bud-framework#Build} via {@link @roots/bud-framework#Hooks} to update config.
15
- *
16
- * Facades:
17
- *
18
- * - {@link @roots/bud-framework#Api} can toggle cache settings with {@link Bud.Persist}
19
- *
20
12
  * @public
21
13
  */
22
14
  class Cache extends cache_dependencies_1.Bud.Cache.Abstract {
23
15
  /**
24
- * Service register event
16
+ * True if cache is enabled
25
17
  *
26
- * @decorator `@bind`
18
+ * @public
27
19
  */
28
- register(app) {
29
- app.hooks
30
- .on('build/cache', () => ({
31
- type: app.hooks.filter('build/cache/type'),
32
- }))
33
- .hooks.on('build/cache/type', () => 'memory');
20
+ get enabled() {
21
+ this.log('info', {
22
+ message: `cache feature flag: ${this.app.store.get('features.cache')}`,
23
+ });
24
+ this.log('info', {
25
+ message: `--cache flag: ${this.app.store.get('cli.flags.cache')}`,
26
+ });
27
+ return this.app.store.get('features.cache') ? true : false;
34
28
  }
35
29
  /**
36
- * Returns sha1 hash as a version string
30
+ * Type
37
31
  *
38
- * @decorator `@bind`
32
+ * @public
39
33
  */
40
- version() {
41
- return (0, cache_dependencies_1.createHash)('sha1')
42
- .update(this.hash())
43
- .digest('base64')
44
- .replace(/[^a-z0-9]/gi, '_')
45
- .toLowerCase();
34
+ get type() {
35
+ const flags = this.app.store.get('cli.flags');
36
+ const cache = this.app.store.get('cache');
37
+ if (!(0, lodash_1.isUndefined)(flags['cache.type'])) {
38
+ this.log('log', {
39
+ message: '--cache.type flag',
40
+ suffix: flags['cache.type'],
41
+ });
42
+ return flags['cache.type'];
43
+ }
44
+ if (!(0, lodash_1.isUndefined)(cache.type)) {
45
+ this.log('log', {
46
+ message: 'bud.store',
47
+ suffix: cache.type,
48
+ });
49
+ return cache.type;
50
+ }
51
+ this.log('warn', 'no cache setting was passed from cli flags or from settings');
52
+ return false;
46
53
  }
47
54
  /**
48
- * Returns cache storage directory
49
- *
50
- * @decorator `@bind`
55
+ * @public
51
56
  */
52
- directory() {
53
- return this.app.path('storage', 'cache');
57
+ boot() {
58
+ return (0, tslib_1.__awaiter)(this, void 0, void 0, function* () {
59
+ this.version = yield this.hashFileContents();
60
+ if (this.enabled) {
61
+ this.app.api.call('persist', this.type);
62
+ }
63
+ });
54
64
  }
55
65
  /**
56
- * Returns array of build dependency paths
57
- *
58
- * @remarks
59
- * @see https://webpack.js.org/configuration/cache/#cachebuilddependencies
60
- *
61
- * @decorator `@bind`
66
+ * @public
62
67
  */
63
- buildDependencies() {
64
- return [
65
- ...new Set(cache_dependencies_1.globby.globbySync([
66
- this.app.path('project', `${this.app.name}.config.{js,ts,yml,json}`),
67
- this.app.path('project', `${this.app.name}.${this.app.mode}.config.{js,ts.yml,json}`),
68
- ...this.getFrameworkEntrypoints(),
69
- this.app.path('storage', 'cache', '**', '*'),
70
- ])),
71
- ];
72
- }
73
- /**
74
- * Cache location: framework entrypoints
75
- *
76
- * @see https://webpack.js.org/configuration/resolve
77
- */
78
- getFrameworkEntrypoints() {
79
- var _a, _b, _c;
80
- const project = (_a = this.app.project) !== null && _a !== void 0 ? _a : this.app.parent.project;
81
- return ((_c = (_b = project.resolveFrom) === null || _b === void 0 ? void 0 : _b.map(dep => `${dep}/lib/cjs/index.js`)) !== null && _c !== void 0 ? _c : []);
82
- }
83
- /**
84
- * Returns hash of all build dependencies and parsed CLI arguments
85
- *
86
- * @decorator `@bind`
87
- */
88
- hash() {
89
- var _a;
90
- return JSON.stringify((_a = this.buildDependencies().reduce((all, file) => all.concat((0, cache_dependencies_1.readFileSync)(file, 'utf8')), process.argv.slice(3).join(''))) !== null && _a !== void 0 ? _a : '{}');
68
+ hashFileContents() {
69
+ return (0, tslib_1.__awaiter)(this, void 0, void 0, function* () {
70
+ const makeHash = (str) => (0, tslib_1.__awaiter)(this, void 0, void 0, function* () {
71
+ return (0, cache_dependencies_1.createHash)('sha1')
72
+ .update(str)
73
+ .digest('base64')
74
+ .replace(/[^a-z0-9]/gi, '_')
75
+ .toLowerCase();
76
+ });
77
+ try {
78
+ const paths = this.app.project.get('dependencies');
79
+ const strings = yield Promise.all(paths.map((path) => (0, tslib_1.__awaiter)(this, void 0, void 0, function* () { return (0, fs_extra_1.readFile)(path, 'utf8'); })));
80
+ const hash = yield makeHash(`${strings}${JSON.stringify(this.app.project.get('cli'))}`);
81
+ this.log('info', {
82
+ message: 'cache hash generated',
83
+ suffix: hash,
84
+ });
85
+ this.app.hooks.async('event.project.write', (project) => (0, tslib_1.__awaiter)(this, void 0, void 0, function* () {
86
+ project.set('cache.hash', hash);
87
+ return project;
88
+ }));
89
+ return hash;
90
+ }
91
+ catch (e) {
92
+ this.app.error('error hashing file contents for cache');
93
+ throw new Error(e);
94
+ }
95
+ });
91
96
  }
92
97
  }
93
98
  (0, tslib_1.__decorate)([
94
- cache_dependencies_1.bind
95
- ], Cache.prototype, "register", null);
96
- (0, tslib_1.__decorate)([
97
- cache_dependencies_1.bind
98
- ], Cache.prototype, "version", null);
99
- (0, tslib_1.__decorate)([
100
- cache_dependencies_1.bind
101
- ], Cache.prototype, "directory", null);
102
- (0, tslib_1.__decorate)([
103
- cache_dependencies_1.bind
104
- ], Cache.prototype, "buildDependencies", null);
99
+ helpful_decorators_1.bind
100
+ ], Cache.prototype, "boot", null);
105
101
  (0, tslib_1.__decorate)([
106
- cache_dependencies_1.bind
107
- ], Cache.prototype, "hash", null);
102
+ helpful_decorators_1.bind
103
+ ], Cache.prototype, "hashFileContents", null);
108
104
  exports.Cache = Cache;
109
105
  //# sourceMappingURL=index.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot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;YAAC,OAAO,CAAC,EAAE;gBACV,IAAI,CAAC,GAAG,CAAC,KAAK,CAAC,uCAAuC,CAAC,CAAA;gBACvD,MAAM,IAAI,KAAK,CAAC,CAAC,CAAC,CAAA;aACnB;QACH,CAAC;KAAA;CACF;AArDC;IADC,yBAAI;iCAOJ;AAMD;IADC,yBAAI;6CAyCJ;AA9HH,sBA+HC"}
package/lib/cjs/index.js CHANGED
@@ -9,7 +9,7 @@ exports.Cache = void 0;
9
9
  * @see https://roots.io/bud
10
10
  * @see https://github.com/roots/bud
11
11
  *
12
- * @packageDocumentation @betaDocumentation
12
+ * @packageDocumentation
13
13
  */
14
14
  var Cache_1 = require("./Cache");
15
15
  Object.defineProperty(exports, "Cache", { enumerable: true, get: function () { return Cache_1.Cache; } });
@@ -1,6 +1,7 @@
1
- export * as Bud from '@roots/bud-framework';
1
+ import * as Bud from '@roots/bud-framework';
2
2
  export { bind, globby } from '@roots/bud-support';
3
3
  export { createHash } from 'crypto';
4
4
  import { fs } from '@roots/bud-support';
5
- export const { readFileSync } = fs;
5
+ export const { ensureFile, readFile, readJson, remove, writeJson, writeFile, } = fs;
6
+ export { Bud };
6
7
  //# sourceMappingURL=cache.dependencies.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"cache.dependencies.js","sourceRoot":"","sources":["../../../src/Cache/cache.dependencies.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,GAAG,MAAM,sBAAsB,CAAA;AAC3C,OAAO,EAAC,IAAI,EAAE,MAAM,EAAC,MAAM,oBAAoB,CAAA;AAC/C,OAAO,EAAC,UAAU,EAAC,MAAM,QAAQ,CAAA;AAEjC,OAAO,EAAC,EAAE,EAAC,MAAM,oBAAoB,CAAA;AACrC,MAAM,CAAC,MAAM,EAAC,YAAY,EAAC,GAAG,EAAE,CAAA"}
1
+ {"version":3,"file":"cache.dependencies.js","sourceRoot":"","sources":["../../../src/Cache/cache.dependencies.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,GAAG,MAAM,sBAAsB,CAAA;AAC3C,OAAO,EAAC,IAAI,EAAE,MAAM,EAAC,MAAM,oBAAoB,CAAA;AAC/C,OAAO,EAAC,UAAU,EAAC,MAAM,QAAQ,CAAA;AAEjC,OAAO,EAAC,EAAE,EAAC,MAAM,oBAAoB,CAAA;AACrC,MAAM,CAAC,MAAM,EACX,UAAU,EACV,QAAQ,EACR,QAAQ,EACR,MAAM,EACN,SAAS,EACT,SAAS,GACV,GAAG,EAAE,CAAA;AACN,OAAO,EAAC,GAAG,EAAC,CAAA"}
@@ -1,105 +1,101 @@
1
- import { __decorate } from "tslib";
2
- import { bind, Bud, createHash, globby, readFileSync, } from './cache.dependencies';
1
+ import { __awaiter, __decorate } from "tslib";
2
+ import { readFile } from 'fs-extra';
3
+ import { bind } from 'helpful-decorators';
4
+ import { isUndefined } from 'lodash';
5
+ import { Bud, createHash } from './cache.dependencies';
3
6
  /**
4
7
  * Cache service class
5
8
  *
6
- * @remarks
7
- * Interfaces with:
8
- *
9
- * - {@link @roots/bud-framework#Project} to determine project dependencies for snapshotting/validation.
10
- *
11
- * - {@link @roots/bud-framework#Build} via {@link @roots/bud-framework#Hooks} to update config.
12
- *
13
- * Facades:
14
- *
15
- * - {@link @roots/bud-framework#Api} can toggle cache settings with {@link Bud.Persist}
16
- *
17
9
  * @public
18
10
  */
19
11
  export class Cache extends Bud.Cache.Abstract {
20
12
  /**
21
- * Service register event
22
- *
23
- * @decorator `@bind`
24
- */
25
- register(app) {
26
- app.hooks
27
- .on('build/cache', () => ({
28
- type: app.hooks.filter('build/cache/type'),
29
- }))
30
- .hooks.on('build/cache/type', () => 'memory');
31
- }
32
- /**
33
- * Returns sha1 hash as a version string
34
- *
35
- * @decorator `@bind`
36
- */
37
- version() {
38
- return createHash('sha1')
39
- .update(this.hash())
40
- .digest('base64')
41
- .replace(/[^a-z0-9]/gi, '_')
42
- .toLowerCase();
43
- }
44
- /**
45
- * Returns cache storage directory
13
+ * True if cache is enabled
46
14
  *
47
- * @decorator `@bind`
15
+ * @public
48
16
  */
49
- directory() {
50
- return this.app.path('storage', 'cache');
17
+ get enabled() {
18
+ this.log('info', {
19
+ message: `cache feature flag: ${this.app.store.get('features.cache')}`,
20
+ });
21
+ this.log('info', {
22
+ message: `--cache flag: ${this.app.store.get('cli.flags.cache')}`,
23
+ });
24
+ return this.app.store.get('features.cache') ? true : false;
51
25
  }
52
26
  /**
53
- * Returns array of build dependency paths
54
- *
55
- * @remarks
56
- * @see https://webpack.js.org/configuration/cache/#cachebuilddependencies
27
+ * Type
57
28
  *
58
- * @decorator `@bind`
29
+ * @public
59
30
  */
60
- buildDependencies() {
61
- return [
62
- ...new Set(globby.globbySync([
63
- this.app.path('project', `${this.app.name}.config.{js,ts,yml,json}`),
64
- this.app.path('project', `${this.app.name}.${this.app.mode}.config.{js,ts.yml,json}`),
65
- ...this.getFrameworkEntrypoints(),
66
- this.app.path('storage', 'cache', '**', '*'),
67
- ])),
68
- ];
31
+ get type() {
32
+ const flags = this.app.store.get('cli.flags');
33
+ const cache = this.app.store.get('cache');
34
+ if (!isUndefined(flags['cache.type'])) {
35
+ this.log('log', {
36
+ message: '--cache.type flag',
37
+ suffix: flags['cache.type'],
38
+ });
39
+ return flags['cache.type'];
40
+ }
41
+ if (!isUndefined(cache.type)) {
42
+ this.log('log', {
43
+ message: 'bud.store',
44
+ suffix: cache.type,
45
+ });
46
+ return cache.type;
47
+ }
48
+ this.log('warn', 'no cache setting was passed from cli flags or from settings');
49
+ return false;
69
50
  }
70
51
  /**
71
- * Cache location: framework entrypoints
72
- *
73
- * @see https://webpack.js.org/configuration/resolve
52
+ * @public
74
53
  */
75
- getFrameworkEntrypoints() {
76
- var _a, _b, _c;
77
- const project = (_a = this.app.project) !== null && _a !== void 0 ? _a : this.app.parent.project;
78
- return ((_c = (_b = project.resolveFrom) === null || _b === void 0 ? void 0 : _b.map(dep => `${dep}/lib/cjs/index.js`)) !== null && _c !== void 0 ? _c : []);
54
+ boot() {
55
+ return __awaiter(this, void 0, void 0, function* () {
56
+ this.version = yield this.hashFileContents();
57
+ if (this.enabled) {
58
+ this.app.api.call('persist', this.type);
59
+ }
60
+ });
79
61
  }
80
62
  /**
81
- * Returns hash of all build dependencies and parsed CLI arguments
82
- *
83
- * @decorator `@bind`
63
+ * @public
84
64
  */
85
- hash() {
86
- var _a;
87
- return JSON.stringify((_a = this.buildDependencies().reduce((all, file) => all.concat(readFileSync(file, 'utf8')), process.argv.slice(3).join(''))) !== null && _a !== void 0 ? _a : '{}');
65
+ hashFileContents() {
66
+ return __awaiter(this, void 0, void 0, function* () {
67
+ const makeHash = (str) => __awaiter(this, void 0, void 0, function* () {
68
+ return createHash('sha1')
69
+ .update(str)
70
+ .digest('base64')
71
+ .replace(/[^a-z0-9]/gi, '_')
72
+ .toLowerCase();
73
+ });
74
+ try {
75
+ const paths = this.app.project.get('dependencies');
76
+ const strings = yield Promise.all(paths.map((path) => __awaiter(this, void 0, void 0, function* () { return readFile(path, 'utf8'); })));
77
+ const hash = yield makeHash(`${strings}${JSON.stringify(this.app.project.get('cli'))}`);
78
+ this.log('info', {
79
+ message: 'cache hash generated',
80
+ suffix: hash,
81
+ });
82
+ this.app.hooks.async('event.project.write', (project) => __awaiter(this, void 0, void 0, function* () {
83
+ project.set('cache.hash', hash);
84
+ return project;
85
+ }));
86
+ return hash;
87
+ }
88
+ catch (e) {
89
+ this.app.error('error hashing file contents for cache');
90
+ throw new Error(e);
91
+ }
92
+ });
88
93
  }
89
94
  }
90
95
  __decorate([
91
96
  bind
92
- ], Cache.prototype, "register", null);
93
- __decorate([
94
- bind
95
- ], Cache.prototype, "version", null);
96
- __decorate([
97
- bind
98
- ], Cache.prototype, "directory", null);
99
- __decorate([
100
- bind
101
- ], Cache.prototype, "buildDependencies", null);
97
+ ], Cache.prototype, "boot", null);
102
98
  __decorate([
103
99
  bind
104
- ], Cache.prototype, "hash", null);
100
+ ], Cache.prototype, "hashFileContents", null);
105
101
  //# sourceMappingURL=index.js.map
@@ -1 +1 @@
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,KAAK,CAClB,qBAAqB,EACrB,CAAM,OAAO,EAAC,EAAE;oBACd,OAAO,CAAC,GAAG,CAAC,YAAY,EAAE,IAAI,CAAC,CAAA;oBAC/B,OAAO,OAAO,CAAA;gBAChB,CAAC,CAAA,CACF,CAAA;gBAED,OAAO,IAAI,CAAA;aACZ;YAAC,OAAO,CAAC,EAAE;gBACV,IAAI,CAAC,GAAG,CAAC,KAAK,CAAC,uCAAuC,CAAC,CAAA;gBACvD,MAAM,IAAI,KAAK,CAAC,CAAC,CAAC,CAAA;aACnB;QACH,CAAC;KAAA;CACF;AArDC;IADC,IAAI;iCAOJ;AAMD;IADC,IAAI;6CAyCJ"}
package/lib/esm/index.js CHANGED
@@ -6,7 +6,7 @@
6
6
  * @see https://roots.io/bud
7
7
  * @see https://github.com/roots/bud
8
8
  *
9
- * @packageDocumentation @betaDocumentation
9
+ * @packageDocumentation
10
10
  */
11
11
  export { Cache } from './Cache';
12
12
  //# sourceMappingURL=index.js.map