UNPKG

marko

Version:

UI Components + streaming, async, high performance, HTML templating for Node.js and the browser.

86 lines (74 loc) 2.86 kB
"use strict";exports.__esModule = true;exports.default = getComponentFiles;var _path = _interopRequireDefault(require("path")); var _escapeRegexp = require("./escape-regexp");function _interopRequireDefault(e) {return e && e.__esModule ? e : { default: e };} const COMPONENT_FILES_KEY = "cu_"; function getComponentFiles({ hub: { file } }) { const meta = file.metadata.marko; if (meta[COMPONENT_FILES_KEY]) { return meta[COMPONENT_FILES_KEY]; } const { filename } = file.opts; const fs = file.markoOpts.fileSystem; const dirname = _path.default.dirname(filename); const dirFiles = fs.readdirSync(dirname).sort(); const base = getBase(filename); const isEntry = "index" === base || "template" === base; const fileMatch = `(${(0, _escapeRegexp.escapeRegExp)(base)}\\.${isEntry ? "|" : ""})`; const styleMatch = new RegExp(`^${fileMatch}style\\.\\w+$`); const componentMatch = new RegExp(`^${fileMatch}component\\.\\w+$`); const splitComponentMatch = new RegExp( `^${fileMatch}component-browser\\.\\w+$` ); const packageMatch = new RegExp(`^${fileMatch}browser\\.\\json$`); let styleFile; let packageFile; let componentFile; let componentBrowserFile; for (const file of dirFiles) { if (!styleFile && styleMatch.test(file)) { styleFile = `./${file}`; } else if (!packageFile && packageMatch.test(file)) { packageFile = `./${file}`; } else if (!componentFile && componentMatch.test(file)) { componentFile = `./${file}`; meta.hasComponent = true; } else if (!componentBrowserFile && splitComponentMatch.test(file)) { componentBrowserFile = `./${file}`; meta.hasComponentBrowser = true; } } return meta[COMPONENT_FILES_KEY] = { styleFile, packageFile, componentFile, componentBrowserFile }; } /** * Given a filename, gets the base name, strips off the file extension * and removes any arc flags (https://github.com/eBay/arc). * * @example * getBase("/dir/foo.marko") // => "foo" * getBase("/dir/foo.bar.marko") // => "foo.bar" * getBase("/dir/foo[bar].marko") // => "foo" * getBase("/dir/foo[bar].baz.marko") // => "foo.baz" */ function getBase(filename) { const start = filename.lastIndexOf(_path.default.sep) + 1; const leftDot = filename.indexOf(".", start); if (leftDot === -1) { return filename.slice(start); } const rightDot = filename.lastIndexOf("."); const closeBracket = leftDot - 1; if (filename[closeBracket] === "]") { const openBracket = filename.lastIndexOf("[", closeBracket); if (openBracket > start) { // If we match a "]" before the extension and find a "[" before that, // then we have an arc flag. Strip it off. return ( filename.slice(start, openBracket) + filename.slice(leftDot, rightDot)); } } return filename.slice(start, rightDot); }