UNPKG

1.93 kBPlain TextView Raw
1import { existsSync } from "fs";
2import { resolve, join } from "path";
3
4export const rootPath = resolve(process.cwd());
5
6declare const __webpack_require__;
7declare const __non_webpack_require__;
8export const requireFx =
9 typeof __webpack_require__ === "function" ? __non_webpack_require__ : require;
10
11export 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
20export 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
35export 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
43export 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
51export const isBackEndConfig = mode => mode === "node";
52
53export const resolveLocal = (...args) => {
54 return join(__dirname, "../../", ...args);
55};
56
57export const resolveContext = (context, path) => {
58 return resolve(context, path);
59};
60
61export const getFile = relativePath => {
62 const filepath = join(rootPath, relativePath);
63 if (existsSync(filepath)) {
64 return requireFx(filepath);
65 }
66};
67
68export const getUserConfig = () => {
69 const userConfigPath = join(rootPath, "./typepack.ts");
70 if (existsSync(userConfigPath)) {
71 return requireFx(userConfigPath);
72 }
73 return {};
74};