@morjs/utils
Version:
mor utils
176 lines • 5.98 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.ComposeModuleStates = exports.cssProcessorFactory = exports.tsTransformerFactory = void 0;
const takin_1 = require("takin");
const typescript_1 = __importDefault(require("typescript"));
/**
* 生成 ts 的 transformer 插件
* 提供 visitor 作为参数
* 遍历所有 Node 节点
* @param visitor - 自定义节点 visitor
*/
function tsTransformerFactory(visitor) {
if (!visitor)
return;
return (ctx) => (sourceFile) => {
/**
* 遍历当前 node 以及所有的 child
*/
function visitAllNode(node) {
if (node == null)
return node;
const result = visitor(node, ctx);
if (result == null)
return result;
if (Array.isArray(result)) {
return result.map(function (item) {
if (item == null)
return item;
return typescript_1.default.visitEachChild(item, (child) => visitAllNode(child), ctx);
});
}
else {
return typescript_1.default.visitEachChild(result, (child) => visitAllNode(child), ctx);
}
}
return typescript_1.default.visitNode(sourceFile, (root) => visitAllNode(root));
};
}
exports.tsTransformerFactory = tsTransformerFactory;
/**
*
* @param name - css 处理器作为 postcss 插件的名称
* @param processor - 自定义 css 处理器
* @returns postcss 插件
*/
function cssProcessorFactory(name, processor) {
return {
postcssPlugin: name,
async Once(root, helpers) {
return await processor(root, helpers);
}
};
}
exports.cssProcessorFactory = cssProcessorFactory;
/**
* Compose 模块状态
*/
var ComposeModuleStates;
(function (ComposeModuleStates) {
/**
* 模块初始化
*/
ComposeModuleStates[ComposeModuleStates["initial"] = 0] = "initial";
/**
* 模块下载完成
*/
ComposeModuleStates[ComposeModuleStates["downloaded"] = 1] = "downloaded";
/**
* 模块前置脚本已执行
*/
ComposeModuleStates[ComposeModuleStates["beforeScriptsExecuted"] = 2] = "beforeScriptsExecuted";
/**
* 模块配置已载入
*/
ComposeModuleStates[ComposeModuleStates["configLoaded"] = 3] = "configLoaded";
/**
* 模块拷贝或编译完成
*/
ComposeModuleStates[ComposeModuleStates["copiedOrCompiled"] = 4] = "copiedOrCompiled";
/**
* 模块后置脚本已执行
*/
ComposeModuleStates[ComposeModuleStates["afterScriptsExecuted"] = 5] = "afterScriptsExecuted";
/**
* 模块已集成
*/
ComposeModuleStates[ComposeModuleStates["composed"] = 6] = "composed";
})(ComposeModuleStates = exports.ComposeModuleStates || (exports.ComposeModuleStates = {}));
/**
* 自定义 hook
*/
const CUSTOM_HOOKS = {
moduleDownloaded() {
return new takin_1.tapable.AsyncSeriesHook(['composeModuleInfo']);
},
moduleBeforeScriptsExecuted() {
return new takin_1.tapable.AsyncSeriesHook(['composeModuleInfo']);
},
moduleCopiedOrCompiled() {
return new takin_1.tapable.AsyncSeriesHook(['composeModuleInfo']);
},
moduleAfterScriptsExecuted() {
return new takin_1.tapable.AsyncSeriesHook(['composeModuleInfo']);
},
moduleConfigLoaded() {
return new takin_1.tapable.AsyncSeriesHook(['composeModuleInfo']);
},
moduleComposed() {
return new takin_1.tapable.AsyncSeriesHook(['composeModuleInfo']);
},
moduleFailedAttempt() {
return new takin_1.tapable.AsyncSeriesHook(['composeModuleInfo', 'error']);
},
webpackWrapper() {
return new takin_1.tapable.SyncHook(['webpackWrapper']);
},
compiler() {
return new takin_1.tapable.SyncHook(['compiler']);
},
entryBuilder() {
return new takin_1.tapable.AsyncSeriesHook(['entryBuilder']);
},
shouldAddPageOrComponent() {
return new takin_1.tapable.SyncBailHook(['pageOrComponentName', 'contextInfo']);
},
addEntry() {
return new takin_1.tapable.SyncWaterfallHook(['entryInfo']);
},
beforeBuildEntries() {
return new takin_1.tapable.AsyncSeriesHook(['entryBuilder']);
},
afterBuildEntries() {
return new takin_1.tapable.AsyncSeriesWaterfallHook(['entries', 'entryBuilder']);
},
configParser() {
return new takin_1.tapable.AsyncSeriesWaterfallHook(['config', 'options']);
},
scriptParser() {
return new takin_1.tapable.SyncWaterfallHook(['customTransformers', 'options']);
},
templateParser() {
return new takin_1.tapable.AsyncSeriesWaterfallHook(['tree', 'options']);
},
styleParser() {
return new takin_1.tapable.AsyncSeriesWaterfallHook(['postcssPlugins', 'options']);
},
sjsParser() {
return new takin_1.tapable.SyncWaterfallHook(['customTransformers', 'options']);
},
preprocessorParser() {
return new takin_1.tapable.AsyncSeriesWaterfallHook([
'fileContent',
'conditionalCompileContext',
'options'
]);
},
postprocessorParser() {
return new takin_1.tapable.AsyncSeriesWaterfallHook(['fileContent', 'options']);
},
generateInitFiles() {
return new takin_1.tapable.SyncWaterfallHook(['fileContent', 'moduleGroup']);
}
};
// 注册自定义 hook
// 避免同名 hook 多次注册
for (const name in CUSTOM_HOOKS) {
const hookName = name;
// 避免同名 hook 工厂函数多次注册引起报错
if (!(0, takin_1.isHookRegistered)(hookName)) {
(0, takin_1.registerHook)(hookName, CUSTOM_HOOKS[hookName]);
}
}
//# sourceMappingURL=hooks.js.map