snowpack-plugin-stylus
Version:
Use the Stylus compiler to build .styl files from source
46 lines (45 loc) • 1.73 kB
JavaScript
;
const tslib_1 = require("tslib");
const fs = tslib_1.__importStar(require("fs"));
const render_1 = require("./render");
const get_dependencies_1 = require("./get-dependencies");
const pkg = require('../package.json');
const plugin = (snowpackConfig, options = {}) => {
const importedByMap = new Map();
function addDependencies(filePath, dependencies) {
dependencies.forEach((file) => {
const currentImports = importedByMap.get(filePath) || [];
importedByMap.set(file, currentImports.concat(filePath));
});
}
return {
name: pkg.name,
resolve: {
input: ['.styl'],
output: ['.css'],
},
onChange({ filePath }) {
const filesToMark = importedByMap.get(filePath) || [];
filesToMark.forEach((file) => this.markChanged && this.markChanged(file));
},
load({ filePath }) {
return tslib_1.__awaiter(this, void 0, void 0, function* () {
try {
const content = yield fs.promises.readFile(filePath, 'utf-8');
const dependencies = get_dependencies_1.getDependencies({ content, filePath, options });
addDependencies(filePath, dependencies);
const result = yield render_1.render(content, Object.assign(Object.assign({}, options), { filename: filePath }));
return {
'.css': {
code: result,
},
};
}
catch (err) {
console.error(err);
}
});
},
};
};
module.exports = plugin;