UNPKG

snowpack-plugin-less

Version:

Use the Less compiler to build .less files from source

56 lines (55 loc) 1.94 kB
"use strict"; const tslib_1 = require("tslib"); const fs = tslib_1.__importStar(require("fs")); const render_1 = require("./render"); const pkg = require('../package.json'); const plugin = (snowpackConfig, options = {}) => { const importedByMap = new Map(); function addImportsToMap(filePath, importedPath) { const importedBy = importedByMap.get(importedPath); if (importedBy) { importedBy.add(filePath); } else { importedByMap.set(importedPath, new Set([filePath])); } } return { name: pkg.name, resolve: { input: ['.less'], output: ['.css'], }, onChange({ filePath }) { if (importedByMap.has(filePath)) { const importedBy = importedByMap.get(filePath); importedByMap.delete(filePath); if (this.markChanged) { for (const importerFilePath of importedBy) { this.markChanged(importerFilePath); } } } }, load({ filePath, isDev }) { return tslib_1.__awaiter(this, void 0, void 0, function* () { try { const content = yield fs.promises.readFile(filePath, 'utf-8'); const result = yield render_1.render(content, Object.assign(Object.assign({}, options), { filename: filePath })); if (isDev) { result.imports.forEach((importedPath) => addImportsToMap(filePath, importedPath)); } return { '.css': { code: result.css, }, }; } catch (err) { console.error(err); } }); }, }; }; module.exports = plugin;