rolldown-string 0.1.0 → 0.2.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/README.md CHANGED
@@ -19,6 +19,25 @@ npm i rolldown-string
19
19
 
20
20
  ## Usage
21
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
+
22
41
  ```ts
23
42
  import { generateTransform, rolldownString } from 'rolldown-string'
24
43
 
@@ -26,7 +45,8 @@ const yourPlugin = {
26
45
  transform(code, id, meta) {
27
46
  const s = rolldownString(code, id, meta)
28
47
 
29
- // ... your transformations
48
+ // your transformations...
49
+ s.replace('42', '43')
30
50
 
31
51
  return generateTransform(s, id)
32
52
  },
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<Args extends any[], This> = (this: This, s: RolldownString, id: string, ...args: Args) => Awaitable<HandlerReturn>;
11
+ declare function withMagicString<Args extends any[], This>(handler: Handler<Args, This>): (this: This, code: string, id: string, ...args: Args) => 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 function(code, id, ...args) {
9
+ const s = rolldownString(code, id, args.at(-1));
10
+ const res = handler.call(this, s, id, ...args);
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.1.0",
5
- "description": "undefined",
4
+ "version": "0.2.1",
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,13 +42,15 @@
36
42
  "devDependencies": {
37
43
  "@sxzz/eslint-config": "^7.4.3",
38
44
  "@sxzz/prettier-config": "^2.2.6",
39
- "@types/node": "^24.10.4",
40
- "@typescript/native-preview": "7.0.0-dev.20251214.1",
45
+ "@sxzz/test-utils": "^0.5.14",
46
+ "@types/node": "^25.0.2",
47
+ "@typescript/native-preview": "7.0.0-dev.20251215.1",
41
48
  "bumpp": "^10.3.2",
42
49
  "eslint": "^9.39.2",
50
+ "magic-string-ast": "^1.0.3",
43
51
  "prettier": "^3.7.4",
44
52
  "rolldown": "1.0.0-beta.54",
45
- "tsdown": "^0.17.4",
53
+ "tsdown": "^0.18.0",
46
54
  "typescript": "^5.9.3",
47
55
  "vitest": "^4.0.15"
48
56
  },