@samual/rolldown-plugin-prettier 0.0.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/LICENSE +23 -0
- package/README.md +57 -0
- package/index.d.ts +29 -0
- package/index.js +10 -0
- package/package.json +40 -0
- package/rollup-plugin-prettier.js +66 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Mickael Jeanroy
|
|
4
|
+
Copyright (c) 2026 @pastelmind (https://github.com/pastelmind)
|
|
5
|
+
Copyright (c) 2026 Samual Norman <me@samual.uk> (https://samual.uk/)
|
|
6
|
+
|
|
7
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
8
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
9
|
+
in the Software without restriction, including without limitation the rights
|
|
10
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
11
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
12
|
+
furnished to do so, subject to the following conditions:
|
|
13
|
+
|
|
14
|
+
The above copyright notice and this permission notice shall be included in all
|
|
15
|
+
copies or substantial portions of the Software.
|
|
16
|
+
|
|
17
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
18
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
19
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
20
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
21
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
22
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
23
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
# Samual's Rolldown Plugin Prettier
|
|
2
|
+
Rollup plugin that can be used to run [Prettier](https://prettier.io/) on the final bundle.
|
|
3
|
+
|
|
4
|
+
## Usage
|
|
5
|
+
Add the plugin `npm install --save-dev @samual/rolldown-plugin-prettier`, then add it to your `rolldown.config.js`, for
|
|
6
|
+
example:
|
|
7
|
+
|
|
8
|
+
```js
|
|
9
|
+
import prettier from "@samual/rolldown-plugin-prettier"
|
|
10
|
+
// …
|
|
11
|
+
export default {
|
|
12
|
+
// …
|
|
13
|
+
plugins: [
|
|
14
|
+
// …
|
|
15
|
+
prettier({
|
|
16
|
+
printWidth: 120,
|
|
17
|
+
tabWidth: 4,
|
|
18
|
+
useTabs: true,
|
|
19
|
+
semi: false,
|
|
20
|
+
trailingComma: "none",
|
|
21
|
+
arrowParens: "avoid",
|
|
22
|
+
experimentalTernaries: true
|
|
23
|
+
})
|
|
24
|
+
]
|
|
25
|
+
}
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
## Plugin Options
|
|
29
|
+
### `cwd`
|
|
30
|
+
- default: `process.cwd()`
|
|
31
|
+
- type: `string`
|
|
32
|
+
- example: `prettier({ cwd: "path/to/dir" })`
|
|
33
|
+
- purpose: The directory prettier will use to find the local config
|
|
34
|
+
|
|
35
|
+
### `sourcemap`
|
|
36
|
+
- default: `null`
|
|
37
|
+
- type: `boolean | 'silent'`
|
|
38
|
+
- example: `prettier({ sourcemap: true })`
|
|
39
|
+
- purpose: Create a sourcemap if the global rollup options don't specify. Set to `"silent"` to avoid
|
|
40
|
+
`@samual/rolldown-plugin-prettier` warning.
|
|
41
|
+
|
|
42
|
+
If source map is enabled in the global rollup options, then a source map will be generated on the formatted bundle
|
|
43
|
+
(except if sourcemap are explicitely disabled in the prettier options).
|
|
44
|
+
|
|
45
|
+
Note that this may take some time since `prettier` package is not able to generate a sourcemap and this plugin must
|
|
46
|
+
compute the diff between the original bundle and the formatted result and generate the corresponding sourcemap: for
|
|
47
|
+
this reason, sourcemap are disabled by default. To enable them, simply pass `sourcemap: true`.
|
|
48
|
+
|
|
49
|
+
## Contributing
|
|
50
|
+
If you find a bug or think about enhancement, feel free to contribute and submit an issue or a pull request. See
|
|
51
|
+
`CONTRIBUTING.md`.
|
|
52
|
+
|
|
53
|
+
## Credit
|
|
54
|
+
Thank you [Mickael Jeanroy](https://github.com/mjeanroy) for making the version of this plugin that this was forked from
|
|
55
|
+
[`rollup-plugin-prettier`](https://github.com/mjeanroy/rollup-plugin-prettier).
|
|
56
|
+
|
|
57
|
+
Please see `LICENSE`.
|
package/index.d.ts
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @file Type definition for rollup-plugin-prettier
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import type { Options as PrettierOptions } from 'prettier';
|
|
6
|
+
import type { Plugin } from 'rolldown';
|
|
7
|
+
|
|
8
|
+
declare namespace prettier {
|
|
9
|
+
interface Options extends PrettierOptions {
|
|
10
|
+
/**
|
|
11
|
+
* Directory to look for a Prettier config file.
|
|
12
|
+
*
|
|
13
|
+
* If omitted, defaults to `process.cwd()`.
|
|
14
|
+
*/
|
|
15
|
+
cwd?: string;
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Whether to generate a sourcemap.
|
|
19
|
+
*
|
|
20
|
+
* Note: This may take some time because rollup-plugin-prettier diffs the
|
|
21
|
+
* output to manually generate a sourcemap.
|
|
22
|
+
*/
|
|
23
|
+
sourcemap?: boolean | 'silent';
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
declare function prettier(options?: prettier.Options): Plugin;
|
|
28
|
+
|
|
29
|
+
export = prettier;
|
package/index.js
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { RollupPluginPrettier } from "./rollup-plugin-prettier.js"
|
|
2
|
+
function rollupPluginPrettier(options) {
|
|
3
|
+
const plugin = new RollupPluginPrettier(options)
|
|
4
|
+
return {
|
|
5
|
+
name: plugin.name,
|
|
6
|
+
renderChunk: (source, chunkInfo, outputOptions) =>
|
|
7
|
+
plugin.reformat(source, { sourcemap: outputOptions.sourcemap })
|
|
8
|
+
}
|
|
9
|
+
}
|
|
10
|
+
export { rollupPluginPrettier as default }
|
package/package.json
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@samual/rolldown-plugin-prettier",
|
|
3
|
+
"version": "0.0.0",
|
|
4
|
+
"description": "Rolldown plugin for code formatting using Prettier",
|
|
5
|
+
"author": "Mickael Jeanroy <mickael.jeanroy@gmail.com>",
|
|
6
|
+
"contributors": [
|
|
7
|
+
"@pastelmind (https://github.com/pastelmind)",
|
|
8
|
+
"Samual Norman <me@samual.uk> (https://samual.uk/)"
|
|
9
|
+
],
|
|
10
|
+
"license": "MIT",
|
|
11
|
+
"homepage": "https://github.com/samualtnorman/rolldown-plugin-prettier#readme",
|
|
12
|
+
"bugs": {
|
|
13
|
+
"url": "https://github.com/samualtnorman/rolldown-plugin-prettier/issues",
|
|
14
|
+
"email": "me@samual.uk"
|
|
15
|
+
},
|
|
16
|
+
"repository": "github:samualtnorman/rolldown-plugin-config",
|
|
17
|
+
"keywords": [
|
|
18
|
+
"rolldown",
|
|
19
|
+
"rolldown-plugin",
|
|
20
|
+
"plugin",
|
|
21
|
+
"prettier"
|
|
22
|
+
],
|
|
23
|
+
"type": "module",
|
|
24
|
+
"engines": {
|
|
25
|
+
"node": "^20.10 || ^22 || >=24"
|
|
26
|
+
},
|
|
27
|
+
"exports": {
|
|
28
|
+
".": "./index.js"
|
|
29
|
+
},
|
|
30
|
+
"dependencies": {
|
|
31
|
+
"diff": "^8",
|
|
32
|
+
"lodash.hasin": "^4",
|
|
33
|
+
"lodash.isempty": "^4",
|
|
34
|
+
"lodash.isnil": "^4",
|
|
35
|
+
"lodash.omitby": "^4",
|
|
36
|
+
"magic-string": "0.30.21",
|
|
37
|
+
"prettier": "^3",
|
|
38
|
+
"rolldown": "1.0.0-rc.6"
|
|
39
|
+
}
|
|
40
|
+
}
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import path from "node:path"
|
|
2
|
+
import hasIn from "lodash.hasin"
|
|
3
|
+
import isEmpty from "lodash.isempty"
|
|
4
|
+
import isNil from "lodash.isnil"
|
|
5
|
+
import omitBy from "lodash.omitby"
|
|
6
|
+
import MagicString from "magic-string"
|
|
7
|
+
import * as diff from "diff"
|
|
8
|
+
import prettier from "prettier"
|
|
9
|
+
const OPTIONS = new Set(["sourcemap", "cwd"])
|
|
10
|
+
function resolvePrettierConfig(cwd) {
|
|
11
|
+
const fromFile = path.join(cwd, "__noop__.js")
|
|
12
|
+
return prettier.resolveConfig
|
|
13
|
+
? prettier.resolveConfig(fromFile)
|
|
14
|
+
: prettier.resolveConfigSync
|
|
15
|
+
? Promise.resolve(prettier.resolveConfigSync(cwd))
|
|
16
|
+
: Promise.resolve(null)
|
|
17
|
+
}
|
|
18
|
+
var RollupPluginPrettier = class {
|
|
19
|
+
constructor(options = {}) {
|
|
20
|
+
this.name = "rollup-plugin-prettier"
|
|
21
|
+
this._options = Promise.resolve(omitBy(options, (value, key) => OPTIONS.has(key)))
|
|
22
|
+
const cwd = hasIn(options, "cwd") ? options.cwd : process.cwd()
|
|
23
|
+
this._options = Promise.all([resolvePrettierConfig(cwd), this._options]).then(results =>
|
|
24
|
+
Object.assign({}, ...results.map(result => result || {}))
|
|
25
|
+
)
|
|
26
|
+
this._options = this._options.then(opts => (isEmpty(opts) ? void 0 : opts))
|
|
27
|
+
this._sourcemap = hasIn(options, "sourcemap") ? options.sourcemap : null
|
|
28
|
+
}
|
|
29
|
+
getSourcemap() {
|
|
30
|
+
return this._sourcemap
|
|
31
|
+
}
|
|
32
|
+
enableSourcemap() {
|
|
33
|
+
this._sourcemap = !0
|
|
34
|
+
}
|
|
35
|
+
reformat(source, outputOptions) {
|
|
36
|
+
return this._options.then(options => this._reformat(source, outputOptions, options))
|
|
37
|
+
}
|
|
38
|
+
_reformat(source, outputOptions, options) {
|
|
39
|
+
const { sourcemap } = outputOptions || {}
|
|
40
|
+
return Promise.resolve(prettier.format(source, options)).then(output =>
|
|
41
|
+
this._processOutput(source, sourcemap, output)
|
|
42
|
+
)
|
|
43
|
+
}
|
|
44
|
+
_processOutput(source, sourcemap, output) {
|
|
45
|
+
const defaultSourcemap = !isNil(this._sourcemap) && this._sourcemap
|
|
46
|
+
if (!(isNil(sourcemap) ? defaultSourcemap : sourcemap)) return { code: output }
|
|
47
|
+
if ("silent" !== defaultSourcemap) {
|
|
48
|
+
console.warn(`[${this.name}] Sourcemap is enabled, computing diff is required`)
|
|
49
|
+
console.warn(`[${this.name}] This may take a moment (depends on the size of your bundle)`)
|
|
50
|
+
}
|
|
51
|
+
const magicString = new MagicString(source),
|
|
52
|
+
changes = diff.diffChars(source, output)
|
|
53
|
+
if (changes && changes.length > 0) {
|
|
54
|
+
let idx = 0
|
|
55
|
+
changes.forEach(part => {
|
|
56
|
+
if (part.added) {
|
|
57
|
+
magicString.prependLeft(idx, part.value)
|
|
58
|
+
idx -= part.count
|
|
59
|
+
} else part.removed && magicString.remove(idx, idx + part.count)
|
|
60
|
+
idx += part.count
|
|
61
|
+
})
|
|
62
|
+
}
|
|
63
|
+
return { code: magicString.toString(), map: magicString.generateMap({ hires: !0 }) }
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
export { RollupPluginPrettier }
|