@modern-js/module-tools-docs 2.23.1 → 2.24.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/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,19 @@
|
|
|
1
1
|
# @modern-js/module-tools-docs
|
|
2
2
|
|
|
3
|
+
## 2.24.1
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- @modern-js/doc-plugin-auto-sidebar@2.24.1
|
|
8
|
+
- @modern-js/doc-tools@2.24.1
|
|
9
|
+
|
|
10
|
+
## 2.24.0
|
|
11
|
+
|
|
12
|
+
### Patch Changes
|
|
13
|
+
|
|
14
|
+
- @modern-js/doc-plugin-auto-sidebar@2.24.0
|
|
15
|
+
- @modern-js/doc-tools@2.24.0
|
|
16
|
+
|
|
3
17
|
## 2.23.1
|
|
4
18
|
|
|
5
19
|
### Patch Changes
|
|
@@ -55,21 +55,39 @@ export default {
|
|
|
55
55
|
|
|
56
56
|
## asset
|
|
57
57
|
|
|
58
|
-
|
|
58
|
+
Contains configuration related to static assets.
|
|
59
|
+
|
|
60
|
+
## asset.path
|
|
59
61
|
|
|
60
62
|
Static resource output path, will be based on [outDir](/api/config/build-config#outDir)
|
|
61
63
|
|
|
62
64
|
- **Type**: `string`
|
|
63
65
|
- **Default**: `assets`
|
|
64
66
|
|
|
65
|
-
|
|
67
|
+
## asset.limit
|
|
68
|
+
|
|
69
|
+
Used to set the threshold for static assets to be automatically inlined as base64.
|
|
70
|
+
|
|
71
|
+
By default, Module Tools will inline assets such as images, fonts and media smaller than 10KB during bundling. They are Base64 encoded and inlined in the bundles, eliminating the need for separate HTTP requests.
|
|
66
72
|
|
|
67
|
-
|
|
73
|
+
You can adjust this threshold by modifying the `limit` config.
|
|
68
74
|
|
|
69
75
|
- **Type**: `number`
|
|
70
76
|
- **Default**: `10 * 1024`
|
|
71
77
|
|
|
72
|
-
|
|
78
|
+
For example, set `limit` to `0` to avoid assets inlining:
|
|
79
|
+
|
|
80
|
+
```js modern.config.ts
|
|
81
|
+
export default defineConfig({
|
|
82
|
+
buildConfig: {
|
|
83
|
+
asset: {
|
|
84
|
+
limit: 0,
|
|
85
|
+
},
|
|
86
|
+
},
|
|
87
|
+
});
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
## asset.publicPath
|
|
73
91
|
|
|
74
92
|
The CDN prefix given to unlinked resources when packaging
|
|
75
93
|
|
|
@@ -88,7 +106,7 @@ export default {
|
|
|
88
106
|
|
|
89
107
|
At this point, all static resources will be prefixed with `https://xxx/`
|
|
90
108
|
|
|
91
|
-
|
|
109
|
+
## asset.svgr
|
|
92
110
|
|
|
93
111
|
Packaged to handle svg as a React component, options reference [svgr](https://react-svgr.com/docs/options/), plus support for two configuration items `include` and `exclude` to match the svg file to be handled
|
|
94
112
|
|
|
@@ -124,14 +142,14 @@ declare module '*.svg' {
|
|
|
124
142
|
/// <reference types='@modern-js/module-tools/types' />
|
|
125
143
|
```
|
|
126
144
|
|
|
127
|
-
|
|
145
|
+
## asset.svgr.include
|
|
128
146
|
|
|
129
147
|
Set the matching svg file
|
|
130
148
|
|
|
131
149
|
- **Type**: `string | RegExp | (string | RegExp)[]`
|
|
132
150
|
- **Default**: `/\.svg$/`
|
|
133
151
|
|
|
134
|
-
|
|
152
|
+
## asset.svgr.exclude
|
|
135
153
|
|
|
136
154
|
Set unmatched svg files
|
|
137
155
|
|
|
@@ -147,7 +165,7 @@ Automatically externalize project dependencies and peerDependencies and not pack
|
|
|
147
165
|
|
|
148
166
|
When we want to turn off the default handling behavior for third-party dependencies, we can do so by:
|
|
149
167
|
|
|
150
|
-
```ts
|
|
168
|
+
```js modern.config.ts
|
|
151
169
|
export default defineConfig({
|
|
152
170
|
buildConfig: {
|
|
153
171
|
autoExternal: false,
|
|
@@ -158,7 +176,7 @@ export default defineConfig({
|
|
|
158
176
|
This way the dependencies under `"dependencies"` and `"peerDependencies"` will be packaged. If you want to turn off the processing of only one of these dependencies, you can use the
|
|
159
177
|
`buildConfig.autoExternal` in the form of an object.
|
|
160
178
|
|
|
161
|
-
```ts
|
|
179
|
+
```js modern.config.ts
|
|
162
180
|
export default defineConfig({
|
|
163
181
|
buildConfig: {
|
|
164
182
|
autoExternal: {
|
|
@@ -169,14 +187,14 @@ export default defineConfig({
|
|
|
169
187
|
});
|
|
170
188
|
```
|
|
171
189
|
|
|
172
|
-
|
|
190
|
+
## autoExternal.dependencies
|
|
173
191
|
|
|
174
192
|
Whether or not the dep dependencies of the external project are needed
|
|
175
193
|
|
|
176
194
|
- **Type**: `boolean`
|
|
177
195
|
- **Default**: `true`
|
|
178
196
|
|
|
179
|
-
|
|
197
|
+
## autoExternal.peerDependencies
|
|
180
198
|
|
|
181
199
|
Whether to require peerDep dependencies for external projects
|
|
182
200
|
|
|
@@ -207,6 +225,36 @@ export default {
|
|
|
207
225
|
|
|
208
226
|
Reference for array settings: [copy-webpack-plugin patterns](https://github.com/webpack-contrib/copy-webpack-plugin#patterns)
|
|
209
227
|
|
|
228
|
+
## copy.patterns
|
|
229
|
+
|
|
230
|
+
- **Type**: `CopyPattern[]`
|
|
231
|
+
- **Default**: `[]`
|
|
232
|
+
|
|
233
|
+
```ts
|
|
234
|
+
interface CopyPattern {
|
|
235
|
+
from: string;
|
|
236
|
+
to?: string;
|
|
237
|
+
context?: string;
|
|
238
|
+
globOptions?: globby.GlobbyOptions;
|
|
239
|
+
}
|
|
240
|
+
```
|
|
241
|
+
|
|
242
|
+
## copy.options
|
|
243
|
+
|
|
244
|
+
- **Type**:
|
|
245
|
+
|
|
246
|
+
```ts
|
|
247
|
+
type Options = {
|
|
248
|
+
concurrency?: number;
|
|
249
|
+
enableCopySync?: boolean;
|
|
250
|
+
};
|
|
251
|
+
```
|
|
252
|
+
|
|
253
|
+
- **Default**: `{ concurrency: 100, enableCopySync: false }`
|
|
254
|
+
|
|
255
|
+
- `concurrency`: Specifies how many copy tasks to execute in parallel.
|
|
256
|
+
- `enableCopySync`: Uses [`fs.copySync`](https://github.com/jprichardson/node-fs-extra/blob/master/lib/copy/copy-sync.js) by default, instead of [`fs.copy`](https://github.com/jprichardson/node-fs-extra/blob/master/lib/copy/copy.js).
|
|
257
|
+
|
|
210
258
|
## define
|
|
211
259
|
|
|
212
260
|
Define global variables that will be injected into the code
|
|
@@ -259,32 +307,44 @@ The dts file generates the relevant configuration, by default it generates.
|
|
|
259
307
|
}
|
|
260
308
|
```
|
|
261
309
|
|
|
262
|
-
|
|
310
|
+
## dts.abortOnError
|
|
263
311
|
|
|
264
|
-
Whether to allow the build to succeed
|
|
265
|
-
|
|
266
|
-
:::warning
|
|
267
|
-
When this configuration is turned on, there is no guarantee that the type files will be generated properly and accurately. In `buildType: 'bundle'` or Bundle build mode, the type file must not be generated.
|
|
268
|
-
:::
|
|
312
|
+
Whether to allow the build to succeed if a type error occurs.
|
|
269
313
|
|
|
270
314
|
- **Type**: `boolean`
|
|
271
315
|
- **Default**: `true`
|
|
272
316
|
|
|
273
|
-
|
|
317
|
+
**By default, type errors will cause the build to fail**. When `abortOnError` is set to `false`, the build will still succeed even if there are type issues in the code:
|
|
318
|
+
|
|
319
|
+
```js modern.config.ts
|
|
320
|
+
export default defineConfig({
|
|
321
|
+
buildConfig: {
|
|
322
|
+
dts: {
|
|
323
|
+
abortOnError: false,
|
|
324
|
+
},
|
|
325
|
+
},
|
|
326
|
+
});
|
|
327
|
+
```
|
|
328
|
+
|
|
329
|
+
:::warning
|
|
330
|
+
When this configuration is disabled, there is no guarantee that the type files will be generated correctly. In `buildType: 'bundle'`, which is the bundle mode, type files will not be generated.
|
|
331
|
+
:::
|
|
332
|
+
|
|
333
|
+
## dts.distPath
|
|
274
334
|
|
|
275
335
|
The output path of the dts file, based on [outDir](/api/config/build-config#outDir)
|
|
276
336
|
|
|
277
337
|
- **Type**: `string`
|
|
278
338
|
- **Default**: `. /types`
|
|
279
339
|
|
|
280
|
-
|
|
340
|
+
## dts.only
|
|
281
341
|
|
|
282
342
|
Generate only dts files, not js files
|
|
283
343
|
|
|
284
344
|
- **Type**: `boolean`
|
|
285
345
|
- **Default**: `false`
|
|
286
346
|
|
|
287
|
-
|
|
347
|
+
## dts.respectExternal
|
|
288
348
|
|
|
289
349
|
When set to `false`, the type of third-party packages will be excluded from the bundle, when set to `true`, it will determine whether third-party types need to be bundled based on [externals](#externals).
|
|
290
350
|
|
|
@@ -292,9 +352,9 @@ When bundle d.ts, export is not analyzed, so any third-party package type you us
|
|
|
292
352
|
So we can avoid it with this configuration.
|
|
293
353
|
|
|
294
354
|
- **Type**: `boolean`
|
|
295
|
-
- Default
|
|
355
|
+
- **Default**: `true`
|
|
296
356
|
|
|
297
|
-
|
|
357
|
+
## dts.tsconfigPath
|
|
298
358
|
|
|
299
359
|
Path to the tsconfig file
|
|
300
360
|
|
|
@@ -311,8 +371,6 @@ Directly modify [esbuild configuration](https://esbuild.github.io/api/)
|
|
|
311
371
|
For example, if we need to modify the file extension of the generated files:
|
|
312
372
|
|
|
313
373
|
```ts modern.config.ts
|
|
314
|
-
import { defineConfig } from '@modern-js/module-tools';
|
|
315
|
-
|
|
316
374
|
export default defineConfig({
|
|
317
375
|
buildConfig: {
|
|
318
376
|
esbuildOptions: options => {
|
|
@@ -345,35 +403,35 @@ Below is a comparison of the product changes before and after using this configu
|
|
|
345
403
|
|
|
346
404
|
Before enable:
|
|
347
405
|
|
|
348
|
-
```
|
|
406
|
+
```js ./dist/index.js
|
|
349
407
|
// helper function
|
|
350
408
|
function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) {
|
|
351
409
|
// ...
|
|
352
410
|
}
|
|
353
411
|
// helper function
|
|
354
412
|
function _async_to_generator(fn) {
|
|
355
|
-
return function() {
|
|
413
|
+
return function () {
|
|
356
414
|
// use asyncGeneratorStep
|
|
357
415
|
// ...
|
|
358
416
|
};
|
|
359
417
|
}
|
|
360
418
|
|
|
361
419
|
// your code
|
|
362
|
-
export var yourCode = function() {
|
|
420
|
+
export var yourCode = function () {
|
|
363
421
|
// use _async_to_generator
|
|
364
|
-
}
|
|
422
|
+
};
|
|
365
423
|
```
|
|
366
424
|
|
|
367
425
|
After enabled:
|
|
368
426
|
|
|
369
|
-
```
|
|
427
|
+
```js ./dist/index.js
|
|
370
428
|
// helper functions imported from @swc/helpers
|
|
371
|
-
import { _ as _async_to_generator } from
|
|
429
|
+
import { _ as _async_to_generator } from '@swc/helpers/_/_async_to_generator';
|
|
372
430
|
|
|
373
431
|
// your code
|
|
374
|
-
export var yourCode = function() {
|
|
432
|
+
export var yourCode = function () {
|
|
375
433
|
// use _async_to_generator
|
|
376
|
-
}
|
|
434
|
+
};
|
|
377
435
|
```
|
|
378
436
|
|
|
379
437
|
## externals
|
|
@@ -461,7 +519,7 @@ In `buildType: 'bundleless'` build mode, the reference path is redirected to ens
|
|
|
461
519
|
|
|
462
520
|
In some scenarios, you may not need these functions, then you can turn it off with this configuration, and its reference path will not be changed after turning it off.
|
|
463
521
|
|
|
464
|
-
```ts
|
|
522
|
+
```js modern.config.ts
|
|
465
523
|
export default {
|
|
466
524
|
buildConfig: {
|
|
467
525
|
redirect: {
|
|
@@ -494,7 +552,7 @@ But the package.json of this third-party package does not have the style file co
|
|
|
494
552
|
}
|
|
495
553
|
```
|
|
496
554
|
|
|
497
|
-
At the same time you set [style.
|
|
555
|
+
At the same time you set [style.inject](#styleinject) to `true` and you will see a warning message like this in the console
|
|
498
556
|
|
|
499
557
|
```bash
|
|
500
558
|
[LIBUILD:ESBUILD_WARN] Ignoring this import because "other-package/dist/index.css" was marked as having no side effects
|
|
@@ -503,7 +561,6 @@ At the same time you set [style.object](#inject) to `true` and you will see a wa
|
|
|
503
561
|
At this point, you can use this configuration item to manually configure the module's `"sideEffects"` to support regular and functional forms.
|
|
504
562
|
|
|
505
563
|
```js modern.config.ts
|
|
506
|
-
import { defineConfig } from '@modern-js/module-tools';
|
|
507
564
|
export default defineConfig({
|
|
508
565
|
buildConfig: {
|
|
509
566
|
sideEffects: [/\.css$/],
|
|
@@ -549,18 +606,18 @@ Whether to enable code splitting
|
|
|
549
606
|
|
|
550
607
|
Configure style-related configuration
|
|
551
608
|
|
|
552
|
-
|
|
609
|
+
## style.less
|
|
553
610
|
|
|
554
611
|
less-related configuration
|
|
555
612
|
|
|
556
|
-
|
|
613
|
+
## style.less.lessOptions
|
|
557
614
|
|
|
558
615
|
Refer to [less](https://less.bootcss.com/usage/#less-options) for detailed configuration
|
|
559
616
|
|
|
560
617
|
- **Type**: `Object`
|
|
561
618
|
- **Default**: `{ javascriptEnabled: true }`
|
|
562
619
|
|
|
563
|
-
|
|
620
|
+
## style.less.additionalData
|
|
564
621
|
|
|
565
622
|
Add `Less` code to the beginning of the entry file.
|
|
566
623
|
|
|
@@ -579,7 +636,7 @@ export default {
|
|
|
579
636
|
};
|
|
580
637
|
```
|
|
581
638
|
|
|
582
|
-
|
|
639
|
+
## style.less.implementation
|
|
583
640
|
|
|
584
641
|
Configure the implementation library used by `Less`, if not specified, the built-in version used is `4.1.3`
|
|
585
642
|
|
|
@@ -614,18 +671,18 @@ export default {
|
|
|
614
671
|
};
|
|
615
672
|
```
|
|
616
673
|
|
|
617
|
-
|
|
674
|
+
## style.sass
|
|
618
675
|
|
|
619
|
-
sass-related configuration
|
|
676
|
+
sass-related configuration.
|
|
620
677
|
|
|
621
|
-
|
|
678
|
+
## style.sass.sassOptions
|
|
622
679
|
|
|
623
680
|
Refer to [node-sass](https://github.com/sass/node-sass#options) for detailed configuration
|
|
624
681
|
|
|
625
682
|
- **Type**: `Object`
|
|
626
683
|
- **Default**: `{}`
|
|
627
684
|
|
|
628
|
-
|
|
685
|
+
## style.sass.additionalData
|
|
629
686
|
|
|
630
687
|
Add `Sass` code to the beginning of the entry file.
|
|
631
688
|
|
|
@@ -645,7 +702,7 @@ export default {
|
|
|
645
702
|
};
|
|
646
703
|
```
|
|
647
704
|
|
|
648
|
-
|
|
705
|
+
## style.sass.implementation
|
|
649
706
|
|
|
650
707
|
Configure the implementation library used by `Sass`, the built-in version used is `1.5.4` if not specified
|
|
651
708
|
|
|
@@ -680,24 +737,36 @@ export default {
|
|
|
680
737
|
};
|
|
681
738
|
```
|
|
682
739
|
|
|
683
|
-
|
|
740
|
+
## style.postcss
|
|
684
741
|
|
|
685
742
|
- plugins
|
|
686
743
|
- processOptions
|
|
687
744
|
|
|
688
745
|
See [PostCSS](https://github.com/postcss/postcss#options) for detailed configuration
|
|
689
746
|
|
|
690
|
-
|
|
747
|
+
## style.inject
|
|
691
748
|
|
|
692
|
-
Configure whether to insert
|
|
749
|
+
Configure whether to insert CSS styles into JavaScript code in bundle mode.
|
|
693
750
|
|
|
694
751
|
- **Type**: `boolean`
|
|
695
752
|
- **Default**: `false`
|
|
696
753
|
|
|
697
|
-
|
|
754
|
+
Set `inject` to `true` to enable this feature:
|
|
755
|
+
|
|
756
|
+
```js modern.config.ts
|
|
757
|
+
export default defineConfig({
|
|
758
|
+
buildConfig: {
|
|
759
|
+
inject: true,
|
|
760
|
+
},
|
|
761
|
+
});
|
|
762
|
+
```
|
|
763
|
+
|
|
764
|
+
Once enabled, you will see the CSS code referenced in the source code included in the bundled JavaScript output.
|
|
765
|
+
|
|
766
|
+
For example, if you write `import './index.scss'` in the source code, you will see the following code in the output:
|
|
698
767
|
|
|
699
768
|
```js dist/index.js
|
|
700
|
-
// node_modules
|
|
769
|
+
// node_modules/style-inject/dist/style-inject.es.js
|
|
701
770
|
function styleInject(css, ref) {
|
|
702
771
|
// ...
|
|
703
772
|
}
|
|
@@ -710,20 +779,18 @@ style_inject_es_default(css_248z);
|
|
|
710
779
|
|
|
711
780
|
:::tip
|
|
712
781
|
|
|
713
|
-
|
|
782
|
+
After enabling `inject`, you need to pay attention to the following points:
|
|
714
783
|
|
|
715
|
-
- `@import` in
|
|
716
|
-
-
|
|
784
|
+
- `@import` in CSS files will not be processed. If your CSS file contains `@import`, you need to manually import the CSS file in the JS file (less and scss files are not required because they have preprocessing).
|
|
785
|
+
- Consider the impact of `sideEffects`. By default, our builder assumes that CSS has side effects. If the `sideEffects` field is set in your project or third-party package's package.json and does not include this CSS file, you will receive a warning:
|
|
717
786
|
|
|
718
|
-
```
|
|
787
|
+
```shell
|
|
719
788
|
[LIBUILD:ESBUILD_WARN] Ignoring this import because "src/index.scss" was marked as having no side effects by plugin "libuild:adapter"
|
|
720
789
|
```
|
|
721
790
|
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
:::
|
|
791
|
+
You can resolve this by configuring [sideEffects](#sideeffects).
|
|
725
792
|
|
|
726
|
-
|
|
793
|
+
## style.autoModules
|
|
727
794
|
|
|
728
795
|
Enable CSS Modules automatically based on the filename.
|
|
729
796
|
|
|
@@ -736,7 +803,7 @@ Enable CSS Modules automatically based on the filename.
|
|
|
736
803
|
|
|
737
804
|
`RegExp` : Enables CSS Modules for all files that match the regular condition.
|
|
738
805
|
|
|
739
|
-
|
|
806
|
+
## style.modules
|
|
740
807
|
|
|
741
808
|
CSS Modules configuration
|
|
742
809
|
|
|
@@ -769,7 +836,7 @@ You can use `styles.boxTitle` to access
|
|
|
769
836
|
|
|
770
837
|
For detailed configuration see [postcss-modules](https://github.com/madyankin/postcss-modules#usage)
|
|
771
838
|
|
|
772
|
-
|
|
839
|
+
## style.tailwindcss
|
|
773
840
|
|
|
774
841
|
tailwindcss related configuration
|
|
775
842
|
|
|
@@ -804,25 +871,49 @@ The rest of the usage is the same as Tailwind CSS: [Quick Portal](https://tailwi
|
|
|
804
871
|
|
|
805
872
|
## target
|
|
806
873
|
|
|
807
|
-
|
|
874
|
+
`target` is used to set the target environment for the generated JavaScript code. It enables Module Tools to transform JavaScript syntax that is not recognized by the target environment into older versions of JavaScript syntax that are compatible with these environments.
|
|
875
|
+
|
|
876
|
+
- **Type**:
|
|
877
|
+
|
|
878
|
+
```ts
|
|
879
|
+
type Target =
|
|
880
|
+
| 'es5'
|
|
881
|
+
| 'es6'
|
|
882
|
+
| 'es2015'
|
|
883
|
+
| 'es2016'
|
|
884
|
+
| 'es2017'
|
|
885
|
+
| 'es2018'
|
|
886
|
+
| 'es2019'
|
|
887
|
+
| 'es2020'
|
|
888
|
+
| 'es2021'
|
|
889
|
+
| 'es2022'
|
|
890
|
+
| 'esnext';
|
|
891
|
+
```
|
|
808
892
|
|
|
809
|
-
- **Type**: `'es5' | 'es6' | 'es2015' | 'es2016' | 'es2017' | 'es2018' | 'es2019' | 'es2020' | 'es2021' | 'es2022' | 'esnext'`
|
|
810
893
|
- **Default**: `'es6'`
|
|
811
894
|
|
|
812
|
-
|
|
895
|
+
For example, compile the code to `es5` syntax:
|
|
896
|
+
|
|
897
|
+
```js modern.config.ts
|
|
898
|
+
export default defineConfig({
|
|
899
|
+
buildConfig: {
|
|
900
|
+
target: 'es5',
|
|
901
|
+
},
|
|
902
|
+
});
|
|
903
|
+
```
|
|
904
|
+
|
|
905
|
+
## transformImport
|
|
813
906
|
|
|
814
907
|
Using [SWC](https://swc.rs/) provides the same ability and configuration as [`babel-plugin-import`](https://github.com/umijs/babel-plugin-import).
|
|
815
908
|
|
|
816
|
-
|
|
817
|
-
|
|
909
|
+
- **Type**: `Array`
|
|
910
|
+
- **Default**: `[]`
|
|
818
911
|
|
|
819
912
|
The elements of the array are configuration objects for `babel-plugin-import`, which can be referred to [options](https://github.com/umijs/babel-plugin-import#options)。
|
|
820
913
|
|
|
821
914
|
**Example:**
|
|
822
915
|
|
|
823
|
-
```ts
|
|
824
|
-
import moduleTools, { defineConfig} from '@modern-js/module-tools';
|
|
825
|
-
|
|
916
|
+
```ts modern.config.ts
|
|
826
917
|
export default defineConfig({
|
|
827
918
|
buildConfig: {
|
|
828
919
|
transformImport: [
|
|
@@ -833,15 +924,12 @@ export default defineConfig({
|
|
|
833
924
|
},
|
|
834
925
|
],
|
|
835
926
|
},
|
|
836
|
-
plugins: [
|
|
837
|
-
moduleTools(),
|
|
838
|
-
],
|
|
839
927
|
});
|
|
840
928
|
```
|
|
841
929
|
|
|
842
930
|
### Notes
|
|
843
931
|
|
|
844
|
-
Reference the [Import Plugin - Notes](plugins/official-list/plugin-import.html#Notes)
|
|
932
|
+
Reference the [Import Plugin - Notes](plugins/official-list/plugin-import.html#Notes)
|
|
845
933
|
|
|
846
934
|
## umdGlobals
|
|
847
935
|
|
|
@@ -617,9 +617,9 @@ export default () => {
|
|
|
617
617
|
|
|
618
618
|
In Bundle mode, third-party dependencies are packaged in.
|
|
619
619
|
|
|
620
|
-
For styles, a separate product file is generated, and there is no code related to importing styles in the
|
|
620
|
+
For styles, a separate product file is generated, and there is no code related to importing styles in the JS output files.
|
|
621
621
|
|
|
622
|
-
If you need to inject styles into
|
|
622
|
+
If you need to inject styles into JS products, you can enable the [`style.inject`](/en/api/config/build-config#styleinject) option.
|
|
623
623
|
|
|
624
624
|
<CH.Code>
|
|
625
625
|
```css ./dist/es/index.css
|
|
@@ -728,8 +728,8 @@ Module projects support the development of styles using CSS Modules. By default,
|
|
|
728
728
|
|
|
729
729
|
If you need to configure CSS Modules, you can check out the API at
|
|
730
730
|
|
|
731
|
-
- [autoModules](en/api/config/build-config#
|
|
732
|
-
- [modules](/en/api/config/build-config#
|
|
731
|
+
- [style.autoModules](en/api/config/build-config#styleautomodules)
|
|
732
|
+
- [style.modules](/en/api/config/build-config#stylemodules)
|
|
733
733
|
|
|
734
734
|
The following is a code example.
|
|
735
735
|
|
|
@@ -16,8 +16,6 @@ sidebar_position: 1
|
|
|
16
16
|
:::
|
|
17
17
|
|
|
18
18
|
```js modern.config.ts
|
|
19
|
-
import { defineConfig } from '@modern-js/module-tools';
|
|
20
|
-
|
|
21
19
|
export default defineConfig({
|
|
22
20
|
buildConfig: {
|
|
23
21
|
alias: {
|
|
@@ -32,8 +30,6 @@ export default defineConfig({
|
|
|
32
30
|
`alias` 的值定义为函数时,可以接受预设的 alias 对象,并对其进行修改。
|
|
33
31
|
|
|
34
32
|
```js modern.config.ts
|
|
35
|
-
import { defineConfig } from '@modern-js/module-tools';
|
|
36
|
-
|
|
37
33
|
export default defineConfig({
|
|
38
34
|
buildConfig: {
|
|
39
35
|
alias: alias => {
|
|
@@ -46,8 +42,6 @@ export default defineConfig({
|
|
|
46
42
|
也可以在函数中返回一个新对象作为最终结果,新对象会覆盖预设的 alias 对象。
|
|
47
43
|
|
|
48
44
|
```js modern.config.ts
|
|
49
|
-
import { defineConfig } from '@modern-js/module-tools';
|
|
50
|
-
|
|
51
45
|
export default defineConfig({
|
|
52
46
|
buildConfig: {
|
|
53
47
|
alias: alias => {
|
|
@@ -61,21 +55,39 @@ export default defineConfig({
|
|
|
61
55
|
|
|
62
56
|
## asset
|
|
63
57
|
|
|
64
|
-
|
|
58
|
+
包含静态资源相关的配置。
|
|
59
|
+
|
|
60
|
+
## asset.path
|
|
65
61
|
|
|
66
62
|
静态资源输出路径,会基于 [outDir](/api/config/build-config#outDir) 进行输出。
|
|
67
63
|
|
|
68
64
|
- 类型: `string`
|
|
69
65
|
- 默认值: `assets`
|
|
70
66
|
|
|
71
|
-
|
|
67
|
+
## asset.limit
|
|
68
|
+
|
|
69
|
+
用于设置静态资源被自动内联为 base64 的体积阈值。
|
|
70
|
+
|
|
71
|
+
Module Tools 在进行打包时,默认会内联体积小于 10KB 的图片、字体、媒体等资源,将它们通过 Base64 编码,并内联到产物中,不再会发送独立的 HTTP 请求。
|
|
72
72
|
|
|
73
|
-
|
|
73
|
+
你可以通过修改 `limit` 参数来调整这个阈值。
|
|
74
74
|
|
|
75
75
|
- 类型: `number`
|
|
76
76
|
- 默认值: `10 * 1024`
|
|
77
77
|
|
|
78
|
-
|
|
78
|
+
例如,将 `limit` 设置为 `0` 来避免资源内联:
|
|
79
|
+
|
|
80
|
+
```js modern.config.ts
|
|
81
|
+
export default defineConfig({
|
|
82
|
+
buildConfig: {
|
|
83
|
+
asset: {
|
|
84
|
+
limit: 0,
|
|
85
|
+
},
|
|
86
|
+
},
|
|
87
|
+
});
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
## asset.publicPath
|
|
79
91
|
|
|
80
92
|
打包时给未内联资源的 CDN 前缀。
|
|
81
93
|
|
|
@@ -83,8 +95,6 @@ export default defineConfig({
|
|
|
83
95
|
- 默认值: `undefined`
|
|
84
96
|
|
|
85
97
|
```js modern.config.ts
|
|
86
|
-
import { defineConfig } from '@modern-js/module-tools';
|
|
87
|
-
|
|
88
98
|
export default defineConfig({
|
|
89
99
|
buildConfig: {
|
|
90
100
|
asset: {
|
|
@@ -96,7 +106,7 @@ export default defineConfig({
|
|
|
96
106
|
|
|
97
107
|
此时,所有静态资源都会添加 `https://xxx/` 前缀。
|
|
98
108
|
|
|
99
|
-
|
|
109
|
+
## asset.svgr
|
|
100
110
|
|
|
101
111
|
打包时将 SVG 作为一个 React 组件处理,options 参考 [svgr](https://react-svgr.com/docs/options/),另外还支持了 `include` 和 `exclude` 两个配置项,用于匹配需要处理的 SVG 文件。
|
|
102
112
|
|
|
@@ -133,14 +143,14 @@ declare module '*.svg' {
|
|
|
133
143
|
/// <reference types='@modern-js/module-tools/types' />
|
|
134
144
|
```
|
|
135
145
|
|
|
136
|
-
|
|
146
|
+
## asset.svgr.include
|
|
137
147
|
|
|
138
148
|
设定匹配的 SVG 文件
|
|
139
149
|
|
|
140
150
|
- 类型: `string | RegExp | (string | RegExp)[]`
|
|
141
151
|
- 默认值: `/\.svg$/`
|
|
142
152
|
|
|
143
|
-
|
|
153
|
+
## asset.svgr.exclude
|
|
144
154
|
|
|
145
155
|
设定不匹配的 SVG 文件
|
|
146
156
|
|
|
@@ -178,14 +188,14 @@ export default defineConfig({
|
|
|
178
188
|
});
|
|
179
189
|
```
|
|
180
190
|
|
|
181
|
-
|
|
191
|
+
## autoExternal.dependencies
|
|
182
192
|
|
|
183
193
|
是否需要外置项目的 `"dependencies"` 依赖。
|
|
184
194
|
|
|
185
195
|
- 类型: `boolean`
|
|
186
196
|
- 默认值: `true`
|
|
187
197
|
|
|
188
|
-
|
|
198
|
+
## autoExternal.peerDependencies
|
|
189
199
|
|
|
190
200
|
是否需要外置项目的 `"peerDependencies"` 依赖。
|
|
191
201
|
|
|
@@ -206,8 +216,6 @@ export default defineConfig({
|
|
|
206
216
|
- 类型: `Object`
|
|
207
217
|
|
|
208
218
|
```js modern.config.ts
|
|
209
|
-
import { defineConfig } from '@modern-js/module-tools';
|
|
210
|
-
|
|
211
219
|
export default defineConfig({
|
|
212
220
|
buildConfig: {
|
|
213
221
|
copy: {
|
|
@@ -217,13 +225,13 @@ export default defineConfig({
|
|
|
217
225
|
});
|
|
218
226
|
```
|
|
219
227
|
|
|
220
|
-
|
|
228
|
+
## copy.patterns
|
|
221
229
|
|
|
222
230
|
- 类型: `CopyPattern[]`
|
|
223
231
|
- 默认值: `[]`
|
|
224
232
|
|
|
225
233
|
```ts
|
|
226
|
-
|
|
234
|
+
interface CopyPattern {
|
|
227
235
|
from: string;
|
|
228
236
|
to?: string;
|
|
229
237
|
context?: string;
|
|
@@ -231,10 +239,9 @@ export interface CopyPattern {
|
|
|
231
239
|
}
|
|
232
240
|
```
|
|
233
241
|
|
|
234
|
-
|
|
242
|
+
## copy.options
|
|
235
243
|
|
|
236
|
-
- 类型:
|
|
237
|
-
- 默认值: `{ concurrency: 100, enableCopySync: false }`
|
|
244
|
+
- 类型:
|
|
238
245
|
|
|
239
246
|
```ts
|
|
240
247
|
type Options = {
|
|
@@ -243,6 +250,8 @@ type Options = {
|
|
|
243
250
|
};
|
|
244
251
|
```
|
|
245
252
|
|
|
253
|
+
- 默认值: `{ concurrency: 100, enableCopySync: false }`
|
|
254
|
+
|
|
246
255
|
- `concurrency`: 指定并行执行多少个复制任务。
|
|
247
256
|
- `enableCopySync`: 使用 [`fs.copySync`](https://github.com/jprichardson/node-fs-extra/blob/master/lib/copy/copy-sync.js),默认情况下 [`fs.copy`](https://github.com/jprichardson/node-fs-extra/blob/master/lib/copy/copy.js)。
|
|
248
257
|
|
|
@@ -256,8 +265,6 @@ type Options = {
|
|
|
256
265
|
由于 `define` 功能是由全局文本替换实现的,所以需要保证全局变量值为字符串,更为安全的做法是将每个全局变量的值转化为字符串,使用 `JSON.stringify` 进行转换,如下所示:
|
|
257
266
|
|
|
258
267
|
```js modern.config.ts
|
|
259
|
-
import { defineConfig } from '@modern-js/module-tools';
|
|
260
|
-
|
|
261
268
|
export default defineConfig({
|
|
262
269
|
buildConfig: {
|
|
263
270
|
define: {
|
|
@@ -300,41 +307,53 @@ export default defineConfig({
|
|
|
300
307
|
}
|
|
301
308
|
```
|
|
302
309
|
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
在出现类型错误的时候,是否允许构建成功。**默认情况下,在出现类型错误的时候会导致构建失败**。
|
|
310
|
+
## dts.abortOnError
|
|
306
311
|
|
|
307
|
-
|
|
308
|
-
当关闭该配置后,无法保证类型文件能正常生成,且不保证内容正确。在 `buildType: 'bundle'`,即打包模式下,类型文件一定不会生成。
|
|
309
|
-
:::
|
|
312
|
+
用于控制在出现类型错误的时候,是否允许构建成功。
|
|
310
313
|
|
|
311
314
|
- 类型:`boolean`
|
|
312
315
|
- 默认值:`true`
|
|
313
316
|
|
|
314
|
-
|
|
317
|
+
**默认情况下,在出现类型错误的时候会导致构建失败**。将 `abortOnError` 设置为 `false` 后,即使代码中出现了类型问题,构建依然会成功:
|
|
318
|
+
|
|
319
|
+
```js modern.config.ts
|
|
320
|
+
export default defineConfig({
|
|
321
|
+
buildConfig: {
|
|
322
|
+
dts: {
|
|
323
|
+
abortOnError: false,
|
|
324
|
+
},
|
|
325
|
+
},
|
|
326
|
+
});
|
|
327
|
+
```
|
|
328
|
+
|
|
329
|
+
:::warning
|
|
330
|
+
当关闭该配置后,无法保证类型文件能正常生成,且不保证内容正确。在 `buildType: 'bundle'` 时,即打包模式下,类型文件一定不会生成。
|
|
331
|
+
:::
|
|
332
|
+
|
|
333
|
+
## dts.distPath
|
|
315
334
|
|
|
316
335
|
类型文件的输出路径,基于 [outDir](/api/config/build-config#outDir) 进行输出。
|
|
317
336
|
|
|
318
337
|
- 类型: `string`
|
|
319
338
|
- 默认值: `./types`
|
|
320
339
|
|
|
321
|
-
|
|
340
|
+
## dts.only
|
|
322
341
|
|
|
323
342
|
只生成类型文件,不生成 js 文件。
|
|
324
343
|
|
|
325
344
|
- 类型: `boolean`
|
|
326
345
|
- 默认值: `false`
|
|
327
346
|
|
|
328
|
-
|
|
347
|
+
## dts.respectExternal
|
|
329
348
|
|
|
330
|
-
当设为 `false` 时,不会打包任何三方包类型,设为 `true` 时,会根据[externals](#externals)来决定是否需要打包三方类型。
|
|
349
|
+
当设为 `false` 时,不会打包任何三方包类型,设为 `true` 时,会根据 [externals](#externals) 来决定是否需要打包三方类型。
|
|
331
350
|
|
|
332
351
|
在对类型文件进行打包时,构建工具还未对 export 进行分析,因此当你引用的任何一个三方包出现类型错误时,都可能会中断当前的构建流程,这会导致构建流程不可控,因此我们可以通过这个配置来避免该问题。
|
|
333
352
|
|
|
334
353
|
- 类型: `boolean`
|
|
335
354
|
- 默认值: `true`
|
|
336
355
|
|
|
337
|
-
|
|
356
|
+
## dts.tsconfigPath
|
|
338
357
|
|
|
339
358
|
TypeScript 配置文件的路径。
|
|
340
359
|
|
|
@@ -351,8 +370,6 @@ TypeScript 配置文件的路径。
|
|
|
351
370
|
例如我们需要修改生成文件的后缀:
|
|
352
371
|
|
|
353
372
|
```js modern.config.ts
|
|
354
|
-
import { defineConfig } from '@modern-js/module-tools';
|
|
355
|
-
|
|
356
373
|
export default defineConfig({
|
|
357
374
|
buildConfig: {
|
|
358
375
|
esbuildOptions: options => {
|
|
@@ -385,35 +402,35 @@ export default defineConfig({
|
|
|
385
402
|
|
|
386
403
|
开启前:
|
|
387
404
|
|
|
388
|
-
```
|
|
405
|
+
```js ./dist/index.js
|
|
389
406
|
// 辅助函数
|
|
390
407
|
function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) {
|
|
391
408
|
// ...
|
|
392
409
|
}
|
|
393
410
|
// 辅助函数
|
|
394
411
|
function _async_to_generator(fn) {
|
|
395
|
-
return function() {
|
|
412
|
+
return function () {
|
|
396
413
|
// use asyncGeneratorStep
|
|
397
414
|
// ...
|
|
398
415
|
};
|
|
399
416
|
}
|
|
400
417
|
|
|
401
418
|
// 你的代码
|
|
402
|
-
export var yourCode = function() {
|
|
419
|
+
export var yourCode = function () {
|
|
403
420
|
// use _async_to_generator
|
|
404
|
-
}
|
|
421
|
+
};
|
|
405
422
|
```
|
|
406
423
|
|
|
407
424
|
开启后:
|
|
408
425
|
|
|
409
|
-
```
|
|
426
|
+
```js ./dist/index.js
|
|
410
427
|
// 从 @swc/helpers 导入的辅助函数
|
|
411
|
-
import { _ as _async_to_generator } from
|
|
428
|
+
import { _ as _async_to_generator } from '@swc/helpers/_/_async_to_generator';
|
|
412
429
|
|
|
413
430
|
// 你的代码
|
|
414
|
-
export var yourCode = function() {
|
|
431
|
+
export var yourCode = function () {
|
|
415
432
|
// use _async_to_generator
|
|
416
|
-
}
|
|
433
|
+
};
|
|
417
434
|
```
|
|
418
435
|
|
|
419
436
|
## externals
|
|
@@ -438,8 +455,6 @@ js 产物输出的格式,其中 `iife` 和 `umd` 只能在 `buildType` 为 `bund
|
|
|
438
455
|
- 默认值: `bundle` 模式下默认为 `['src/index.ts']`,`bundleless` 模式下默认为 `['src']`
|
|
439
456
|
|
|
440
457
|
```js modern.config.ts
|
|
441
|
-
import { defineConfig } from '@modern-js/module-tools';
|
|
442
|
-
|
|
443
458
|
export default defineConfig({
|
|
444
459
|
buildConfig: {
|
|
445
460
|
input: ['src/index.ts', 'src/index2.ts'],
|
|
@@ -469,8 +484,6 @@ esbuild 以 JSON 格式生成有关构建的一些元数据,可以通过例如
|
|
|
469
484
|
- 默认值: `false`
|
|
470
485
|
|
|
471
486
|
```js modern.config.ts
|
|
472
|
-
import { defineConfig } from '@modern-js/module-tools';
|
|
473
|
-
|
|
474
487
|
export default defineConfig({
|
|
475
488
|
buildConfig: {
|
|
476
489
|
minify: {
|
|
@@ -505,7 +518,7 @@ export default defineConfig({
|
|
|
505
518
|
|
|
506
519
|
在某些场景下,你可能不需要这些功能,那么可以通过此配置关闭它,关闭后,其引用路径将不会发生改变。
|
|
507
520
|
|
|
508
|
-
```ts
|
|
521
|
+
```js modern.config.ts
|
|
509
522
|
export default {
|
|
510
523
|
buildConfig: {
|
|
511
524
|
redirect: {
|
|
@@ -539,7 +552,7 @@ import 'other-package/dist/index.css';
|
|
|
539
552
|
}
|
|
540
553
|
```
|
|
541
554
|
|
|
542
|
-
同时你又设置了[style.inject](#
|
|
555
|
+
同时你又设置了 [style.inject](#styleinject) 为 `true`,在控制台可以看到类似的警告信息:
|
|
543
556
|
|
|
544
557
|
```bash
|
|
545
558
|
[LIBUILD:ESBUILD_WARN] Ignoring this import because "other-package/dist/index.css" was marked as having no side effects
|
|
@@ -548,7 +561,6 @@ import 'other-package/dist/index.css';
|
|
|
548
561
|
这时候就可以使用这个配置项,手动配置模块的`"sideEffects"`,配置支持正则和函数形式。
|
|
549
562
|
|
|
550
563
|
```js modern.config.ts
|
|
551
|
-
import { defineConfig } from '@modern-js/module-tools';
|
|
552
564
|
export default defineConfig({
|
|
553
565
|
buildConfig: {
|
|
554
566
|
sideEffects: [/\.css$/],
|
|
@@ -594,18 +606,18 @@ export default defineConfig({
|
|
|
594
606
|
|
|
595
607
|
配置样式相关的配置。
|
|
596
608
|
|
|
597
|
-
|
|
609
|
+
## style.less
|
|
598
610
|
|
|
599
611
|
less 相关配置。
|
|
600
612
|
|
|
601
|
-
|
|
613
|
+
## style.less.lessOptions
|
|
602
614
|
|
|
603
615
|
详细配置参考 [less](https://less.bootcss.com/usage/#less-options)。
|
|
604
616
|
|
|
605
617
|
- 类型: `Object`
|
|
606
618
|
- 默认值: `{ javascriptEnabled: true }`
|
|
607
619
|
|
|
608
|
-
|
|
620
|
+
## style.less.additionalData
|
|
609
621
|
|
|
610
622
|
在入口文件起始添加 `Less` 代码。
|
|
611
623
|
|
|
@@ -613,8 +625,6 @@ less 相关配置。
|
|
|
613
625
|
- 默认值: `undefined`
|
|
614
626
|
|
|
615
627
|
```js modern.config.ts
|
|
616
|
-
import { defineConfig } from '@modern-js/module-tools';
|
|
617
|
-
|
|
618
628
|
export default defineConfig({
|
|
619
629
|
buildConfig: {
|
|
620
630
|
style: {
|
|
@@ -626,7 +636,7 @@ export default defineConfig({
|
|
|
626
636
|
});
|
|
627
637
|
```
|
|
628
638
|
|
|
629
|
-
|
|
639
|
+
## style.less.implementation
|
|
630
640
|
|
|
631
641
|
配置 `Less` 使用的实现库,在不指定的情况下,使用的内置版本是 `4.1.3`。
|
|
632
642
|
|
|
@@ -636,8 +646,6 @@ export default defineConfig({
|
|
|
636
646
|
`Object` 类型时,指定 `Less` 的实现库
|
|
637
647
|
|
|
638
648
|
```js modern.config.ts
|
|
639
|
-
import { defineConfig } from '@modern-js/module-tools';
|
|
640
|
-
|
|
641
649
|
export default defineConfig({
|
|
642
650
|
buildConfig: {
|
|
643
651
|
style: {
|
|
@@ -652,8 +660,6 @@ export default defineConfig({
|
|
|
652
660
|
`string` 类型时,指定 `Less` 的实现库的路径
|
|
653
661
|
|
|
654
662
|
```js modern.config.ts
|
|
655
|
-
import { defineConfig } from '@modern-js/module-tools';
|
|
656
|
-
|
|
657
663
|
export default defineConfig({
|
|
658
664
|
buildConfig: {
|
|
659
665
|
style: {
|
|
@@ -665,18 +671,18 @@ export default defineConfig({
|
|
|
665
671
|
});
|
|
666
672
|
```
|
|
667
673
|
|
|
668
|
-
|
|
674
|
+
## sass
|
|
669
675
|
|
|
670
676
|
Sass 相关配置。
|
|
671
677
|
|
|
672
|
-
|
|
678
|
+
## style.sass.sassOptions
|
|
673
679
|
|
|
674
680
|
详细配置参考 [node-sass](https://github.com/sass/node-sass#options)
|
|
675
681
|
|
|
676
682
|
- 类型: `Object`
|
|
677
683
|
- 默认值: `{}`
|
|
678
684
|
|
|
679
|
-
|
|
685
|
+
## style.sass.additionalData
|
|
680
686
|
|
|
681
687
|
在入口文件起始添加 `Sass` 代码。
|
|
682
688
|
|
|
@@ -684,8 +690,6 @@ Sass 相关配置。
|
|
|
684
690
|
- 默认值: `undefined`
|
|
685
691
|
|
|
686
692
|
```js modern.config.ts
|
|
687
|
-
import { defineConfig } from '@modern-js/module-tools';
|
|
688
|
-
|
|
689
693
|
export default defineConfig({
|
|
690
694
|
buildConfig: {
|
|
691
695
|
style: {
|
|
@@ -698,7 +702,7 @@ export default defineConfig({
|
|
|
698
702
|
});
|
|
699
703
|
```
|
|
700
704
|
|
|
701
|
-
|
|
705
|
+
## style.sass.implementation
|
|
702
706
|
|
|
703
707
|
配置 `Sass` 使用的实现库,在不指定的情况下,使用的内置版本是 `1.5.4`。
|
|
704
708
|
|
|
@@ -708,8 +712,6 @@ export default defineConfig({
|
|
|
708
712
|
`Object` 类型时,指定 `Sass` 的实现库
|
|
709
713
|
|
|
710
714
|
```js modern.config.ts
|
|
711
|
-
import { defineConfig } from '@modern-js/module-tools';
|
|
712
|
-
|
|
713
715
|
export default defineConfig({
|
|
714
716
|
buildConfig: {
|
|
715
717
|
style: {
|
|
@@ -724,8 +726,6 @@ export default defineConfig({
|
|
|
724
726
|
`string` 类型时,指定 `Sass` 的实现库的路径
|
|
725
727
|
|
|
726
728
|
```js modern.config.ts
|
|
727
|
-
import { defineConfig } from '@modern-js/module-tools';
|
|
728
|
-
|
|
729
729
|
export default defineConfig({
|
|
730
730
|
buildConfig: {
|
|
731
731
|
style: {
|
|
@@ -737,24 +737,36 @@ export default defineConfig({
|
|
|
737
737
|
});
|
|
738
738
|
```
|
|
739
739
|
|
|
740
|
-
|
|
740
|
+
## style.postcss
|
|
741
741
|
|
|
742
742
|
- plugins
|
|
743
743
|
- processOptions
|
|
744
744
|
|
|
745
745
|
详细配置查看 [PostCSS](https://github.com/postcss/postcss#options)。
|
|
746
746
|
|
|
747
|
-
|
|
747
|
+
## style.inject
|
|
748
748
|
|
|
749
|
-
配置打包模式下是否将
|
|
749
|
+
配置打包模式下是否将 CSS 样式插入到 JavaScript 代码中。
|
|
750
750
|
|
|
751
751
|
- 类型: `boolean`
|
|
752
752
|
- 默认值: `false`
|
|
753
753
|
|
|
754
|
-
|
|
754
|
+
将 `inject` 设置为 `true` 来开启此功能:
|
|
755
|
+
|
|
756
|
+
```js modern.config.ts
|
|
757
|
+
export default defineConfig({
|
|
758
|
+
buildConfig: {
|
|
759
|
+
inject: true,
|
|
760
|
+
},
|
|
761
|
+
});
|
|
762
|
+
```
|
|
763
|
+
|
|
764
|
+
开启后,你会看到源码中引用的 CSS 代码被包含在了打包后的 JS 产物中。
|
|
765
|
+
|
|
766
|
+
例如,你在源码里写了 `import './index.scss'`,那么在产物中你会看到以下代码:
|
|
755
767
|
|
|
756
768
|
```js dist/index.js
|
|
757
|
-
// node_modules
|
|
769
|
+
// node_modules/style-inject/dist/style-inject.es.js
|
|
758
770
|
function styleInject(css, ref) {
|
|
759
771
|
// ...
|
|
760
772
|
}
|
|
@@ -767,20 +779,20 @@ style_inject_es_default(css_248z);
|
|
|
767
779
|
|
|
768
780
|
:::tip
|
|
769
781
|
|
|
770
|
-
|
|
782
|
+
开启 `inject` 后,你需要注意以下几点:
|
|
771
783
|
|
|
772
|
-
-
|
|
773
|
-
- 需要考虑 `sideEffects
|
|
784
|
+
- CSS 文件中的 `@import` 不会被处理。如果你的 CSS 文件中有 `@import` ,那么你需要在 JS 文件中手动引入 CSS 文件(less,scss 文件不需要,因为它们会有预处理)。
|
|
785
|
+
- 需要考虑 `sideEffects` 的影响。默认情况下,我们的构建器会认为 CSS 是有副作用的,如果你的项目中或者三方包的 package.json 设置了 `sideEffects` 字段并且没有包含此 CSS 文件,那么你将会得到一个警告:
|
|
774
786
|
|
|
775
|
-
```
|
|
787
|
+
```shell
|
|
776
788
|
[LIBUILD:ESBUILD_WARN] Ignoring this import because "src/index.scss" was marked as having no side effects by plugin "libuild:adapter"
|
|
777
789
|
```
|
|
778
790
|
|
|
779
|
-
此时可以通过配置[sideEffects](#sideeffects)来解决。
|
|
791
|
+
此时可以通过配置 [sideEffects](#sideeffects) 来解决。
|
|
780
792
|
|
|
781
793
|
:::
|
|
782
794
|
|
|
783
|
-
|
|
795
|
+
## style.autoModules
|
|
784
796
|
|
|
785
797
|
根据文件名自动启用 CSS Modules。
|
|
786
798
|
|
|
@@ -793,7 +805,7 @@ style_inject_es_default(css_248z);
|
|
|
793
805
|
|
|
794
806
|
`RegExp` : 为匹配正则条件的所有文件启用 CSS Modules.
|
|
795
807
|
|
|
796
|
-
|
|
808
|
+
## style.modules
|
|
797
809
|
|
|
798
810
|
CSS Modules 配置
|
|
799
811
|
|
|
@@ -803,8 +815,6 @@ CSS Modules 配置
|
|
|
803
815
|
一个常用的配置是 `localsConvention`,它可以改变 CSS Modules 的类名生成规则。
|
|
804
816
|
|
|
805
817
|
```js modern.config.ts
|
|
806
|
-
import { defineConfig } from '@modern-js/module-tools';
|
|
807
|
-
|
|
808
818
|
export default defineConfig({
|
|
809
819
|
buildConfig: {
|
|
810
820
|
style: {
|
|
@@ -828,7 +838,7 @@ export default defineConfig({
|
|
|
828
838
|
|
|
829
839
|
详细配置查看 [postcss-modules](https://github.com/madyankin/postcss-modules#usage)
|
|
830
840
|
|
|
831
|
-
|
|
841
|
+
## style.tailwindcss
|
|
832
842
|
|
|
833
843
|
Tailwind CSS 相关配置。
|
|
834
844
|
|
|
@@ -863,11 +873,37 @@ const tailwind = {
|
|
|
863
873
|
|
|
864
874
|
## target
|
|
865
875
|
|
|
866
|
-
|
|
876
|
+
`target` 用于为生成的 JavaScript 代码设置目标环境。它让 Module Tools 将目标环境无法识别的 JavaScript 语法转换为在这些环境中可用的低版本 JavaScript 语法。
|
|
877
|
+
|
|
878
|
+
- 类型:
|
|
879
|
+
|
|
880
|
+
```ts
|
|
881
|
+
type Target =
|
|
882
|
+
| 'es5'
|
|
883
|
+
| 'es6'
|
|
884
|
+
| 'es2015'
|
|
885
|
+
| 'es2016'
|
|
886
|
+
| 'es2017'
|
|
887
|
+
| 'es2018'
|
|
888
|
+
| 'es2019'
|
|
889
|
+
| 'es2020'
|
|
890
|
+
| 'es2021'
|
|
891
|
+
| 'es2022'
|
|
892
|
+
| 'esnext';
|
|
893
|
+
```
|
|
867
894
|
|
|
868
|
-
- 类型: `'es5' | 'es6' | 'es2015' | 'es2016' | 'es2017' | 'es2018' | 'es2019' | 'es2020' | 'es2021' | 'es2022' | 'esnext'`
|
|
869
895
|
- 默认值: `'es6'`
|
|
870
896
|
|
|
897
|
+
例如,将代码编译到 `es5` 语法:
|
|
898
|
+
|
|
899
|
+
```js modern.config.ts
|
|
900
|
+
export default defineConfig({
|
|
901
|
+
buildConfig: {
|
|
902
|
+
target: 'es5',
|
|
903
|
+
},
|
|
904
|
+
});
|
|
905
|
+
```
|
|
906
|
+
|
|
871
907
|
## transformImport
|
|
872
908
|
|
|
873
909
|
提供与 babel-plugin-import 等价的能力和配置,基于 SWC 实现。
|
|
@@ -879,9 +915,7 @@ const tailwind = {
|
|
|
879
915
|
|
|
880
916
|
使用示例:
|
|
881
917
|
|
|
882
|
-
```ts
|
|
883
|
-
import moduleTools, { defineConfig} from '@modern-js/module-tools';
|
|
884
|
-
|
|
918
|
+
```js modern.config.ts
|
|
885
919
|
export default defineConfig({
|
|
886
920
|
buildConfig: {
|
|
887
921
|
transformImport: [
|
|
@@ -892,9 +926,6 @@ export default defineConfig({
|
|
|
892
926
|
},
|
|
893
927
|
],
|
|
894
928
|
},
|
|
895
|
-
plugins: [
|
|
896
|
-
moduleTools(),
|
|
897
|
-
],
|
|
898
929
|
});
|
|
899
930
|
```
|
|
900
931
|
|
|
@@ -910,8 +941,6 @@ export default defineConfig({
|
|
|
910
941
|
- 默认值: `{}`
|
|
911
942
|
|
|
912
943
|
```ts modern.config.ts
|
|
913
|
-
import { defineConfig } from '@modern-js/module-tools';
|
|
914
|
-
|
|
915
944
|
export default defineConfig({
|
|
916
945
|
buildConfig: {
|
|
917
946
|
umdGlobals: {
|
|
@@ -932,8 +961,6 @@ export default defineConfig({
|
|
|
932
961
|
- 默认值: `name => name`
|
|
933
962
|
|
|
934
963
|
```js modern.config.ts
|
|
935
|
-
import { defineConfig } from '@modern-js/module-tools';
|
|
936
|
-
|
|
937
964
|
export default defineConfig({
|
|
938
965
|
buildConfig: {
|
|
939
966
|
format: 'umd',
|
|
@@ -954,8 +981,6 @@ export default defineConfig({
|
|
|
954
981
|
同时函数形式可以接收一个参数,为当前打包文件的输出路径
|
|
955
982
|
|
|
956
983
|
```js modern.config.ts
|
|
957
|
-
import { defineConfig } from '@modern-js/module-tools';
|
|
958
|
-
|
|
959
984
|
export default defineConfig({
|
|
960
985
|
buildConfig: {
|
|
961
986
|
format: 'umd',
|
|
@@ -621,9 +621,9 @@ export default () => {
|
|
|
621
621
|
|
|
622
622
|
Bundle 模式下,会将第三方依赖打包进来。
|
|
623
623
|
|
|
624
|
-
对于样式则会生成一份单独的产物文件,并且在
|
|
624
|
+
对于样式则会生成一份单独的产物文件,并且在 JS 产物文件中并不会存在导入样式的相关代码。
|
|
625
625
|
|
|
626
|
-
如果需要将样式注入
|
|
626
|
+
如果需要将样式注入 JS 产物中,可以开启 [`style.inject`](/api/config/build-config#styleinject) 选项。
|
|
627
627
|
|
|
628
628
|
<CH.Code>
|
|
629
629
|
```css ./dist/es/index.css
|
|
@@ -730,10 +730,10 @@ export { src_default as default };
|
|
|
730
730
|
- `.module.scss`
|
|
731
731
|
- `.module.sass`
|
|
732
732
|
|
|
733
|
-
如果需要对 CSS Modules
|
|
733
|
+
如果需要对 CSS Modules 进行配置,可以查看以下 API:
|
|
734
734
|
|
|
735
|
-
- [autoModules](/api/config/build-config#
|
|
736
|
-
- [modules](/api/config/build-config#
|
|
735
|
+
- [style.autoModules](/api/config/build-config#styleautomodules)
|
|
736
|
+
- [style.modules](/api/config/build-config#stylemodules)
|
|
737
737
|
|
|
738
738
|
下面是一个代码示例:
|
|
739
739
|
|
package/package.json
CHANGED
|
@@ -9,15 +9,15 @@
|
|
|
9
9
|
"directory": "packages/document/module-doc"
|
|
10
10
|
},
|
|
11
11
|
"license": "MIT",
|
|
12
|
-
"version": "2.
|
|
12
|
+
"version": "2.24.1",
|
|
13
13
|
"main": "index.js",
|
|
14
14
|
"dependencies": {
|
|
15
15
|
"@code-hike/mdx": "^0.7.4",
|
|
16
16
|
"react": "^18.2.0",
|
|
17
17
|
"react-dom": "^18.2.0",
|
|
18
18
|
"shiki": "^0.11.1",
|
|
19
|
-
"@modern-js/doc-tools": "2.
|
|
20
|
-
"@modern-js/doc-plugin-auto-sidebar": "2.
|
|
19
|
+
"@modern-js/doc-tools": "2.24.1",
|
|
20
|
+
"@modern-js/doc-plugin-auto-sidebar": "2.24.1"
|
|
21
21
|
},
|
|
22
22
|
"scripts": {
|
|
23
23
|
"dev": "modern dev",
|