@modern-js/module-tools-docs 2.5.0 → 2.6.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.
@@ -295,7 +295,7 @@ Module sideEffects
295
295
  Normally, we configure the module's side effects via the sideEffects field in package.json, but in some cases, such as when we reference a three-party package style file
296
296
 
297
297
  ```js
298
- import 'other-package/dist/index.css'
298
+ import 'other-package/dist/index.css';
299
299
  ```
300
300
 
301
301
  But the package.json of this three-party package does not have the style file configured in the sideEffects
@@ -493,6 +493,35 @@ Configure whether to insert style into js in packaged mode
493
493
  - type: `boolean`
494
494
  - default: `false`
495
495
 
496
+ After opening inject, you will see an extra piece of style code in the js file. For example, if you write `import '. /index.scss'`,you will see the following code.
497
+
498
+ ```js dist/index.js
499
+ // node_modules/.pnpm/style-inject@0.3.0/node_modules/style-inject/dist/style-inject.es.js
500
+ function styleInject(css, ref) {
501
+ // ...
502
+ }
503
+ var style_inject_es_default = styleInject;
504
+
505
+ // src/index.scss
506
+ var css_248z = '.body {\n color: black;\n}';
507
+ style_inject_es_default(css_248z);
508
+ ```
509
+
510
+ :::tip {title="Note"}
511
+
512
+ With `inject` turned on, you need to pay attention to the following points.
513
+
514
+ - `@import` in the css file will not be processed. So if you have `@import` in your css file, then you need to bring in the css file manually in js (less,scss files don't need it because they will be preprocessed).
515
+ - The impact of `sideEffects` needs to be considered, by default our builder will consider it as a side effect, if the `sideEffects` field set in your project or in the package.json of the three-way package and it does not contain this css file, then you will get a warning
516
+
517
+ ```
518
+ [LIBUILD:ESBUILD_WARN] Ignoring this import because "src/index.scss" was marked as having no side effects by plugin "libuild:adapter"
519
+ ```
520
+
521
+ This can be fixed by configuring [sideEffects](#sideeffects) in this case.
522
+
523
+ :::
524
+
496
525
  ### autoModules
497
526
 
498
527
  Enable CSS Modules automatically based on the filename.
@@ -620,7 +649,7 @@ At this point the umd product will go to mount on `global.myLib`
620
649
  :::tip
621
650
 
622
651
  - The module name of the umd product must not conflict with the global variable name.
623
- - Module names should not contain special characters like `-`, `@`, `/`, etc.
652
+ - Module names will be converted to camelCase, e.g. `my-lib` will be converted to `myLib`, refer to [toIdentifier](https://github.com/babel/babel/blob/main/packages/babel-types/src/converters/toIdentifier.ts).
624
653
 
625
654
  :::
626
655
 
@@ -213,6 +213,7 @@ export default defineConfig({
213
213
  ## Function
214
214
 
215
215
  The way the function is configured, you can get the preset value from the `preset` parameter and then modify the build configuration inside to customize your build configuration.
216
+
216
217
  The following is an example of how a function can be configured to compress a build product.
217
218
 
218
219
  ```js modern.config.ts
@@ -232,3 +233,82 @@ export default defineConfig({
232
233
  },
233
234
  });
234
235
  ```
236
+
237
+ ### Function Parameters
238
+
239
+ The function form of `buildPreset` contains a function parameter in the form of an object. The object contains the following fields.
240
+
241
+ * `preset`
242
+ * `extendPreset`
243
+
244
+ #### `preset`
245
+
246
+ Type: **Object**
247
+
248
+ Contains the build configurations corresponding to all available build presets. Build configurations can be used by means of the strings supported by `buildPreset` or by means of underscore commands for those strings. The following are examples of the use of both approaches.
249
+
250
+ <CH.Spotlight>
251
+
252
+ ```ts modern.config.ts
253
+ import { defineConfig } from '@modern-js/module-tools';
254
+
255
+ export default defineConfig({
256
+ buildPreset({ preset }) {
257
+ return preset['npm-library'];
258
+ },
259
+ });
260
+ ```
261
+ ---
262
+
263
+ Use the strings supported by `buildPreset`.
264
+
265
+ ```ts modern.config.ts
266
+ import { defineConfig } from '@modern-js/module-tools';
267
+
268
+ export default defineConfig({
269
+ buildPreset({ preset }) {
270
+ return preset['npm-library'];
271
+ },
272
+ });
273
+ ```
274
+
275
+ ---
276
+
277
+ Use the underscore naming convention for strings supported by `buildPreset`.
278
+
279
+ ```ts modern.config.ts
280
+ import { defineConfig } from '@modern-js/module-tools';
281
+
282
+ export default defineConfig({
283
+ buildPreset({ preset }) {
284
+ return preset.NPM_LIBRARY;
285
+ },
286
+ });
287
+ ```
288
+
289
+ </CH.Spotlight>
290
+
291
+ #### `extendPreset`
292
+
293
+ Type: `Function`
294
+
295
+ Tool function for extending a `buildPreset` to modify the build configuration corresponding to the `buildPreset`.
296
+
297
+ > The base uses something like `{...oldBuildConfig, ...extendBuildConfig}` approach to processing.
298
+
299
+ For example, adding the `define` configuration to the `'npm-library'` build preset.
300
+
301
+ ```ts modern.config.ts
302
+ import { defineConfig } from '@modern-js/module-tools';
303
+
304
+ export default defineConfig({
305
+ buildPreset({ extendPreset }) {
306
+ return extendPreset('npm-library', {
307
+ define: {
308
+ VERSION: '1.0.1',
309
+ },
310
+ });
311
+ },
312
+ });
313
+ ```
314
+
@@ -352,7 +352,7 @@ export default defineConfig({
352
352
  通常情况下,我们通过 package.json 的 `"sideEffects"` 字段来配置模块的副作用,但是在某些情况下,例如我们引用了一个三方包的样式文件。
353
353
 
354
354
  ```js
355
- import 'other-package/dist/index.css'
355
+ import 'other-package/dist/index.css';
356
356
  ```
357
357
 
358
358
  但是这个三方包的 package.json 里并没有将样式文件配置到 `"sideEffects"` 里。
@@ -562,6 +562,35 @@ export default defineConfig({
562
562
  - 类型: `boolean`
563
563
  - 默认值: `false`
564
564
 
565
+ 开启 inject 后,你会看到打包后的 js 文件中会多出一段 style 的代码。例如你在源码里写了`import './index.scss'`,那么你会看到以下代码。
566
+
567
+ ```js dist/index.js
568
+ // node_modules/.pnpm/style-inject@0.3.0/node_modules/style-inject/dist/style-inject.es.js
569
+ function styleInject(css, ref) {
570
+ // ...
571
+ }
572
+ var style_inject_es_default = styleInject;
573
+
574
+ // src/index.scss
575
+ var css_248z = ".body {\n color: black;\n}";
576
+ style_inject_es_default(css_248z);
577
+ ```
578
+
579
+ :::tip {title="注意"}
580
+
581
+ 开启了 `inject` 后,你需要注意以下几点:
582
+
583
+ - css 文件中的 `@import` 不会被处理。因此如果你的 css 文件中有 `@import` ,那么你需要在 js 中手动引入 css 文件(less,scss 文件不需要,因为它们会有预处理)。
584
+ - 需要考虑 `sideEffects`的影响,默认情况下我们的构建器会认为它是有副作用的,如果你的项目中或者三方包的 package.json 设置了 `sideEffects` 字段并且没有包含此 css 文件,那么你将会得到一个警告
585
+
586
+ ```
587
+ [LIBUILD:ESBUILD_WARN] Ignoring this import because "src/index.scss" was marked as having no side effects by plugin "libuild:adapter"
588
+ ```
589
+
590
+ 此时可以通过配置[sideEffects](#sideeffects)来解决。
591
+
592
+ :::
593
+
565
594
  ### autoModules
566
595
 
567
596
  根据文件名自动启用 CSS Modules。
@@ -694,8 +723,8 @@ export default defineConfig({
694
723
 
695
724
  :::tip
696
725
 
697
- - 需要遵守 UMD 规范,UMD 产物的模块名不能和全局变量名冲突
698
- - 模块名不能含有 `-`,`@`,`/` 等特殊字符
726
+ - 需要遵守 UMD 规范,UMD 产物的模块名不能和全局变量名冲突。
727
+ - 模块名会被转换为驼峰命名,如 `my-lib` 会被转换为 `myLib`,可参考[toIdentifier](https://github.com/babel/babel/blob/main/packages/babel-types/src/converters/toIdentifier.ts)。
699
728
 
700
729
  :::
701
730
 
@@ -232,3 +232,82 @@ export default defineConfig({
232
232
  },
233
233
  });
234
234
  ```
235
+
236
+ ### 函数参数
237
+
238
+ `buildPreset` 的函数形式包含了一个对象形式的函数参数。该对象包含以下字段:
239
+
240
+ * `preset`
241
+ * `extendPreset`
242
+
243
+ #### `preset`
244
+
245
+ 类型:**Object**
246
+
247
+ 包含了所有可用的构建预设对应的构建配置。可以通过 `buildPreset` 所支持的字符串来使用构建配置,也可以使用这些字符串的下划线命令的方式。下面是两种方式的使用示例:
248
+
249
+
250
+ <CH.Spotlight>
251
+
252
+ ```ts modern.config.ts
253
+ import { defineConfig } from '@modern-js/module-tools';
254
+
255
+ export default defineConfig({
256
+ buildPreset({ preset }) {
257
+ return preset['npm-library'];
258
+ },
259
+ });
260
+ ```
261
+ ---
262
+
263
+ 使用 `buildPreset` 所支持的字符串。
264
+
265
+ ```ts modern.config.ts
266
+ import { defineConfig } from '@modern-js/module-tools';
267
+
268
+ export default defineConfig({
269
+ buildPreset({ preset }) {
270
+ return preset['npm-library'];
271
+ },
272
+ });
273
+ ```
274
+
275
+ ---
276
+
277
+ 使用 `buildPreset` 所支持的字符串的下划线命名方式。
278
+
279
+ ```ts modern.config.ts
280
+ import { defineConfig } from '@modern-js/module-tools';
281
+
282
+ export default defineConfig({
283
+ buildPreset({ preset }) {
284
+ return preset.NPM_LIBRARY;
285
+ },
286
+ });
287
+ ```
288
+
289
+ </CH.Spotlight>
290
+
291
+ #### `extendPreset`
292
+
293
+ 类型:`Function`
294
+
295
+ 用于扩展某个 `buildPreset` 的工具函数,可以修改 `buildPreset` 对应的构建配置。
296
+
297
+ > 底层使用类似 `{...oldBuildConfig, ...extendBuildConfig}` 方式进行处理。
298
+
299
+ 例如在 `'npm-library'` 构建预设的基础上增加 `define` 配置:
300
+
301
+ ```ts modern.config.ts
302
+ import { defineConfig } from '@modern-js/module-tools';
303
+
304
+ export default defineConfig({
305
+ buildPreset({ extendPreset }) {
306
+ return extendPreset('npm-library', {
307
+ define: {
308
+ VERSION: '1.0.1',
309
+ },
310
+ });
311
+ },
312
+ });
313
+ ```
package/package.json CHANGED
@@ -5,15 +5,15 @@
5
5
  "bugs": "https://github.com/modern-js-dev/modern.js/issues",
6
6
  "repository": "modern-js-dev/modern.js",
7
7
  "license": "MIT",
8
- "version": "2.5.0",
8
+ "version": "2.6.0",
9
9
  "main": "index.js",
10
10
  "dependencies": {
11
11
  "@code-hike/mdx": "^0.7.4",
12
12
  "react": "^18.2.0",
13
13
  "react-dom": "^18.2.0",
14
14
  "shiki": "^0.11.1",
15
- "@modern-js/doc-plugin-auto-sidebar": "2.5.0",
16
- "@modern-js/doc-tools": "2.5.0"
15
+ "@modern-js/doc-plugin-auto-sidebar": "2.6.0",
16
+ "@modern-js/doc-tools": "2.6.0"
17
17
  },
18
18
  "scripts": {
19
19
  "dev": "modern dev",