UNPKG

5.55 kBJavaScriptView Raw
1"use strict";
2var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3 if (k2 === undefined) k2 = k;
4 Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
5}) : (function(o, m, k, k2) {
6 if (k2 === undefined) k2 = k;
7 o[k2] = m[k];
8}));
9var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
10 Object.defineProperty(o, "default", { enumerable: true, value: v });
11}) : function(o, v) {
12 o["default"] = v;
13});
14var __importStar = (this && this.__importStar) || function (mod) {
15 if (mod && mod.__esModule) return mod;
16 var result = {};
17 if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
18 __setModuleDefault(result, mod);
19 return result;
20};
21var __exportStar = (this && this.__exportStar) || function(m, exports) {
22 for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
23};
24var __importDefault = (this && this.__importDefault) || function (mod) {
25 return (mod && mod.__esModule) ? mod : { "default": mod };
26};
27Object.defineProperty(exports, "__esModule", { value: true });
28exports.Ream = void 0;
29const path_1 = require("path");
30const resolve_from_1 = __importDefault(require("resolve-from"));
31const load_config_1 = require("./utils/load-config");
32const load_plugins_1 = require("./load-plugins");
33const store_1 = require("./store");
34const http_1 = require("http");
35const fs_extra_1 = require("fs-extra");
36const constants_1 = require("./utils/constants");
37class Ream {
38 constructor(options = {}, configOverride = {}) {
39 this.rootDir = path_1.resolve(options.rootDir || '.');
40 if (options.srcDir) {
41 this.srcDir = path_1.join(this.rootDir, options.srcDir);
42 }
43 else {
44 const hasPagesInSrc = fs_extra_1.existsSync(path_1.join(this.rootDir, 'src/pages'));
45 this.srcDir = hasPagesInSrc ? path_1.join(this.rootDir, 'src') : this.rootDir;
46 }
47 this.isDev = Boolean(options.dev);
48 if (!process.env.NODE_ENV) {
49 process.env.NODE_ENV = this.isDev ? 'development' : 'production';
50 }
51 this.store = store_1.store;
52 const { data: projectConfig = {}, path: configPath } = load_config_1.loadConfig(this.rootDir);
53 this.configPath = configPath;
54 this.config = {
55 ...projectConfig,
56 env: {
57 ...projectConfig.env,
58 ...configOverride.env,
59 },
60 plugins: [
61 ...(configOverride.plugins || []),
62 ...(projectConfig.plugins || []),
63 ],
64 imports: projectConfig.imports || [],
65 server: {
66 ...projectConfig.server,
67 },
68 };
69 process.env.PORT = String(this.config.server.port);
70 }
71 resolveRootDir(...args) {
72 return path_1.resolve(this.rootDir, ...args);
73 }
74 resolveSrcDir(...args) {
75 return path_1.resolve(this.srcDir, ...args);
76 }
77 resolveDotReam(...args) {
78 return this.resolveRootDir('.ream', ...args);
79 }
80 resolveOwnDir(...args) {
81 return path_1.resolve(constants_1.OWN_DIR, ...args);
82 }
83 resolveInPackage(pkg, target) {
84 const pkgDir = path_1.dirname(resolve_from_1.default(process.cwd(), `${pkg}/package.json`));
85 return resolve_from_1.default(pkgDir, target);
86 }
87 async prepare({ shouldCleanDir, shouldPrepreFiles, }) {
88 await load_plugins_1.loadPlugins(this);
89 if (shouldCleanDir) {
90 // Remove everything but cache
91 await Promise.all(['templates', 'manifest', 'server', 'client', 'export'].map((name) => {
92 return fs_extra_1.remove(this.resolveDotReam(name));
93 }));
94 }
95 // Preparing for webpack build process
96 if (shouldPrepreFiles) {
97 console.log('Preparing Ream files');
98 const { prepareFiles } = await Promise.resolve().then(() => __importStar(require('./prepare-files')));
99 await prepareFiles(this);
100 }
101 }
102 localResolve(name) {
103 return resolve_from_1.default.silent(this.rootDir, name);
104 }
105 localRequire(name) {
106 const path = this.localResolve(name);
107 return path && require(path);
108 }
109 async getRequestHandler() {
110 await this.prepare({
111 shouldCleanDir: this.isDev,
112 shouldPrepreFiles: this.isDev,
113 });
114 const { createServer } = await Promise.resolve().then(() => __importStar(require('./server')));
115 const server = await createServer(this);
116 return server;
117 }
118 async serve() {
119 const handler = await this.getRequestHandler();
120 const server = http_1.createServer(handler);
121 const port = this.config.server.port || 3000;
122 server.listen(port);
123 console.log(`> http://localhost:${port}`);
124 return server;
125 }
126 async build(fullyExport) {
127 await this.prepare({ shouldCleanDir: true, shouldPrepreFiles: true });
128 const { build } = await Promise.resolve().then(() => __importStar(require('./build')));
129 await build(this);
130 // Export static pages
131 const { exportSite } = await Promise.resolve().then(() => __importStar(require('./export')));
132 await exportSite(this.resolveDotReam(), fullyExport);
133 }
134}
135exports.Ream = Ream;
136__exportStar(require("./types"), exports);