UNPKG

reboost

Version:

A super fast dev server for rapid web development

115 lines (114 loc) 5.09 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.UsePlugin = void 0; const tslib_1 = require("tslib"); const anymatch_1 = (0, tslib_1.__importDefault)(require("anymatch")); const chalk_1 = (0, tslib_1.__importDefault)(require("chalk")); // @ts-expect-error No need to install declaration file const hash_sum_1 = (0, tslib_1.__importDefault)(require("hash-sum")); const utils_1 = require("../utils"); const createPlugin = (options) => { // TODO: Remove `options.test` in v1.0 const aOpt = options; if (aOpt.test) { if (!options.include) options.include = aOpt.test; let message = 'UsePlugin: options.test is deprecated and will be removed in next major release. '; message += 'Use options.include instead.'; console.log(chalk_1.default.yellow(message)); } const plugins = Array.isArray(options.use) ? [].concat(...options.use) : [options.use]; const getProperties = (hookName, filterFn = true) => (plugins.map((plugin) => plugin[hookName]).filter(filterFn ? ((hook) => typeof hook === 'function') : () => true)); const test = (string) => ((0, anymatch_1.default)(options.include, string) && (options.exclude ? !(0, anymatch_1.default)(options.exclude, string) : true)); const names = getProperties('name', false); const cacheKeyGetterHooks = getProperties('getCacheKey'); const setupHooks = getProperties('setup'); const stopHooks = getProperties('stop'); const resolveHooks = getProperties('resolve'); const loadHooks = getProperties('load'); const transformContentHooks = getProperties('transformContent'); const transformIntoJSHooks = getProperties('transformIntoJS'); const transformJSContent = getProperties('transformJSContent'); const transformASTHooks = getProperties('transformAST'); return { name: 'core-use-plugin', getCacheKey(utils) { const cacheKeys = cacheKeyGetterHooks.map((getCacheKey) => getCacheKey(utils)); return (0, hash_sum_1.default)(names.join('') + '@' + cacheKeys.join('')); }, async setup(data) { const normalizedRootDir = data.config.rootDir.replace(/[\\/]*$/, '/').replace(/\\/g, '/'); const regex = /^(!?)(?:\.\/)(.*)/; const replacement = '$1' + normalizedRootDir + '$2'; const normalizeGlob = (glob) => glob.replace(regex, replacement); const fixIfGlob = (item) => typeof item === 'string' ? normalizeGlob(item) : item; ['include', 'exclude'].forEach((key) => { const value = options[key]; options[key] = Array.isArray(value) ? value.map(fixIfGlob) : fixIfGlob(value); }); for (const hook of setupHooks) await hook(data); }, async stop() { for (const hook of stopHooks) await hook(); }, async resolve(pathToResolve, relativeTo) { if (test(relativeTo)) { for (const hook of resolveHooks) { const result = await hook(pathToResolve, relativeTo); if (result) return result; } } }, async load(filePath) { if (test(filePath)) { for (const hook of loadHooks) { const result = await (0, utils_1.bind)(hook, this)(filePath); if (result) return result; } } }, async transformContent(data, filePath) { if (test(filePath)) { for (const hook of transformContentHooks) { const result = await (0, utils_1.bind)(hook, this)(data, filePath); if (result) return result; } } }, async transformIntoJS(data, filePath) { if (test(filePath)) { for (const hook of transformIntoJSHooks) { const result = await (0, utils_1.bind)(hook, this)(data, filePath); if (result) return result; } } }, async transformJSContent(data, filePath) { if (test(filePath)) { for (const hook of transformJSContent) { const result = await (0, utils_1.bind)(hook, this)(data, filePath); if (result) return result; } } }, async transformAST(ast, babel, filePath) { if (test(filePath)) { for (const hook of transformASTHooks) { await (0, utils_1.bind)(hook, this)(ast, babel, filePath); } } } }; }; function UsePlugin(...options) { return options.map(createPlugin); } exports.UsePlugin = UsePlugin;