@workleap/swc-configs 1.0.1 → 2.1.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/CHANGELOG.md CHANGED
@@ -1,5 +1,17 @@
1
1
  # @workleap/swc-configs
2
2
 
3
+ ## 2.1.0
4
+
5
+ ### Minor Changes
6
+
7
+ - [#106](https://github.com/gsoft-inc/wl-web-configs/pull/106) [`a747302`](https://github.com/gsoft-inc/wl-web-configs/commit/a74730233d78c71e45bcd8911a706630d359a67c) Thanks [@patricklafrance](https://github.com/patricklafrance)! - Fixed SWC targets and support TS 5.2
8
+
9
+ ## 2.0.0
10
+
11
+ ### Major Changes
12
+
13
+ - [#98](https://github.com/gsoft-inc/wl-web-configs/pull/98) [`56ca32e`](https://github.com/gsoft-inc/wl-web-configs/commit/56ca32ee3194c51210aacc5189f3ebbec5a4a7b6) Thanks [@patricklafrance](https://github.com/patricklafrance)! - New start, every package has a major version bump
14
+
3
15
  ## 1.0.1
4
16
 
5
17
  ### Patch Changes
package/README.md CHANGED
@@ -1,395 +1,19 @@
1
- # @workleap/swc-configs
1
+ # swc-configs
2
2
 
3
- ## Installation
4
-
5
- Install the following packages:
6
-
7
- **With pnpm**
8
-
9
- ```shell
10
- pnpm add -D @workleap/swc-configs @swc/core @swc/helpers
11
- ```
12
-
13
- **With yarn**
14
-
15
- ```shell
16
- yarn add -D @workleap/swc-configs @swc/core @swc/helpers
17
- ```
18
-
19
- **With npm**
20
-
21
- ```shell
22
- npm install -D @workleap/swc-configs @swc/core @swc/helpers
23
- ```
3
+ [![License](https://img.shields.io/badge/License-Apache_2.0-blue.svg)](../../LICENSE)
4
+ [![npm version](https://img.shields.io/npm/v/@workleap/swc-configs)](https://www.npmjs.com/package/@workleap/swc-configs)
24
5
 
25
6
  ## Usage
26
7
 
27
- ### Dev
28
-
29
- Create a `swc.dev.js` file at the root of your project:
30
-
31
- ```
32
- root
33
- |---/src
34
- |---swc.dev.js
35
- |---webpack.dev.js
36
- ```
37
-
38
- Then, in the newly created file, import and execute the `defineDevConfig` function to export a valid SWC configuration object:
39
-
40
- ```ts
41
- // swc.dev.js
42
-
43
- import { defineDevConfig } from "@workleap/swc-configs";
44
-
45
- export const swcConfig = defineDevConfig();
46
- ```
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:
49
-
50
- ```js
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
- }
68
- }
69
- }
70
- ```
71
-
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`.
73
-
74
- #### Customizing the configuration
75
-
76
- If you wish to customize the default development configuration, there are a few possibilities.
77
-
78
- ##### Use predefined options
79
-
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.
81
-
82
- To enable [React fast refresh](https://github.com/pmmmwh/react-refresh-webpack-plugin), use the `fastRefresh` option:
83
-
84
- ```ts
85
- // swc.dev.js
86
-
87
- import { defineDevConfig } from "@workleap/swc-configs";
88
-
89
- return defineDevConfig({
90
- fastRefresh: true
91
- });
92
- ```
93
-
94
- > Don't forget to install [ReactRefreshWebpackPlugin](https://github.com/pmmmwh/react-refresh-webpack-plugin) and update your Webpack configuration accordingly.
95
-
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:
97
-
98
- ```ts
99
- // swc.dev.js
100
-
101
- import { defineDevConfig } from "@workleap/swc-configs";
102
-
103
- return defineDevConfig({
104
- parser: "ecmascript"
105
- });
106
- ```
107
-
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.
281
-
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).
283
-
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.
285
-
286
- #### Custom object
287
-
288
- The easiest way to override manually the SWC config is with the object syntax:
289
-
290
- ```ts
291
- // swc.dev.ts
292
-
293
- import { defineDevConfig } from "@workleap/swc-configs";
294
-
295
- return defineDevConfig({
296
- configOverride: {
297
- jsc: {
298
- parser: {
299
- dynamicImport: true
300
- }
301
- }
302
- }
303
- });
304
- ```
305
-
306
- In the previous example, the whole `jsx` section of the default configuration would be overrided for:
307
-
308
- ```js
309
- jsc: {
310
- parser: {
311
- dynamicImport: true
312
- }
313
- }
314
- ```
315
-
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:
317
-
318
- ```ts
319
- // swc.dev.ts
320
-
321
- import { defineDevConfig, DefaultDevConfig } from "@workleap/swc-configs";
322
- import merge from "deepmerge";
323
-
324
- return defineDevConfig({
325
- configOverride: merge.all(DefaultDevConfig, {
326
- jsc: {
327
- parser: {
328
- dynamicImport: true
329
- }
330
- }
331
- })
332
- });
333
- ```
334
-
335
- ##### Function
336
-
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**.
338
-
339
- Take the following example:
340
-
341
- ```ts
342
- // swc.dev.ts
343
-
344
- import { defineDevConfig, DefaultDevConfig } from "@workleap/swc-configs";
345
- import merge from "deepmerge";
346
-
347
- return defineDevConfig({
348
- fastRefresh: true,
349
- configOverride: merge.all(DefaultDevConfig, {
350
- jsc: {
351
- parser: {
352
- dynamicImport: true
353
- }
354
- }
355
- })
356
- });
357
- ```
358
-
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.
360
-
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.
362
-
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.
364
-
365
- To extend the default development configuration and also use predefined options, you must provide a function rather than an object:
366
-
367
- ```ts
368
- // swc.dev.ts
369
-
370
- import { defineDevConfig, DefaultDevConfig } from "@workleap/swc-configs";
371
- import merge from "deepmerge";
8
+ View the [user's documentation](https://gsoft-inc.github.io/wl-web-configs/).
372
9
 
373
- return defineDevConfig({
374
- fastRefresh: true,
375
- configOverride: (config) => {
376
- // The "config" argument is the DefaultDevConfig object including the fast refresh configuration.
377
- return merge.all(config, {
378
- jsc: {
379
- parser: {
380
- dynamicImport: true
381
- }
382
- }
383
- });
384
- }
385
- });
386
- ```
10
+ ## 🤝 Contributing
387
11
 
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.
12
+ View the [contributor's documentation](../../CONTRIBUTING.md).
389
13
 
390
- ## Maintainers notes
14
+ ### Notes
391
15
 
392
- ### CJS support
16
+ #### CJS support
393
17
 
394
18
  To support CJS projects, this package is build for ESM and CJS formats. To support CJS, `type: "module"` has been temporary removed from the `package.json` file.
395
19
 
@@ -397,4 +21,4 @@ Once all our projects use ESM, CJS support can be removed.
397
21
 
398
22
  ## License
399
23
 
400
- Copyright © 2023, GSoft inc. This code is licensed under the Apache License, Version 2.0. You may obtain a copy of this license at https://github.com/gsoft-inc/gsoft-license/blob/master/LICENSE.
24
+ Copyright © 2023, Workleap. This code is licensed under the Apache License, Version 2.0. You may obtain a copy of this license at https://github.com/gsoft-inc/workleap-license/blob/master/LICENSE.
@@ -0,0 +1,9 @@
1
+ import { Config } from '@swc/core';
2
+
3
+ interface SwcConfigTransformerContext {
4
+ environment: "dev" | "build" | "jest";
5
+ }
6
+ type SwcConfigTransformer = (config: Config, context: SwcConfigTransformerContext) => Config;
7
+ declare function applyTransformers(config: Config, transformers: SwcConfigTransformer[], context: SwcConfigTransformerContext): Config;
8
+
9
+ export { SwcConfigTransformer, SwcConfigTransformerContext, applyTransformers };
@@ -0,0 +1,9 @@
1
+ import browserslist from 'browserslist';
2
+
3
+ declare function createSwcTargetsFromBrowserslistEntries(entries: string[]): Record<keyof readonly ["ie", "edge", "firefox", "chrome", "safari", "opera", "ios", "android", "op_mob", "and_chr", "and_ff", "ie_mob", "samsung"], string>;
4
+ interface BrowserslistToSwcOptions extends Omit<browserslist.Options, "path"> {
5
+ queries?: string | string[];
6
+ }
7
+ declare function browserslistToSwc(options?: BrowserslistToSwcOptions): Record<keyof readonly ["ie", "edge", "firefox", "chrome", "safari", "opera", "ios", "android", "op_mob", "and_chr", "and_ff", "ie_mob", "samsung"], string>;
8
+
9
+ export { BrowserslistToSwcOptions, browserslistToSwc, createSwcTargetsFromBrowserslistEntries };
@@ -0,0 +1,9 @@
1
+ import browserslist from 'browserslist';
2
+
3
+ declare function createSwcTargetsFromBrowserslistEntries(entries: string[]): Record<keyof readonly ["ie", "edge", "firefox", "chrome", "safari", "opera", "ios", "android", "op_mob", "and_chr", "and_ff", "ie_mob", "samsung"], string>;
4
+ interface BrowserslistToSwcOptions extends Omit<browserslist.Options, "path"> {
5
+ queries?: string | string[];
6
+ }
7
+ declare function browserslistToSwc(options?: BrowserslistToSwcOptions): Record<keyof readonly ["ie", "edge", "firefox", "chrome", "safari", "opera", "ios", "android", "op_mob", "and_chr", "and_ff", "ie_mob", "samsung"], string>;
8
+
9
+ export { BrowserslistToSwcOptions, browserslistToSwc, createSwcTargetsFromBrowserslistEntries };
@@ -0,0 +1,80 @@
1
+ 'use strict';
2
+
3
+ var browserslist = require('browserslist');
4
+
5
+ function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
6
+
7
+ var browserslist__default = /*#__PURE__*/_interopDefault(browserslist);
8
+
9
+ // src/browserslistToSwc.ts
10
+ var SwcSupportedBrowsers = [
11
+ "ie",
12
+ "edge",
13
+ "firefox",
14
+ "chrome",
15
+ "safari",
16
+ "opera",
17
+ "ios",
18
+ "android",
19
+ "op_mob",
20
+ "and_chr",
21
+ "and_ff",
22
+ "ie_mob",
23
+ "samsung"
24
+ ];
25
+ var SwcMobileBrowserAliases = {
26
+ "and_chr": "chrome",
27
+ "and_ff": "firefox",
28
+ "ie_mob": "ie",
29
+ "ios_saf": "ios",
30
+ "op_mob": "opera"
31
+ };
32
+ function parseBrowserslistEntry(entry) {
33
+ const values = entry.split(" ");
34
+ let browser = values[0];
35
+ let version = values[1];
36
+ if (SwcMobileBrowserAliases[browser]) {
37
+ browser = SwcMobileBrowserAliases[browser];
38
+ }
39
+ if (version.includes("-")) {
40
+ version = version.slice(0, version.indexOf("-"));
41
+ }
42
+ if (version.endsWith(".0")) {
43
+ version = version.slice(0, -2);
44
+ }
45
+ return {
46
+ browser,
47
+ version
48
+ };
49
+ }
50
+ function createSwcTargetsFromBrowserslistEntries(entries) {
51
+ return entries.reduce((acc, x) => {
52
+ const { browser, version } = parseBrowserslistEntry(x);
53
+ if (SwcSupportedBrowsers.indexOf(browser) === -1) {
54
+ return acc;
55
+ }
56
+ const _browser = browser;
57
+ if (acc[_browser]) {
58
+ if (parseFloat(acc[_browser]) > parseFloat(version)) {
59
+ acc[_browser] = version;
60
+ }
61
+ } else {
62
+ acc[_browser] = version;
63
+ }
64
+ return acc;
65
+ }, {});
66
+ }
67
+ function browserslistToSwc(options = {}) {
68
+ const {
69
+ queries,
70
+ ...browserlistsOptions
71
+ } = options;
72
+ const entries = browserslist__default.default(queries, {
73
+ path: "./browserslistrc",
74
+ ...browserlistsOptions
75
+ });
76
+ return createSwcTargetsFromBrowserslistEntries(entries);
77
+ }
78
+
79
+ exports.browserslistToSwc = browserslistToSwc;
80
+ exports.createSwcTargetsFromBrowserslistEntries = createSwcTargetsFromBrowserslistEntries;
@@ -0,0 +1 @@
1
+ export { browserslistToSwc, createSwcTargetsFromBrowserslistEntries } from './chunk-MGMFB3UY.mjs';
@@ -0,0 +1,11 @@
1
+ import { Config } from '@swc/core';
2
+ import { SwcConfigTransformer } from './applyTransformers.mjs';
3
+
4
+ interface DefineBuildConfigOptions {
5
+ targets: Record<string, string>;
6
+ parser?: "ecmascript" | "typescript";
7
+ transformers?: SwcConfigTransformer[];
8
+ }
9
+ declare function defineBuildConfig(options: DefineBuildConfigOptions): Config;
10
+
11
+ export { DefineBuildConfigOptions, defineBuildConfig };
package/dist/build.d.ts CHANGED
@@ -2,7 +2,7 @@ import { Config } from '@swc/core';
2
2
  import { SwcConfigTransformer } from './applyTransformers.js';
3
3
 
4
4
  interface DefineBuildConfigOptions {
5
- browsers: any;
5
+ targets: Record<string, string>;
6
6
  parser?: "ecmascript" | "typescript";
7
7
  transformers?: SwcConfigTransformer[];
8
8
  }
package/dist/build.js CHANGED
@@ -8,7 +8,7 @@ function applyTransformers(config, transformers, context) {
8
8
  // src/build.ts
9
9
  function defineBuildConfig(options) {
10
10
  const {
11
- browsers,
11
+ targets,
12
12
  parser = "typescript",
13
13
  transformers = []
14
14
  } = options;
@@ -21,8 +21,6 @@ function defineBuildConfig(options) {
21
21
  syntax: "typescript",
22
22
  tsx: true
23
23
  },
24
- // The output environment that the code will be compiled for.
25
- target: "es2022",
26
24
  // View https://swc.rs/docs/configuration/minification for options.
27
25
  minify: {
28
26
  compress: true,
@@ -49,7 +47,8 @@ function defineBuildConfig(options) {
49
47
  ignoreDynamic: true
50
48
  },
51
49
  env: {
52
- targets: browsers
50
+ // jsc.target is not provided because the provided targets takes precedence.
51
+ targets
53
52
  }
54
53
  };
55
54
  const transformedConfig = applyTransformers(config, transformers, {
package/dist/build.mjs CHANGED
@@ -1,2 +1,2 @@
1
- export { defineBuildConfig } from './chunk-UFEY2RGG.mjs';
1
+ export { defineBuildConfig } from './chunk-4XVTQNRR.mjs';
2
2
  import './chunk-3UBBOKDF.mjs';
@@ -3,7 +3,7 @@ import { applyTransformers } from './chunk-3UBBOKDF.mjs';
3
3
  // src/build.ts
4
4
  function defineBuildConfig(options) {
5
5
  const {
6
- browsers,
6
+ targets,
7
7
  parser = "typescript",
8
8
  transformers = []
9
9
  } = options;
@@ -16,8 +16,6 @@ function defineBuildConfig(options) {
16
16
  syntax: "typescript",
17
17
  tsx: true
18
18
  },
19
- // The output environment that the code will be compiled for.
20
- target: "es2022",
21
19
  // View https://swc.rs/docs/configuration/minification for options.
22
20
  minify: {
23
21
  compress: true,
@@ -44,7 +42,8 @@ function defineBuildConfig(options) {
44
42
  ignoreDynamic: true
45
43
  },
46
44
  env: {
47
- targets: browsers
45
+ // jsc.target is not provided because the provided targets takes precedence.
46
+ targets
48
47
  }
49
48
  };
50
49
  const transformedConfig = applyTransformers(config, transformers, {
@@ -0,0 +1,73 @@
1
+ import browserslist from 'browserslist';
2
+
3
+ // src/browserslistToSwc.ts
4
+ var SwcSupportedBrowsers = [
5
+ "ie",
6
+ "edge",
7
+ "firefox",
8
+ "chrome",
9
+ "safari",
10
+ "opera",
11
+ "ios",
12
+ "android",
13
+ "op_mob",
14
+ "and_chr",
15
+ "and_ff",
16
+ "ie_mob",
17
+ "samsung"
18
+ ];
19
+ var SwcMobileBrowserAliases = {
20
+ "and_chr": "chrome",
21
+ "and_ff": "firefox",
22
+ "ie_mob": "ie",
23
+ "ios_saf": "ios",
24
+ "op_mob": "opera"
25
+ };
26
+ function parseBrowserslistEntry(entry) {
27
+ const values = entry.split(" ");
28
+ let browser = values[0];
29
+ let version = values[1];
30
+ if (SwcMobileBrowserAliases[browser]) {
31
+ browser = SwcMobileBrowserAliases[browser];
32
+ }
33
+ if (version.includes("-")) {
34
+ version = version.slice(0, version.indexOf("-"));
35
+ }
36
+ if (version.endsWith(".0")) {
37
+ version = version.slice(0, -2);
38
+ }
39
+ return {
40
+ browser,
41
+ version
42
+ };
43
+ }
44
+ function createSwcTargetsFromBrowserslistEntries(entries) {
45
+ return entries.reduce((acc, x) => {
46
+ const { browser, version } = parseBrowserslistEntry(x);
47
+ if (SwcSupportedBrowsers.indexOf(browser) === -1) {
48
+ return acc;
49
+ }
50
+ const _browser = browser;
51
+ if (acc[_browser]) {
52
+ if (parseFloat(acc[_browser]) > parseFloat(version)) {
53
+ acc[_browser] = version;
54
+ }
55
+ } else {
56
+ acc[_browser] = version;
57
+ }
58
+ return acc;
59
+ }, {});
60
+ }
61
+ function browserslistToSwc(options = {}) {
62
+ const {
63
+ queries,
64
+ ...browserlistsOptions
65
+ } = options;
66
+ const entries = browserslist(queries, {
67
+ path: "./browserslistrc",
68
+ ...browserlistsOptions
69
+ });
70
+ return createSwcTargetsFromBrowserslistEntries(entries);
71
+ }
72
+
73
+ export { browserslistToSwc, createSwcTargetsFromBrowserslistEntries };
@@ -17,7 +17,7 @@ function defineJestConfig(options = {}) {
17
17
  tsx: react
18
18
  },
19
19
  // The output environment that the code will be compiled for.
20
- target: "es2022",
20
+ target: "esnext",
21
21
  transform: react ? {
22
22
  react: {
23
23
  // Use "react/jsx-runtime".
@@ -3,7 +3,7 @@ import { applyTransformers } from './chunk-3UBBOKDF.mjs';
3
3
  // src/dev.ts
4
4
  function defineDevConfig(options) {
5
5
  const {
6
- browsers,
6
+ targets,
7
7
  fastRefresh = false,
8
8
  parser = "typescript",
9
9
  transformers = []
@@ -17,8 +17,6 @@ function defineDevConfig(options) {
17
17
  syntax: "typescript",
18
18
  tsx: true
19
19
  },
20
- // The output environment that the code will be compiled for.
21
- target: "es2022",
22
20
  transform: {
23
21
  react: {
24
22
  // Use "react/jsx-runtime".
@@ -43,7 +41,8 @@ function defineDevConfig(options) {
43
41
  ignoreDynamic: true
44
42
  },
45
43
  env: {
46
- targets: browsers
44
+ // jsc.target is not provided because the provided targets takes precedence.
45
+ targets
47
46
  }
48
47
  };
49
48
  const transformedConfig = applyTransformers(config, transformers, {
package/dist/dev.d.mts ADDED
@@ -0,0 +1,12 @@
1
+ import { Config } from '@swc/core';
2
+ import { SwcConfigTransformer } from './applyTransformers.mjs';
3
+
4
+ interface DefineDevConfigOptions {
5
+ targets: Record<string, string>;
6
+ fastRefresh?: boolean;
7
+ parser?: "ecmascript" | "typescript";
8
+ transformers?: SwcConfigTransformer[];
9
+ }
10
+ declare function defineDevConfig(options: DefineDevConfigOptions): Config;
11
+
12
+ export { DefineDevConfigOptions, defineDevConfig };
package/dist/dev.d.ts CHANGED
@@ -2,7 +2,7 @@ import { Config } from '@swc/core';
2
2
  import { SwcConfigTransformer } from './applyTransformers.js';
3
3
 
4
4
  interface DefineDevConfigOptions {
5
- browsers: any;
5
+ targets: Record<string, string>;
6
6
  fastRefresh?: boolean;
7
7
  parser?: "ecmascript" | "typescript";
8
8
  transformers?: SwcConfigTransformer[];
package/dist/dev.js CHANGED
@@ -8,7 +8,7 @@ function applyTransformers(config, transformers, context) {
8
8
  // src/dev.ts
9
9
  function defineDevConfig(options) {
10
10
  const {
11
- browsers,
11
+ targets,
12
12
  fastRefresh = false,
13
13
  parser = "typescript",
14
14
  transformers = []
@@ -22,8 +22,6 @@ function defineDevConfig(options) {
22
22
  syntax: "typescript",
23
23
  tsx: true
24
24
  },
25
- // The output environment that the code will be compiled for.
26
- target: "es2022",
27
25
  transform: {
28
26
  react: {
29
27
  // Use "react/jsx-runtime".
@@ -48,7 +46,8 @@ function defineDevConfig(options) {
48
46
  ignoreDynamic: true
49
47
  },
50
48
  env: {
51
- targets: browsers
49
+ // jsc.target is not provided because the provided targets takes precedence.
50
+ targets
52
51
  }
53
52
  };
54
53
  const transformedConfig = applyTransformers(config, transformers, {
package/dist/dev.mjs CHANGED
@@ -1,2 +1,2 @@
1
- export { defineDevConfig } from './chunk-LPGVRYUE.mjs';
1
+ export { defineDevConfig } from './chunk-VPNQWY2U.mjs';
2
2
  import './chunk-3UBBOKDF.mjs';
@@ -0,0 +1,7 @@
1
+ export { Config as SwcConfig } from '@swc/core';
2
+ export { SwcConfigTransformer, SwcConfigTransformerContext } from './applyTransformers.mjs';
3
+ export { BrowserslistToSwcOptions, browserslistToSwc, createSwcTargetsFromBrowserslistEntries } from './browserslistToSwc.mjs';
4
+ export { DefineBuildConfigOptions, defineBuildConfig } from './build.mjs';
5
+ export { DefineDevConfigOptions, defineDevConfig } from './dev.mjs';
6
+ export { DefineJestConfigOptions, defineJestConfig } from './jest.mjs';
7
+ import 'browserslist';
package/dist/index.d.ts CHANGED
@@ -1,5 +1,7 @@
1
+ export { Config as SwcConfig } from '@swc/core';
1
2
  export { SwcConfigTransformer, SwcConfigTransformerContext } from './applyTransformers.js';
3
+ export { BrowserslistToSwcOptions, browserslistToSwc, createSwcTargetsFromBrowserslistEntries } from './browserslistToSwc.js';
2
4
  export { DefineBuildConfigOptions, defineBuildConfig } from './build.js';
3
5
  export { DefineDevConfigOptions, defineDevConfig } from './dev.js';
4
6
  export { DefineJestConfigOptions, defineJestConfig } from './jest.js';
5
- import '@swc/core';
7
+ import 'browserslist';
package/dist/index.js CHANGED
@@ -1,5 +1,81 @@
1
1
  'use strict';
2
2
 
3
+ var browserslist = require('browserslist');
4
+
5
+ function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
6
+
7
+ var browserslist__default = /*#__PURE__*/_interopDefault(browserslist);
8
+
9
+ // src/browserslistToSwc.ts
10
+ var SwcSupportedBrowsers = [
11
+ "ie",
12
+ "edge",
13
+ "firefox",
14
+ "chrome",
15
+ "safari",
16
+ "opera",
17
+ "ios",
18
+ "android",
19
+ "op_mob",
20
+ "and_chr",
21
+ "and_ff",
22
+ "ie_mob",
23
+ "samsung"
24
+ ];
25
+ var SwcMobileBrowserAliases = {
26
+ "and_chr": "chrome",
27
+ "and_ff": "firefox",
28
+ "ie_mob": "ie",
29
+ "ios_saf": "ios",
30
+ "op_mob": "opera"
31
+ };
32
+ function parseBrowserslistEntry(entry) {
33
+ const values = entry.split(" ");
34
+ let browser = values[0];
35
+ let version = values[1];
36
+ if (SwcMobileBrowserAliases[browser]) {
37
+ browser = SwcMobileBrowserAliases[browser];
38
+ }
39
+ if (version.includes("-")) {
40
+ version = version.slice(0, version.indexOf("-"));
41
+ }
42
+ if (version.endsWith(".0")) {
43
+ version = version.slice(0, -2);
44
+ }
45
+ return {
46
+ browser,
47
+ version
48
+ };
49
+ }
50
+ function createSwcTargetsFromBrowserslistEntries(entries) {
51
+ return entries.reduce((acc, x) => {
52
+ const { browser, version } = parseBrowserslistEntry(x);
53
+ if (SwcSupportedBrowsers.indexOf(browser) === -1) {
54
+ return acc;
55
+ }
56
+ const _browser = browser;
57
+ if (acc[_browser]) {
58
+ if (parseFloat(acc[_browser]) > parseFloat(version)) {
59
+ acc[_browser] = version;
60
+ }
61
+ } else {
62
+ acc[_browser] = version;
63
+ }
64
+ return acc;
65
+ }, {});
66
+ }
67
+ function browserslistToSwc(options = {}) {
68
+ const {
69
+ queries,
70
+ ...browserlistsOptions
71
+ } = options;
72
+ const entries = browserslist__default.default(queries, {
73
+ path: "./browserslistrc",
74
+ ...browserlistsOptions
75
+ });
76
+ return createSwcTargetsFromBrowserslistEntries(entries);
77
+ }
78
+
3
79
  // src/applyTransformers.ts
4
80
  function applyTransformers(config, transformers, context) {
5
81
  return transformers.reduce((acc, transformer) => transformer(acc, context), config);
@@ -8,7 +84,7 @@ function applyTransformers(config, transformers, context) {
8
84
  // src/build.ts
9
85
  function defineBuildConfig(options) {
10
86
  const {
11
- browsers,
87
+ targets,
12
88
  parser = "typescript",
13
89
  transformers = []
14
90
  } = options;
@@ -21,8 +97,6 @@ function defineBuildConfig(options) {
21
97
  syntax: "typescript",
22
98
  tsx: true
23
99
  },
24
- // The output environment that the code will be compiled for.
25
- target: "es2022",
26
100
  // View https://swc.rs/docs/configuration/minification for options.
27
101
  minify: {
28
102
  compress: true,
@@ -49,7 +123,8 @@ function defineBuildConfig(options) {
49
123
  ignoreDynamic: true
50
124
  },
51
125
  env: {
52
- targets: browsers
126
+ // jsc.target is not provided because the provided targets takes precedence.
127
+ targets
53
128
  }
54
129
  };
55
130
  const transformedConfig = applyTransformers(config, transformers, {
@@ -61,7 +136,7 @@ function defineBuildConfig(options) {
61
136
  // src/dev.ts
62
137
  function defineDevConfig(options) {
63
138
  const {
64
- browsers,
139
+ targets,
65
140
  fastRefresh = false,
66
141
  parser = "typescript",
67
142
  transformers = []
@@ -75,8 +150,6 @@ function defineDevConfig(options) {
75
150
  syntax: "typescript",
76
151
  tsx: true
77
152
  },
78
- // The output environment that the code will be compiled for.
79
- target: "es2022",
80
153
  transform: {
81
154
  react: {
82
155
  // Use "react/jsx-runtime".
@@ -101,7 +174,8 @@ function defineDevConfig(options) {
101
174
  ignoreDynamic: true
102
175
  },
103
176
  env: {
104
- targets: browsers
177
+ // jsc.target is not provided because the provided targets takes precedence.
178
+ targets
105
179
  }
106
180
  };
107
181
  const transformedConfig = applyTransformers(config, transformers, {
@@ -127,7 +201,7 @@ function defineJestConfig(options = {}) {
127
201
  tsx: react
128
202
  },
129
203
  // The output environment that the code will be compiled for.
130
- target: "es2022",
204
+ target: "esnext",
131
205
  transform: react ? {
132
206
  react: {
133
207
  // Use "react/jsx-runtime".
@@ -152,6 +226,8 @@ function defineJestConfig(options = {}) {
152
226
  return transformedConfig;
153
227
  }
154
228
 
229
+ exports.browserslistToSwc = browserslistToSwc;
230
+ exports.createSwcTargetsFromBrowserslistEntries = createSwcTargetsFromBrowserslistEntries;
155
231
  exports.defineBuildConfig = defineBuildConfig;
156
232
  exports.defineDevConfig = defineDevConfig;
157
233
  exports.defineJestConfig = defineJestConfig;
package/dist/index.mjs CHANGED
@@ -1,4 +1,5 @@
1
- export { defineBuildConfig } from './chunk-UFEY2RGG.mjs';
2
- export { defineDevConfig } from './chunk-LPGVRYUE.mjs';
3
- export { defineJestConfig } from './chunk-6PUKUTDM.mjs';
1
+ export { browserslistToSwc, createSwcTargetsFromBrowserslistEntries } from './chunk-MGMFB3UY.mjs';
2
+ export { defineBuildConfig } from './chunk-4XVTQNRR.mjs';
3
+ export { defineDevConfig } from './chunk-VPNQWY2U.mjs';
4
+ export { defineJestConfig } from './chunk-MYINGMHM.mjs';
4
5
  import './chunk-3UBBOKDF.mjs';
@@ -0,0 +1,11 @@
1
+ import { Config } from '@swc/core';
2
+ import { SwcConfigTransformer } from './applyTransformers.mjs';
3
+
4
+ interface DefineJestConfigOptions {
5
+ react?: boolean;
6
+ parser?: "ecmascript" | "typescript";
7
+ transformers?: SwcConfigTransformer[];
8
+ }
9
+ declare function defineJestConfig(options?: DefineJestConfigOptions): Config;
10
+
11
+ export { DefineJestConfigOptions, defineJestConfig };
package/dist/jest.js CHANGED
@@ -22,7 +22,7 @@ function defineJestConfig(options = {}) {
22
22
  tsx: react
23
23
  },
24
24
  // The output environment that the code will be compiled for.
25
- target: "es2022",
25
+ target: "esnext",
26
26
  transform: react ? {
27
27
  react: {
28
28
  // Use "react/jsx-runtime".
package/dist/jest.mjs CHANGED
@@ -1,2 +1,2 @@
1
- export { defineJestConfig } from './chunk-6PUKUTDM.mjs';
1
+ export { defineJestConfig } from './chunk-MYINGMHM.mjs';
2
2
  import './chunk-3UBBOKDF.mjs';
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": "1.0.1",
4
+ "version": "2.1.0",
5
5
  "license": "Apache-2.0",
6
6
  "keywords": [
7
7
  "workleap",
@@ -10,12 +10,17 @@
10
10
  ],
11
11
  "exports": {
12
12
  ".": {
13
- "require": "./dist/index.js",
14
- "import": "./dist/index.mjs",
15
- "types": "./dist/index.d.ts",
16
- "default": "./dist/index.mjs"
13
+ "require": {
14
+ "default": "./dist/index.js",
15
+ "types": "./dist/index.d.ts"
16
+ },
17
+ "import": {
18
+ "default": "./dist/index.mjs",
19
+ "types": "./dist/index.d.mts"
20
+ }
17
21
  }
18
22
  },
23
+ "types": "./dist/index.d.ts",
19
24
  "files": [
20
25
  "dist",
21
26
  "CHANGELOG.md",
@@ -27,26 +32,31 @@
27
32
  "directory": "packages/swc-configs"
28
33
  },
29
34
  "devDependencies": {
30
- "@swc/core": "1.3.63",
35
+ "@swc/core": "1.3.80",
31
36
  "@swc/helpers": "0.5.1",
32
- "@swc/jest": "0.2.26",
33
- "@types/jest": "29.5.2",
34
- "jest": "29.5.0",
37
+ "@swc/jest": "0.2.29",
38
+ "@types/jest": "29.5.4",
39
+ "browserslist": "4.21.10",
40
+ "jest": "29.6.4",
35
41
  "ts-node": "10.9.1",
36
- "tsup": "6.7.0",
37
- "typescript": "5.0.4",
38
- "@workleap/eslint-plugin": "1.8.3",
39
- "@workleap/tsup-configs": "2.0.1",
40
- "@workleap/typescript-configs": "2.3.3"
42
+ "tsup": "7.2.0",
43
+ "typescript": "5.2.2",
44
+ "@workleap/eslint-plugin": "2.1.0",
45
+ "@workleap/tsup-configs": "3.0.0",
46
+ "@workleap/typescript-configs": "3.0.1"
41
47
  },
42
48
  "peerDependencies": {
43
49
  "@swc/core": "*",
44
50
  "@swc/helpers": "*",
45
- "@swc/jest": "*"
51
+ "@swc/jest": "*",
52
+ "browserslist": "*"
46
53
  },
47
54
  "peerDependenciesMeta": {
48
55
  "@swc/jest": {
49
56
  "optional": true
57
+ },
58
+ "browserslist": {
59
+ "optional": true
50
60
  }
51
61
  },
52
62
  "publishConfig": {