Skip to content
This repository was archived by the owner on Jul 1, 2020. It is now read-only.

Commit b6acd64

Browse files
author
aniketfuryrocks
committed
v0.17.0
1 parent 75002d1 commit b6acd64

34 files changed

+1199
-0
lines changed

dist/CustomRenderer.d.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import Page from "./classes/Page";
2+
import StaticArchitect from "./architects/StaticArchitect";
3+
import { PathRelatives } from "./FireJS";
4+
interface RenderReturn {
5+
html: string;
6+
map: string;
7+
}
8+
export default class {
9+
readonly map: Map<string, Page>;
10+
readonly renderer: StaticArchitect;
11+
readonly rel: PathRelatives;
12+
readonly rootDir: string;
13+
constructor(pathToLibDir: string, rootDir?: string);
14+
loadPagePlugin(pluginPath: string): void;
15+
cachePluginData(_page: string): Promise<void>;
16+
renderWithPluginData(__page: string, path: string): Promise<{
17+
html: string;
18+
map: string;
19+
}>;
20+
render(__page: string, path: string, content?: any): RenderReturn;
21+
}
22+
export {};

dist/CustomRenderer.js

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
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 Page_1 = require("./classes/Page");
14+
const StaticArchitect_1 = require("./architects/StaticArchitect");
15+
const path_1 = require("path");
16+
const PluginMapper_1 = require("./mappers/PluginMapper");
17+
const fs = require("fs");
18+
class default_1 {
19+
constructor(pathToLibDir, rootDir = process.cwd()) {
20+
this.map = new Map();
21+
const firejs_map = JSON.parse(fs.readFileSync(path_1.join(this.rootDir = rootDir, pathToLibDir, "firejs.map.json")).toString());
22+
firejs_map.staticConfig.pathToLib = path_1.join(rootDir, pathToLibDir);
23+
this.rel = firejs_map.staticConfig.rel;
24+
this.renderer = new StaticArchitect_1.default(firejs_map.staticConfig);
25+
for (const __page in firejs_map.pageMap) {
26+
const page = new Page_1.default(__page);
27+
page.chunks = firejs_map.pageMap[__page];
28+
// @ts-ignore
29+
page.plugin.paths = new Map();
30+
this.map.set(__page, page);
31+
}
32+
}
33+
loadPagePlugin(pluginPath) {
34+
PluginMapper_1.mapPlugin(pluginPath, { pageMap: this.map, rootPath: this.rootDir }, undefined);
35+
}
36+
cachePluginData(_page) {
37+
return __awaiter(this, void 0, void 0, function* () {
38+
const page = this.map.get(_page).plugin;
39+
// @ts-ignore
40+
page.paths.clear();
41+
yield page.onBuild((path, content) => {
42+
// @ts-ignore
43+
page.paths.set(path, content);
44+
});
45+
});
46+
}
47+
renderWithPluginData(__page, path) {
48+
return __awaiter(this, void 0, void 0, function* () {
49+
const page = this.map.get(__page);
50+
// @ts-ignore
51+
const content = page.plugin.paths.get(path);
52+
return {
53+
html: this.renderer.finalize(this.renderer.render(this.renderer.param.template, page, path, content || {})),
54+
map: `window.__MAP__=${JSON.stringify({
55+
content,
56+
chunks: page.chunks
57+
})}`
58+
};
59+
});
60+
}
61+
render(__page, path, content = {}) {
62+
const page = this.map.get(__page);
63+
return {
64+
html: this.renderer.finalize(this.renderer.render(this.renderer.param.template, page, path, content)),
65+
map: `window.__MAP__=${JSON.stringify({
66+
content,
67+
chunks: page.chunks
68+
})}`
69+
};
70+
}
71+
}
72+
exports.default = default_1;

dist/FireJS.d.ts

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import GlobalPlugin from "./classes/Plugins/GlobalPlugin";
2+
import { Config } from "./mappers/ConfigMapper";
3+
import Cli from "./utils/Cli";
4+
import Page from "./classes/Page";
5+
import { Configuration, Stats } from "webpack";
6+
import PageArchitect from "./architects/PageArchitect";
7+
import StaticArchitect, { StaticConfig } from "./architects/StaticArchitect";
8+
export declare type WebpackConfig = Configuration;
9+
export declare type WebpackStat = Stats;
10+
export interface PathRelatives {
11+
libRel: string;
12+
mapRel: string;
13+
}
14+
export interface $ {
15+
config?: Config;
16+
pageMap?: Map<string, Page>;
17+
cli?: Cli;
18+
rel?: PathRelatives;
19+
outputFileSystem?: any;
20+
inputFileSystem?: any;
21+
renderer?: StaticArchitect;
22+
pageArchitect?: PageArchitect;
23+
globalPlugins?: GlobalPlugin[];
24+
}
25+
export interface Params {
26+
config?: Config;
27+
outputFileSystem?: any;
28+
inputFileSystem?: any;
29+
}
30+
export interface FIREJS_MAP {
31+
staticConfig: StaticConfig;
32+
pageMap: {
33+
[key: string]: string[];
34+
};
35+
}
36+
export default class {
37+
private readonly $;
38+
private constructParams;
39+
constructor(params?: Params);
40+
init(): Promise<void>;
41+
buildPage(page: Page): Promise<void>;
42+
export(): Promise<any[]>;
43+
exportFly(): Promise<unknown>;
44+
getContext(): $;
45+
}

dist/FireJS.js

Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
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;

dist/GlobalsSetter.d.ts

Whitespace-only changes.

dist/GlobalsSetter.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
// @ts-ignore
2+
global.__FIREJS_VERSION__ = "0.17.0";

dist/architects/PageArchitect.d.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import WebpackArchitect from "./WebpackArchitect";
2+
import { $, WebpackConfig, WebpackStat } from "../FireJS";
3+
import Page from "../classes/Page";
4+
export default class {
5+
private readonly $;
6+
readonly webpackArchitect: WebpackArchitect;
7+
isOutputCustom: boolean;
8+
isInputCustom: boolean;
9+
constructor(globalData: $, webpackArchitect: any, isOutputCustom: boolean, isInputCustom: boolean);
10+
buildExternals(): Promise<string[]>;
11+
buildPage(page: Page, resolve: () => void, reject: (err: any | undefined) => void): void;
12+
build(config: WebpackConfig, resolve: (stat: any) => void, reject: (err: any) => void): void;
13+
logStat(stat: WebpackStat): boolean;
14+
}

dist/architects/PageArchitect.js

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
"use strict";
2+
Object.defineProperty(exports, "__esModule", { value: true });
3+
const webpack = require("webpack");
4+
class default_1 {
5+
constructor(globalData, webpackArchitect, isOutputCustom, isInputCustom) {
6+
this.$ = globalData;
7+
this.webpackArchitect = webpackArchitect;
8+
this.isOutputCustom = isOutputCustom;
9+
this.isInputCustom = isInputCustom;
10+
}
11+
buildExternals() {
12+
return new Promise((resolve, reject) => {
13+
this.build(this.webpackArchitect.forExternals(), stat => {
14+
const externals = [];
15+
stat.compilation.chunks.forEach(chunk => {
16+
externals.push(...chunk.files);
17+
});
18+
resolve(externals);
19+
}, reject);
20+
});
21+
}
22+
buildPage(page, resolve, reject) {
23+
this.build(this.webpackArchitect.forPage(page), (stat) => {
24+
if (this.logStat(stat)) //true if errors
25+
reject(undefined);
26+
else {
27+
page.chunks = []; //reinit chunks
28+
stat.compilation.chunks.forEach(chunk => {
29+
chunk.files.forEach(file => {
30+
if (chunk.name === "main")
31+
page.chunks.unshift(file); //add main chunk to the top
32+
else
33+
page.chunks.push(file);
34+
});
35+
});
36+
resolve();
37+
}
38+
}, reject);
39+
}
40+
build(config, resolve, reject) {
41+
const compiler = webpack(config);
42+
if (this.isOutputCustom)
43+
compiler.outputFileSystem = this.$.outputFileSystem;
44+
if (this.isInputCustom)
45+
compiler.inputFileSystem = this.$.inputFileSystem;
46+
if (config.watch)
47+
compiler.watch(config.watchOptions, (err, stat) => {
48+
if (err)
49+
reject(err);
50+
else
51+
resolve(stat);
52+
});
53+
else
54+
compiler.run((err, stat) => {
55+
if (err)
56+
reject(err);
57+
else
58+
resolve(stat);
59+
});
60+
}
61+
logStat(stat) {
62+
if (this.$.config.verbose) {
63+
this.$.cli.log("Stat");
64+
this.$.cli.normal(stat);
65+
}
66+
if (stat.hasWarnings()) {
67+
// @ts-ignore
68+
this.$.cli.warn(`Warning in page ${stat.compilation.name}\n`, ...stat.compilation.warnings);
69+
}
70+
if (stat.hasErrors()) {
71+
if (stat.compilation.errors.length === 0) {
72+
// @ts-ignore
73+
this.$.cli.error(`Error in page ${stat.compilation.name}`);
74+
}
75+
else {
76+
// @ts-ignore
77+
this.$.cli.error(`Error in page ${stat.compilation.name}\n`, ...stat.compilation.errors);
78+
}
79+
// @ts-ignore
80+
this.$.cli.error(`Unable to build page ${stat.compilation.name} with ${stat.compilation.errors.length} error(s)`);
81+
return true;
82+
}
83+
}
84+
}
85+
exports.default = default_1;

0 commit comments

Comments
 (0)