@merkur/plugin-router 0.38.0 → 0.40.0

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/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  <p align="center">
2
2
  <a href="https://merkur.js.org/docs/getting-started" title="Getting started">
3
- <img src="https://raw.githubusercontent.com/mjancarik/merkur/master/images/merkur-illustration.png" width="100px" height="100px" alt="Merkur illustration"/>
3
+ <img src="https://raw.githubusercontent.com/mjancarik/merkur/master/images/merkur-logo.png" width="100px" height="100px" alt="Merkur illustration"/>
4
4
  </a>
5
5
  </p>
6
6
 
@@ -3,7 +3,7 @@
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
5
  var RouterEvents = Object.freeze({
6
- REDIRECT: '@merkur/plugin-router.redirect',
6
+ REDIRECT: '@merkur/plugin-router.redirect'
7
7
  });
8
8
 
9
9
  exports.default = RouterEvents;
@@ -3,7 +3,7 @@
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
5
  var RouterEvents = Object.freeze({
6
- REDIRECT: '@merkur/plugin-router.redirect',
6
+ REDIRECT: '@merkur/plugin-router.redirect'
7
7
  });
8
8
 
9
9
  exports.default = RouterEvents;
@@ -1,5 +1,5 @@
1
1
  var RouterEvents = Object.freeze({
2
- REDIRECT: '@merkur/plugin-router.redirect',
2
+ REDIRECT: '@merkur/plugin-router.redirect'
3
3
  });
4
4
 
5
5
  export { RouterEvents as default };
package/lib/index.cjs CHANGED
@@ -5,19 +5,16 @@ var UniversalRouter = require('universal-router');
5
5
  var generateUrls = require('universal-router/generateUrls');
6
6
 
7
7
  var RouterEvents = Object.freeze({
8
- REDIRECT: '@merkur/plugin-router.redirect',
8
+ REDIRECT: '@merkur/plugin-router.redirect'
9
9
  });
10
10
 
11
11
  const DEV = 'development';
12
- const ENV =
13
- typeof process !== 'undefined' && process && process.env
14
- ? process.env.NODE_ENV
15
- : DEV;
16
-
12
+ const ENV = typeof process !== 'undefined' && process && process.env ? process.env.NODE_ENV : DEV;
17
13
  function createRouter(widget, routes, options) {
18
14
  let wrappedRoutes = routes.reduce((result, route) => {
19
- const clonedRoute = { ...route };
20
-
15
+ const clonedRoute = {
16
+ ...route
17
+ };
21
18
  if (core.isFunction(clonedRoute.action)) {
22
19
  const originAction = clonedRoute.action;
23
20
  clonedRoute.action = (...rest) => {
@@ -25,28 +22,22 @@ function createRouter(widget, routes, options) {
25
22
  // For some application can be helpful to have named route parameters and query parameters merged
26
23
  // because link method support both named parameters and query parameters
27
24
  widget.$in.router.context = rest[0];
28
-
29
25
  return originAction(...rest);
30
26
  };
31
27
  }
32
-
33
28
  result.push(clonedRoute);
34
-
35
29
  return result;
36
30
  }, []);
37
-
38
31
  widget.$dependencies.router = new UniversalRouter(wrappedRoutes, options);
39
32
  widget.$dependencies.link = generateUrls(widget.$dependencies.router, {
40
- stringifyQueryParams: (params) => new URLSearchParams(params).toString(),
33
+ stringifyQueryParams: params => new URLSearchParams(params).toString()
41
34
  });
42
35
  widget.$in.router.options = options;
43
36
  }
44
-
45
37
  function routerPlugin() {
46
38
  return {
47
39
  async setup(widget) {
48
40
  core.assignMissingKeys(widget, routerAPI());
49
-
50
41
  widget.$in.router = {
51
42
  route: null,
52
43
  context: null,
@@ -54,72 +45,58 @@ function routerPlugin() {
54
45
  pathname: null,
55
46
  isMounting: false,
56
47
  isRouteActivated: false,
57
- isBootstrapCalled: false,
48
+ isBootstrapCalled: false
58
49
  };
59
-
60
50
  core.bindWidgetToFunctions(widget, widget.router);
61
-
62
51
  return widget;
63
52
  },
64
53
  async create(widget) {
65
54
  if (ENV === DEV) {
66
55
  if (!widget.$in.component) {
67
- throw new Error(
68
- 'You must install missing plugin: npm i @merkur/plugin-component',
69
- );
56
+ throw new Error('You must install missing plugin: npm i @merkur/plugin-component');
70
57
  }
71
-
72
58
  if (!widget.$in.eventEmitter) {
73
- throw new Error(
74
- 'You must install missing plugin: npm i @merkur/plugin-event-emitter',
75
- );
59
+ throw new Error('You must install missing plugin: npm i @merkur/plugin-event-emitter');
76
60
  }
77
61
  }
78
-
79
- widget.$in.component.lifeCycle = core.setDefaultValueForUndefined(
80
- widget.$in.component.lifeCycle,
81
- ['load'],
82
- () => {},
83
- );
62
+ widget.$in.component.lifeCycle = core.setDefaultValueForUndefined(widget.$in.component.lifeCycle, ['load'], () => {});
84
63
  core.hookMethod(widget, '$in.component.lifeCycle.load', loadHook);
85
64
  core.hookMethod(widget, 'bootstrap', bootstrapHook);
86
65
  core.hookMethod(widget, 'mount', mountHook);
87
66
  core.hookMethod(widget, 'unmount', unmountHook);
88
67
  core.hookMethod(widget, 'update', updateHook);
89
-
90
68
  return widget;
91
- },
69
+ }
92
70
  };
93
71
  }
94
-
95
72
  function getOrigin(widget) {
96
- const { protocol, host } = widget.$in.router.options;
97
-
73
+ const {
74
+ protocol,
75
+ host
76
+ } = widget.$in.router.options;
98
77
  if (!host) {
99
78
  return '';
100
79
  }
101
-
102
80
  if (!protocol) {
103
81
  return `//${host}`;
104
82
  }
105
-
106
83
  return `${protocol.replace(':', '').trim()}://${host.trim()}`;
107
84
  }
108
-
109
85
  function routerAPI() {
110
86
  return {
111
87
  router: {
112
88
  redirect(widget, url, data = {}) {
113
- widget.emit(RouterEvents.REDIRECT, { url, ...data });
89
+ widget.emit(RouterEvents.REDIRECT, {
90
+ url,
91
+ ...data
92
+ });
114
93
  },
115
94
  link(widget, routeName, data = {}) {
116
95
  const origin = getOrigin(widget);
117
96
  const path = widget.$dependencies.link(routeName, data);
118
-
119
97
  if (origin && path === '/') {
120
98
  return origin;
121
99
  }
122
-
123
100
  return `${origin}${path}`;
124
101
  },
125
102
  getCurrentRoute(widget) {
@@ -127,181 +104,143 @@ function routerAPI() {
127
104
  },
128
105
  getCurrentContext(widget) {
129
106
  return widget.$in.router.context;
130
- },
131
- },
107
+ }
108
+ }
132
109
  };
133
110
  }
134
-
135
111
  async function bootstrapHook(widget, originalBootstrap, ...rest) {
136
112
  if (widget.$in.router.isBootstrapCalled) {
137
113
  return;
138
114
  }
139
-
140
115
  widget.$in.router.isBootstrapCalled = true;
141
-
142
116
  return originalBootstrap(...rest);
143
117
  }
144
118
 
145
119
  // hook Component
146
120
  async function loadHook(widget, originalLoad, ...rest) {
147
121
  const plugin = widget.$in.router;
148
-
149
122
  if (!plugin.isMounting && widget.props.pathname !== plugin.pathname) {
150
123
  await tearDownRouterCycle(widget, ...rest);
151
-
152
124
  await setupRouterCycle(widget, ...rest);
153
125
  }
154
-
155
126
  if (!core.isFunction(plugin.route.load)) {
156
127
  throw new Error('The load method is mandatory.');
157
128
  }
158
-
159
- const globalStatePromise = core.isFunction(originalLoad)
160
- ? originalLoad(widget, ...rest)
161
- : Promise.resolve({});
129
+ const globalStatePromise = core.isFunction(originalLoad) ? originalLoad(widget, ...rest) : Promise.resolve({});
162
130
  const routeStatePromise = plugin.route.load(widget, {
163
131
  route: plugin.route,
164
132
  context: plugin.context,
165
133
  args: rest,
166
- globalState: globalStatePromise,
134
+ globalState: globalStatePromise
167
135
  });
168
-
169
- const [globalState, routeState] = await Promise.all([
170
- globalStatePromise,
171
- routeStatePromise,
172
- ]);
173
-
174
- return { ...globalState, ...routeState };
136
+ const [globalState, routeState] = await Promise.all([globalStatePromise, routeStatePromise]);
137
+ return {
138
+ ...globalState,
139
+ ...routeState
140
+ };
175
141
  }
176
142
 
177
143
  // hook Component
178
144
  async function mountHook(widget, originalMount, ...rest) {
179
145
  await widget.bootstrap(...rest);
180
-
181
146
  const plugin = widget.$in.router;
182
147
  if (!plugin.route) {
183
148
  await resolveRoute(widget);
184
149
  plugin.isMounting = true;
185
150
  }
186
-
187
151
  const result = await originalMount(...rest);
188
-
189
152
  if (plugin.isMounting && core.isFunction(plugin.route.init)) {
190
153
  await plugin.route.init(widget, {
191
154
  route: plugin.route,
192
155
  context: plugin.context,
193
- args: rest,
156
+ args: rest
194
157
  });
195
158
  }
196
-
197
- if (
198
- core.isFunction(plugin.route.activate) &&
199
- isClient() &&
200
- !plugin.isRouteActivated
201
- ) {
159
+ if (core.isFunction(plugin.route.activate) && isClient() && !plugin.isRouteActivated) {
202
160
  plugin.isRouteActivated = true;
203
161
  plugin.route.activate(widget, {
204
162
  route: plugin.route,
205
163
  context: plugin.context,
206
- args: rest,
164
+ args: rest
207
165
  });
208
166
  }
209
-
210
167
  plugin.isMounting = false;
211
-
212
168
  return result;
213
169
  }
214
170
 
215
171
  // hook Component
216
172
  async function updateHook(widget, originalUpdate, ...rest) {
217
173
  const result = await originalUpdate(...rest);
218
-
219
174
  const plugin = widget.$in.router;
220
-
221
- if (
222
- core.isFunction(plugin.route.activate) &&
223
- isClient() &&
224
- !plugin.isRouteActivated
225
- ) {
175
+ if (core.isFunction(plugin.route.activate) && isClient() && !plugin.isRouteActivated) {
226
176
  plugin.isRouteActivated = true;
227
177
  plugin.route.activate(widget, {
228
178
  route: plugin.route,
229
179
  context: plugin.context,
230
- args: rest,
180
+ args: rest
231
181
  });
232
182
  }
233
-
234
183
  return result;
235
184
  }
236
185
 
237
186
  // hook Component
238
187
  async function unmountHook(widget, originalUnmount, ...rest) {
239
188
  const result = await originalUnmount(...rest);
240
-
241
189
  await tearDownRouterCycle(widget, ...rest);
242
-
243
190
  return result;
244
191
  }
245
-
246
192
  async function resolveRoute(widget) {
247
193
  if (ENV === DEV) {
248
194
  if (!widget.props.pathname) {
249
195
  throw new Error('The props pathname is not defined.');
250
196
  }
251
-
252
197
  if (!widget.$dependencies.router) {
253
- throw new Error(
254
- 'You must add calling of createRouter(widget, routes, options) to widget.setup method.',
255
- );
198
+ throw new Error('You must add calling of createRouter(widget, routes, options) to widget.setup method.');
256
199
  }
257
200
  }
258
-
259
201
  const route = await widget.$dependencies.router.resolve({
260
202
  pathname: widget.props.pathname,
261
- widget,
203
+ widget
262
204
  });
263
-
264
205
  widget.$in.router.route = route;
265
206
  widget.$in.router.pathname = widget.props.pathname;
266
-
267
207
  return route;
268
208
  }
269
-
270
209
  async function setupRouterCycle(widget, ...rest) {
271
210
  const route = await resolveRoute(widget);
272
211
  const plugin = widget.$in.router;
273
-
274
212
  if (core.isFunction(route.init)) {
275
- await route.init(widget, { route, context: plugin.context, args: rest });
213
+ await route.init(widget, {
214
+ route,
215
+ context: plugin.context,
216
+ args: rest
217
+ });
276
218
  }
277
219
  }
278
-
279
220
  async function tearDownRouterCycle(widget, ...rest) {
280
221
  const plugin = widget.$in.router;
281
-
282
- const { route, isRouteActivated } = plugin;
283
-
222
+ const {
223
+ route,
224
+ isRouteActivated
225
+ } = plugin;
284
226
  if (route) {
285
227
  if (core.isFunction(route.deactivate) && isRouteActivated === true) {
286
228
  await route.deactivate(widget, {
287
229
  route,
288
230
  context: plugin.context,
289
- args: rest,
231
+ args: rest
290
232
  });
291
233
  }
292
-
293
234
  if (core.isFunction(route.destroy)) {
294
235
  await route.destroy(widget, {
295
236
  route,
296
237
  context: plugin.context,
297
- args: rest,
238
+ args: rest
298
239
  });
299
240
  }
300
241
  }
301
-
302
242
  plugin.isRouteActivated = false;
303
243
  }
304
-
305
244
  function isClient() {
306
245
  return typeof window !== 'undefined';
307
246
  }
package/lib/index.js CHANGED
@@ -5,19 +5,16 @@ var UniversalRouter = require('universal-router');
5
5
  var generateUrls = require('universal-router/generateUrls');
6
6
 
7
7
  var RouterEvents = Object.freeze({
8
- REDIRECT: '@merkur/plugin-router.redirect',
8
+ REDIRECT: '@merkur/plugin-router.redirect'
9
9
  });
10
10
 
11
11
  const DEV = 'development';
12
- const ENV =
13
- typeof process !== 'undefined' && process && process.env
14
- ? process.env.NODE_ENV
15
- : DEV;
16
-
12
+ const ENV = typeof process !== 'undefined' && process && process.env ? process.env.NODE_ENV : DEV;
17
13
  function createRouter(widget, routes, options) {
18
14
  let wrappedRoutes = routes.reduce((result, route) => {
19
- const clonedRoute = { ...route };
20
-
15
+ const clonedRoute = {
16
+ ...route
17
+ };
21
18
  if (core.isFunction(clonedRoute.action)) {
22
19
  const originAction = clonedRoute.action;
23
20
  clonedRoute.action = (...rest) => {
@@ -25,28 +22,22 @@ function createRouter(widget, routes, options) {
25
22
  // For some application can be helpful to have named route parameters and query parameters merged
26
23
  // because link method support both named parameters and query parameters
27
24
  widget.$in.router.context = rest[0];
28
-
29
25
  return originAction(...rest);
30
26
  };
31
27
  }
32
-
33
28
  result.push(clonedRoute);
34
-
35
29
  return result;
36
30
  }, []);
37
-
38
31
  widget.$dependencies.router = new UniversalRouter(wrappedRoutes, options);
39
32
  widget.$dependencies.link = generateUrls(widget.$dependencies.router, {
40
- stringifyQueryParams: (params) => new URLSearchParams(params).toString(),
33
+ stringifyQueryParams: params => new URLSearchParams(params).toString()
41
34
  });
42
35
  widget.$in.router.options = options;
43
36
  }
44
-
45
37
  function routerPlugin() {
46
38
  return {
47
39
  async setup(widget) {
48
40
  core.assignMissingKeys(widget, routerAPI());
49
-
50
41
  widget.$in.router = {
51
42
  route: null,
52
43
  context: null,
@@ -54,72 +45,58 @@ function routerPlugin() {
54
45
  pathname: null,
55
46
  isMounting: false,
56
47
  isRouteActivated: false,
57
- isBootstrapCalled: false,
48
+ isBootstrapCalled: false
58
49
  };
59
-
60
50
  core.bindWidgetToFunctions(widget, widget.router);
61
-
62
51
  return widget;
63
52
  },
64
53
  async create(widget) {
65
54
  if (ENV === DEV) {
66
55
  if (!widget.$in.component) {
67
- throw new Error(
68
- 'You must install missing plugin: npm i @merkur/plugin-component',
69
- );
56
+ throw new Error('You must install missing plugin: npm i @merkur/plugin-component');
70
57
  }
71
-
72
58
  if (!widget.$in.eventEmitter) {
73
- throw new Error(
74
- 'You must install missing plugin: npm i @merkur/plugin-event-emitter',
75
- );
59
+ throw new Error('You must install missing plugin: npm i @merkur/plugin-event-emitter');
76
60
  }
77
61
  }
78
-
79
- widget.$in.component.lifeCycle = core.setDefaultValueForUndefined(
80
- widget.$in.component.lifeCycle,
81
- ['load'],
82
- () => {},
83
- );
62
+ widget.$in.component.lifeCycle = core.setDefaultValueForUndefined(widget.$in.component.lifeCycle, ['load'], () => {});
84
63
  core.hookMethod(widget, '$in.component.lifeCycle.load', loadHook);
85
64
  core.hookMethod(widget, 'bootstrap', bootstrapHook);
86
65
  core.hookMethod(widget, 'mount', mountHook);
87
66
  core.hookMethod(widget, 'unmount', unmountHook);
88
67
  core.hookMethod(widget, 'update', updateHook);
89
-
90
68
  return widget;
91
- },
69
+ }
92
70
  };
93
71
  }
94
-
95
72
  function getOrigin(widget) {
96
- const { protocol, host } = widget.$in.router.options;
97
-
73
+ const {
74
+ protocol,
75
+ host
76
+ } = widget.$in.router.options;
98
77
  if (!host) {
99
78
  return '';
100
79
  }
101
-
102
80
  if (!protocol) {
103
81
  return `//${host}`;
104
82
  }
105
-
106
83
  return `${protocol.replace(':', '').trim()}://${host.trim()}`;
107
84
  }
108
-
109
85
  function routerAPI() {
110
86
  return {
111
87
  router: {
112
88
  redirect(widget, url, data = {}) {
113
- widget.emit(RouterEvents.REDIRECT, { url, ...data });
89
+ widget.emit(RouterEvents.REDIRECT, {
90
+ url,
91
+ ...data
92
+ });
114
93
  },
115
94
  link(widget, routeName, data = {}) {
116
95
  const origin = getOrigin(widget);
117
96
  const path = widget.$dependencies.link(routeName, data);
118
-
119
97
  if (origin && path === '/') {
120
98
  return origin;
121
99
  }
122
-
123
100
  return `${origin}${path}`;
124
101
  },
125
102
  getCurrentRoute(widget) {
@@ -127,181 +104,143 @@ function routerAPI() {
127
104
  },
128
105
  getCurrentContext(widget) {
129
106
  return widget.$in.router.context;
130
- },
131
- },
107
+ }
108
+ }
132
109
  };
133
110
  }
134
-
135
111
  async function bootstrapHook(widget, originalBootstrap, ...rest) {
136
112
  if (widget.$in.router.isBootstrapCalled) {
137
113
  return;
138
114
  }
139
-
140
115
  widget.$in.router.isBootstrapCalled = true;
141
-
142
116
  return originalBootstrap(...rest);
143
117
  }
144
118
 
145
119
  // hook Component
146
120
  async function loadHook(widget, originalLoad, ...rest) {
147
121
  const plugin = widget.$in.router;
148
-
149
122
  if (!plugin.isMounting && widget.props.pathname !== plugin.pathname) {
150
123
  await tearDownRouterCycle(widget, ...rest);
151
-
152
124
  await setupRouterCycle(widget, ...rest);
153
125
  }
154
-
155
126
  if (!core.isFunction(plugin.route.load)) {
156
127
  throw new Error('The load method is mandatory.');
157
128
  }
158
-
159
- const globalStatePromise = core.isFunction(originalLoad)
160
- ? originalLoad(widget, ...rest)
161
- : Promise.resolve({});
129
+ const globalStatePromise = core.isFunction(originalLoad) ? originalLoad(widget, ...rest) : Promise.resolve({});
162
130
  const routeStatePromise = plugin.route.load(widget, {
163
131
  route: plugin.route,
164
132
  context: plugin.context,
165
133
  args: rest,
166
- globalState: globalStatePromise,
134
+ globalState: globalStatePromise
167
135
  });
168
-
169
- const [globalState, routeState] = await Promise.all([
170
- globalStatePromise,
171
- routeStatePromise,
172
- ]);
173
-
174
- return { ...globalState, ...routeState };
136
+ const [globalState, routeState] = await Promise.all([globalStatePromise, routeStatePromise]);
137
+ return {
138
+ ...globalState,
139
+ ...routeState
140
+ };
175
141
  }
176
142
 
177
143
  // hook Component
178
144
  async function mountHook(widget, originalMount, ...rest) {
179
145
  await widget.bootstrap(...rest);
180
-
181
146
  const plugin = widget.$in.router;
182
147
  if (!plugin.route) {
183
148
  await resolveRoute(widget);
184
149
  plugin.isMounting = true;
185
150
  }
186
-
187
151
  const result = await originalMount(...rest);
188
-
189
152
  if (plugin.isMounting && core.isFunction(plugin.route.init)) {
190
153
  await plugin.route.init(widget, {
191
154
  route: plugin.route,
192
155
  context: plugin.context,
193
- args: rest,
156
+ args: rest
194
157
  });
195
158
  }
196
-
197
- if (
198
- core.isFunction(plugin.route.activate) &&
199
- isClient() &&
200
- !plugin.isRouteActivated
201
- ) {
159
+ if (core.isFunction(plugin.route.activate) && isClient() && !plugin.isRouteActivated) {
202
160
  plugin.isRouteActivated = true;
203
161
  plugin.route.activate(widget, {
204
162
  route: plugin.route,
205
163
  context: plugin.context,
206
- args: rest,
164
+ args: rest
207
165
  });
208
166
  }
209
-
210
167
  plugin.isMounting = false;
211
-
212
168
  return result;
213
169
  }
214
170
 
215
171
  // hook Component
216
172
  async function updateHook(widget, originalUpdate, ...rest) {
217
173
  const result = await originalUpdate(...rest);
218
-
219
174
  const plugin = widget.$in.router;
220
-
221
- if (
222
- core.isFunction(plugin.route.activate) &&
223
- isClient() &&
224
- !plugin.isRouteActivated
225
- ) {
175
+ if (core.isFunction(plugin.route.activate) && isClient() && !plugin.isRouteActivated) {
226
176
  plugin.isRouteActivated = true;
227
177
  plugin.route.activate(widget, {
228
178
  route: plugin.route,
229
179
  context: plugin.context,
230
- args: rest,
180
+ args: rest
231
181
  });
232
182
  }
233
-
234
183
  return result;
235
184
  }
236
185
 
237
186
  // hook Component
238
187
  async function unmountHook(widget, originalUnmount, ...rest) {
239
188
  const result = await originalUnmount(...rest);
240
-
241
189
  await tearDownRouterCycle(widget, ...rest);
242
-
243
190
  return result;
244
191
  }
245
-
246
192
  async function resolveRoute(widget) {
247
193
  if (ENV === DEV) {
248
194
  if (!widget.props.pathname) {
249
195
  throw new Error('The props pathname is not defined.');
250
196
  }
251
-
252
197
  if (!widget.$dependencies.router) {
253
- throw new Error(
254
- 'You must add calling of createRouter(widget, routes, options) to widget.setup method.',
255
- );
198
+ throw new Error('You must add calling of createRouter(widget, routes, options) to widget.setup method.');
256
199
  }
257
200
  }
258
-
259
201
  const route = await widget.$dependencies.router.resolve({
260
202
  pathname: widget.props.pathname,
261
- widget,
203
+ widget
262
204
  });
263
-
264
205
  widget.$in.router.route = route;
265
206
  widget.$in.router.pathname = widget.props.pathname;
266
-
267
207
  return route;
268
208
  }
269
-
270
209
  async function setupRouterCycle(widget, ...rest) {
271
210
  const route = await resolveRoute(widget);
272
211
  const plugin = widget.$in.router;
273
-
274
212
  if (core.isFunction(route.init)) {
275
- await route.init(widget, { route, context: plugin.context, args: rest });
213
+ await route.init(widget, {
214
+ route,
215
+ context: plugin.context,
216
+ args: rest
217
+ });
276
218
  }
277
219
  }
278
-
279
220
  async function tearDownRouterCycle(widget, ...rest) {
280
221
  const plugin = widget.$in.router;
281
-
282
- const { route, isRouteActivated } = plugin;
283
-
222
+ const {
223
+ route,
224
+ isRouteActivated
225
+ } = plugin;
284
226
  if (route) {
285
227
  if (core.isFunction(route.deactivate) && isRouteActivated === true) {
286
228
  await route.deactivate(widget, {
287
229
  route,
288
230
  context: plugin.context,
289
- args: rest,
231
+ args: rest
290
232
  });
291
233
  }
292
-
293
234
  if (core.isFunction(route.destroy)) {
294
235
  await route.destroy(widget, {
295
236
  route,
296
237
  context: plugin.context,
297
- args: rest,
238
+ args: rest
298
239
  });
299
240
  }
300
241
  }
301
-
302
242
  plugin.isRouteActivated = false;
303
243
  }
304
-
305
244
  function isClient() {
306
245
  return typeof window !== 'undefined';
307
246
  }
package/lib/index.mjs CHANGED
@@ -3,19 +3,16 @@ import UniversalRouter from 'universal-router';
3
3
  import generateUrls from 'universal-router/generateUrls';
4
4
 
5
5
  var RouterEvents = Object.freeze({
6
- REDIRECT: '@merkur/plugin-router.redirect',
6
+ REDIRECT: '@merkur/plugin-router.redirect'
7
7
  });
8
8
 
9
9
  const DEV = 'development';
10
- const ENV =
11
- typeof process !== 'undefined' && process && process.env
12
- ? process.env.NODE_ENV
13
- : DEV;
14
-
10
+ const ENV = typeof process !== 'undefined' && process && process.env ? process.env.NODE_ENV : DEV;
15
11
  function createRouter(widget, routes, options) {
16
12
  let wrappedRoutes = routes.reduce((result, route) => {
17
- const clonedRoute = { ...route };
18
-
13
+ const clonedRoute = {
14
+ ...route
15
+ };
19
16
  if (isFunction(clonedRoute.action)) {
20
17
  const originAction = clonedRoute.action;
21
18
  clonedRoute.action = (...rest) => {
@@ -23,28 +20,22 @@ function createRouter(widget, routes, options) {
23
20
  // For some application can be helpful to have named route parameters and query parameters merged
24
21
  // because link method support both named parameters and query parameters
25
22
  widget.$in.router.context = rest[0];
26
-
27
23
  return originAction(...rest);
28
24
  };
29
25
  }
30
-
31
26
  result.push(clonedRoute);
32
-
33
27
  return result;
34
28
  }, []);
35
-
36
29
  widget.$dependencies.router = new UniversalRouter(wrappedRoutes, options);
37
30
  widget.$dependencies.link = generateUrls(widget.$dependencies.router, {
38
- stringifyQueryParams: (params) => new URLSearchParams(params).toString(),
31
+ stringifyQueryParams: params => new URLSearchParams(params).toString()
39
32
  });
40
33
  widget.$in.router.options = options;
41
34
  }
42
-
43
35
  function routerPlugin() {
44
36
  return {
45
37
  async setup(widget) {
46
38
  assignMissingKeys(widget, routerAPI());
47
-
48
39
  widget.$in.router = {
49
40
  route: null,
50
41
  context: null,
@@ -52,72 +43,58 @@ function routerPlugin() {
52
43
  pathname: null,
53
44
  isMounting: false,
54
45
  isRouteActivated: false,
55
- isBootstrapCalled: false,
46
+ isBootstrapCalled: false
56
47
  };
57
-
58
48
  bindWidgetToFunctions(widget, widget.router);
59
-
60
49
  return widget;
61
50
  },
62
51
  async create(widget) {
63
52
  if (ENV === DEV) {
64
53
  if (!widget.$in.component) {
65
- throw new Error(
66
- 'You must install missing plugin: npm i @merkur/plugin-component',
67
- );
54
+ throw new Error('You must install missing plugin: npm i @merkur/plugin-component');
68
55
  }
69
-
70
56
  if (!widget.$in.eventEmitter) {
71
- throw new Error(
72
- 'You must install missing plugin: npm i @merkur/plugin-event-emitter',
73
- );
57
+ throw new Error('You must install missing plugin: npm i @merkur/plugin-event-emitter');
74
58
  }
75
59
  }
76
-
77
- widget.$in.component.lifeCycle = setDefaultValueForUndefined(
78
- widget.$in.component.lifeCycle,
79
- ['load'],
80
- () => {},
81
- );
60
+ widget.$in.component.lifeCycle = setDefaultValueForUndefined(widget.$in.component.lifeCycle, ['load'], () => {});
82
61
  hookMethod(widget, '$in.component.lifeCycle.load', loadHook);
83
62
  hookMethod(widget, 'bootstrap', bootstrapHook);
84
63
  hookMethod(widget, 'mount', mountHook);
85
64
  hookMethod(widget, 'unmount', unmountHook);
86
65
  hookMethod(widget, 'update', updateHook);
87
-
88
66
  return widget;
89
- },
67
+ }
90
68
  };
91
69
  }
92
-
93
70
  function getOrigin(widget) {
94
- const { protocol, host } = widget.$in.router.options;
95
-
71
+ const {
72
+ protocol,
73
+ host
74
+ } = widget.$in.router.options;
96
75
  if (!host) {
97
76
  return '';
98
77
  }
99
-
100
78
  if (!protocol) {
101
79
  return `//${host}`;
102
80
  }
103
-
104
81
  return `${protocol.replace(':', '').trim()}://${host.trim()}`;
105
82
  }
106
-
107
83
  function routerAPI() {
108
84
  return {
109
85
  router: {
110
86
  redirect(widget, url, data = {}) {
111
- widget.emit(RouterEvents.REDIRECT, { url, ...data });
87
+ widget.emit(RouterEvents.REDIRECT, {
88
+ url,
89
+ ...data
90
+ });
112
91
  },
113
92
  link(widget, routeName, data = {}) {
114
93
  const origin = getOrigin(widget);
115
94
  const path = widget.$dependencies.link(routeName, data);
116
-
117
95
  if (origin && path === '/') {
118
96
  return origin;
119
97
  }
120
-
121
98
  return `${origin}${path}`;
122
99
  },
123
100
  getCurrentRoute(widget) {
@@ -125,181 +102,143 @@ function routerAPI() {
125
102
  },
126
103
  getCurrentContext(widget) {
127
104
  return widget.$in.router.context;
128
- },
129
- },
105
+ }
106
+ }
130
107
  };
131
108
  }
132
-
133
109
  async function bootstrapHook(widget, originalBootstrap, ...rest) {
134
110
  if (widget.$in.router.isBootstrapCalled) {
135
111
  return;
136
112
  }
137
-
138
113
  widget.$in.router.isBootstrapCalled = true;
139
-
140
114
  return originalBootstrap(...rest);
141
115
  }
142
116
 
143
117
  // hook Component
144
118
  async function loadHook(widget, originalLoad, ...rest) {
145
119
  const plugin = widget.$in.router;
146
-
147
120
  if (!plugin.isMounting && widget.props.pathname !== plugin.pathname) {
148
121
  await tearDownRouterCycle(widget, ...rest);
149
-
150
122
  await setupRouterCycle(widget, ...rest);
151
123
  }
152
-
153
124
  if (!isFunction(plugin.route.load)) {
154
125
  throw new Error('The load method is mandatory.');
155
126
  }
156
-
157
- const globalStatePromise = isFunction(originalLoad)
158
- ? originalLoad(widget, ...rest)
159
- : Promise.resolve({});
127
+ const globalStatePromise = isFunction(originalLoad) ? originalLoad(widget, ...rest) : Promise.resolve({});
160
128
  const routeStatePromise = plugin.route.load(widget, {
161
129
  route: plugin.route,
162
130
  context: plugin.context,
163
131
  args: rest,
164
- globalState: globalStatePromise,
132
+ globalState: globalStatePromise
165
133
  });
166
-
167
- const [globalState, routeState] = await Promise.all([
168
- globalStatePromise,
169
- routeStatePromise,
170
- ]);
171
-
172
- return { ...globalState, ...routeState };
134
+ const [globalState, routeState] = await Promise.all([globalStatePromise, routeStatePromise]);
135
+ return {
136
+ ...globalState,
137
+ ...routeState
138
+ };
173
139
  }
174
140
 
175
141
  // hook Component
176
142
  async function mountHook(widget, originalMount, ...rest) {
177
143
  await widget.bootstrap(...rest);
178
-
179
144
  const plugin = widget.$in.router;
180
145
  if (!plugin.route) {
181
146
  await resolveRoute(widget);
182
147
  plugin.isMounting = true;
183
148
  }
184
-
185
149
  const result = await originalMount(...rest);
186
-
187
150
  if (plugin.isMounting && isFunction(plugin.route.init)) {
188
151
  await plugin.route.init(widget, {
189
152
  route: plugin.route,
190
153
  context: plugin.context,
191
- args: rest,
154
+ args: rest
192
155
  });
193
156
  }
194
-
195
- if (
196
- isFunction(plugin.route.activate) &&
197
- isClient() &&
198
- !plugin.isRouteActivated
199
- ) {
157
+ if (isFunction(plugin.route.activate) && isClient() && !plugin.isRouteActivated) {
200
158
  plugin.isRouteActivated = true;
201
159
  plugin.route.activate(widget, {
202
160
  route: plugin.route,
203
161
  context: plugin.context,
204
- args: rest,
162
+ args: rest
205
163
  });
206
164
  }
207
-
208
165
  plugin.isMounting = false;
209
-
210
166
  return result;
211
167
  }
212
168
 
213
169
  // hook Component
214
170
  async function updateHook(widget, originalUpdate, ...rest) {
215
171
  const result = await originalUpdate(...rest);
216
-
217
172
  const plugin = widget.$in.router;
218
-
219
- if (
220
- isFunction(plugin.route.activate) &&
221
- isClient() &&
222
- !plugin.isRouteActivated
223
- ) {
173
+ if (isFunction(plugin.route.activate) && isClient() && !plugin.isRouteActivated) {
224
174
  plugin.isRouteActivated = true;
225
175
  plugin.route.activate(widget, {
226
176
  route: plugin.route,
227
177
  context: plugin.context,
228
- args: rest,
178
+ args: rest
229
179
  });
230
180
  }
231
-
232
181
  return result;
233
182
  }
234
183
 
235
184
  // hook Component
236
185
  async function unmountHook(widget, originalUnmount, ...rest) {
237
186
  const result = await originalUnmount(...rest);
238
-
239
187
  await tearDownRouterCycle(widget, ...rest);
240
-
241
188
  return result;
242
189
  }
243
-
244
190
  async function resolveRoute(widget) {
245
191
  if (ENV === DEV) {
246
192
  if (!widget.props.pathname) {
247
193
  throw new Error('The props pathname is not defined.');
248
194
  }
249
-
250
195
  if (!widget.$dependencies.router) {
251
- throw new Error(
252
- 'You must add calling of createRouter(widget, routes, options) to widget.setup method.',
253
- );
196
+ throw new Error('You must add calling of createRouter(widget, routes, options) to widget.setup method.');
254
197
  }
255
198
  }
256
-
257
199
  const route = await widget.$dependencies.router.resolve({
258
200
  pathname: widget.props.pathname,
259
- widget,
201
+ widget
260
202
  });
261
-
262
203
  widget.$in.router.route = route;
263
204
  widget.$in.router.pathname = widget.props.pathname;
264
-
265
205
  return route;
266
206
  }
267
-
268
207
  async function setupRouterCycle(widget, ...rest) {
269
208
  const route = await resolveRoute(widget);
270
209
  const plugin = widget.$in.router;
271
-
272
210
  if (isFunction(route.init)) {
273
- await route.init(widget, { route, context: plugin.context, args: rest });
211
+ await route.init(widget, {
212
+ route,
213
+ context: plugin.context,
214
+ args: rest
215
+ });
274
216
  }
275
217
  }
276
-
277
218
  async function tearDownRouterCycle(widget, ...rest) {
278
219
  const plugin = widget.$in.router;
279
-
280
- const { route, isRouteActivated } = plugin;
281
-
220
+ const {
221
+ route,
222
+ isRouteActivated
223
+ } = plugin;
282
224
  if (route) {
283
225
  if (isFunction(route.deactivate) && isRouteActivated === true) {
284
226
  await route.deactivate(widget, {
285
227
  route,
286
228
  context: plugin.context,
287
- args: rest,
229
+ args: rest
288
230
  });
289
231
  }
290
-
291
232
  if (isFunction(route.destroy)) {
292
233
  await route.destroy(widget, {
293
234
  route,
294
235
  context: plugin.context,
295
- args: rest,
236
+ args: rest
296
237
  });
297
238
  }
298
239
  }
299
-
300
240
  plugin.isRouteActivated = false;
301
241
  }
302
-
303
242
  function isClient() {
304
243
  return typeof window !== 'undefined';
305
244
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@merkur/plugin-router",
3
- "version": "0.38.0",
3
+ "version": "0.40.0",
4
4
  "description": "Merkur router plugin.",
5
5
  "main": "lib/index",
6
6
  "module": "lib/index",
@@ -33,8 +33,7 @@
33
33
  "preversion": "npm test",
34
34
  "test": "jest --no-watchman -c ./jest.config.js",
35
35
  "test:es:version": "es-check es11 ./lib/index.mjs --module && es-check es9 ./lib/index.es9.mjs --module && es-check es9 ./lib/index.es9.cjs --module",
36
- "build": "rollup -c rollup.config.mjs",
37
- "prepare": "npm run build"
36
+ "build": "rollup -c rollup.config.mjs"
38
37
  },
39
38
  "repository": {
40
39
  "type": "git",
@@ -56,9 +55,9 @@
56
55
  },
57
56
  "homepage": "https://merkur.js.org/",
58
57
  "devDependencies": {
59
- "@merkur/core": "^0.38.0",
60
- "@merkur/plugin-component": "^0.38.0",
61
- "@merkur/plugin-event-emitter": "^0.38.0"
58
+ "@merkur/core": "^0.40.0",
59
+ "@merkur/plugin-component": "^0.40.0",
60
+ "@merkur/plugin-event-emitter": "^0.40.0"
62
61
  },
63
62
  "peerDependencies": {
64
63
  "@merkur/core": "*",
@@ -68,5 +67,5 @@
68
67
  "dependencies": {
69
68
  "universal-router": "^9.1.0"
70
69
  },
71
- "gitHead": "a6e379c0cb887898e34465dc3db9231feb68e6a5"
70
+ "gitHead": "a7bf45d46a5c0fca7130ae6a86e1cd94e5894ca2"
72
71
  }