webpackServe.js 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. "use strict";
  2. var __assign = (this && this.__assign) || function () {
  3. __assign = Object.assign || function(t) {
  4. for (var s, i = 1, n = arguments.length; i < n; i++) {
  5. s = arguments[i];
  6. for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
  7. t[p] = s[p];
  8. }
  9. return t;
  10. };
  11. return __assign.apply(this, arguments);
  12. };
  13. var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
  14. function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
  15. return new (P || (P = Promise))(function (resolve, reject) {
  16. function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
  17. function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
  18. function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
  19. step((generator = generator.apply(thisArg, _arguments || [])).next());
  20. });
  21. };
  22. var __generator = (this && this.__generator) || function (thisArg, body) {
  23. var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
  24. return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
  25. function verb(n) { return function (v) { return step([n, v]); }; }
  26. function step(op) {
  27. if (f) throw new TypeError("Generator is already executing.");
  28. while (_) try {
  29. 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;
  30. if (y = 0, t) op = [op[0] & 2, t.value];
  31. switch (op[0]) {
  32. case 0: case 1: t = op; break;
  33. case 4: _.label++; return { value: op[1], done: false };
  34. case 5: _.label++; y = op[1]; op = [0]; continue;
  35. case 7: op = _.ops.pop(); _.trys.pop(); continue;
  36. default:
  37. if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
  38. if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
  39. if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
  40. if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
  41. if (t[2]) _.ops.pop();
  42. _.trys.pop(); continue;
  43. }
  44. op = body.call(thisArg, _);
  45. } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
  46. if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
  47. }
  48. };
  49. var __spreadArrays = (this && this.__spreadArrays) || function () {
  50. for (var s = 0, i = 0, il = arguments.length; i < il; i++) s += arguments[i].length;
  51. for (var r = Array(s), k = 0, i = 0; i < il; i++)
  52. for (var a = arguments[i], j = 0, jl = a.length; j < jl; j++, k++)
  53. r[k] = a[j];
  54. return r;
  55. };
  56. var __importDefault = (this && this.__importDefault) || function (mod) {
  57. return (mod && mod.__esModule) ? mod : { "default": mod };
  58. };
  59. var __importStar = (this && this.__importStar) || function (mod) {
  60. if (mod && mod.__esModule) return mod;
  61. var result = {};
  62. if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
  63. result["default"] = mod;
  64. return result;
  65. };
  66. Object.defineProperty(exports, "__esModule", { value: true });
  67. require("source-map-support/register");
  68. var path_1 = __importDefault(require("path"));
  69. var fs_1 = __importDefault(require("fs"));
  70. var glob_1 = __importDefault(require("glob"));
  71. var yargs_parser_1 = __importDefault(require("yargs-parser"));
  72. var webpack_1 = __importDefault(require("webpack"));
  73. var webpack_inject_plugin_1 = __importDefault(require("webpack-inject-plugin"));
  74. var webpack_dev_server_1 = __importDefault(require("webpack-dev-server"));
  75. var express_1 = __importDefault(require("express"));
  76. var create_html_1 = __importDefault(require("create-html"));
  77. var WebpackDevServerUtils_1 = require("react-dev-utils/WebpackDevServerUtils");
  78. var webpackHelpers = __importStar(require("./utils/webpackHelpers"));
  79. var CordovaConfigParser_1 = __importDefault(require("./utils/CordovaConfigParser"));
  80. module.exports = function (ctx) { return __awaiter(void 0, void 0, void 0, function () {
  81. var platforms, targetPlatforms, argv, customWebpackConfig, customDevServerConfig, protocol, host, defaultPort, port, urls, defaultAccessHost, webpackConfig, devServerConfig, compiler, server;
  82. return __generator(this, function (_a) {
  83. switch (_a.label) {
  84. case 0:
  85. platforms = ['browser', 'android', 'ios'];
  86. targetPlatforms = platforms.filter(function (platform) {
  87. return ctx.opts.platforms.includes(platform);
  88. });
  89. if (!platforms.some(function (platform) { return ctx.opts.platforms.includes(platform); })) {
  90. return [2];
  91. }
  92. if (!ctx.opts.options || !ctx.opts.options.argv) {
  93. return [2];
  94. }
  95. argv = yargs_parser_1.default(ctx.opts.options.argv.join(' '));
  96. if (!argv.livereload && !argv.l) {
  97. return [2];
  98. }
  99. customWebpackConfig = webpackHelpers.webpackConfig(ctx.opts.projectRoot, argv.webpackConfig || argv.w);
  100. customDevServerConfig = customWebpackConfig.devServer || {};
  101. protocol = customDevServerConfig.https ? 'https' : 'http';
  102. host = customDevServerConfig.host || '0.0.0.0';
  103. defaultPort = customDevServerConfig.port || 8080;
  104. return [4, WebpackDevServerUtils_1.choosePort(host, defaultPort)];
  105. case 1:
  106. port = _a.sent();
  107. if (!port) {
  108. return [2];
  109. }
  110. urls = WebpackDevServerUtils_1.prepareUrls(protocol, host, port);
  111. defaultAccessHost = {
  112. android: "10.0.2.2",
  113. ios: "localhost",
  114. };
  115. webpackConfig = __assign(__assign({}, customWebpackConfig), { mode: 'development', plugins: __spreadArrays((customWebpackConfig.plugins || []), [
  116. new webpack_inject_plugin_1.default(function () {
  117. return fs_1.default.readFileSync(path_1.default.join(__dirname, 'www/injectCSP.js'), 'utf8');
  118. }),
  119. new webpack_inject_plugin_1.default(function () {
  120. return fs_1.default.readFileSync(path_1.default.join(__dirname, 'www/injectCordovaScript.js'), 'utf8');
  121. }),
  122. ]) });
  123. devServerConfig = __assign(__assign({ contentBase: path_1.default.join(ctx.opts.projectRoot, 'www'), historyApiFallback: true, watchContentBase: true, hot: true }, customDevServerConfig), { host: host,
  124. port: port, before: function (app, server) {
  125. if (customDevServerConfig.before) {
  126. customDevServerConfig.before(app, server);
  127. }
  128. targetPlatforms.forEach(function (platform) {
  129. app.use("/" + platform, express_1.default.static(path_1.default.join(ctx.opts.projectRoot, 'platforms', platform, 'platform_www')));
  130. });
  131. } });
  132. if (devServerConfig.hot)
  133. webpack_dev_server_1.default.addDevServerEntrypoints(webpackConfig, devServerConfig);
  134. targetPlatforms.forEach(function (platform) {
  135. if (platform === 'browser') {
  136. var html = create_html_1.default({
  137. head: "<meta http-equiv=\"refresh\" content=\"0;URL=" + urls.localUrlForBrowser + "\">",
  138. });
  139. fs_1.default.writeFileSync(path_1.default.join(ctx.opts.projectRoot, 'platforms', platform, 'www/index.html'), html);
  140. return;
  141. }
  142. glob_1.default
  143. .sync(path_1.default.join(ctx.opts.projectRoot, 'platforms', platform, '**/config.xml'))
  144. .forEach(function (configXmlPath) {
  145. var configXml = new CordovaConfigParser_1.default(configXmlPath);
  146. configXml.setContent(protocol + "://" + (urls.lanUrlForConfig ||
  147. defaultAccessHost[platform]) + ":" + port);
  148. if (platform === 'ios') {
  149. configXml.setElement('allow-navigation', { href: '*' });
  150. }
  151. configXml.write();
  152. });
  153. });
  154. compiler = webpack_1.default(webpackConfig);
  155. server = new webpack_dev_server_1.default(compiler, devServerConfig);
  156. return [4, new Promise(function (resolve, reject) {
  157. server.listen(port, host, function (err) {
  158. if (err) {
  159. reject(err);
  160. }
  161. console.log('Starting the development server...\n');
  162. resolve();
  163. });
  164. })];
  165. case 2:
  166. _a.sent();
  167. return [2];
  168. }
  169. });
  170. }); };
  171. //# sourceMappingURL=webpackServe.js.map