rolldown-string 0.0.0 → 0.2.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/README.md CHANGED
@@ -4,7 +4,12 @@
4
4
  [![npm downloads][npm-downloads-src]][npm-downloads-href]
5
5
  [![Unit Test][unit-test-src]][unit-test-href]
6
6
 
7
- undefined
7
+ A compatibility layer for [magic-string](https://github.com/Rich-Harris/magic-string) to work with Rolldown and other bundlers.
8
+
9
+ - In Rolldown, [native `magic-string`](https://rolldown.rs/in-depth/native-magic-string) is used to optimize performance.
10
+ - If native support is unavailable, it gracefully falls back to the JavaScript implementation of `magic-string`.
11
+
12
+ Recommended for use with [unplugin](https://github.com/unjs/unplugin).
8
13
 
9
14
  ## Install
10
15
 
@@ -12,6 +17,42 @@ undefined
12
17
  npm i rolldown-string
13
18
  ```
14
19
 
20
+ ## Usage
21
+
22
+ ### `withMagicString`
23
+
24
+ Higher-order function to create a `transform` hook with `magic-string` support.
25
+
26
+ ```ts
27
+ import { withMagicString } from 'rolldown-string'
28
+
29
+ const plugin = {
30
+ transform: withMagicString((s, id) => {
31
+ // your transformations...
32
+ s.replace('42', '43')
33
+ }),
34
+ }
35
+ ```
36
+
37
+ ### `rolldownString` / `generateTransform`
38
+
39
+ More flexible way to use `rolldown-string`.
40
+
41
+ ```ts
42
+ import { generateTransform, rolldownString } from 'rolldown-string'
43
+
44
+ const yourPlugin = {
45
+ transform(code, id, meta) {
46
+ const s = rolldownString(code, id, meta)
47
+
48
+ // your transformations...
49
+ s.replace('42', '43')
50
+
51
+ return generateTransform(s, id)
52
+ },
53
+ }
54
+ ```
55
+
15
56
  ## Sponsors
16
57
 
17
58
  <p align="center">
package/dist/index.d.ts CHANGED
@@ -5,6 +5,10 @@ import { BindingMagicString } from "rolldown";
5
5
  type ObjectIntersection<A, B> = { [K in keyof A & keyof B]: A[K] };
6
6
  type RolldownString = ObjectIntersection<MagicString, BindingMagicString>;
7
7
  declare function rolldownString(code: string, id: string, meta?: any): RolldownString;
8
+ type Awaitable<T> = T | Promise<T>;
9
+ type HandlerReturn = string | MagicString | BindingMagicString | RolldownString | void | undefined;
10
+ type Handler<Meta> = (s: RolldownString, id: string, meta: Meta) => Awaitable<HandlerReturn>;
11
+ declare function withMagicString<Meta>(handler: Handler<Meta>): (code: string, id: string, meta: Meta) => Awaitable<CodeTransform | undefined>;
8
12
  /**
9
13
  * The result of code transformation.
10
14
  */
@@ -15,6 +19,6 @@ interface CodeTransform {
15
19
  /**
16
20
  * Generate an object of code and source map from MagicString.
17
21
  */
18
- declare function generateTransform(s: MagicString | BindingMagicString | RolldownString | undefined, id: string): CodeTransform | undefined;
22
+ declare function generateTransform(s: MagicString | BindingMagicString | RolldownString | undefined, id: string, force?: boolean): CodeTransform | undefined;
19
23
  //#endregion
20
- export { CodeTransform, RolldownString, generateTransform, rolldownString };
24
+ export { CodeTransform, Handler, HandlerReturn, RolldownString, generateTransform, rolldownString, withMagicString };
package/dist/index.js CHANGED
@@ -4,12 +4,24 @@ import MagicString from "magic-string";
4
4
  function rolldownString(code, id, meta) {
5
5
  return meta?.magicString || new MagicString(code, { filename: id });
6
6
  }
7
+ function withMagicString(handler) {
8
+ return (code, id, meta) => {
9
+ const s = rolldownString(code, id, meta);
10
+ const res = handler(s, id, meta);
11
+ const callback = (res$1) => {
12
+ if (typeof res$1 === "string") return { code: res$1 };
13
+ return generateTransform(res$1 || s, id, !!res$1);
14
+ };
15
+ if (res instanceof Promise) return res.then(callback);
16
+ return callback(res);
17
+ };
18
+ }
7
19
  /**
8
20
  * Generate an object of code and source map from MagicString.
9
21
  */
10
- function generateTransform(s, id) {
22
+ function generateTransform(s, id, force) {
11
23
  if (s?.constructor.name === "BindingMagicString") return { code: s };
12
- if (s?.hasChanged()) return {
24
+ if (s && (force || s.hasChanged())) return {
13
25
  code: s.toString(),
14
26
  get map() {
15
27
  return s.generateMap({
@@ -22,4 +34,4 @@ function generateTransform(s, id) {
22
34
  }
23
35
 
24
36
  //#endregion
25
- export { generateTransform, rolldownString };
37
+ export { generateTransform, rolldownString, withMagicString };
package/package.json CHANGED
@@ -1,8 +1,8 @@
1
1
  {
2
2
  "name": "rolldown-string",
3
3
  "type": "module",
4
- "version": "0.0.0",
5
- "description": "undefined",
4
+ "version": "0.2.0",
5
+ "description": "A compatibility layer for magic-string to work with Rolldown and other bundlers.",
6
6
  "author": "Kevin Deng <sxzz@sxzz.moe>",
7
7
  "license": "MIT",
8
8
  "funding": "https://github.com/sponsors/sxzz",
@@ -14,6 +14,12 @@
14
14
  "bugs": {
15
15
  "url": "https://github.com/sxzz/rolldown-string/issues"
16
16
  },
17
+ "keywords": [
18
+ "rolldown",
19
+ "unplugin",
20
+ "magic-string",
21
+ "rollup"
22
+ ],
17
23
  "exports": {
18
24
  ".": "./dist/index.js",
19
25
  "./package.json": "./package.json"
@@ -36,6 +42,7 @@
36
42
  "devDependencies": {
37
43
  "@sxzz/eslint-config": "^7.4.3",
38
44
  "@sxzz/prettier-config": "^2.2.6",
45
+ "@sxzz/test-utils": "^0.5.14",
39
46
  "@types/node": "^24.10.4",
40
47
  "@typescript/native-preview": "7.0.0-dev.20251214.1",
41
48
  "bumpp": "^10.3.2",