UNPKG

@tarojs/webpack-runner

Version:

webpack runner for taro

103 lines 4.95 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); const helper_1 = require("@tarojs/helper"); const lodash_1 = require("lodash"); const path = require("path"); const utils_1 = require("../utils"); const TaroComponentsExportsPlugin_1 = require("./TaroComponentsExportsPlugin"); const PLUGIN_NAME = 'TaroH5Plugin'; class TaroH5Plugin { constructor(options = {}) { this.tryAsync = fn => (arg, callback) => __awaiter(this, void 0, void 0, function* () { try { yield fn(arg); callback(); } catch (err) { callback(err); } }); this.options = (0, lodash_1.defaults)(options || {}, { appPath: '', sourceDir: '', entryFileName: 'app', routerConfig: {}, framework: helper_1.FRAMEWORK_MAP.NERV, frameworkExts: helper_1.SCRIPT_EXT, runtimePath: [], pxTransformConfig: { baseFontSize: 20, deviceRatio: {}, designWidth: 750, minRootSize: 20, unitPrecision: 5, targetUnit: 'rem' } }); } apply(compiler) { const { entry } = compiler.options; this.appHelper = new utils_1.AppHelper(entry, this.options); compiler.hooks.run.tapAsync(PLUGIN_NAME, this.tryAsync(() => { this.run(); })); compiler.hooks.watchRun.tapAsync(PLUGIN_NAME, this.tryAsync(() => { this.run(); })); compiler.hooks.make.tapAsync(PLUGIN_NAME, this.tryAsync((compilation) => __awaiter(this, void 0, void 0, function* () { var _a, _b; yield ((_b = (_a = this.options).onCompilerMake) === null || _b === void 0 ? void 0 : _b.call(_a, compilation, compiler, this)); }))); compiler.hooks.compilation.tap(PLUGIN_NAME, compilation => { compilation.hooks.normalModuleLoader.tap(PLUGIN_NAME, (_loaderContext, module) => { const { entryFileName, sourceDir, routerConfig, isBuildNativeComp } = this.options; const { dir, name } = path.parse(module.resource); const suffixRgx = /\.(boot|config)/; if (!suffixRgx.test(name)) return; const filePath = path.join(dir, name); const pageName = filePath.replace(sourceDir + (process.platform === 'win32' ? '\\' : '/'), '').replace(suffixRgx, ''); const routerMode = (routerConfig === null || routerConfig === void 0 ? void 0 : routerConfig.mode) || 'hash'; const isMultiRouterMode = routerMode === 'multi'; const isApp = !isMultiRouterMode && pageName === entryFileName; if (isBuildNativeComp ? this.appHelper.compsConfigList.has(pageName) : (isApp || this.appHelper.pagesConfigList.has(pageName.split(path.sep).join('/')))) { module.loaders.push({ loader: require.resolve('@tarojs/taro-loader/lib/h5'), options: { /** paths */ entryFileName, filename: name.replace(suffixRgx, ''), runtimePath: this.options.runtimePath, sourceDir, /** config & message */ config: Object.assign({ router: routerConfig }, this.appHelper.appConfig), framework: this.options.framework, loaderMeta: this.options.loaderMeta, pages: this.appHelper.pagesConfigList, pxTransformConfig: this.options.pxTransformConfig, /** building mode */ isBuildNativeComp } }); } }); }); new TaroComponentsExportsPlugin_1.default(this.options).apply(compiler); } run() { this.appHelper.clear(); } } exports.default = TaroH5Plugin; //# sourceMappingURL=H5Plugin.js.map