UNPKG

@morfeo/compiler

Version:

![Morfeo logo](https://morfeo.dev/img/morfeo.png)

128 lines 5.83 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.collector = void 0; const tslib_1 = require("tslib"); const fs = tslib_1.__importStar(require("fs")); const path = tslib_1.__importStar(require("path")); const chokidar = tslib_1.__importStar(require("chokidar")); const glob_1 = require("glob"); const babel = tslib_1.__importStar(require("@babel/core")); const babel_plugin_1 = tslib_1.__importDefault(require("@morfeo/babel-plugin")); const web_1 = require("@morfeo/web"); const jss_1 = require("@morfeo/jss"); const utils_1 = require("@morfeo/utils"); const writer_1 = require("./writer"); const logger_1 = require("./logger"); const DEFAULT_BABEL_PRESETS = ['@babel/preset-env', '@babel/preset-typescript']; const DEFAULT_BABEL_PLUGINS = ['@babel/plugin-syntax-jsx']; const DEFAULT_OPTIONS = { emojis: false, classNamePrefix: '', entryPoints: [path.join('.', '**/*.{ts,tsx,js,jsx}')], output: path.join(__dirname, '..', 'css', 'morfeo.css'), theme: {}, watch: false, }; function createCollector() { const cache = new Map(); let options = DEFAULT_OPTIONS; let writer = (0, writer_1.createWriter)({ output: DEFAULT_OPTIONS.output, }); function init(pluginOptions) { var _a, _b, _c, _d; options = Object.assign(Object.assign(Object.assign({}, DEFAULT_OPTIONS), pluginOptions), { babel: Object.assign(Object.assign({}, pluginOptions.babel), { presets: Array.from(new Set([ ...DEFAULT_BABEL_PRESETS, ...(((_a = options.babel) === null || _a === void 0 ? void 0 : _a.presets) || []), ...(((_b = pluginOptions.babel) === null || _b === void 0 ? void 0 : _b.presets) || []), ])), plugins: Array.from(new Set([ ...DEFAULT_BABEL_PLUGINS, ...(((_c = options.babel) === null || _c === void 0 ? void 0 : _c.plugins) || []), ...(((_d = pluginOptions.babel) === null || _d === void 0 ? void 0 : _d.plugins) || []), [babel_plugin_1.default, {}], ])) }) }); web_1.morfeo.theme.set(options.theme); writer = (0, writer_1.createWriter)({ delay: 10, output: pluginOptions.output || DEFAULT_OPTIONS.output, }); } function write() { return tslib_1.__awaiter(this, void 0, void 0, function* () { function onWrite() { const { light = {}, dark = {} } = web_1.morfeo.theme.getMetadata(); const variables = (0, jss_1.getStyles)({ '@global': { ':root': light, [(0, web_1.resolveMultiThemeValue)('dark')]: { ':root': dark, }, }, }).sheet.toString(); const mergedCss = Array.from(cache.entries()).reduce((acc, [, { globalStyles, styles }]) => { return { globalStyles: (0, utils_1.deepMerge)(acc.globalStyles, globalStyles), styles: (0, utils_1.deepMerge)(acc.styles, styles), }; }, { globalStyles: {}, styles: {} }); const css = [ ...Object.values(mergedCss.globalStyles), ...Object.values(mergedCss.styles), ].join('\n'); return `${variables}\n${css}`; } return writer(onWrite); }); } function extract(fileName) { return tslib_1.__awaiter(this, void 0, void 0, function* () { const fileContent = fs.readFileSync(fileName, { encoding: 'utf-8' }); const result = yield babel.transformAsync(fileContent, Object.assign(Object.assign({}, options.babel), { sourceFileName: fileName, filename: path.basename(fileName), sourceMaps: false, code: false, ast: false })); // @ts-ignore if (!result || !result.metadata || !result.metadata.morfeo) { return { globalStyles: {}, styles: {} }; } // @ts-ignore return result.metadata.morfeo; }); } function collectFile(fileName) { return tslib_1.__awaiter(this, void 0, void 0, function* () { const css = yield extract(fileName); cache.set(fileName, css); write(); }); } function watch(entryPoints) { const watcher = chokidar.watch(entryPoints, { ignoreInitial: true, }); watcher.add(entryPoints).on('add', fileName => { logger_1.logger.debug(`${path.basename(fileName)} added`); collectFile(fileName); }); watcher.add(entryPoints).on('change', fileName => { logger_1.logger.debug(`${path.basename(fileName)} changed`); collectFile(fileName); }); } function collect() { return tslib_1.__awaiter(this, void 0, void 0, function* () { const entryPoints = options.entryPoints || DEFAULT_OPTIONS.entryPoints; logger_1.logger.debug(`Start extracting CSS from ${entryPoints}`); const fileNames = yield (0, glob_1.glob)(entryPoints); logger_1.logger.startTimer('css extraction'); Promise.all(fileNames.map(collectFile)).then(() => { const diff = logger_1.logger.endTimer('css extraction'); logger_1.logger.debug(`CSS extracted in ${diff} seconds.`); }); if (options.watch) { logger_1.logger.debug('Watching for file changes'); watch(entryPoints); } }); } return { init, collect }; } exports.collector = createCollector(); //# sourceMappingURL=collector.js.map