--- /dev/null
+Copyright (C) 2018 Nick Downing <nick@ndcode.org>
+SPDX-License-Identifier: MIT
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to
+deal in the Software without restriction, including without limitation the
+rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
+sell copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
+IN THE SOFTWARE.
--- /dev/null
+/*
+ * Copyright (C) 2018 Nick Downing <nick@ndcode.org>
+ * SPDX-License-Identifier: MIT
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to
+ * deal in the Software without restriction, including without limitation the
+ * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
+ * sell copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
+ * IN THE SOFTWARE.
+ */
+
+let BuildCache = require('@ndcode/build_cache')
+let SVGO = require('svgo')
+let assert = require('assert')
+let disk_build = require('@ndcode/disk_build')
+let fs = require('fs')
+let path = require('path')
+let util = require('util')
+
+let fs_readFile = util.promisify(fs.readFile)
+let fs_writeFile = util.promisify(fs.writeFile)
+let svgo = new SVGO(
+ {
+ plugins: [
+ {cleanupAttrs: true},
+ {removeDoctype: true},
+ {removeXMLProcInst: true},
+ {removeComments: true},
+ {removeMetadata: true},
+ {removeTitle: true},
+ {removeDesc: true},
+ {removeUselessDefs: true},
+ {removeEditorsNSData: true},
+ {removeEmptyAttrs: true},
+ {removeHiddenElems: true},
+ {removeEmptyText: true},
+ {removeEmptyContainers: true},
+ {removeViewBox: false},
+ {cleanupEnableBackground: true},
+ {convertStyleToAttrs: true},
+ {convertColors: true},
+ {convertPathData: true},
+ {convertTransform: true},
+ {removeUnknownsAndDefaults: true},
+ {removeNonInheritableGroupAttrs: true},
+ {removeUselessStrokeAndFill: true},
+ {removeUnusedNS: true},
+ {cleanupIDs: true},
+ {cleanupNumericValues: true},
+ {moveElemsAttrsToGroup: true},
+ {moveGroupAttrsToElems: true},
+ {collapseGroups: true},
+ {removeRasterImages: false},
+ {mergePaths: true},
+ {convertShapeToPath: true},
+ {sortAttrs: true},
+ {removeDimensions: true}//,
+ //{removeAttrs: {attrs: '(stroke|fill)'}}
+ ]
+ }
+)
+
+let MinSVGCache = function(diag) {
+ if (!this instanceof MinSVGCache)
+ throw Error('MinSVGCache is a constructor')
+ this.diag = diag || false
+
+ this.build_cache = new BuildCache()
+}
+
+MinSVGCache.prototype.get = function(key) {
+ return /*await*/ this.build_cache.get(
+ key,
+ async result => {
+ let render = await disk_build(
+ key,
+ async temp_pathname => {
+ let render = await svgo.optimize(
+ await fs_readFile(key, {encoding: 'utf-8'}),
+ {path: key}
+ )
+ return /*await*/ fs_writeFile(
+ temp_pathname,
+ render.data,
+ {encoding: 'utf-8'}
+ )
+ },
+ this.diag
+ )
+ assert(render.deps.length === 0)
+ result.value = await fs_readFile(render.pathname)
+ }
+ )
+}
+
+module.exports = MinSVGCache