|
1 | 1 | # Modern.js Module
|
| 2 | + |
| 3 | +The migration path from `Modern.js Module` to `Rslib` is straightforward. The reason for it is the same underlying `Rsbuild` configuration. |
| 4 | + |
| 5 | +## Adapt package.json |
| 6 | + |
| 7 | +`Rslib` has a minimal dependency footprint. For the basic functionality you only need the package `@rslib/core`. Let's update your `package.json` file. |
| 8 | + |
| 9 | +- Remove the fields `main`, `lint-staged`, `simple-git-hooks`, `sideEffects` and `publishConfig` |
| 10 | +- Change the `types` field from `./dist/types/index.d.ts` to `./dist/index.d.ts` |
| 11 | +- Change the `module` field from `./dist/es/index.js` to `./dist/index.js` |
| 12 | +- Remove the scripts fields `prepare`, `build:watch`, `reset`, `change`, `bump`, `pre`, `change-status`, `gen-release-note`, `release`, `new`, `upgrade` |
| 13 | +- Change the script `build` from `modern build` to `rslib build` |
| 14 | +- Change the script `lint` name to `check` and keep the value |
| 15 | +- Add a new script `format` with the value `biome format --write` |
| 16 | +- Change the script `dev` from `modern dev` to `rslib build --watch` |
| 17 | +- Add the script `test` with the value `vitest run` |
| 18 | +- Add the field `exports` (object) |
| 19 | + - Add the field `"."` (object) |
| 20 | + - Add the fields `"types": "./dist/index.d.ts"` and `"import": "./dist/index.js"` |
| 21 | +- Add the field `files` with the value `["dist"]` |
| 22 | +- Depending on your configuration and use-case the `devDependencies` can vary |
| 23 | + - It is important to replace `"@modern-js/module-tools"` with `"@rslib/core"` |
| 24 | + - We do not need `rimraf`, `lint-staged` and `simple-git-hooks` anymore for starters |
| 25 | +- Copy over your required `dependencies` and `peerDependencies` if needed |
| 26 | + |
| 27 | +Your `package.json` should look something like this: |
| 28 | + |
| 29 | +```json title="package.json" |
| 30 | +{ |
| 31 | + "name": "rslib", |
| 32 | + "version": "1.0.0", |
| 33 | + "type": "module", |
| 34 | + "exports": { |
| 35 | + ".": { |
| 36 | + "types": "./dist/index.d.ts", |
| 37 | + "import": "./dist/index.js" |
| 38 | + } |
| 39 | + }, |
| 40 | + "module": "./dist/index.js", |
| 41 | + "types": "./dist/index.d.ts", |
| 42 | + "files": ["dist"], |
| 43 | + "scripts": { |
| 44 | + "build": "rslib build", |
| 45 | + "check": "biome check --write", |
| 46 | + "dev": "rslib build --watch", |
| 47 | + "format": "biome format --write", |
| 48 | + "test": "vitest run" |
| 49 | + }, |
| 50 | + "devDependencies": { |
| 51 | + "@biomejs/biome": "^1.9.3", |
| 52 | + "@rslib/core": "^0.0.16", |
| 53 | + "typescript": "^5.6.3" |
| 54 | + }, |
| 55 | + "peerDependencies": {}, |
| 56 | + "dependencies": {} |
| 57 | +} |
| 58 | +``` |
| 59 | + |
| 60 | +## Adapt bundler config |
| 61 | + |
| 62 | +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: |
| 63 | +Replace your `modern.config.ts` with a `rslib.config.ts`: |
| 64 | + |
| 65 | +```js title="rslib.config.ts" |
| 66 | +import { defineConfig } from '@rslib/core'; |
| 67 | + |
| 68 | +export default defineConfig({ |
| 69 | + source: { |
| 70 | + entry: { |
| 71 | + index: ['./src/**'], |
| 72 | + }, |
| 73 | + }, |
| 74 | + lib: [ |
| 75 | + { |
| 76 | + bundle: false, |
| 77 | + dts: true, |
| 78 | + format: 'esm', |
| 79 | + }, |
| 80 | + ], |
| 81 | +}); |
| 82 | +``` |
| 83 | + |
| 84 | +## Typescript |
| 85 | + |
| 86 | +If you use Typescript in your `Modern.js Module`, add the following changes: |
| 87 | + |
| 88 | +```js title="rslib.config.ts" |
| 89 | +import { defineConfig } from '@rslib/core'; |
| 90 | + |
| 91 | +export default defineConfig({ |
| 92 | + //... |
| 93 | + lib: [ |
| 94 | + { |
| 95 | + //... |
| 96 | + dts: true, |
| 97 | + }, |
| 98 | + ], |
| 99 | +}); |
| 100 | +``` |
| 101 | + |
| 102 | +## React |
| 103 | + |
| 104 | +If you use React in your `Modern.js Module`, add the following changes: |
| 105 | + |
| 106 | +```js title="rslib.config.ts" |
| 107 | +import { defineConfig } from '@rslib/core'; |
| 108 | +// Quick tip: You can use all Rsbuild plugins here since they are compatible with Rslib |
| 109 | +import { pluginReact } from '@rsbuild/plugin-react'; |
| 110 | + |
| 111 | +export default defineConfig({ |
| 112 | + //... |
| 113 | + plugins: [pluginReact()], |
| 114 | +}); |
| 115 | +``` |
| 116 | + |
| 117 | +In addition, you have to install the `@rsbuild/plugin-react` package as devDependency |
| 118 | + |
| 119 | +## Sass |
| 120 | + |
| 121 | +If you use Sass in your `Modern.js Module`, add the following changes: |
| 122 | + |
| 123 | +```js title="rslib.config.ts" |
| 124 | +import { defineConfig } from '@rslib/core'; |
| 125 | +// Quick tip: You can use all Rsbuild plugins here since they are compatible with Rslib |
| 126 | +import { pluginSass } from '@rsbuild/plugin-sass'; |
| 127 | + |
| 128 | +export default defineConfig({ |
| 129 | + //... |
| 130 | + plugins: [pluginSass()], |
| 131 | +}); |
| 132 | +``` |
| 133 | + |
| 134 | +In addition, you have to install the `@rsbuild/plugin-sass` package as devDependency. |
| 135 | + |
| 136 | +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. |
| 137 | + |
| 138 | +```ts title="global.d.ts" |
| 139 | +declare module '*.scss' { |
| 140 | + const content: { [className: string]: string }; |
| 141 | + export default content; |
| 142 | +} |
| 143 | +``` |
| 144 | + |
| 145 | +## css-modules |
| 146 | + |
| 147 | +If you use css-modules in your `Modern.js Module`, add the following changes: |
| 148 | + |
| 149 | +```js title="rslib.config.ts" |
| 150 | +import { defineConfig } from '@rslib/core'; |
| 151 | +import { pluginSass } from '@rsbuild/plugin-sass'; |
| 152 | + |
| 153 | +export default defineConfig({ |
| 154 | + lib: [ |
| 155 | + { |
| 156 | + //... |
| 157 | + output: { |
| 158 | + cssModules: { |
| 159 | + // the css-modules options are 1:1 the same as in the official "css-modules" package |
| 160 | + localIdentName: '[local]--[hash:base64:5]', |
| 161 | + }, |
| 162 | + }, |
| 163 | + }, |
| 164 | + ], |
| 165 | + plugins: [pluginSass()], |
| 166 | +}); |
| 167 | +``` |
0 commit comments