|
| 1 | +"use strict"; |
| 2 | +var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { |
| 3 | + function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } |
| 4 | + return new (P || (P = Promise))(function (resolve, reject) { |
| 5 | + function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } |
| 6 | + function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } |
| 7 | + function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } |
| 8 | + step((generator = generator.apply(thisArg, _arguments || [])).next()); |
| 9 | + }); |
| 10 | +}; |
| 11 | +Object.defineProperty(exports, "__esModule", { value: true }); |
| 12 | +require("./GlobalsSetter"); |
| 13 | +const ConfigMapper_1 = require("./mappers/ConfigMapper"); |
| 14 | +const Cli_1 = require("./utils/Cli"); |
| 15 | +const path_1 = require("path"); |
| 16 | +const PluginMapper_1 = require("./mappers/PluginMapper"); |
| 17 | +const PageArchitect_1 = require("./architects/PageArchitect"); |
| 18 | +const Fs_1 = require("./utils/Fs"); |
| 19 | +const fs_extra_1 = require("fs-extra"); |
| 20 | +const fs = require("fs"); |
| 21 | +const StaticArchitect_1 = require("./architects/StaticArchitect"); |
| 22 | +const PathMapper_1 = require("./mappers/PathMapper"); |
| 23 | +const WebpackArchitect_1 = require("./architects/WebpackArchitect"); |
| 24 | +class default_1 { |
| 25 | + constructor(params = {}) { |
| 26 | + this.$ = { globalPlugins: [] }; |
| 27 | + this.constructParams(params); |
| 28 | + process.env.NODE_ENV = params.config.pro ? 'production' : 'development'; |
| 29 | + // @ts-ignore |
| 30 | + fs.mkdirp = fs_extra_1.mkdirp; |
| 31 | + this.$.inputFileSystem = params.inputFileSystem || fs; |
| 32 | + this.$.outputFileSystem = params.outputFileSystem || fs; |
| 33 | + //config |
| 34 | + this.$.config = new ConfigMapper_1.default(this.$.inputFileSystem, this.$.outputFileSystem).getConfig(params.config); |
| 35 | + //cli |
| 36 | + this.$.cli = new Cli_1.default(this.$.config.logMode); |
| 37 | + //log |
| 38 | + this.$.cli.ok(`NODE_ENV : ${process.env.NODE_ENV}`); |
| 39 | + this.$.cli.ok(`SSR : ${this.$.config.ssr}`); |
| 40 | + //pageMap |
| 41 | + this.$.pageMap = PathMapper_1.createMap(this.$.config.paths.pages, this.$.inputFileSystem); |
| 42 | + //rel |
| 43 | + this.$.rel = { |
| 44 | + libRel: path_1.relative(this.$.config.paths.dist, this.$.config.paths.lib), |
| 45 | + mapRel: path_1.relative(this.$.config.paths.dist, this.$.config.paths.map) |
| 46 | + }; |
| 47 | + //pageArchitect |
| 48 | + this.$.pageArchitect = new PageArchitect_1.default(this.$, new WebpackArchitect_1.default(this.$), !!params.outputFileSystem, !!params.inputFileSystem); |
| 49 | + //mapPlugins |
| 50 | + if (this.$.config.plugins.length > 0) { |
| 51 | + this.$.cli.log("Mapping Plugins"); |
| 52 | + this.$.config.plugins.forEach(plugin => PluginMapper_1.mapPlugin(plugin, undefined, this.$)); |
| 53 | + } |
| 54 | + } |
| 55 | + constructParams(params) { |
| 56 | + params.config = params.config || {}; |
| 57 | + params.config.paths = params.config.paths || {}; |
| 58 | + params.config.templateTags = params.config.templateTags || {}; |
| 59 | + } |
| 60 | + init() { |
| 61 | + return __awaiter(this, void 0, void 0, function* () { |
| 62 | + this.$.cli.log("Building Externals"); |
| 63 | + this.$.renderer = new StaticArchitect_1.default({ |
| 64 | + rel: this.$.rel, |
| 65 | + pathToLib: this.$.config.paths.lib, |
| 66 | + externals: yield this.$.pageArchitect.buildExternals(), |
| 67 | + explicitPages: this.$.config.pages, |
| 68 | + tags: this.$.config.templateTags, |
| 69 | + template: this.$.inputFileSystem.readFileSync(this.$.config.paths.template).toString(), |
| 70 | + ssr: this.$.config.ssr |
| 71 | + }); |
| 72 | + }); |
| 73 | + } |
| 74 | + buildPage(page) { |
| 75 | + return new Promise((resolve, reject) => { |
| 76 | + this.$.pageArchitect.buildPage(page, () => { |
| 77 | + this.$.cli.ok(`Successfully built page ${page.toString()}`); |
| 78 | + page.plugin.onBuild((path, content) => { |
| 79 | + Fs_1.writeFileRecursively(path_1.join(this.$.config.paths.map, `${path}.map.js`), `window.__MAP__=${JSON.stringify({ |
| 80 | + content, |
| 81 | + chunks: page.chunks |
| 82 | + })}`, this.$.outputFileSystem).catch(err => { |
| 83 | + throw err; |
| 84 | + }); |
| 85 | + Fs_1.writeFileRecursively(path_1.join(this.$.config.paths.dist, `${path}.html`), this.$.renderer.finalize(this.$.renderer.render(this.$.renderer.param.template, page, path, content)), this.$.outputFileSystem).catch(err => { |
| 86 | + throw err; |
| 87 | + }); |
| 88 | + }).then(resolve).catch(err => { |
| 89 | + throw err; |
| 90 | + }); |
| 91 | + }, reject); |
| 92 | + }); |
| 93 | + } |
| 94 | + export() { |
| 95 | + return __awaiter(this, void 0, void 0, function* () { |
| 96 | + const promises = []; |
| 97 | + this.$.pageMap.forEach(page => { |
| 98 | + promises.push(this.buildPage(page)); |
| 99 | + }); |
| 100 | + return Promise.all(promises); |
| 101 | + }); |
| 102 | + } |
| 103 | + exportFly() { |
| 104 | + return new Promise((resolve) => { |
| 105 | + const map = { |
| 106 | + staticConfig: Object.assign(Object.assign({}, this.$.renderer.param), { template: this.$.inputFileSystem.readFileSync(this.$.config.paths.template).toString() }), |
| 107 | + pageMap: {}, |
| 108 | + }; |
| 109 | + const promises = []; |
| 110 | + for (const page of this.$.pageMap.values()) { |
| 111 | + promises.push(new Promise(resolve => { |
| 112 | + this.buildPage(page).then(() => { |
| 113 | + map.pageMap[page.toString()] = page.chunks; |
| 114 | + const chunkPath = path_1.join(this.$.config.paths.lib, page.chunks[0]); |
| 115 | + this.$.outputFileSystem.copyFile(chunkPath, path_1.join(this.$.config.paths.fly, page.chunks[0]), err => { |
| 116 | + resolve(); |
| 117 | + if (err) |
| 118 | + throw new Error(`Error while moving ${chunkPath} to ${this.$.config.paths.fly}`); |
| 119 | + }); |
| 120 | + }); |
| 121 | + })); |
| 122 | + } |
| 123 | + const fullExternalName = map.staticConfig.externals[0].substr(map.staticConfig.externals[0].lastIndexOf("/") + 1); |
| 124 | + this.$.outputFileSystem.rename(path_1.join(this.$.config.paths.lib, map.staticConfig.externals[0]), path_1.join(this.$.config.paths.fly, fullExternalName), err => { |
| 125 | + if (err) |
| 126 | + throw new Error(`Error while moving ${fullExternalName} to ${this.$.config.paths.fly}`); |
| 127 | + map.staticConfig.externals[0] = fullExternalName; |
| 128 | + Promise.all(promises).then(() => this.$.outputFileSystem.writeFile(path_1.join(this.$.config.paths.fly, "firejs.map.json"), JSON.stringify(map), resolve)); |
| 129 | + }); |
| 130 | + }); |
| 131 | + } |
| 132 | + getContext() { |
| 133 | + return this.$; |
| 134 | + } |
| 135 | +} |
| 136 | +exports.default = default_1; |
0 commit comments