@vnxjs/service
Version:
Vnmf Service
194 lines • 8.07 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.VnmfPlatformBase = void 0;
class Transaction {
constructor() {
this.wrappers = [];
}
perform(fn, scope, ...args) {
return __awaiter(this, void 0, void 0, function* () {
this.initAll(scope);
yield fn.call(scope, ...args);
this.closeAll(scope);
});
}
initAll(scope) {
const wrappers = this.wrappers;
wrappers.forEach(wrapper => { var _a; return (_a = wrapper.init) === null || _a === void 0 ? void 0 : _a.call(scope); });
}
closeAll(scope) {
const wrappers = this.wrappers;
wrappers.forEach(wrapper => { var _a; return (_a = wrapper.close) === null || _a === void 0 ? void 0 : _a.call(scope); });
}
addWrapper(wrapper) {
this.wrappers.push(wrapper);
}
}
class VnmfPlatformBase {
constructor(ctx, config) {
this.setupTransaction = new Transaction();
this.buildTransaction = new Transaction();
this.ctx = ctx;
this.helper = ctx.helper;
this.config = config;
const _compiler = config.compiler;
this.compiler = typeof _compiler === 'object' ? _compiler.type : _compiler;
}
/**
* 1. Empty dist Folder
* 2. Output compile hint
* 3. Generate project.config.json
*/
setup() {
var _a, _b;
return __awaiter(this, void 0, void 0, function* () {
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;
const { needClearOutput } = this.config;
if (typeof needClearOutput === 'undefined' || !!needClearOutput) {
this.emptyOutputDir();
}
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(processTypeEnum.START, 'Developer Tool-Project Directory', `${this.ctx.paths.outputPath}`);
}
}
emptyOutputDir() {
const { outputPath } = this.ctx.paths;
this.helper.emptyDirectory(outputPath);
}
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 && vnmf build --type ${platform} --watch`
: `$ NODE_ENV=production vnmf build --type ${platform} --watch`;
tips.push(chalk.yellowBright(`Larger files generated by preview mode,Settings NODE_ENV Yes. production You can turn on the compression.。
Example:
${exampleCommand}`));
}
if (this.compiler === 'webpack5' && !((_a = config.cache) === null || _a === void 0 ? void 0 : _a.enable)) {
tips.push(chalk.yellowBright('It\'s recommended to start the durability cache.,It\'s an effective way to increase the speed of secondary editing.,Please refer to the details.: https://docs.vnmf.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');
}
}
/**
* Return to the current item @vnxjs/mini-runner Bag.
*/
getRunner() {
return __awaiter(this, void 0, void 0, function* () {
const { appPath } = this.ctx.paths;
const { npm } = this.helper;
let runnerPkg;
switch (this.compiler) {
case 'webpack5':
runnerPkg = '@vnxjs/webpack5-runner';
break;
default:
runnerPkg = '@vnxjs/mini-runner';
}
const runner = yield npm.getNpmPkg(runnerPkg, appPath);
return runner.bind(null, appPath);
});
}
/**
* Ready. mini-runner Parameters
* @param extraOptions Could not close temporary folder: %s Options Configuration entry
*/
getOptions(extraOptions = {}) {
const { ctx, config, globalObject, fileType, template } = this;
return Object.assign(Object.assign(Object.assign({}, config), { nodeModulesPath: ctx.paths.nodeModulesPath, buildAdapter: config.platform, globalObject,
fileType,
template }), extraOptions);
}
/**
* Call mini-runner Commence compilation.
* @param extraOptions Could not close temporary folder: %s @vnxjs/mini-runner Configuration entry
*/
build(extraOptions = {}) {
var _a, _b;
return __awaiter(this, void 0, void 0, function* () {
(_b = (_a = this.ctx).onBuildInit) === null || _b === void 0 ? void 0 : _b.call(_a, this);
yield this.buildTransaction.perform(this.buildImpl, this, extraOptions);
});
}
buildImpl(extraOptions) {
return __awaiter(this, void 0, void 0, function* () {
const runner = yield this.getRunner();
const options = this.getOptions(Object.assign({
runtimePath: this.runtimePath,
vnmfComponentsPath: this.vnmfComponentsPath
}, extraOptions));
yield runner(options);
});
}
/**
* Generate project.config.json
* @param src Name of the profile in project source
* @param dist Name of the compiled configuration file,Default As 'project.config.json'
*/
generateProjectConfig(src, dist = 'project.config.json') {
if (this.config.isBuildNativeComp)
return;
this.ctx.generateProjectConfig({
srcConfigName: src,
distConfigName: dist
});
}
/**
* Recursively replace object key Value
*/
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);
}
});
}
/**
* Call mini-runner Turn on the compiler.
*/
start() {
return __awaiter(this, void 0, void 0, function* () {
yield this.setup();
yield this.build();
});
}
}
exports.VnmfPlatformBase = VnmfPlatformBase;
//# sourceMappingURL=platform-plugin-base.js.map