UNPKG

build-plugin-rax-app

Version:

The basic webpack configuration for rax project

249 lines 13.9 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()); }); }; var __generator = (this && this.__generator) || function (thisArg, body) { var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; function verb(n) { return function (v) { return step([n, v]); }; } function step(op) { if (f) throw new TypeError("Generator is already executing."); while (g && (g = 0, op[0] && (_ = 0)), _) try { if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; if (y = 0, t) op = [op[0] & 2, t.value]; switch (op[0]) { case 0: case 1: t = op; break; case 4: _.label++; return { value: op[1], done: false }; case 5: _.label++; y = op[1]; op = [0]; continue; case 7: op = _.ops.pop(); _.trys.pop(); continue; default: if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } if (t[2]) _.ops.pop(); _.trys.pop(); continue; } op = body.call(thisArg, _); } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; } }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); var chalk_1 = __importDefault(require("chalk")); var openBrowser_1 = __importDefault(require("react-dev-utils/openBrowser")); var qrcode_terminal_1 = __importDefault(require("qrcode-terminal")); var path_1 = __importDefault(require("path")); var fs_extra_1 = __importDefault(require("fs-extra")); var chokidar_1 = __importDefault(require("chokidar")); var miniapp_builder_shared_1 = require("miniapp-builder-shared"); var logWebpackConfig_1 = __importDefault(require("../utils/logWebpackConfig")); var constants_1 = require("../constants"); var generateTempFile_1 = __importDefault(require("../utils/generateTempFile")); var formatMessage_1 = __importDefault(require("../utils/formatMessage")); var highlightPrint = chalk_1.default.hex('#F4AF3D'); function watchAppJson(rootDir, log) { var watcher = chokidar_1.default.watch(path_1.default.resolve(rootDir, 'src/app.json'), { ignoreInitial: true, }); watcher.on('change', function () { console.log('\n'); log.info('app.json has been changed'); log.info('restart dev server'); // add process env for mark restart dev process process.send({ type: 'RESTART_DEV' }); }); watcher.on('error', function (error) { log.error('fail to watch file', error); process.exit(1); }); } function default_1(api) { var _this = this; // eslint-disable-next-line global-require var context = api.context, onHook = api.onHook, getValue = api.getValue, log = api.log, applyMethod = api.applyMethod; var commandArgs = context.commandArgs, rootDir = context.rootDir; var webEntryKeys = []; var weexEntryKeys = []; var krakenEntryKeys = []; var webMpa = false; var weexMpa = false; var krakenMpa = false; var isFirstCompile = true; var pha = false; watchAppJson(rootDir, log); var getWebpackInfo = function (configs, configName) { var taskConfig = configs.find(function (webpackConfig) { return webpackConfig.name === configName; }); if (!taskConfig) { return { entry: {}, }; } return { entry: taskConfig.entry || {}, publicPath: taskConfig.output.publicPath, }; }; var devInfo = { urls: {}, framework: 'rax', }; onHook('before.start.run', function (_a) { var configs = _a.config; var userConfig = context.userConfig; var webWebpackInfo = getWebpackInfo(configs, 'web'); var weexWebpackInfo = getWebpackInfo(configs, 'weex'); var krakenWebpackInfo = getWebpackInfo(configs, 'kraken'); devInfo.publicPath = webWebpackInfo.publicPath; webEntryKeys = Object.keys(webWebpackInfo.entry); weexEntryKeys = Object.keys(weexWebpackInfo.entry); krakenEntryKeys = Object.keys(krakenWebpackInfo.entry); webMpa = userConfig.web && userConfig.web.mpa; weexMpa = userConfig.weex && userConfig.weex.mpa; krakenMpa = userConfig.kraken && userConfig.kraken.mpa; pha = userConfig.web && userConfig.web.pha; // Remove outputDir when start devServer var _b = userConfig.outputDir, outputDir = _b === void 0 ? 'build' : _b; configs.forEach(function (config) { fs_extra_1.default.emptyDirSync(path_1.default.resolve(rootDir, outputDir, config.name)); }); (0, logWebpackConfig_1.default)(configs); }); onHook('after.start.compile', function (_a) { var stats = _a.stats; return __awaiter(_this, void 0, void 0, function () { var statsJson, messages, isSuccessful, userConfig, _b, outputDir, targets, _c, web, urlPrefix, showLocalUrl, appConfig, needGenerateMultipleManifest, shouldOpenBrowser_1, openEntries_1, devUrls, manifestUrl; return __generator(this, function (_d) { statsJson = stats.toJson({ all: false, errors: true, warnings: true, timings: true, }); messages = (0, formatMessage_1.default)(statsJson); isSuccessful = !messages.errors.length; userConfig = context.userConfig; _b = userConfig.outputDir, outputDir = _b === void 0 ? 'build' : _b, targets = userConfig.targets, _c = userConfig.web, web = _c === void 0 ? {} : _c; urlPrefix = getValue(constants_1.DEV_URL_PREFIX); showLocalUrl = !process.env.CLOUDIDE_ENV; if (isSuccessful) { if (commandArgs.enableAssets) { console.log( // eslint-disable-next-line @iceworks/best-practices/recommend-polyfill stats.toString({ errors: false, warnings: false, colors: true, assets: true, chunks: false, entrypoints: false, modules: false, timings: false, })); } constants_1.MINIAPP_PLATFORMS.forEach(function (miniappPlatform) { if (targets.includes(miniappPlatform)) { var miniappOutputPath = path_1.default.resolve(rootDir, outputDir, miniappPlatform); devInfo.urls[miniappPlatform === constants_1.MINIAPP ? 'miniapp' : miniapp_builder_shared_1.platformMap[miniappPlatform].type] = [miniappOutputPath]; console.log(highlightPrint(" [".concat(miniapp_builder_shared_1.platformMap[miniappPlatform].name, "] Use ").concat(miniapp_builder_shared_1.platformMap[miniappPlatform].name.toLowerCase(), " developer tools to open the following folder:"))); console.log(' ', chalk_1.default.underline.white(miniappOutputPath)); console.log(); } }); appConfig = getValue('staticConfig'); needGenerateMultipleManifest = pha && !appConfig.tabBar; if (targets.includes(constants_1.WEB)) { devInfo.urls.web = []; console.log(highlightPrint(' [Web] Development server at: ')); shouldOpenBrowser_1 = !commandArgs.disableOpen && !process.env.RESTART_DEV && isFirstCompile && showLocalUrl; isFirstCompile = false; if (webEntryKeys.length > 0) { openEntries_1 = []; if (commandArgs.mpaEntry) { openEntries_1 = commandArgs.mpaEntry.split(','); } else { openEntries_1.push(webEntryKeys[0]); } webEntryKeys.forEach(function (entryKey) { var entryPath = webMpa ? "".concat(entryKey).concat(web.ssr ? '' : '.html') : ''; var lanUrl = "".concat(urlPrefix, "/").concat(entryPath); devInfo.urls.web.push(lanUrl); console.log(" ".concat(chalk_1.default.underline.white(lanUrl))); if (shouldOpenBrowser_1 && openEntries_1.includes(entryKey)) { (0, openBrowser_1.default)(lanUrl); } }); } else { devInfo.urls.web.push("".concat(urlPrefix, "/")); console.log(" ".concat(chalk_1.default.underline.white("".concat(urlPrefix, "/")))); if (shouldOpenBrowser_1) { (0, openBrowser_1.default)("".concat(urlPrefix, "/")); } } console.log(); } if (targets.includes(constants_1.KRAKEN)) { devInfo.urls.kraken = []; console.log(highlightPrint(' [Kraken] Development server at: ')); krakenEntryKeys.forEach(function (entryKey) { var krakenURL = "".concat(urlPrefix, "/kraken/").concat(krakenMpa ? entryKey : 'index', ".kbc1"); devInfo.urls.kraken.push(krakenURL); console.log(" ".concat(chalk_1.default.underline.white(krakenURL))); }); console.log(); } if (targets.includes(constants_1.WEEX)) { devInfo.urls.weex = []; // Use Weex App to scan ip address (mobile phone can't visit localhost). console.log(highlightPrint(' [Weex] Development server at: ')); weexEntryKeys.forEach(function (entryKey) { var weexUrl = "".concat(urlPrefix, "/weex/").concat(weexMpa ? entryKey : 'index', ".js?wh_weex=true"); devInfo.urls.weex.push(weexUrl); console.log(" ".concat(chalk_1.default.underline.white(weexUrl))); qrcode_terminal_1.default.generate(weexUrl, { small: true }); }); console.log(); } /** * @TODO: Delete it first, and then open it after the PHA supports it */ if (pha) { console.log(highlightPrint(' [PHA] Development server at: ')); if (needGenerateMultipleManifest) { devUrls = applyMethod('rax.getPHADevUrls'); devInfo.urls.pha = devUrls; devUrls.forEach(function (url) { console.log(" ".concat(chalk_1.default.underline.white(url))); }); console.log(); } else { manifestUrl = "".concat(urlPrefix, "/manifest.json?pha=true"); devInfo.urls.pha = [manifestUrl]; console.log(" ".concat(chalk_1.default.underline.white(manifestUrl))); console.log(); qrcode_terminal_1.default.generate(manifestUrl, { small: true }); console.log(); } } devInfo.compiledTime = Date.now(); (0, generateTempFile_1.default)('dev.json', JSON.stringify(devInfo), { rootDir: rootDir }); } return [2 /*return*/]; }); }); }); } exports.default = default_1; //# sourceMappingURL=setupDev.js.map