-
-
Notifications
You must be signed in to change notification settings - Fork 57
docs: add Modern.js Module migration docs #378
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Timeless0911
merged 8 commits into
web-infra-dev:main
from
YanPes:docs/add-modern-js-module-docs
Nov 7, 2024
Merged
Changes from 2 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
eb7df4f
docs/add-error-catalog-entries-for-mf-types
fab7c0f
docs/add-error-catalog-entries-for-mf-types
f272a40
Update website/docs/en/guide/migration/modernjs-module.mdx
Timeless0911 0edf85c
Update website/docs/en/guide/migration/modernjs-module.mdx
Timeless0911 c99cbcb
Update website/docs/en/guide/migration/modernjs-module.mdx
Timeless0911 c6d9b6c
Update website/docs/en/guide/migration/modernjs-module.mdx
Timeless0911 d6a7e09
Update website/docs/en/guide/migration/modernjs-module.mdx
Timeless0911 8c23df1
Update website/docs/en/guide/migration/modernjs-module.mdx
Timeless0911 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1,167 @@ | ||
| # Modern.js Module | ||
|
|
||
| The migration path from `Modern.js Module` to `Rslib` is straightforward. The reason for it is the same underlying `Rsbuild` configuration. | ||
|
|
||
| ### Adapt package.json | ||
|
|
||
| `Rslib` has a minimal dependency footprint. For the basic functionality you only need the package `@rslib/core`. Let's update your `package.json` file. | ||
|
|
||
| - Remove the fields `main`, `lint-staged`, `simple-git-hooks`, `sideEffects` and `publishConfig` | ||
| - Change the `types` field from `./dist/types/index.d.ts` to `./dist/index.d.ts` | ||
| - Change the `module` field from `./dist/es/index.js` to `./dist/index.js` | ||
| - Remove the scripts fields `prepare`, `build:watch`, `reset`, `change`, `bump`, `pre`, `change-status`, `gen-release-note`, `release`, `new`, `upgrade` | ||
| - Change the script `build` from `modern build` to `rslib build` | ||
| - Change the script `lint` name to `check` and keep the value | ||
| - Add a new script `format` with the value `biome format --write` | ||
| - Change the script `dev` from `modern dev` to `rslib build --watch` | ||
| - Add the script `test` with the value `vitest run` | ||
| - Add the field `exports` (object) | ||
| - Add the field `"."` (object) | ||
| - Add the fields `"types": "./dist/index.d.ts"` and `"import": "./dist/index.js"` | ||
| - Add the field `files` with the value `["dist"]` | ||
| - Depending on your configuration and use-case the `devDependencies` can vary | ||
| - It is important to replace `"@modern-js/module-tools"` with `"@rslib/core"` | ||
| - We do not need `rimraf`, `lint-staged` and `simple-git-hooks` anymore for starters | ||
| - Copy over your required `dependencies` and `peerDependencies` if needed | ||
|
|
||
| Your `package.json` should look something like this: | ||
|
|
||
| ```json title="package.json" | ||
| { | ||
| "name": "rslib", | ||
| "version": "1.0.0", | ||
| "type": "module", | ||
| "exports": { | ||
| ".": { | ||
| "types": "./dist/index.d.ts", | ||
| "import": "./dist/index.js" | ||
| } | ||
| }, | ||
| "module": "./dist/index.js", | ||
| "types": "./dist/index.d.ts", | ||
| "files": ["dist"], | ||
| "scripts": { | ||
| "build": "rslib build", | ||
| "check": "biome check --write", | ||
| "dev": "rslib build --watch", | ||
| "format": "biome format --write", | ||
| "test": "vitest run" | ||
| }, | ||
| "devDependencies": { | ||
| "@biomejs/biome": "^1.9.3", | ||
| "@rslib/core": "^0.0.16", | ||
| "typescript": "^5.6.3" | ||
| }, | ||
| "peerDependencies": {}, | ||
| "dependencies": {} | ||
| } | ||
| ``` | ||
|
|
||
| ### Adapt bundler config | ||
Timeless0911 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| Now we have a clean slate to work with. We will continue with the `Rslib` configuration. It follows the same pattern as all `Rs*` projects. Since this step is very unique for every environment, we will only touch the basics here: | ||
| Replace your `modern.config.ts` with a `rslib.config.ts`: | ||
|
|
||
| ```js title="rslib.config.ts" | ||
| import { defineConfig } from '@rslib/core'; | ||
|
|
||
| export default defineConfig({ | ||
| source: { | ||
| entry: { | ||
| index: ['./src/**'], | ||
| }, | ||
| }, | ||
| lib: [ | ||
| { | ||
| bundle: false, | ||
| dts: true, | ||
| format: 'esm', | ||
| }, | ||
| ], | ||
| }); | ||
| ``` | ||
|
|
||
| ### Typescript | ||
Timeless0911 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| If you use Typescript in your `Modern.js Module`, add the following changes: | ||
|
|
||
| ```js title="rslib.config.ts" | ||
| import { defineConfig } from '@rslib/core'; | ||
|
|
||
| export default defineConfig({ | ||
| //... | ||
| lib: [ | ||
| { | ||
| //... | ||
| dts: true, | ||
| }, | ||
| ], | ||
| }); | ||
| ``` | ||
|
|
||
| ### React | ||
Timeless0911 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| If you use React in your `Modern.js Module`, add the following changes: | ||
|
|
||
| ```js title="rslib.config.ts" | ||
| import { defineConfig } from '@rslib/core'; | ||
| // Quick tip: You can use all Rsbuild plugins here since they are compatible with Rslib | ||
| import { pluginReact } from '@rsbuild/plugin-react'; | ||
|
|
||
| export default defineConfig({ | ||
| //... | ||
| plugins: [pluginReact()], | ||
| }); | ||
| ``` | ||
|
|
||
| In addition, you have to install the `@rsbuild/plugin-react` package as devDependency | ||
|
|
||
| ### Sass | ||
Timeless0911 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| If you use Sass in your `Modern.js Module`, add the following changes: | ||
|
|
||
| ```js title="rslib.config.ts" | ||
| import { defineConfig } from '@rslib/core'; | ||
| // Quick tip: You can use all Rsbuild plugins here since they are compatible with Rslib | ||
| import { pluginSass } from '@rsbuild/plugin-sass'; | ||
|
|
||
| export default defineConfig({ | ||
| //... | ||
| plugins: [pluginSass()], | ||
| }); | ||
| ``` | ||
|
|
||
| In addition, you have to install the `@rsbuild/plugin-sass` package as devDependency. | ||
|
|
||
| If you run Typescript together with Sass, you might run into DTS generation errors. This can be resolved by adding a `global.d.ts` file in your `/src` directory. | ||
|
|
||
| ```ts title="global.d.ts" | ||
| declare module '*.scss' { | ||
| const content: { [className: string]: string }; | ||
| export default content; | ||
| } | ||
| ``` | ||
|
|
||
| ### css-modules | ||
Timeless0911 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| If you use css-modules in your `Modern.js Module`, add the following changes: | ||
|
|
||
| ```js title="rslib.config.ts" | ||
| import { defineConfig } from '@rslib/core'; | ||
| import { pluginSass } from '@rsbuild/plugin-sass'; | ||
|
|
||
| export default defineConfig({ | ||
| lib: [ | ||
| { | ||
| //... | ||
| output: { | ||
| cssModules: { | ||
| // the css-modules options are 1:1 the same as in the official "css-modules" package | ||
| localIdentName: '[local]--[hash:base64:5]', | ||
| }, | ||
| }, | ||
| }, | ||
| ], | ||
| plugins: [pluginSass()], | ||
| }); | ||
| ``` | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.