@tarojs/service
Version:
Taro Service
192 lines • 8.56 kB
JavaScript
"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 });
exports.TaroPlatformBase = void 0;
const path = require("node:path");
const helper_1 = require("@tarojs/helper");
const shared_1 = require("@tarojs/shared");
const package_1 = require("../utils/package");
const platform_1 = require("./platform");
class TaroPlatformBase extends platform_1.default {
constructor() {
super(...arguments);
this.platformType = shared_1.PLATFORM_TYPE.MINI;
// Note: 给所有的小程序平台一个默认的 taroComponentsPath
this.taroComponentsPath = helper_1.taroJsMiniComponentsPath;
}
/**
* 1. 清空 dist 文件夹
* 2. 输出编译提示
* 3. 生成 project.config.json
*/
setup() {
return __awaiter(this, void 0, void 0, function* () {
var _a, _b;
yield this.setupTransaction.perform(this.setupImpl, this);
(_b = (_a = this.ctx).onSetupClose) === null || _b === void 0 ? void 0 : _b.call(_a, this);
});
}
setupImpl() {
var _a, _b;
const { output } = this.config;
// webpack5 原生支持 output.clean 选项,但是 webpack4 不支持, 为统一行为,这里做一下兼容
// (在 packages/taro-mini-runner/src/webpack/chain.ts 和 packages/taro-webpack-runner/src/utils/chain.ts 的 makeConfig 中对 clean 选项做了过滤)
// 仅 output.clean 为 false 时不清空输出目录
// eslint-disable-next-line eqeqeq
if (output == undefined || output.clean == undefined || output.clean === true) {
this.emptyOutputDir();
}
else if ((0, shared_1.isObject)(output.clean)) {
this.emptyOutputDir(output.clean.keep || []);
}
this.printDevelopmentTip(this.platform);
if (this.projectConfigJson) {
this.generateProjectConfig(this.projectConfigJson);
}
if (((_a = this.ctx.initialConfig.logger) === null || _a === void 0 ? void 0 : _a.quiet) === false) {
const { printLog, processTypeEnum } = this.ctx.helper;
printLog("start" /* processTypeEnum.START */, '开发者工具-项目目录', `${this.ctx.paths.outputPath}`);
}
// Webpack5 代码自动热重载
if (this.compiler === 'webpack5' && this.config.isWatch && this.projectConfigJsonOutputPath) {
try {
const projectConfig = require(this.projectConfigJsonOutputPath);
if (((_b = projectConfig.setting) === null || _b === void 0 ? void 0 : _b.compileHotReLoad) === true) {
this.ctx.modifyWebpackChain(({ chain }) => {
chain.plugin('TaroMiniHMRPlugin')
.use(require(path.join(__dirname, './webpack/hmr-plugin.js')).default);
});
}
}
catch (e) { } // eslint-disable-line no-empty
}
}
printDevelopmentTip(platform) {
var _a;
const tips = [];
const config = this.config;
const { chalk } = this.helper;
if (process.env.NODE_ENV !== 'production' && process.env.NODE_ENV !== 'test') {
const { isWindows } = this.helper;
const exampleCommand = isWindows
? `$ set NODE_ENV=production && taro build --type ${platform} --watch`
: `$ NODE_ENV=production taro build --type ${platform} --watch`;
tips.push(chalk.yellowBright(`预览模式生成的文件较大,设置 NODE_ENV 为 production 可以开启压缩。
Example:
${exampleCommand}`));
}
if (this.compiler === 'webpack5' && !((_a = config.cache) === null || _a === void 0 ? void 0 : _a.enable)) {
tips.push(chalk.yellowBright('建议开启持久化缓存功能,能有效提升二次编译速度,详情请参考: https://docs.taro.zone/docs/config-detail#cache。'));
}
if (tips.length) {
console.log(chalk.yellowBright('Tips:'));
tips.forEach((item, index) => console.log(`${chalk.yellowBright(index + 1)}. ${item}`));
console.log('\n');
}
}
/**
* 返回当前项目内的 runner 包
*/
getRunner() {
return __awaiter(this, void 0, void 0, function* () {
const { appPath } = this.ctx.paths;
const { npm } = this.helper;
const runnerPkg = this.compiler === 'vite' ? '@tarojs/vite-runner' : '@tarojs/webpack5-runner';
const runner = yield npm.getNpmPkg(runnerPkg, appPath);
return runner.bind(null, appPath);
});
}
/**
* 准备 runner 参数
* @param extraOptions 需要额外合入 Options 的配置项
*/
getOptions(extraOptions = {}) {
const { ctx, globalObject, fileType, template } = this;
const config = (0, helper_1.recursiveMerge)(Object.assign({}, this.config), {
env: {
FRAMEWORK: JSON.stringify(this.config.framework),
TARO_ENV: JSON.stringify(this.platform),
TARO_PLATFORM: JSON.stringify(this.platformType),
TARO_VERSION: JSON.stringify((0, package_1.getPkgVersion)())
}
});
return Object.assign(Object.assign(Object.assign({}, config), { nodeModulesPath: ctx.paths.nodeModulesPath, buildAdapter: config.platform, platformType: this.platformType, globalObject,
fileType,
template }), extraOptions);
}
/**
* 调用 runner 开始编译
* @param extraOptions 需要额外传入 runner 的配置项
*/
build() {
return __awaiter(this, arguments, void 0, function* (extraOptions = {}) {
var _a, _b;
(_b = (_a = this.ctx).onBuildInit) === null || _b === void 0 ? void 0 : _b.call(_a, this);
yield this.buildTransaction.perform(this.buildImpl, this, extraOptions);
});
}
buildImpl() {
return __awaiter(this, arguments, void 0, function* (extraOptions = {}) {
const runner = yield this.getRunner();
const options = this.getOptions(Object.assign({
runtimePath: this.runtimePath,
taroComponentsPath: this.taroComponentsPath,
behaviorsName: this.behaviorsName,
}, extraOptions));
yield runner(options);
});
}
/**
* 生成 project.config.json
* @param src 项目源码中配置文件的名称
* @param dist 编译后配置文件的名称,默认为 'project.config.json'
*/
generateProjectConfig(src, dist = 'project.config.json') {
if (this.config.isBuildNativeComp)
return;
this.ctx.generateProjectConfig({
srcConfigName: src,
distConfigName: dist
});
this.projectConfigJsonOutputPath = `${this.ctx.paths.outputPath}/${dist}`;
}
/**
* 递归替换对象的 key 值
*/
recursiveReplaceObjectKeys(obj, keyMap) {
Object.keys(obj).forEach((key) => {
if (keyMap[key]) {
obj[keyMap[key]] = obj[key];
if (typeof obj[key] === 'object') {
this.recursiveReplaceObjectKeys(obj[keyMap[key]], keyMap);
}
delete obj[key];
}
else if (keyMap[key] === false) {
delete obj[key];
}
else if (typeof obj[key] === 'object') {
this.recursiveReplaceObjectKeys(obj[key], keyMap);
}
});
}
/**
* 调用 runner 开启编译
*/
start() {
return __awaiter(this, void 0, void 0, function* () {
yield this.setup();
yield this.build();
});
}
}
exports.TaroPlatformBase = TaroPlatformBase;
//# sourceMappingURL=mini.js.map