piral-core 0.15.0-beta.4672 → 0.15.0-beta.4699

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/app.codegen CHANGED
@@ -1,172 +1,25 @@
1
- const cat = process.env.NODE_ENV === 'test' ? 'src' : '_';
2
-
3
- function getRouterVersion() {
4
- try {
5
- const { version } = require('react-router/package.json');
6
- const [major] = version.split('.');
7
- return parseInt(major, 10);
8
- } catch {
9
- console.warn('Could not determine version of react-router. Falling back to v5');
10
- return 5;
11
- }
12
- }
13
-
14
- function getIdentifiers(packageName) {
15
- const packageJson = `${packageName}/package.json`;
16
-
17
- try {
18
- const details = require(packageJson);
19
-
20
- if (details.version) {
21
- return [packageName, `${packageName}@${details.version}`];
22
- }
23
- } catch {}
24
-
25
- return [packageName];
26
- }
27
-
28
- function createDependencies(imports, exports) {
29
- const appName = process.env.BUILD_PCKG_NAME || '';
30
- const externals = (process.env.SHARED_DEPENDENCIES || '').split(',').filter(Boolean);
31
- const assignments = [];
32
-
33
- if (appName) {
34
- assignments.push(`deps['${appName}']={}`);
35
- }
36
-
37
- for (const name of externals) {
38
- const identifiers = getIdentifiers(name);
39
- const ref = `_${imports.length}`;
40
- imports.push(`import * as ${ref} from ${JSON.stringify(name)}`);
41
-
42
- for (const id of identifiers) {
43
- assignments.push(`deps[${JSON.stringify(id)}]=${ref}`);
44
- }
45
- }
46
-
47
- exports.push(`
48
- export function fillDependencies(deps) {
49
- ${assignments.join(';')}
50
- }
51
- `);
52
- }
53
-
54
- function createDefaultState(imports, exports) {
55
- const publicPath = process.env.PIRAL_PUBLIC_PATH || '/';
56
- const routerVersion = getRouterVersion();
57
-
58
- imports.push(
59
- `import { DefaultErrorInfo } from 'piral-core/${cat}/defaults/DefaultErrorInfo';`,
60
- `import { DefaultLoadingIndicator } from 'piral-core/${cat}/defaults/DefaultLoadingIndicator';`,
61
- `import { DefaultLayout } from 'piral-core/${cat}/defaults/DefaultLayout';`,
62
- );
63
-
64
- if (routerVersion < 6) {
65
- imports.push(
66
- `import { DefaultRouter } from 'piral-core/${cat}/defaults/DefaultRouter_v5';`,
67
- `import { DefaultRouteSwitch } from 'piral-core/${cat}/defaults/DefaultRouteSwitch_v5';`,
68
- `import { createRedirect, createNavigation, useCurrentNavigation, useRouterContext } from 'piral-core/${cat}/defaults/navigator_v5'`,
69
- );
70
- } else {
71
- imports.push(
72
- `import { DefaultRouter } from 'piral-core/${cat}/defaults/DefaultRouter_v6';`,
73
- `import { DefaultRouteSwitch } from 'piral-core/${cat}/defaults/DefaultRouteSwitch_v6';`,
74
- `import { createRedirect, createNavigation, useCurrentNavigation, useRouterContext } from 'piral-core/${cat}/defaults/navigator_v6'`,
75
- );
76
- }
77
-
78
- exports.push(`
79
- export { createRedirect, createNavigation, useRouterContext };
80
- `);
81
-
82
- exports.push(`
83
- export const publicPath = ${JSON.stringify(publicPath)};
84
- `);
85
-
86
- exports.push(`
87
- export function createDefaultState() {
88
- return {
89
- app: {
90
- error: undefined,
91
- loading: typeof window !== 'undefined',
92
- },
93
- components: {
94
- ErrorInfo: DefaultErrorInfo,
95
- LoadingIndicator: DefaultLoadingIndicator,
96
- Router: DefaultRouter,
97
- RouteSwitch: DefaultRouteSwitch,
98
- Layout: DefaultLayout,
99
- },
100
- errorComponents: {},
101
- registry: {
102
- extensions: {},
103
- pages: {},
104
- wrappers: {},
105
- },
106
- routes: {},
107
- data: {},
108
- portals: {},
109
- modules: [],
110
- };
111
- }
112
- `);
113
- }
114
-
115
- function createDebugHandler(imports, exports) {
116
- // if we build the debug version of piral (debug and emulator build)
117
- if (process.env.DEBUG_PIRAL) {
118
- imports.push(`import { integrateDebugger } from "piral-core/${cat}/debugger"`);
119
- exports.push(`export { integrateDebugger }`);
120
- } else {
121
- exports.push(`export function integrateDebugger() {}`);
122
- }
123
-
124
- // if we build the emulator version of piral (shipped to pilets)
125
- if (process.env.DEBUG_PILET) {
126
- imports.push(`import { integrateEmulator } from "piral-core/${cat}/emulator"`);
127
- exports.push(`export { integrateEmulator }`);
128
- } else {
129
- exports.push(`export function integrateEmulator() {}`);
130
- }
131
- }
132
-
133
- function createRouteHandler(imports, exports) {
134
- const assignments = [];
135
-
136
- imports.push(
137
- `import { useGlobalStateContext } from 'piral-core/${cat}/hooks/globalState';`,
138
- );
139
-
140
- assignments.push(`
141
- useCurrentNavigation();
142
- `);
143
-
144
- if (process.env.DEBUG_PILET) {
145
- imports.push(`import { debugRouteFilter } from 'piral-debug-utils';`);
146
- assignments.push('return debugRouteFilter(paths);');
147
- } else {
148
- assignments.push('return paths;');
149
- }
150
-
151
- exports.push(`
152
- export function useRouteFilter(paths) {
153
- ${assignments.join('\n')}
154
- }
155
- `);
156
- }
1
+ const cg = require('./dist/codegen');
157
2
 
158
3
  module.exports = function () {
159
4
  const exports = [];
160
5
  const imports = [];
161
-
162
- createDependencies(imports, exports);
163
- createDefaultState(imports, exports);
164
- createDebugHandler(imports, exports);
165
- createRouteHandler(imports, exports);
6
+ const opts = {
7
+ root: process.cwd(),
8
+ cat: process.env.NODE_ENV === 'test' ? 'src' : '_',
9
+ appName: process.env.BUILD_PCKG_NAME || '',
10
+ externals: (process.env.SHARED_DEPENDENCIES || '').split(',').filter(Boolean),
11
+ publicPath: process.env.PIRAL_PUBLIC_PATH || '/',
12
+ debug: !!process.env.DEBUG_PIRAL,
13
+ emulator: !!process.env.DEBUG_PILET,
14
+ };
15
+
16
+ cg.createDependencies(imports, exports, opts);
17
+ cg.createDefaultState(imports, exports, opts);
18
+ cg.createDebugHandler(imports, exports, opts);
19
+ cg.createRouteHandler(imports, exports, opts);
166
20
 
167
21
  return `
168
22
  ${imports.join('\n')}
169
-
170
23
  ${exports.join('\n')}
171
24
  `;
172
25
  };