@cley_faye/boilerplate 3.14.0 → 3.16.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
@@ -29,8 +29,6 @@ required, and for what. It's up to the calling project to have the proper depend
29
29
 
30
30
  For webapp building:
31
31
 
32
- - resolve-typescript-plugin (due to the way webpack plugins are handled, this is mandatory for dev
33
- install)
34
32
  - babel-loader
35
33
  - eslint-webpack-plugin
36
34
  - @babel/core
@@ -464,6 +462,30 @@ It is also possible to trigger a stop by calling the `closeServer()` function on
464
462
  `@cley_faye/boilerplate/lib/express/autoclose.js`.
465
463
  This feature only supports one server at a time.
466
464
 
465
+ Extra middleware
466
+ ----------------
467
+
468
+ ### singlepageapp
469
+ If you want to serve a statically built webapp based on a single html entrypoint, you can use the
470
+ middleware provided in `/lib/express/middlewares/singlepageapp.js`.
471
+
472
+ Basic usage:
473
+
474
+ ```JavaScript
475
+ import express from "express";
476
+ import {singlePageApp} from "@cley_faye/boilerplate/lib/express/middlewares/singlepageapp.js";
477
+
478
+ const someRouter = express.Router();
479
+ someRouter.use("/app", singlePageApp({rootDir: "dist/webapp"});
480
+ ```
481
+
482
+ Available config options are:
483
+
484
+ - `rootDir`: mandatory, root directory containing the SPA
485
+ - `htmlFile`: optional, name of the HTML file to serve (defaults to "index.html")
486
+ - `staticRootDirectories`: optional, name of directories (in the above root directory) to serve as
487
+ static files. Defaults to `["js", "css", "img"]`.
488
+
467
489
  Console logging
468
490
  ---------------
469
491
  For convenience, a winston logger using console as output is provided:
@@ -0,0 +1,41 @@
1
+ import { RequestHandler } from "express";
2
+ interface SinglePageAppConfig {
3
+ /** Path to the file to serve for the whole application
4
+ *
5
+ * Any request coming for a directory not listed in the static list will reply with index.html
6
+ * from this directory.
7
+ */
8
+ rootDir: string;
9
+ /** Name of the app html file
10
+ *
11
+ * Defaults to "index.html"
12
+ */
13
+ htmlFile?: string;
14
+ /** Directories to serve as-is
15
+ *
16
+ * These are served by a static middleware.
17
+ * It is advised, if possible, to serve these from outside of node.
18
+ *
19
+ * Defaults to `["js", "css", "img"]`.
20
+ */
21
+ staticRootDirectories?: Array<string>;
22
+ }
23
+ /** Serve a single page webapp using a modern browser URL router
24
+ *
25
+ * This will serve all requests from the router using the provided index.html file, except for files
26
+ * in the listed static directories.
27
+ *
28
+ * Note that it is advised to serve these static directories from outside of node (ideally from a
29
+ * frontend server upper in the request handling).
30
+ *
31
+ * Basic usage:
32
+ * ```JavaScript
33
+ * import express from "express";
34
+ * import {singlePageApp} from "@cley_faye/boilerplate/lib/express/middlewares/singlepageapp.js";
35
+ *
36
+ * const someRouter = express.Router();
37
+ * someRouter.use("/app", singlePageApp({rootDir: "dist/webapp"});
38
+ * ```
39
+ */
40
+ export declare const singlePageApp: (config: SinglePageAppConfig) => Array<RequestHandler>;
41
+ export {};
@@ -0,0 +1,88 @@
1
+ /**
2
+ * @license
3
+ * @preserve
4
+ * MIT License
5
+ *
6
+ * Copyright (c) 2021 Gabriel Paul "Cley Faye" Risterucci
7
+ *
8
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
9
+ * of this software and associated documentation files (the "Software"), to deal
10
+ * in the Software without restriction, including without limitation the rights
11
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12
+ * copies of the Software, and to permit persons to whom the Software is
13
+ * furnished to do so, subject to the following conditions:
14
+ *
15
+ * The above copyright notice and this permission notice shall be included in all
16
+ * copies or substantial portions of the Software.
17
+ *
18
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24
+ * SOFTWARE.
25
+ *
26
+ */
27
+ "use strict";
28
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
29
+ if (k2 === undefined) k2 = k;
30
+ var desc = Object.getOwnPropertyDescriptor(m, k);
31
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
32
+ desc = { enumerable: true, get: function() { return m[k]; } };
33
+ }
34
+ Object.defineProperty(o, k2, desc);
35
+ }) : (function(o, m, k, k2) {
36
+ if (k2 === undefined) k2 = k;
37
+ o[k2] = m[k];
38
+ }));
39
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
40
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
41
+ }) : function(o, v) {
42
+ o["default"] = v;
43
+ });
44
+ var __importStar = (this && this.__importStar) || function (mod) {
45
+ if (mod && mod.__esModule) return mod;
46
+ var result = {};
47
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
48
+ __setModuleDefault(result, mod);
49
+ return result;
50
+ };
51
+ var __importDefault = (this && this.__importDefault) || function (mod) {
52
+ return (mod && mod.__esModule) ? mod : { "default": mod };
53
+ };
54
+ Object.defineProperty(exports, "__esModule", { value: true });
55
+ exports.singlePageApp = void 0;
56
+ const path = __importStar(require("node:path"));
57
+ const express_1 = __importDefault(require("express"));
58
+ /** Serve a single page webapp using a modern browser URL router
59
+ *
60
+ * This will serve all requests from the router using the provided index.html file, except for files
61
+ * in the listed static directories.
62
+ *
63
+ * Note that it is advised to serve these static directories from outside of node (ideally from a
64
+ * frontend server upper in the request handling).
65
+ *
66
+ * Basic usage:
67
+ * ```JavaScript
68
+ * import express from "express";
69
+ * import {singlePageApp} from "@cley_faye/boilerplate/lib/express/middlewares/singlepageapp.js";
70
+ *
71
+ * const someRouter = express.Router();
72
+ * someRouter.use("/app", singlePageApp({rootDir: "dist/webapp"});
73
+ * ```
74
+ */
75
+ const singlePageApp = (config) => {
76
+ const rootDir = path.resolve(config.rootDir);
77
+ const htmlFile = config.htmlFile ?? "index.html";
78
+ const staticDirs = (config.staticRootDirectories ?? ["js", "css", "img"]).map(c => `/${c}`);
79
+ const appRoute = (req, res, next) => {
80
+ for (const candidates of staticDirs)
81
+ if (req.url.startsWith(candidates))
82
+ return next();
83
+ res.sendFile(htmlFile, { root: rootDir }, (err) => next(err));
84
+ };
85
+ const staticRoute = express_1.default.static(rootDir);
86
+ return [appRoute, staticRoute];
87
+ };
88
+ exports.singlePageApp = singlePageApp;
@@ -1,3 +1,4 @@
1
+ import { ResolveOptions } from "webpack";
1
2
  import { BaseOptions } from "../util.js";
2
3
  import { HandlerFunctionResult } from "../reactapp.js";
3
4
  import { GruntConfig } from "./util.js";
@@ -27,7 +28,8 @@ export interface WebpackLoadersOptions {
27
28
  * Version of core-js to use (default to 3)
28
29
  *
29
30
  * @param [options.babel.targets]
30
- * Browser targets (default to " > 1%, last 1 safari version")
31
+ * Browser targets (default to "> 2% and not dead, last 2 safari version, last 2 firefox version,
32
+ * last 2 chrome version, last 2 edge version, Firefox ESR")
31
33
  *
32
34
  * @param [options.babel.plugins]
33
35
  * Plugins to add to babel
@@ -49,7 +51,7 @@ export interface WebpackOptions extends BaseOptions {
49
51
  plugins?: Array<Record<string, unknown>>;
50
52
  babelPlugins?: Array<Record<string, unknown>>;
51
53
  defines?: Record<string, string>;
52
- resolve?: Record<string, unknown>;
54
+ resolve?: ResolveOptions;
53
55
  typescript?: boolean;
54
56
  }
55
57
  /** Add a webpack task for the reactApp recipe
@@ -33,7 +33,6 @@ exports.handle = exports.webpackLoadersDefault = void 0;
33
33
  const path_1 = require("path");
34
34
  const node_fs_1 = require("node:fs");
35
35
  const eslint_webpack_plugin_1 = __importDefault(require("eslint-webpack-plugin"));
36
- const resolve_typescript_plugin_1 = __importDefault(require("resolve-typescript-plugin"));
37
36
  const util_js_1 = require("./util.js");
38
37
  const defaultCoreJS = 3;
39
38
  const eslintPlugin = () => new eslint_webpack_plugin_1.default({ cache: true });
@@ -50,7 +49,7 @@ const defaultLoadersDefine = (options) => [
50
49
  const defaultLoadersPresetEnv = (options) => [
51
50
  "@babel/preset-env",
52
51
  {
53
- targets: (options.babel?.targets) ?? "> 1%, last 1 safari version",
52
+ targets: (options.babel?.targets) ?? "> 2% and not dead, last 2 safari version, last 2 firefox version, last 2 chrome version, last 2 edge version, Firefox ESR",
54
53
  useBuiltIns: "usage",
55
54
  corejs: (options.babel?.corejs) ?? defaultCoreJS,
56
55
  modules: false,
@@ -76,7 +75,8 @@ const defaultLoadersReact = (options) => [
76
75
  * Version of core-js to use (default to 3)
77
76
  *
78
77
  * @param [options.babel.targets]
79
- * Browser targets (default to " > 1%, last 1 safari version")
78
+ * Browser targets (default to "> 2% and not dead, last 2 safari version, last 2 firefox version,
79
+ * last 2 chrome version, last 2 edge version, Firefox ESR")
80
80
  *
81
81
  * @param [options.babel.plugins]
82
82
  * Plugins to add to babel
@@ -170,10 +170,11 @@ const registerTasks = (gruntConfig, targetName, webpackConfig) => {
170
170
  const computeWebpackResolve = (webpackOptions) => {
171
171
  const res = { ...webpackOptions.resolve };
172
172
  if (webpackOptions.typescript) {
173
- if (res.plugins === undefined) {
174
- res.plugins = [];
173
+ if (res.extensionAlias === undefined) {
174
+ res.extensionAlias = {};
175
175
  }
176
- res.plugins.push(new resolve_typescript_plugin_1.default());
176
+ res.extensionAlias[".js"] = [".ts", ".tsx", ".js", ".jsx"];
177
+ res.extensionAlias[".mjs"] = [".mts", ".mtsx", ".mjs", ".mjsx"];
177
178
  }
178
179
  return res;
179
180
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cley_faye/boilerplate",
3
- "version": "3.14.0",
3
+ "version": "3.16.0",
4
4
  "description": "Boilerplate code generator for generic projects",
5
5
  "repository": {
6
6
  "type": "git",
@@ -18,14 +18,14 @@
18
18
  },
19
19
  "homepage": "https://github.com/CleyFaye/boilerplate#readme",
20
20
  "devDependencies": {
21
- "@keeex/eslint-config": "^1.9.2",
21
+ "@keeex/eslint-config": "^1.9.3",
22
22
  "@types/express": "^4.17.17",
23
23
  "@types/grunt": "^0.4.27",
24
24
  "@types/http-errors": "^2.0.1",
25
- "@types/node": "^18.11.19",
26
- "@typescript-eslint/eslint-plugin": "^5.50.0",
27
- "@typescript-eslint/parser": "^5.50.0",
28
- "eslint": "^8.33.0",
25
+ "@types/node": "^18.14.2",
26
+ "@typescript-eslint/eslint-plugin": "^5.53.0",
27
+ "@typescript-eslint/parser": "^5.53.0",
28
+ "eslint": "^8.35.0",
29
29
  "eslint-plugin-deprecation": "^1.3.3",
30
30
  "eslint-plugin-import": "^2.27.5",
31
31
  "eslint-plugin-promise": "^6.1.1",
@@ -36,8 +36,9 @@
36
36
  "grunt-contrib-clean": "^2.0.1",
37
37
  "grunt-run": "^0.8.1",
38
38
  "load-grunt-tasks": "^5.1.0",
39
- "logform": "^2.4.2",
39
+ "logform": "^2.5.1",
40
40
  "typescript": "^4.9.5",
41
+ "webpack": "^5.75.0",
41
42
  "winston": "^3.8.2"
42
43
  },
43
44
  "peerDependencies": {
@@ -57,7 +58,6 @@
57
58
  "grunt-contrib-watch": "1.x",
58
59
  "grunt-sass": "3.x",
59
60
  "grunt-webpack": "5.x",
60
- "resolve-typescript-plugin": "^2.0.0",
61
61
  "sass": "1.x",
62
62
  "ts-loader": "^9.4.1",
63
63
  "winston": "3.x"
@@ -111,9 +111,6 @@
111
111
  "grunt-webpack": {
112
112
  "optional": true
113
113
  },
114
- "resolve-typescript-plugin": {
115
- "optional": false
116
- },
117
114
  "sass": {
118
115
  "optional": true
119
116
  },