@koine/next 2.0.0-beta.2 → 2.0.0-beta.21

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.
Files changed (156) hide show
  1. package/12/AnalyticsGoogle.d.ts +0 -1
  2. package/12/AnalyticsGoogle.js +15 -21
  3. package/12/DisableErrorOverlay.d.ts +0 -5
  4. package/12/DisableErrorOverlay.js +5 -15
  5. package/12/DynamicNamespaces.d.ts +2 -7
  6. package/12/DynamicNamespaces.js +3 -12
  7. package/12/Favicon.d.ts +0 -0
  8. package/12/Favicon.js +6 -10
  9. package/12/Link.d.ts +1 -4
  10. package/12/Link.js +8 -14
  11. package/12/NextProgress.d.ts +1 -4
  12. package/12/NextProgress.js +13 -19
  13. package/12/Seo.d.ts +2 -26
  14. package/12/Seo.js +6 -33
  15. package/12/SeoDefaults.d.ts +1 -4
  16. package/12/SeoDefaults.js +6 -16
  17. package/12/T.d.ts +1 -7
  18. package/12/T.js +6 -15
  19. package/12/TransText.d.ts +0 -0
  20. package/12/TransText.js +3 -7
  21. package/12/app/AppHead.d.ts +0 -0
  22. package/12/app/AppHead.js +5 -10
  23. package/12/app/AppMain.d.ts +4 -22
  24. package/12/app/AppMain.js +1 -2
  25. package/12/app/css/AppMain.d.ts +1 -6
  26. package/12/app/css/AppMain.js +7 -16
  27. package/12/app/css/AppTheme.d.ts +2 -5
  28. package/12/app/css/AppTheme.js +5 -16
  29. package/12/app/css/auth/index.d.ts +2 -5
  30. package/12/app/css/auth/index.js +9 -16
  31. package/12/app/css/index.d.ts +2 -50
  32. package/12/app/css/index.js +8 -60
  33. package/12/app/index.d.ts +2 -2
  34. package/12/app/index.js +2 -5
  35. package/12/app/sc/AppMain.d.ts +1 -8
  36. package/12/app/sc/AppMain.js +12 -23
  37. package/12/app/sc/AppTheme.d.ts +1 -7
  38. package/12/app/sc/AppTheme.js +6 -13
  39. package/12/app/sc/auth/index.d.ts +2 -5
  40. package/12/app/sc/auth/index.js +9 -16
  41. package/12/app/sc/index.d.ts +2 -52
  42. package/12/app/sc/index.js +8 -62
  43. package/12/document/Document.d.ts +0 -10
  44. package/12/document/Document.js +11 -24
  45. package/12/document/css/index.d.ts +0 -12
  46. package/12/document/css/index.js +11 -38
  47. package/12/document/index.d.ts +2 -2
  48. package/12/document/index.js +2 -7
  49. package/12/document/sc/index.d.ts +0 -13
  50. package/12/document/sc/index.js +21 -38
  51. package/12/getT.d.ts +1 -6
  52. package/12/getT.js +3 -13
  53. package/12/index.d.ts +3 -24
  54. package/12/index.js +3 -52
  55. package/12/seoBuildTags.d.ts +2 -17
  56. package/12/seoBuildTags.js +15 -43
  57. package/12/to.d.ts +1 -11
  58. package/12/to.js +4 -14
  59. package/12/translationAsOptions.d.ts +2 -1
  60. package/12/translationAsOptions.js +2 -5
  61. package/12/types-i18n.d.ts +0 -87
  62. package/12/types-i18n.js +1 -14
  63. package/12/types-seo.d.ts +0 -0
  64. package/12/types-seo.js +1 -2
  65. package/12/types.d.ts +7 -0
  66. package/12/types.js +2 -0
  67. package/12/useBackUrl.d.ts +0 -0
  68. package/12/useBackUrl.js +11 -20
  69. package/12/useDateFormat.d.ts +0 -4
  70. package/12/useDateFormat.js +15 -23
  71. package/12/useForm.d.ts +2 -2
  72. package/12/useForm.js +9 -33
  73. package/12/useLocale.d.ts +0 -0
  74. package/12/useLocale.js +4 -9
  75. package/12/useT.d.ts +1 -1
  76. package/12/useT.js +6 -33
  77. package/12/useTo.d.ts +1 -1
  78. package/12/useTo.js +7 -11
  79. package/ThemeContext.d.ts +11 -0
  80. package/{12/ThemeContext.mjs → ThemeContext.js} +0 -1
  81. package/ThemeProvider.d.ts +17 -0
  82. package/{12/ThemeProvider.mjs → ThemeProvider.js} +11 -42
  83. package/config-i18n.d.ts +52 -0
  84. package/config-i18n.js +251 -0
  85. package/config.d.ts +109 -0
  86. package/config.js +118 -0
  87. package/getSiteUrl.d.ts +0 -6
  88. package/getSiteUrl.js +4 -15
  89. package/index.d.ts +5 -2
  90. package/index.js +5 -7
  91. package/load.d.ts +0 -8
  92. package/load.js +2 -14
  93. package/package.json +159 -39
  94. package/useTheme.d.ts +4 -0
  95. package/{12/useTheme.mjs → useTheme.js} +1 -4
  96. package/12/AnalyticsGoogle.mjs +0 -34
  97. package/12/DisableErrorOverlay.mjs +0 -13
  98. package/12/DynamicNamespaces.mjs +0 -9
  99. package/12/Favicon.mjs +0 -6
  100. package/12/Link.mjs +0 -12
  101. package/12/NextProgress.mjs +0 -42
  102. package/12/Seo.mjs +0 -29
  103. package/12/SeoDefaults.mjs +0 -12
  104. package/12/T.mjs +0 -13
  105. package/12/ThemeContext.d.ts +0 -17
  106. package/12/ThemeContext.js +0 -10
  107. package/12/ThemeProvider.d.ts +0 -35
  108. package/12/ThemeProvider.js +0 -239
  109. package/12/TransText.mjs +0 -4
  110. package/12/app/AppHead.mjs +0 -6
  111. package/12/app/AppMain.mjs +0 -1
  112. package/12/app/css/AppMain.mjs +0 -14
  113. package/12/app/css/AppTheme.mjs +0 -14
  114. package/12/app/css/auth/index.mjs +0 -13
  115. package/12/app/css/auth/package.json +0 -6
  116. package/12/app/css/index.mjs +0 -57
  117. package/12/app/css/package.json +0 -6
  118. package/12/app/index.mjs +0 -2
  119. package/12/app/package.json +0 -6
  120. package/12/app/sc/AppMain.mjs +0 -25
  121. package/12/app/sc/AppTheme.mjs +0 -11
  122. package/12/app/sc/auth/index.mjs +0 -13
  123. package/12/app/sc/auth/package.json +0 -6
  124. package/12/app/sc/index.mjs +0 -59
  125. package/12/app/sc/package.json +0 -6
  126. package/12/document/Document.mjs +0 -28
  127. package/12/document/css/index.mjs +0 -45
  128. package/12/document/css/package.json +0 -6
  129. package/12/document/index.mjs +0 -2
  130. package/12/document/package.json +0 -6
  131. package/12/document/sc/index.mjs +0 -64
  132. package/12/document/sc/package.json +0 -6
  133. package/12/getT.mjs +0 -10
  134. package/12/index.mjs +0 -27
  135. package/12/package.json +0 -6
  136. package/12/seoBuildTags.mjs +0 -117
  137. package/12/to.mjs +0 -25
  138. package/12/translationAsOptions.mjs +0 -9
  139. package/12/types-i18n.mjs +0 -13
  140. package/12/types-seo.mjs +0 -1
  141. package/12/useBackUrl.mjs +0 -28
  142. package/12/useDateFormat.mjs +0 -39
  143. package/12/useForm.mjs +0 -35
  144. package/12/useLocale.mjs +0 -6
  145. package/12/useT.mjs +0 -21
  146. package/12/useTheme.d.ts +0 -7
  147. package/12/useTheme.js +0 -12
  148. package/12/useTo.mjs +0 -15
  149. package/README.md +0 -13
  150. package/config/index.d.ts +0 -160
  151. package/config/index.js +0 -343
  152. package/config/index.mjs +0 -332
  153. package/config/package.json +0 -6
  154. package/getSiteUrl.mjs +0 -12
  155. package/index.mjs +0 -2
  156. package/load.mjs +0 -14
package/config/index.js DELETED
@@ -1,343 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.withKoine = exports.getRewrites = exports.getRedirects = exports.getPathRedirect = exports.getPathRewrite = exports.encodePathname = exports.toPath = exports.normaliseUrlPathname = void 0;
4
- var tslib_1 = require("tslib");
5
- /**
6
- * Normalise pathname
7
- *
8
- * From a path like `/some//malformed/path///` it returns `some/malformed/path`
9
- *
10
- * - Removes subsequent slashes
11
- * - Removing initial and ending slashes
12
- * - Returns an empty string `"""` if only slashes are given
13
- */
14
- function normaliseUrlPathname(pathname) {
15
- if (pathname === void 0) { pathname = ""; }
16
- // with return pathname.replace(/\/+\//g, "/").replace(/^\/+(.*?)\/+$/, "$1");
17
- // we would instead return a single slash if only slashes are given
18
- return pathname.replace(/\/+\//g, "/").replace(/^\/*(.*?)\/*$/, "$1");
19
- }
20
- exports.normaliseUrlPathname = normaliseUrlPathname;
21
- /**
22
- * Transform to path any absolute or relative URL
23
- *
24
- * Useful when setting up `rewrites` and `redirects` especally in a [multi-zones
25
- * setup](https://nextjs.org/docs/advanced-features/multi-zones).
26
- *
27
- * From a path like `http://localhost/some//malformed/path///` it returns `/some/malformed/path`
28
- *
29
- * @see {@link normaliseUrlPathname}
30
- */
31
- function toPath(urlOrPathname) {
32
- if (urlOrPathname === void 0) { urlOrPathname = ""; }
33
- var pathname = "";
34
- try {
35
- var parsed = new URL(urlOrPathname);
36
- pathname = parsed.pathname;
37
- }
38
- catch (e) {
39
- pathname = urlOrPathname;
40
- }
41
- // with return pathname.replace(/\/+\//g, "/").replace(/^\/+(.*?)\/+$/, "$1");
42
- // we would instead return a single slash if only slashes are given
43
- return pathname.replace(/\/+\//g, "/").replace(/^\/*(.*?)\/*$/, "$1");
44
- }
45
- exports.toPath = toPath;
46
- /**
47
- * Clean a pathname and encode each part
48
- *
49
- * @see {@link normaliseUrlPathname}
50
- */
51
- function encodePathname(pathname) {
52
- if (pathname === void 0) { pathname = ""; }
53
- var parts = normaliseUrlPathname(pathname).split("/");
54
- return parts
55
- .filter(function (part) { return !!part; })
56
- .map(function (part) { return encodeURIComponent(part); })
57
- .join("/");
58
- }
59
- exports.encodePathname = encodePathname;
60
- /**
61
- * Transform the route translated defintion into a `pathname` and a `template`.
62
- *
63
- * Here we add the wildcard flag maybe found in the pathname to the template
64
- * name too, this is because we do not want to have the wildcard in the JSON
65
- * keys as those are also used to produce links throught the `useTo` hook and
66
- * having asterisks there is a bit cumbersome.
67
- *
68
- * @see https://nextjs.org/docs/messages/invalid-multi-match
69
- */
70
- function transformRoute(route) {
71
- var rawPathname = route.pathname, rawTemplate = route.template;
72
- var pathnameParts = rawPathname.split("/").filter(function (part) { return !!part; });
73
- var templateParts = rawTemplate.split("/").filter(function (part) { return !!part; });
74
- var mapPartsByIdx = {};
75
- var pathname = pathnameParts
76
- .map(function (part) {
77
- var _a, _b;
78
- var hasWildcard = part.endsWith("*");
79
- part = part.replace("*", "");
80
- var isDynamic = part.startsWith("{{") && part.endsWith("}}");
81
- var asValue = isDynamic
82
- ? (_b = (_a = part.match(/{{(.+)}}/)) === null || _a === void 0 ? void 0 : _a[1].trim()) !== null && _b !== void 0 ? _b : ""
83
- : part.trim();
84
- var asPath = encodeURIComponent(asValue) + (hasWildcard ? "*" : "");
85
- mapPartsByIdx[asValue] = {
86
- isDynamic: isDynamic,
87
- hasWildcard: hasWildcard,
88
- };
89
- return isDynamic ? ":".concat(asPath) : asPath;
90
- })
91
- .join("/");
92
- var template = templateParts
93
- .map(function (part) {
94
- var _a, _b, _c;
95
- var isDynamic = part.startsWith("[") && part.endsWith("]");
96
- var asValue = isDynamic
97
- ? (_b = (_a = part.match(/\[(.+)\]/)) === null || _a === void 0 ? void 0 : _a[1].trim()) !== null && _b !== void 0 ? _b : ""
98
- : part.trim();
99
- var hasWildcard = (_c = mapPartsByIdx[asValue]) === null || _c === void 0 ? void 0 : _c.hasWildcard;
100
- var asPath = encodeURIComponent(asValue) + (hasWildcard ? "*" : "");
101
- return isDynamic ? ":".concat(asPath) : asPath;
102
- })
103
- .join("/");
104
- return { pathname: pathname, template: template };
105
- }
106
- /**
107
- * Get routes map dictionary
108
- */
109
- function getRoutesMap(map, routes, pathnameBuffer, templateBuffer) {
110
- if (map === void 0) { map = {}; }
111
- if (pathnameBuffer === void 0) { pathnameBuffer = ""; }
112
- if (templateBuffer === void 0) { templateBuffer = ""; }
113
- for (var key in routes) {
114
- var pathOrNestedRoutes = routes[key];
115
- var template = "".concat(templateBuffer, "/").concat(key);
116
- if (typeof pathOrNestedRoutes === "string") {
117
- map[template] = {
118
- template: template,
119
- pathname: pathOrNestedRoutes,
120
- wildcard: pathOrNestedRoutes.includes("*"),
121
- };
122
- }
123
- else {
124
- getRoutesMap(map, pathOrNestedRoutes, pathnameBuffer, template);
125
- }
126
- }
127
- return map;
128
- }
129
- /**
130
- * Removes `/index` from a template/url path
131
- */
132
- function getWithoutIndex(template) {
133
- return template.replace(/\/index$/, "");
134
- }
135
- /**
136
- * Get path rewrite
137
- */
138
- function getPathRewrite(route) {
139
- var _a = transformRoute(route), pathname = _a.pathname, template = _a.template;
140
- var source = "/".concat(normaliseUrlPathname(pathname));
141
- var destination = "/".concat(normaliseUrlPathname(template));
142
- // console.log(`rewrite pathname "${source}" to template "${destination}"`);
143
- return {
144
- source: source,
145
- destination: getWithoutIndex(destination),
146
- };
147
- }
148
- exports.getPathRewrite = getPathRewrite;
149
- /**
150
- * Get path redirect
151
- */
152
- function getPathRedirect(locale, route, permanent) {
153
- if (locale === void 0) { locale = ""; }
154
- var _a = transformRoute(route), template = _a.template, pathname = _a.pathname;
155
- var source = "/".concat(normaliseUrlPathname((locale ? "/".concat(locale, "/") : "/") + template));
156
- var destination = "/".concat(normaliseUrlPathname(pathname));
157
- // console.log(`redirect template "${source}" to pathname "${destination}"`);
158
- return {
159
- source: getWithoutIndex(source),
160
- destination: destination,
161
- permanent: Boolean(permanent),
162
- locale: false,
163
- };
164
- }
165
- exports.getPathRedirect = getPathRedirect;
166
- /**
167
- */
168
- function getRedirects(defaultLocale, routes, permanent, debug) {
169
- return tslib_1.__awaiter(this, void 0, void 0, function () {
170
- var redirects, routesMap;
171
- return tslib_1.__generator(this, function (_a) {
172
- redirects = [];
173
- routesMap = getRoutesMap({}, routes);
174
- Object.keys(routesMap).forEach(function (template) {
175
- var route = routesMap[template];
176
- // TODO: add option hideDefaultLocaleInUrl?
177
- // this is meant to redirect the URL with the default locale to the same
178
- // url without locale prefix, e.g.: /en/about -> /about (assuming en is the
179
- // defualt locale).
180
- // Actually this redirect seem not to be necessary, probably the i18n routing
181
- // mechanism of next 12 already does this, enabling causes infinite redirects
182
- // if (hideDefaultLocaleInUrl) {
183
- // redirects.push(getPathRedirect(defaultLocale, route, permanent));
184
- // }
185
- if (route.pathname !== getWithoutIndex(template)) {
186
- redirects.push(getPathRedirect("", route, permanent));
187
- }
188
- });
189
- if (debug)
190
- console.info("[@koine/next/config:getRedirects]", redirects);
191
- return [2 /*return*/, redirects];
192
- });
193
- });
194
- }
195
- exports.getRedirects = getRedirects;
196
- /**
197
- */
198
- function getRewrites(routes, debug) {
199
- return tslib_1.__awaiter(this, void 0, void 0, function () {
200
- var rewrites, routesMap;
201
- return tslib_1.__generator(this, function (_a) {
202
- rewrites = [];
203
- routesMap = getRoutesMap({}, routes);
204
- Object.keys(routesMap).forEach(function (template) {
205
- var route = routesMap[template];
206
- if (route.pathname !== getWithoutIndex(template)) {
207
- rewrites.push(getPathRewrite(route));
208
- }
209
- });
210
- if (debug)
211
- console.info("[@koine/next/config:getRewrites]", rewrites);
212
- return [2 /*return*/, rewrites];
213
- });
214
- });
215
- }
216
- exports.getRewrites = getRewrites;
217
- /**
218
- * Get Next.js config with some basic opinionated defaults
219
- *
220
- * @param {object} options
221
- * @property {boolean} [options.nx=false] Nx monorepo setup
222
- * @property {boolean} [options.svg=false] Svg to react components
223
- * @property {boolean} [options.sc=false] Styled components enabled
224
- * @property {boolean} [options.page=false] When `true` uses `*.page.ts` or `*.page.tsx`
225
- * extension for next.js config option [`pageExtensions`](https://nextjs.org/docs/api-reference/next.config.js/custom-page-extensions#including-non-page-files-in-the-pages-directory)
226
- * and it enables the same for `next-translate`.
227
- */
228
- function withKoine(_a) {
229
- var _b;
230
- if (_a === void 0) { _a = {
231
- i18n: { locales: ["en"], defaultLocale: "en" },
232
- }; }
233
- var _c = _a.nx, nx = _c === void 0 ? true : _c, _d = _a.svg, svg = _d === void 0 ? true : _d, _e = _a.sc, sc = _e === void 0 ? true : _e, page = _a.page, routes = _a.routes, permanent = _a.permanent, debug = _a.debug, custom = tslib_1.__rest(_a, ["nx", "svg", "sc", "page", "routes", "permanent", "debug"]);
234
- var nextConfig = tslib_1.__assign({
235
- // @see https://nextjs.org/docs/api-reference/next.config.js/custom-page-extensions#including-non-page-files-in-the-pages-directory
236
- pageExtensions: page ? ["page.tsx", "page.ts"] : undefined, eslint: {
237
- ignoreDuringBuilds: true, // we have this strict check on each commit
238
- }, typescript: {
239
- ignoreBuildErrors: true, // we have this strict check on each commit
240
- }, poweredByHeader: false, swcMinify: true, modularizeImports: tslib_1.__assign({ "@koine/api": { transform: "@koine/api/{{member}}" }, "@koine/browser": { transform: "@koine/browser/{{member}}" }, "@koine/dom": { transform: "@koine/dom/{{member}}" }, "@koine/next/?(((\\w*)?/?)*)": {
241
- transform: "@koine/next/{{ matches.[1] }}/{{member}}",
242
- }, "@koine/react/?(((\\w*)?/?)*)": {
243
- transform: "@koine/react/{{ matches.[1] }}/{{member}}",
244
- }, "@koine/utils": { transform: "@koine/utils/{{member}}" } }, (custom["modularizeImports"] || {})), experimental: tslib_1.__assign({
245
- // @see https://github.com/vercel/vercel/discussions/5973#discussioncomment-472618
246
- // @see critters error https://github.com/vercel/next.js/issues/20742
247
- // optimizeCss: true,
248
- // @see https://github.com/vercel/next.js/discussions/30174#discussion-3643870
249
- scrollRestoration: true }, (custom["experimental"] || {})) }, custom);
250
- if (svg) {
251
- if (nx) {
252
- // @see https://github.com/gregberge/svgr
253
- nextConfig["nx"] = {
254
- svgr: true,
255
- };
256
- }
257
- else {
258
- nextConfig.webpack = function (_config, options) {
259
- var webpackConfig = typeof nextConfig.webpack === "function"
260
- ? nextConfig.webpack(_config, options)
261
- : _config;
262
- // @see https://dev.to/dolearning/importing-svgs-to-next-js-nna#svgr
263
- webpackConfig.module.rules.push({
264
- test: /\.svg$/,
265
- use: [
266
- {
267
- loader: "@svgr/webpack",
268
- options: {
269
- svgoConfig: {
270
- plugins: [
271
- {
272
- name: "removeViewBox",
273
- active: false,
274
- },
275
- ],
276
- },
277
- },
278
- },
279
- ],
280
- });
281
- return webpackConfig;
282
- };
283
- }
284
- }
285
- if (sc) {
286
- nextConfig.compiler = {
287
- styledComponents: true,
288
- };
289
- }
290
- if (routes) {
291
- // we pass the default values, so we can assert I guess
292
- var defaultLocale_1 = (_b = nextConfig === null || nextConfig === void 0 ? void 0 : nextConfig.i18n) === null || _b === void 0 ? void 0 : _b.defaultLocale;
293
- return tslib_1.__assign(tslib_1.__assign({}, nextConfig), { redirects: function () {
294
- return tslib_1.__awaiter(this, void 0, void 0, function () {
295
- var defaults, customs;
296
- return tslib_1.__generator(this, function (_a) {
297
- switch (_a.label) {
298
- case 0: return [4 /*yield*/, getRedirects(defaultLocale_1, routes, permanent, debug)];
299
- case 1:
300
- defaults = _a.sent();
301
- if (!nextConfig.redirects) return [3 /*break*/, 3];
302
- return [4 /*yield*/, nextConfig.redirects()];
303
- case 2:
304
- customs = _a.sent();
305
- return [2 /*return*/, tslib_1.__spreadArray(tslib_1.__spreadArray([], defaults, true), customs, true)];
306
- case 3: return [2 /*return*/, defaults];
307
- }
308
- });
309
- });
310
- }, rewrites: function () {
311
- return tslib_1.__awaiter(this, void 0, void 0, function () {
312
- var defaults, customs;
313
- return tslib_1.__generator(this, function (_a) {
314
- switch (_a.label) {
315
- case 0: return [4 /*yield*/, getRewrites(routes, debug)];
316
- case 1:
317
- defaults = _a.sent();
318
- if (!nextConfig.rewrites) return [3 /*break*/, 3];
319
- return [4 /*yield*/, nextConfig.rewrites()];
320
- case 2:
321
- customs = _a.sent();
322
- if (Array.isArray(customs)) {
323
- return [2 /*return*/, {
324
- beforeFiles: defaults,
325
- afterFiles: customs,
326
- fallback: [],
327
- }];
328
- }
329
- return [2 /*return*/, tslib_1.__assign(tslib_1.__assign({}, customs), { beforeFiles: tslib_1.__spreadArray(tslib_1.__spreadArray([], defaults, true), (customs.beforeFiles || []), true) })];
330
- case 3: return [2 /*return*/, {
331
- afterFiles: [],
332
- beforeFiles: defaults,
333
- fallback: [],
334
- }];
335
- }
336
- });
337
- });
338
- } });
339
- }
340
- return nextConfig;
341
- }
342
- exports.withKoine = withKoine;
343
- exports.default = withKoine;
package/config/index.mjs DELETED
@@ -1,332 +0,0 @@
1
- import { __assign, __awaiter, __generator, __rest, __spreadArray } from "tslib";
2
- /**
3
- * Normalise pathname
4
- *
5
- * From a path like `/some//malformed/path///` it returns `some/malformed/path`
6
- *
7
- * - Removes subsequent slashes
8
- * - Removing initial and ending slashes
9
- * - Returns an empty string `"""` if only slashes are given
10
- */
11
- export function normaliseUrlPathname(pathname) {
12
- if (pathname === void 0) { pathname = ""; }
13
- // with return pathname.replace(/\/+\//g, "/").replace(/^\/+(.*?)\/+$/, "$1");
14
- // we would instead return a single slash if only slashes are given
15
- return pathname.replace(/\/+\//g, "/").replace(/^\/*(.*?)\/*$/, "$1");
16
- }
17
- /**
18
- * Transform to path any absolute or relative URL
19
- *
20
- * Useful when setting up `rewrites` and `redirects` especally in a [multi-zones
21
- * setup](https://nextjs.org/docs/advanced-features/multi-zones).
22
- *
23
- * From a path like `http://localhost/some//malformed/path///` it returns `/some/malformed/path`
24
- *
25
- * @see {@link normaliseUrlPathname}
26
- */
27
- export function toPath(urlOrPathname) {
28
- if (urlOrPathname === void 0) { urlOrPathname = ""; }
29
- var pathname = "";
30
- try {
31
- var parsed = new URL(urlOrPathname);
32
- pathname = parsed.pathname;
33
- }
34
- catch (e) {
35
- pathname = urlOrPathname;
36
- }
37
- // with return pathname.replace(/\/+\//g, "/").replace(/^\/+(.*?)\/+$/, "$1");
38
- // we would instead return a single slash if only slashes are given
39
- return pathname.replace(/\/+\//g, "/").replace(/^\/*(.*?)\/*$/, "$1");
40
- }
41
- /**
42
- * Clean a pathname and encode each part
43
- *
44
- * @see {@link normaliseUrlPathname}
45
- */
46
- export function encodePathname(pathname) {
47
- if (pathname === void 0) { pathname = ""; }
48
- var parts = normaliseUrlPathname(pathname).split("/");
49
- return parts
50
- .filter(function (part) { return !!part; })
51
- .map(function (part) { return encodeURIComponent(part); })
52
- .join("/");
53
- }
54
- /**
55
- * Transform the route translated defintion into a `pathname` and a `template`.
56
- *
57
- * Here we add the wildcard flag maybe found in the pathname to the template
58
- * name too, this is because we do not want to have the wildcard in the JSON
59
- * keys as those are also used to produce links throught the `useTo` hook and
60
- * having asterisks there is a bit cumbersome.
61
- *
62
- * @see https://nextjs.org/docs/messages/invalid-multi-match
63
- */
64
- function transformRoute(route) {
65
- var rawPathname = route.pathname, rawTemplate = route.template;
66
- var pathnameParts = rawPathname.split("/").filter(function (part) { return !!part; });
67
- var templateParts = rawTemplate.split("/").filter(function (part) { return !!part; });
68
- var mapPartsByIdx = {};
69
- var pathname = pathnameParts
70
- .map(function (part) {
71
- var _a, _b;
72
- var hasWildcard = part.endsWith("*");
73
- part = part.replace("*", "");
74
- var isDynamic = part.startsWith("{{") && part.endsWith("}}");
75
- var asValue = isDynamic
76
- ? (_b = (_a = part.match(/{{(.+)}}/)) === null || _a === void 0 ? void 0 : _a[1].trim()) !== null && _b !== void 0 ? _b : ""
77
- : part.trim();
78
- var asPath = encodeURIComponent(asValue) + (hasWildcard ? "*" : "");
79
- mapPartsByIdx[asValue] = {
80
- isDynamic: isDynamic,
81
- hasWildcard: hasWildcard,
82
- };
83
- return isDynamic ? ":".concat(asPath) : asPath;
84
- })
85
- .join("/");
86
- var template = templateParts
87
- .map(function (part) {
88
- var _a, _b, _c;
89
- var isDynamic = part.startsWith("[") && part.endsWith("]");
90
- var asValue = isDynamic
91
- ? (_b = (_a = part.match(/\[(.+)\]/)) === null || _a === void 0 ? void 0 : _a[1].trim()) !== null && _b !== void 0 ? _b : ""
92
- : part.trim();
93
- var hasWildcard = (_c = mapPartsByIdx[asValue]) === null || _c === void 0 ? void 0 : _c.hasWildcard;
94
- var asPath = encodeURIComponent(asValue) + (hasWildcard ? "*" : "");
95
- return isDynamic ? ":".concat(asPath) : asPath;
96
- })
97
- .join("/");
98
- return { pathname: pathname, template: template };
99
- }
100
- /**
101
- * Get routes map dictionary
102
- */
103
- function getRoutesMap(map, routes, pathnameBuffer, templateBuffer) {
104
- if (map === void 0) { map = {}; }
105
- if (pathnameBuffer === void 0) { pathnameBuffer = ""; }
106
- if (templateBuffer === void 0) { templateBuffer = ""; }
107
- for (var key in routes) {
108
- var pathOrNestedRoutes = routes[key];
109
- var template = "".concat(templateBuffer, "/").concat(key);
110
- if (typeof pathOrNestedRoutes === "string") {
111
- map[template] = {
112
- template: template,
113
- pathname: pathOrNestedRoutes,
114
- wildcard: pathOrNestedRoutes.includes("*"),
115
- };
116
- }
117
- else {
118
- getRoutesMap(map, pathOrNestedRoutes, pathnameBuffer, template);
119
- }
120
- }
121
- return map;
122
- }
123
- /**
124
- * Removes `/index` from a template/url path
125
- */
126
- function getWithoutIndex(template) {
127
- return template.replace(/\/index$/, "");
128
- }
129
- /**
130
- * Get path rewrite
131
- */
132
- export function getPathRewrite(route) {
133
- var _a = transformRoute(route), pathname = _a.pathname, template = _a.template;
134
- var source = "/".concat(normaliseUrlPathname(pathname));
135
- var destination = "/".concat(normaliseUrlPathname(template));
136
- // console.log(`rewrite pathname "${source}" to template "${destination}"`);
137
- return {
138
- source: source,
139
- destination: getWithoutIndex(destination),
140
- };
141
- }
142
- /**
143
- * Get path redirect
144
- */
145
- export function getPathRedirect(locale, route, permanent) {
146
- if (locale === void 0) { locale = ""; }
147
- var _a = transformRoute(route), template = _a.template, pathname = _a.pathname;
148
- var source = "/".concat(normaliseUrlPathname((locale ? "/".concat(locale, "/") : "/") + template));
149
- var destination = "/".concat(normaliseUrlPathname(pathname));
150
- // console.log(`redirect template "${source}" to pathname "${destination}"`);
151
- return {
152
- source: getWithoutIndex(source),
153
- destination: destination,
154
- permanent: Boolean(permanent),
155
- locale: false,
156
- };
157
- }
158
- /**
159
- */
160
- export function getRedirects(defaultLocale, routes, permanent, debug) {
161
- return __awaiter(this, void 0, void 0, function () {
162
- var redirects, routesMap;
163
- return __generator(this, function (_a) {
164
- redirects = [];
165
- routesMap = getRoutesMap({}, routes);
166
- Object.keys(routesMap).forEach(function (template) {
167
- var route = routesMap[template];
168
- // TODO: add option hideDefaultLocaleInUrl?
169
- // this is meant to redirect the URL with the default locale to the same
170
- // url without locale prefix, e.g.: /en/about -> /about (assuming en is the
171
- // defualt locale).
172
- // Actually this redirect seem not to be necessary, probably the i18n routing
173
- // mechanism of next 12 already does this, enabling causes infinite redirects
174
- // if (hideDefaultLocaleInUrl) {
175
- // redirects.push(getPathRedirect(defaultLocale, route, permanent));
176
- // }
177
- if (route.pathname !== getWithoutIndex(template)) {
178
- redirects.push(getPathRedirect("", route, permanent));
179
- }
180
- });
181
- if (debug)
182
- console.info("[@koine/next/config:getRedirects]", redirects);
183
- return [2 /*return*/, redirects];
184
- });
185
- });
186
- }
187
- /**
188
- */
189
- export function getRewrites(routes, debug) {
190
- return __awaiter(this, void 0, void 0, function () {
191
- var rewrites, routesMap;
192
- return __generator(this, function (_a) {
193
- rewrites = [];
194
- routesMap = getRoutesMap({}, routes);
195
- Object.keys(routesMap).forEach(function (template) {
196
- var route = routesMap[template];
197
- if (route.pathname !== getWithoutIndex(template)) {
198
- rewrites.push(getPathRewrite(route));
199
- }
200
- });
201
- if (debug)
202
- console.info("[@koine/next/config:getRewrites]", rewrites);
203
- return [2 /*return*/, rewrites];
204
- });
205
- });
206
- }
207
- /**
208
- * Get Next.js config with some basic opinionated defaults
209
- *
210
- * @param {object} options
211
- * @property {boolean} [options.nx=false] Nx monorepo setup
212
- * @property {boolean} [options.svg=false] Svg to react components
213
- * @property {boolean} [options.sc=false] Styled components enabled
214
- * @property {boolean} [options.page=false] When `true` uses `*.page.ts` or `*.page.tsx`
215
- * extension for next.js config option [`pageExtensions`](https://nextjs.org/docs/api-reference/next.config.js/custom-page-extensions#including-non-page-files-in-the-pages-directory)
216
- * and it enables the same for `next-translate`.
217
- */
218
- export function withKoine(_a) {
219
- var _b;
220
- if (_a === void 0) { _a = {
221
- i18n: { locales: ["en"], defaultLocale: "en" },
222
- }; }
223
- var _c = _a.nx, nx = _c === void 0 ? true : _c, _d = _a.svg, svg = _d === void 0 ? true : _d, _e = _a.sc, sc = _e === void 0 ? true : _e, page = _a.page, routes = _a.routes, permanent = _a.permanent, debug = _a.debug, custom = __rest(_a, ["nx", "svg", "sc", "page", "routes", "permanent", "debug"]);
224
- var nextConfig = __assign({
225
- // @see https://nextjs.org/docs/api-reference/next.config.js/custom-page-extensions#including-non-page-files-in-the-pages-directory
226
- pageExtensions: page ? ["page.tsx", "page.ts"] : undefined, eslint: {
227
- ignoreDuringBuilds: true, // we have this strict check on each commit
228
- }, typescript: {
229
- ignoreBuildErrors: true, // we have this strict check on each commit
230
- }, poweredByHeader: false, swcMinify: true, modularizeImports: __assign({ "@koine/api": { transform: "@koine/api/{{member}}" }, "@koine/browser": { transform: "@koine/browser/{{member}}" }, "@koine/dom": { transform: "@koine/dom/{{member}}" }, "@koine/next/?(((\\w*)?/?)*)": {
231
- transform: "@koine/next/{{ matches.[1] }}/{{member}}",
232
- }, "@koine/react/?(((\\w*)?/?)*)": {
233
- transform: "@koine/react/{{ matches.[1] }}/{{member}}",
234
- }, "@koine/utils": { transform: "@koine/utils/{{member}}" } }, (custom["modularizeImports"] || {})), experimental: __assign({
235
- // @see https://github.com/vercel/vercel/discussions/5973#discussioncomment-472618
236
- // @see critters error https://github.com/vercel/next.js/issues/20742
237
- // optimizeCss: true,
238
- // @see https://github.com/vercel/next.js/discussions/30174#discussion-3643870
239
- scrollRestoration: true }, (custom["experimental"] || {})) }, custom);
240
- if (svg) {
241
- if (nx) {
242
- // @see https://github.com/gregberge/svgr
243
- nextConfig["nx"] = {
244
- svgr: true,
245
- };
246
- }
247
- else {
248
- nextConfig.webpack = function (_config, options) {
249
- var webpackConfig = typeof nextConfig.webpack === "function"
250
- ? nextConfig.webpack(_config, options)
251
- : _config;
252
- // @see https://dev.to/dolearning/importing-svgs-to-next-js-nna#svgr
253
- webpackConfig.module.rules.push({
254
- test: /\.svg$/,
255
- use: [
256
- {
257
- loader: "@svgr/webpack",
258
- options: {
259
- svgoConfig: {
260
- plugins: [
261
- {
262
- name: "removeViewBox",
263
- active: false,
264
- },
265
- ],
266
- },
267
- },
268
- },
269
- ],
270
- });
271
- return webpackConfig;
272
- };
273
- }
274
- }
275
- if (sc) {
276
- nextConfig.compiler = {
277
- styledComponents: true,
278
- };
279
- }
280
- if (routes) {
281
- // we pass the default values, so we can assert I guess
282
- var defaultLocale_1 = (_b = nextConfig === null || nextConfig === void 0 ? void 0 : nextConfig.i18n) === null || _b === void 0 ? void 0 : _b.defaultLocale;
283
- return __assign(__assign({}, nextConfig), { redirects: function () {
284
- return __awaiter(this, void 0, void 0, function () {
285
- var defaults, customs;
286
- return __generator(this, function (_a) {
287
- switch (_a.label) {
288
- case 0: return [4 /*yield*/, getRedirects(defaultLocale_1, routes, permanent, debug)];
289
- case 1:
290
- defaults = _a.sent();
291
- if (!nextConfig.redirects) return [3 /*break*/, 3];
292
- return [4 /*yield*/, nextConfig.redirects()];
293
- case 2:
294
- customs = _a.sent();
295
- return [2 /*return*/, __spreadArray(__spreadArray([], defaults, true), customs, true)];
296
- case 3: return [2 /*return*/, defaults];
297
- }
298
- });
299
- });
300
- }, rewrites: function () {
301
- return __awaiter(this, void 0, void 0, function () {
302
- var defaults, customs;
303
- return __generator(this, function (_a) {
304
- switch (_a.label) {
305
- case 0: return [4 /*yield*/, getRewrites(routes, debug)];
306
- case 1:
307
- defaults = _a.sent();
308
- if (!nextConfig.rewrites) return [3 /*break*/, 3];
309
- return [4 /*yield*/, nextConfig.rewrites()];
310
- case 2:
311
- customs = _a.sent();
312
- if (Array.isArray(customs)) {
313
- return [2 /*return*/, {
314
- beforeFiles: defaults,
315
- afterFiles: customs,
316
- fallback: [],
317
- }];
318
- }
319
- return [2 /*return*/, __assign(__assign({}, customs), { beforeFiles: __spreadArray(__spreadArray([], defaults, true), (customs.beforeFiles || []), true) })];
320
- case 3: return [2 /*return*/, {
321
- afterFiles: [],
322
- beforeFiles: defaults,
323
- fallback: [],
324
- }];
325
- }
326
- });
327
- });
328
- } });
329
- }
330
- return nextConfig;
331
- }
332
- export default withKoine;
@@ -1,6 +0,0 @@
1
- {
2
- "sideEffects": false,
3
- "module": "./index.mjs",
4
- "main": "./index.js",
5
- "types": "./index.d.ts"
6
- }