Change from pnpm to npm, add ./link.sh shortcut for npm style package linking
[min_css_cache.git] / README.md
1 # Build Cache wrapper for minifying CSS files
2
3 An NDCODE project.
4
5 ## Overview
6
7 The `min_css_cache` package exports a single constructor
8   `MinCSSCache(diag)`
9 which must be called with the `new` operator. The resulting cache object stores
10 the `utf-8`-encoded text of CSS files loaded from disk and minified.
11
12 See the `build_cache`, `disk_build`, and `clean-css` packages for more
13 information. The `MinCSSCache` object is essentially a wrapper object which
14 routes the request between these packages, to ensure that the minified CSS text
15 is either retrieved from RAM or minified from a source file as required.
16
17 ## Calling API
18
19 Suppose one has a `MinCSSCache` instance named `mcc`. It behaves somewhat like
20 an ES6 `Map` object, except that it only has the `mcc.get()` function, because
21 new objects are added to the cache by attempting to `get` them.
22
23 The interface for the `MinCSSCache`-provided instance function `mcc.get()` is:
24
25 `await mcc.get(key)` — retrieves the object stored under `key`, where
26 `key` is the on-disk pathname to a CSS file. A `Buffer` object is returned,
27 containing the `utf-8`-encoded text resulting from running `clean-css` on the
28 source file. The pathname and the returned result are cached for future reuse.
29
30 Before returning the cached copy, the existence and modification time of the
31 CSS file on disk is checked to make sure that the cache is up-to-date.
32 Otherwise, if the file doesn't exist an `ENOENT` exception is thrown, or if the
33 file exists it is loaded and minified and the cache updated for next time. The
34 minification is via disk, and skipped if an up-to-date disk result is present.
35
36 ## About dependencies
37
38 The `min_css_cache` is in NDCODE org-scope, as in `@ndcode/min_css_cache`. We
39 also have in NDCODE org-scope, some lightly modified versions of popular
40 packages such as `clean-css`, with added NDCODE hooks. Packages such as this
41 one, which simply use `clean-css` in a standard way without using the NDCODE
42 hooks, still depend on `@ndcode/clean-css` for consistency. This means that
43 when embedded in an NDCODE project, only one copy of each dependency is loaded.
44 If this isn't desired, please feel free to fork the package and adjust it.
45
46 ## About diagnostics
47
48 The `diag` argument to the constructor is a `bool`, which if `true` causes
49 messages to be printed via `console.log()` for all activities except for the
50 common case of retrieval when the object is already up-to-date. A `diag` value
51 of `undefined` is treated as `false`, thus it can be omitted in the usual case.
52
53 ## To be implemented
54
55 The `clean-css` package is called with standard NDCODE options. This is part of
56 the reason to have `min_css_cache`, so as to provide a consistent interface to
57 minifiers for JavaScript, CSS, SVG and so on. However, if some control of the
58 underlying minifier is needed in future, we could add an `options` object to
59 pass through, in which we'd inject our NDCODE options where not overridden.
60
61 It is intended that we will shortly add a timer function (or possibly just a
62 function that the user should call periodically) to flush built templates from
63 the cache after a stale time. There is otherwise no way to delete an object
64 from the cache, except by first deleting it on disk, then trying to `get` it.
65
66 ## GIT repository
67
68 The development version can be cloned, downloaded, or browsed with `gitweb` at:
69 https://git.ndcode.org/public/min_css_cache.git
70
71 ## License
72
73 All of our NPM packages are MIT licensed, please see LICENSE in the repository.
74
75 ## Contributions
76
77 The caching system is under active development (and is part of a larger project
78 that is also under development) and thus the API is tentative. Please go ahead
79 and incorporate the system into your project, or try out our example webserver
80 built on the system, subject to the caution that the API could change. Please
81 send us your experience and feedback, and let us know of improvements you make.
82
83 Contact: Nick Downing <nick@ndcode.org>