Skip to content

Commit 3521507

Browse files
committed
feat: allow FileContentSetter to return string or SourceDescription
- Update `FileContentSetter` type to include string as a possible return value - Modify `getFileContent` function to handle both string and SourceDescription returns - Update README.md to reflect the new return type in the documentation
1 parent 9a5ae0c commit 3521507

File tree

3 files changed

+11
-6
lines changed

3 files changed

+11
-6
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ export type FileContentSpecifier = string | FileContentSetter;
5959
export type FileContentSetter = (
6060
id: string,
6161
originalCode: string
62-
) => SourceDescription;
62+
) => string | SourceDescription;
6363
```
6464

6565
details:
@@ -93,12 +93,12 @@ Values are handled in the same way as [`include`](#include)
9393

9494
### `content`
9595

96-
Type: `String` | `(id: string, originalCode: string) => SourceDescription`
96+
Type: `String` | `(id: string, originalCode: string) => string | SourceDescription`
9797
**Mandatory**
9898

9999
Specifies the content of the imported module.
100100

101-
For functions, the string value returned by the function will be passed to Rollup to change the content of the file
101+
For functions, the string or SourceDescription value returned by the function will be passed to Rollup to change the content of the file
102102
The [plugin context](https://rollupjs.org/plugin-development/#plugin-context) will be `this` for the function
103103

104104
> `SourceDescription` is a rollup interface (see more in [Rollup Docs](https://rollupjs.org/plugin-development/#transform)):

src/file-content.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,4 @@ export type FileContentSpecifier = string | FileContentSetter;
1717
export type FileContentSetter = (
1818
id: string,
1919
originalCode: string
20-
) => SourceDescription;
20+
) => string | SourceDescription;

src/utils.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,15 +47,20 @@ export function getFileContent<self>(
4747
};
4848
} else if (typeof specifier === 'function') {
4949
const ret = specifier.call(_this, id, originalCode);
50-
if (typeof ret === 'object') {
50+
if (typeof ret === 'string') {
51+
return {
52+
code: ret,
53+
map: null,
54+
};
55+
} else if (typeof ret === 'object') {
5156
if (typeof ret.code !== 'string')
5257
throw new TypeError(
5358
'The returned SourceDescription of the content function must have a code property'
5459
);
5560
return ret;
5661
} else
5762
throw new TypeError(
58-
'The return value of the content function must be SourceDescription'
63+
'The return value of the content function must be string | SourceDescription'
5964
);
6065
} else
6166
throw new TypeError(

0 commit comments

Comments
 (0)