1 | import { existsSync } from "fs";
|
2 | import { resolve, join } from "path";
|
3 |
|
4 | export const rootPath = resolve(process.cwd());
|
5 |
|
6 | declare const __webpack_require__;
|
7 | declare const __non_webpack_require__;
|
8 | export const requireFx =
|
9 | typeof __webpack_require__ === "function" ? __non_webpack_require__ : require;
|
10 |
|
11 | export const getPaths = (userPaths = {}) => ({
|
12 | src: join(rootPath, "./src"),
|
13 | entry: {
|
14 | main: join(rootPath, "./src/index")
|
15 | },
|
16 | outputFolder: join(rootPath, "./dist"),
|
17 | ...userPaths
|
18 | });
|
19 |
|
20 | export const resolveHtmlTemplate = () => {
|
21 | const srcHtmlPath = resolve(rootPath, "./src/index.html");
|
22 | const assetsHtmlPath = resolve(rootPath, "./assets/index.html");
|
23 | const rootHtmlPath = resolve(rootPath, "./index.html");
|
24 | if (existsSync(srcHtmlPath)) {
|
25 | return srcHtmlPath;
|
26 | }
|
27 | if (existsSync(assetsHtmlPath)) {
|
28 | return assetsHtmlPath;
|
29 | }
|
30 | if (existsSync(rootHtmlPath)) {
|
31 | return rootHtmlPath;
|
32 | }
|
33 | };
|
34 |
|
35 | export const makeDevEntry = (entry, port) => {
|
36 | return [
|
37 | require.resolve("webpack-dev-server/client") + `?http://localhost:${port}`,
|
38 | require.resolve("webpack/hot/dev-server"),
|
39 | entry
|
40 | ];
|
41 | };
|
42 |
|
43 | export const makeDevEntries = (entries, port) => {
|
44 | Object.keys(entries).forEach(entryKey => {
|
45 | const entry = entries[entryKey];
|
46 | entries[entryKey] = makeDevEntry(entry, port);
|
47 | });
|
48 | return entries;
|
49 | };
|
50 |
|
51 | export const isBackEndConfig = mode => mode === "node";
|
52 |
|
53 | export const resolveLocal = (...args) => {
|
54 | return join(__dirname, "../../", ...args);
|
55 | };
|
56 |
|
57 | export const resolveContext = (context, path) => {
|
58 | return resolve(context, path);
|
59 | };
|
60 |
|
61 | export const getFile = relativePath => {
|
62 | const filepath = join(rootPath, relativePath);
|
63 | if (existsSync(filepath)) {
|
64 | return requireFx(filepath);
|
65 | }
|
66 | };
|
67 |
|
68 | export const getUserConfig = () => {
|
69 | const userConfigPath = join(rootPath, "./typepack.ts");
|
70 | if (existsSync(userConfigPath)) {
|
71 | return requireFx(userConfigPath);
|
72 | }
|
73 | return {};
|
74 | };
|