@cley_faye/boilerplate 3.2.2 → 3.3.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
@@ -19,6 +19,8 @@ required, and for what. It's up to the calling project to have the proper depend
19
19
 
20
20
  For webapp building:
21
21
 
22
+ - resolve-typescript-plugin (due to the way webpack plugins are handled, this is mandatory for dev
23
+ install)
22
24
  - babel-loader
23
25
  - eslint-webpack-plugin
24
26
  - @babel/core
@@ -34,6 +36,7 @@ For webapp building:
34
36
  - sass
35
37
  - grunt-webpack
36
38
  - grunt
39
+ - ts-loader (for supporting TypeScript import in webpack/babel)
37
40
 
38
41
  For express:
39
42
 
@@ -426,8 +429,10 @@ Typical use of TypeScript implies converting `.ts` file to `.js`.
426
429
  For a React app using webpack, there's two approach: either instruct webpack to accept `.ts` files
427
430
  (for example, using `ts-loader`) or have your entry point be a `.js` file that imports the generated
428
431
  output from TypeScript.
429
- Either way, watch mode will be capable of detecting change in the source material, as long as the
430
- TypeScript compiler is also running.
432
+ If you reference the output of the TypeScript compiler, you can keep it updated using `npx tsc -w`.
433
+
434
+ Using `ts-loader` can be done automatically by setting the `typescript` property in the webpack
435
+ configuration to `true`.
431
436
 
432
437
  For Express app, there is no particular things to take care, except that if you use a facility like
433
438
  `nodemon` you have to watch the TypeScript output instead of the actual source files.
@@ -9,6 +9,7 @@ export interface WebpackLoadersOptions {
9
9
  targets?: string;
10
10
  plugins?: Array<Record<string, unknown>>;
11
11
  };
12
+ typescript?: boolean;
12
13
  }
13
14
  /** Build the default webpack loaders list.
14
15
  *
@@ -30,6 +31,9 @@ export interface WebpackLoadersOptions {
30
31
  *
31
32
  * @param [options.babel.plugins]
32
33
  * Plugins to add to babel
34
+ *
35
+ * @param [options.typescript]
36
+ * Add support for importing .ts and .tsx files
33
37
  */
34
38
  export declare const webpackLoadersDefault: (options: WebpackLoadersOptions) => Array<Record<string, unknown>>;
35
39
  export interface WebpackOptions extends BaseOptions {
@@ -45,7 +49,8 @@ export interface WebpackOptions extends BaseOptions {
45
49
  plugins?: Array<Record<string, unknown>>;
46
50
  babelPlugins?: Array<Record<string, unknown>>;
47
51
  defines?: Record<string, string>;
48
- resolve?: unknown;
52
+ resolve?: Record<string, unknown>;
53
+ typescript?: boolean;
49
54
  }
50
55
  /** Add a webpack task for the reactApp recipe
51
56
  *
@@ -99,6 +104,9 @@ export interface WebpackOptions extends BaseOptions {
99
104
  * Build mode. "development", "production" or "none".
100
105
  * Defaults to "development"
101
106
  *
107
+ * @param [webpackOptions.typescript]
108
+ * Allow loading typescript source for webpack/babel.
109
+ *
102
110
  * @return
103
111
  * List of tasks added.
104
112
  * This function add "webpack:<targetName>".
@@ -33,6 +33,7 @@ exports.handle = exports.webpackLoadersDefault = void 0;
33
33
  const path_1 = require("path");
34
34
  const util_js_1 = require("./util.js");
35
35
  const eslint_webpack_plugin_1 = __importDefault(require("eslint-webpack-plugin"));
36
+ const resolve_typescript_plugin_1 = __importDefault(require("resolve-typescript-plugin"));
36
37
  const defaultCoreJS = 3;
37
38
  const eslintPlugin = () => new eslint_webpack_plugin_1.default({ cache: true });
38
39
  const defaultLoadersDefine = (options) => [
@@ -78,27 +79,43 @@ const defaultLoadersReact = (options) => [
78
79
  *
79
80
  * @param [options.babel.plugins]
80
81
  * Plugins to add to babel
82
+ *
83
+ * @param [options.typescript]
84
+ * Add support for importing .ts and .tsx files
81
85
  */
82
- const webpackLoadersDefault = (options) => [
83
- {
84
- test: /.js$/u,
85
- exclude: /node_modules/u,
86
- use: {
87
- loader: "babel-loader",
88
- options: {
89
- cacheDirectory: true,
90
- presets: [
91
- defaultLoadersPresetEnv(options),
92
- defaultLoadersReact(options),
93
- ],
94
- plugins: [
95
- defaultLoadersDefine(options),
96
- ...(options.babel?.plugins ?? []),
97
- ],
86
+ const webpackLoadersDefault = (options) => {
87
+ const allFilesCheck = options.typescript
88
+ ? /.(?:js|ts|tsx)$/u
89
+ : /.js$/u;
90
+ const res = [
91
+ {
92
+ test: allFilesCheck,
93
+ exclude: /node_modules/u,
94
+ use: {
95
+ loader: "babel-loader",
96
+ options: {
97
+ cacheDirectory: true,
98
+ presets: [
99
+ defaultLoadersPresetEnv(options),
100
+ defaultLoadersReact(options),
101
+ ],
102
+ plugins: [
103
+ defaultLoadersDefine(options),
104
+ ...(options.babel?.plugins ?? []),
105
+ ],
106
+ },
98
107
  },
99
108
  },
100
- },
101
- ];
109
+ ];
110
+ if (options.typescript) {
111
+ res.push({
112
+ test: /.(?:ts|tsx)$/u,
113
+ use: "ts-loader",
114
+ exclude: /node_modules/u,
115
+ });
116
+ }
117
+ return res;
118
+ };
102
119
  exports.webpackLoadersDefault = webpackLoadersDefault;
103
120
  const computeWebpackOutput = (webpackOptions, targetName) => webpackOptions.output ?? {
104
121
  path: (0, path_1.resolve)("dist", targetName, "js"),
@@ -143,6 +160,16 @@ const registerTasks = (gruntConfig, targetName, webpackConfig) => {
143
160
  (0, util_js_1.insertTask)(gruntConfig, "webpack", targetName, webpackConfig),
144
161
  ];
145
162
  };
163
+ const computeWebpackResolve = (webpackOptions) => {
164
+ const res = { ...webpackOptions.resolve };
165
+ if (webpackOptions.typescript) {
166
+ if (res.plugins === undefined) {
167
+ res.plugins = [];
168
+ }
169
+ res.plugins.push(new resolve_typescript_plugin_1.default());
170
+ }
171
+ return res;
172
+ };
146
173
  /** Add a webpack task for the reactApp recipe
147
174
  *
148
175
  * @param gruntConfig
@@ -195,6 +222,9 @@ const registerTasks = (gruntConfig, targetName, webpackConfig) => {
195
222
  * Build mode. "development", "production" or "none".
196
223
  * Defaults to "development"
197
224
  *
225
+ * @param [webpackOptions.typescript]
226
+ * Allow loading typescript source for webpack/babel.
227
+ *
198
228
  * @return
199
229
  * List of tasks added.
200
230
  * This function add "webpack:<targetName>".
@@ -209,7 +239,9 @@ const handle = (gruntConfig, targetName, webpackOptions) => {
209
239
  development: webpackOptions.mode === "development",
210
240
  defines: webpackOptions.defines,
211
241
  babel: { plugins: webpackOptions.babelPlugins },
242
+ typescript: webpackOptions.typescript,
212
243
  });
244
+ const resolve = computeWebpackResolve(webpackOptions);
213
245
  const webpackConfig = {
214
246
  mode: webpackOptions.mode,
215
247
  devtool: webpackOptions.mode === "development" ? "eval-source-map" : false,
@@ -221,7 +253,7 @@ const handle = (gruntConfig, targetName, webpackOptions) => {
221
253
  eslintPlugin(),
222
254
  ...webpackOptions.plugins ?? [],
223
255
  ],
224
- resolve: webpackOptions.resolve,
256
+ resolve,
225
257
  };
226
258
  // Special case: I'm not sure I can move options in the task-specific part of
227
259
  // the configuration, so I add it at the toplevel of the "webpack" task.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cley_faye/boilerplate",
3
- "version": "3.2.2",
3
+ "version": "3.3.0",
4
4
  "description": "Boilerplate code generator for generic projects",
5
5
  "repository": {
6
6
  "type": "git",
@@ -53,7 +53,9 @@
53
53
  "grunt-contrib-watch": "1.x",
54
54
  "grunt-sass": "3.x",
55
55
  "grunt-webpack": "5.x",
56
+ "resolve-typescript-plugin": "^1.1.1",
56
57
  "sass": "1.x",
58
+ "ts-loader": "^9.2.6",
57
59
  "winston": "3.x"
58
60
  },
59
61
  "peerDependenciesMeta": {