@workleap/swc-configs 0.0.1 → 1.0.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.
Files changed (3) hide show
  1. package/CHANGELOG.md +7 -0
  2. package/README.md +217 -36
  3. package/package.json +4 -4
package/CHANGELOG.md ADDED
@@ -0,0 +1,7 @@
1
+ # @workleap/swc-configs
2
+
3
+ ## 1.0.0
4
+
5
+ ### Major Changes
6
+
7
+ - [#85](https://github.com/workleap/wl-web-configs/pull/85) [`bad2df7`](https://github.com/workleap/wl-web-configs/commit/bad2df75593fb70d431d73bdced653b157c50caa) Thanks [@patricklafrance](https://github.com/patricklafrance)! - Updated TSUP configuration and added a new SWC config package
package/README.md CHANGED
@@ -26,50 +26,63 @@ npm install -D @workleap/swc-configs @swc/core @swc/helpers
26
26
 
27
27
  ### Dev
28
28
 
29
- Create a `swc.dev.ts` file at the root of your project:
29
+ Create a `swc.dev.js` file at the root of your project:
30
30
 
31
31
  ```
32
32
  root
33
33
  |---/src
34
- |---swc.dev.ts
34
+ |---swc.dev.js
35
+ |---webpack.dev.js
35
36
  ```
36
37
 
37
- Then, in the newly created file, import and execute the `defineDevConfig` function to return a valid SWC configuration object:
38
+ Then, in the newly created file, import and execute the `defineDevConfig` function to export a valid SWC configuration object:
38
39
 
39
40
  ```ts
40
- // swc.dev.ts
41
+ // swc.dev.js
41
42
 
42
43
  import { defineDevConfig } from "@workleap/swc-configs";
43
44
 
44
- return defineDevConfig();
45
+ export const swcConfig = defineDevConfig();
45
46
  ```
46
47
 
47
48
  Finally, install the Webpack [swc-loader](https://swc.rs/docs/usage/swc-loader) package and add the following `rule` to your Webpack configuration:
48
49
 
49
50
  ```js
50
- {
51
- test: /\.(ts|tsx)/i,
52
- exclude: /node_modules/,
53
- use: {
54
- loader: "swc-loader"
51
+ // webpack.dev.js
52
+
53
+ // @ts-check
54
+
55
+ import { swcConfig } from "./swc.dev.js";
56
+
57
+ /** @type {import("webpack").Configuration} */
58
+ export default {
59
+ module: {
60
+ rules: {
61
+ test: /\.(ts|tsx)/i,
62
+ exclude: /node_modules/,
63
+ use: {
64
+ loader: "swc-loader",
65
+ options: swcConfig
66
+ }
67
+ }
55
68
  }
56
69
  }
57
70
  ```
58
71
 
59
- > The rule declaration may vary depending of your project configuration. For example, you might change the `test` property to target ECMAScript files instead: `test: /\.(js|jsx)/i`.
72
+ > The rule declaration may vary depending of your project configuration. For example, you might want to change the `test` property to target ECMAScript files instead: `test: /\.(js|jsx)/i`.
60
73
 
61
- #### Customize the configuration
74
+ #### Customizing the configuration
62
75
 
63
- If you wish to customize the default development configuration, there a few options.
76
+ If you wish to customize the default development configuration, there are a few possibilities.
64
77
 
65
78
  ##### Use predefined options
66
79
 
67
- The `defineDevConfig` function accepts a few convivial options to customize the default SWC configuration without having to learn any [SWC specific configuration syntax](https://swc.rs/docs/configuration/swcrc) or mangling with complicated options.
80
+ The `defineDevConfig` function accepts predefined options to customize the default SWC configuration without having to learn any [SWC specific configuration syntax](https://swc.rs/docs/configuration/swcrc) or mangling with complicated configuration properties.
68
81
 
69
82
  To enable [React fast refresh](https://github.com/pmmmwh/react-refresh-webpack-plugin), use the `fastRefresh` option:
70
83
 
71
84
  ```ts
72
- // swc.dev.ts
85
+ // swc.dev.js
73
86
 
74
87
  import { defineDevConfig } from "@workleap/swc-configs";
75
88
 
@@ -80,10 +93,10 @@ return defineDevConfig({
80
93
 
81
94
  > Don't forget to install [ReactRefreshWebpackPlugin](https://github.com/pmmmwh/react-refresh-webpack-plugin) and update your Webpack configuration accordingly.
82
95
 
83
- By default the development configuration expect to parse TypeScript code. If your project is coded with regular JavaScript (ECMAScript), use the `parser` option to tell SWC to expect regular JavaScript code rather than TypeScript code:
96
+ The development configuration expect to parse TypeScript code. If your project is coded with regular JavaScript (ECMAScript), use the `parser` option to configure SWC to parse regular JavaScript code rather than TypeScript code:
84
97
 
85
98
  ```ts
86
- // swc.dev.ts
99
+ // swc.dev.js
87
100
 
88
101
  import { defineDevConfig } from "@workleap/swc-configs";
89
102
 
@@ -92,13 +105,187 @@ return defineDevConfig({
92
105
  });
93
106
  ```
94
107
 
95
- ##### Provide a custom configuration object
108
+ ##### Manually overriding SWC configuration
109
+
110
+ Refer to the [configuration override](#configuration-override) section.
111
+
112
+ ### Build
113
+
114
+ Create a `swc.build.js` file at the root of your project:
115
+
116
+ ```
117
+ root
118
+ |---/src
119
+ |---swc.build.js
120
+ |---webpack.build.js
121
+ ```
122
+
123
+ Then, in the newly created file, import and execute the `defineBuildConfig` function to export a valid SWC configuration object:
124
+
125
+ ```ts
126
+ // swc.build.js
127
+
128
+ import { defineBuildConfig } from "@workleap/swc-configs";
129
+
130
+ export const swcConfig = defineBuildConfig();
131
+ ```
132
+
133
+ Finally, install the Webpack [swc-loader](https://swc.rs/docs/usage/swc-loader) package and add the following `rule` to your Webpack configuration:
134
+
135
+ ```js
136
+ // webpack.build.js
137
+
138
+ // @ts-check
139
+
140
+ import { swcConfig } from "./swc.build.js";
141
+
142
+ /** @type {import("webpack").Configuration} */
143
+ export default {
144
+ module: {
145
+ rules: {
146
+ test: /\.(ts|tsx)/i,
147
+ exclude: /node_modules/,
148
+ use: {
149
+ loader: "swc-loader",
150
+ options: swcConfig
151
+ }
152
+ }
153
+ }
154
+ }
155
+ ```
156
+
157
+ #### Customizing the configuration
158
+
159
+ If you wish to customize the default build configuration, there are a few possibilities.
160
+
161
+ ##### Use predefined options
162
+
163
+ The `defineBuildConfig` function accepts predefined options to customize the default SWC configuration without having to learn any [SWC specific configuration syntax](https://swc.rs/docs/configuration/swcrc) or mangling with complicated configuration properties.
164
+
165
+ The build configuration expect to parse TypeScript code. If your project is coded with regular JavaScript (ECMAScript), use the `parser` option to configure SWC to parse regular JavaScript code rather than TypeScript code:
166
+
167
+ ```ts
168
+ // swc.build.js
169
+
170
+ import { defineBuildConfig } from "@workleap/swc-configs";
171
+
172
+ return defineBuildConfig({
173
+ parser: "ecmascript"
174
+ });
175
+ ```
176
+
177
+ ##### Manually overriding SWC configuration
178
+
179
+ Refer to the [configuration override](#configuration-override) section.
180
+
181
+ ### Jest
182
+
183
+ #### Installation
184
+
185
+ First install the following package:
186
+
187
+ **With pnpm**
188
+
189
+ ```shell
190
+ pnpm add -D @swc/jest
191
+ ```
192
+
193
+ **With yarn**
194
+
195
+ ```shell
196
+ yarn add -D @swc/jest
197
+ ```
198
+
199
+ **With npm**
200
+
201
+ ```shell
202
+ yarn add -D @swc/jest
203
+ ```
204
+
205
+ #### Usage
206
+
207
+ Create a `swc.jest.ts` file at the root of your project:
208
+
209
+ ```
210
+ root
211
+ |---/src
212
+ |---swc.jest.ts
213
+ |---jest.config.ts
214
+ ```
215
+
216
+ Then, in the newly created file, import and execute the `defineJestConfig` function to export a valid SWC configuration object:
217
+
218
+ ```ts
219
+ // swc.jest.ts
220
+
221
+ import { defineJestConfig } from "@workleap/swc-configs";
222
+
223
+ export const swcConfig = defineJestConfig();
224
+ ```
225
+
226
+ Finally, add the following Jest [code transform](https://jestjs.io/docs/code-transformation) to your `jest.config.ts` file:
227
+
228
+ ```js
229
+ // jest.config.ts
230
+
231
+ import { swcCnfig } from "./swc.jest.ts";
232
+
233
+ const config = {
234
+ transform: {
235
+ "^.+\\.(ts|tsx)$": ["@swc/jest", swcConfig as Record<string, unknown>]
236
+ }
237
+ };
238
+ ```
239
+
240
+ > The transform declaration may vary depending of your project configuration. For example, you might want to change the file extensions to target ECMAScript files instead: `^.+\\.(js|jsx)$`.
241
+
242
+ #### Customizing the configuration
243
+
244
+ If you wish to customize the default Jest configuration, there are a few possibilities.
245
+
246
+ ##### Use predefined options
247
+
248
+ The `defineJestConfig` function accepts predefined options to customize the default SWC configuration without having to learn any [SWC specific configuration syntax](https://swc.rs/docs/configuration/swcrc) or mangling with complicated configuration properties.
249
+
250
+ The default Jest configuration doesn't include React. To include React, use the `react` option:
251
+
252
+ ```ts
253
+ // swc.jest.ts
254
+
255
+ import { defineJestConfig } from "@workleap/swc-configs";
256
+
257
+ return defineJestConfig({
258
+ react: true
259
+ });
260
+ ```
261
+
262
+ The Jest configuration expect to parse TypeScript code. If your project is coded with regular JavaScript (ECMAScript), use the `parser` option to configure SWC to parse regular JavaScript code rather than TypeScript code:
263
+
264
+ ```ts
265
+ // swc.jest.ts
266
+
267
+ import { defineJestConfig } from "@workleap/swc-configs";
268
+
269
+ return defineJestConfig({
270
+ parser: "ecmascript"
271
+ });
272
+ ```
273
+
274
+ ##### Manually overriding SWC configuration
275
+
276
+ Refer to the [configuration override](#configuration-override) section.
277
+
278
+ ### Configuration override
279
+
280
+ > This documentation section will use the [Dev configuration](#dev) `defineDevConfig` function and `DefaultDevConfig` object to showcase examples but keep in mind that it also available for the [Build](#build) and [Jest](#jest) configurations.
96
281
 
97
- The `defineDevConfig` function also accepts a `configOverride` property. This property allow you to override any options of the default development configuration. It's important to note that the provided configuration object **will not** merge with the default configuration, it will override any matching properties of the default configuration.
282
+ All "define" functions accepts a `configOverride` property. This property allow the consumer to override any property of the default SWC configuration. The `configOverride` supports 2 syntaxes, a [custom object](#custom-object) and a [function](#function).
98
283
 
99
- > Prefer using [predefined options](#use-predefined-options) whenever possible.
284
+ It's important to note that with both syntaxes, the provided configuration **will not** be merged with the default configuration, it will override any matching properties of the default configuration.
100
285
 
101
- Given the following code:
286
+ #### Custom object
287
+
288
+ The easiest way to override manually the SWC config is with the object syntax:
102
289
 
103
290
  ```ts
104
291
  // swc.dev.ts
@@ -116,7 +303,7 @@ return defineDevConfig({
116
303
  });
117
304
  ```
118
305
 
119
- The whole `jsx` section of the default configuration would be overrided for:
306
+ In the previous example, the whole `jsx` section of the default configuration would be overrided for:
120
307
 
121
308
  ```js
122
309
  jsc: {
@@ -126,7 +313,7 @@ jsc: {
126
313
  }
127
314
  ```
128
315
 
129
- To extend the default configuration options rather than overriding them, use the `DefaultDevConfig` object to merge additional options with the default configuration:
316
+ While in some cases it's fine, often a consumer would prefer to extend the default configuration rather than overriding it's properties. To extend the configuration, use the `DefaultDevConfig` object to merge new properties with the default configuration properties:
130
317
 
131
318
  ```ts
132
319
  // swc.dev.ts
@@ -145,9 +332,9 @@ return defineDevConfig({
145
332
  });
146
333
  ```
147
334
 
148
- ##### Using a function to customize the final configuration object
335
+ ##### Function
149
336
 
150
- The `configOverride` property also accept a function. While there are a wide range of additional use cases that can be solved by using a function rather than an object, the main purpose of supporting a function here is to allow mixing [predefined options](#use-predefined-options) with a custom configuration object.
337
+ The `configOverride` property also accept a function. While there are a wide range of additional use cases that can be solved with a function, the main purpose of using a function here is to allow mixing **predefined options** with a **custom configuration object**.
151
338
 
152
339
  Take the following example:
153
340
 
@@ -169,13 +356,13 @@ return defineDevConfig({
169
356
  });
170
357
  ```
171
358
 
172
- The consumer wish to enable [React fast refresh](https://github.com/pmmmwh/react-refresh-webpack-plugin) and ESM dynamic imports. That's a good idea to use the `fastRefresh` [predefined options](#use-predefined-options) to enable fast refresh. Since there are no predefined option to enable ESM dynamic imports, a custom configuration object must be provided to extend the `jsc.parser` section with the `dynamicImport` property.
359
+ In this example, the consumer wish to enable [React fast refresh](https://github.com/pmmmwh/react-refresh-webpack-plugin) and ESM dynamic imports. That's a good idea to use the `fastRefresh` predefined options to enable fast refresh. However, since there are no predefined option to enable ESM dynamic imports, a custom configuration object must be provided to extend the `jsc.parser` section with the `dynamicImport` property.
173
360
 
174
- Given that the consumer extends the configuration rather than overriding it, we could expect that the React fast refresh configuration will be included in the final configuration.
361
+ Given that the consumer extends the configuration rather than overriding it, one could expect that the React fast refresh configuration will be included in the final configuration.
175
362
 
176
- Wrong, the React fast refresh configuration won't be included because it's not part of the `DefaultDevConfig` object.
363
+ Wrong, the React fast refresh configuration won't be included because it's not part of the `DefaultDevConfig` object that the consumer is extending.
177
364
 
178
- To extend the default development configuration and support predefined options, you must provide a function rather than an object:
365
+ To extend the default development configuration and also use predefined options, you must provide a function rather than an object:
179
366
 
180
367
  ```ts
181
368
  // swc.dev.ts
@@ -198,13 +385,7 @@ return defineDevConfig({
198
385
  });
199
386
  ```
200
387
 
201
- Using a function allow the consumer to receive as argument a configuration object derived from the `DefaultDevConfig` including any additional configuration added with [predefined options](#use-predefined-options).
202
-
203
- ### Build
204
-
205
- ### Jest
206
-
207
- ### Configuration override
388
+ Using a function allow the consumer to receive as argument a configuration object derived from the `DefaultDevConfig` object, including any additional properties added with predefined options.
208
389
 
209
390
  ## Maintainers notes
210
391
 
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@workleap/swc-configs",
3
3
  "description": "Workleap recommended SWC configs.",
4
- "version": "0.0.1",
4
+ "version": "1.0.0",
5
5
  "license": "Apache-2.0",
6
6
  "keywords": [
7
7
  "workleap",
@@ -34,9 +34,9 @@
34
34
  "ts-node": "10.9.1",
35
35
  "tsup": "6.7.0",
36
36
  "typescript": "5.0.4",
37
- "@workleap/eslint-plugin": "1.8.1",
38
- "@workleap/tsup-configs": "1.0.1",
39
- "@workleap/typescript-configs": "2.3.1"
37
+ "@workleap/eslint-plugin": "1.8.2",
38
+ "@workleap/tsup-configs": "2.0.0",
39
+ "@workleap/typescript-configs": "2.3.2"
40
40
  },
41
41
  "peerDependencies": {
42
42
  "@swc/core": "*",