@cley_faye/boilerplate 3.2.0 → 3.3.1

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
@@ -31,9 +33,10 @@ For webapp building:
31
33
  - grunt-contrib-copy
32
34
  - grunt-contrib-watch
33
35
  - grunt-sass
34
- - node-sass
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
 
@@ -154,6 +157,7 @@ The webpack options support a host of properties:
154
157
  - babelPlugins: array of plugins for Babel
155
158
  - mode: The build mode. "development", "production" or "none"
156
159
  - defines: a record of value to define using Babel transform-define
160
+ - resolve: a custom "resolve" object for webpack config
157
161
 
158
162
  ### SASS
159
163
  All `.sass` and `.scss` files in the webapp directory will be processed as `.css` files in the
@@ -199,7 +203,7 @@ module.exports = grunt => {
199
203
  )
200
204
  );
201
205
  grunt.initConfig(baseGruntConfig);
202
- grunt.register(
206
+ grunt.registerTask(
203
207
  "myWebApp",
204
208
  "Build the webapp",
205
209
  requiredTasks
@@ -425,8 +429,10 @@ Typical use of TypeScript implies converting `.ts` file to `.js`.
425
429
  For a React app using webpack, there's two approach: either instruct webpack to accept `.ts` files
426
430
  (for example, using `ts-loader`) or have your entry point be a `.js` file that imports the generated
427
431
  output from TypeScript.
428
- Either way, watch mode will be capable of detecting change in the source material, as long as the
429
- 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`.
430
436
 
431
437
  For Express app, there is no particular things to take care, except that if you use a facility like
432
438
  `nodemon` you have to watch the TypeScript output instead of the actual source files.
@@ -66,6 +66,10 @@ export declare type OptionDefinition = OptionDefinitionString | OptionDefinition
66
66
  * Expected options from command line
67
67
  */
68
68
  export declare type ConfigDefinition = Record<string, OptionDefinition>;
69
+ /**
70
+ * Display help to the user
71
+ */
72
+ export declare const displayHelp: (configDef: ConfigDefinition) => void;
69
73
  /**
70
74
  * Construct the full config object from command line values.
71
75
  *
package/lib/grunt/args.js CHANGED
@@ -26,7 +26,7 @@
26
26
  */
27
27
  "use strict";
28
28
  Object.defineProperty(exports, "__esModule", { value: true });
29
- exports.getOptionsFromGruntCLI = exports.OptType = void 0;
29
+ exports.getOptionsFromGruntCLI = exports.displayHelp = exports.OptType = void 0;
30
30
  const util_js_1 = require("../util.js");
31
31
  // eslint-disable-next-line no-shadow
32
32
  var OptType;
@@ -125,6 +125,7 @@ const displayHelp = (configDef) => {
125
125
  outputHelp("Accepted command line arguments:");
126
126
  Object.keys(configDef).forEach(configKey => displayHelpForConfigKey(configKey, configDef[configKey]));
127
127
  };
128
+ exports.displayHelp = displayHelp;
128
129
  /**
129
130
  * Construct the full config object from command line values.
130
131
  *
@@ -132,7 +133,7 @@ const displayHelp = (configDef) => {
132
133
  */
133
134
  const getOptionsFromGruntCLI = (optionProvider, configDef) => {
134
135
  if (getFromOptionProvider("help", optionProvider) === true) {
135
- displayHelp(configDef);
136
+ (0, exports.displayHelp)(configDef);
136
137
  return null;
137
138
  }
138
139
  const result = {};
@@ -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,6 +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>;
52
+ resolve?: Record<string, unknown>;
53
+ typescript?: boolean;
48
54
  }
49
55
  /** Add a webpack task for the reactApp recipe
50
56
  *
@@ -98,6 +104,9 @@ export interface WebpackOptions extends BaseOptions {
98
104
  * Build mode. "development", "production" or "none".
99
105
  * Defaults to "development"
100
106
  *
107
+ * @param [webpackOptions.typescript]
108
+ * Allow loading typescript source for webpack/babel.
109
+ *
101
110
  * @return
102
111
  * List of tasks added.
103
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,6 +253,7 @@ const handle = (gruntConfig, targetName, webpackOptions) => {
221
253
  eslintPlugin(),
222
254
  ...webpackOptions.plugins ?? [],
223
255
  ],
256
+ resolve,
224
257
  };
225
258
  // Special case: I'm not sure I can move options in the task-specific part of
226
259
  // the configuration, so I add it at the toplevel of the "webpack" task.
@@ -103,9 +103,6 @@ const createWatchTasks = (gruntConfig, mergedResults, watchMode) => {
103
103
  if (watchMode === false) {
104
104
  return;
105
105
  }
106
- if (mergedResults.watchTasks.length > 0) {
107
- (0, util_js_2.insertTask)(gruntConfig, "watch", "options", getWatcherOptions(watchMode));
108
- }
109
106
  mergedResults.watchTasks.forEach(watchTask => {
110
107
  (0, util_js_2.insertTask)(gruntConfig, "watch", getWatchTaskName(watchTask), {
111
108
  options: getWatcherOptions(watchMode),
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cley_faye/boilerplate",
3
- "version": "3.2.0",
3
+ "version": "3.3.1",
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": {