@samual/rolldown-plugin-prettier 0.0.1-7350a61 → 0.0.1-7fd8404
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/index.d.ts +30 -27
- package/index.js +32 -6
- package/package.json +1 -5
- package/rollup-plugin-prettier.js +0 -66
package/index.d.ts
CHANGED
|
@@ -1,29 +1,32 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
*/
|
|
4
|
-
|
|
5
|
-
import type { Options as PrettierOptions } from 'prettier';
|
|
6
|
-
import type { Plugin } from 'rolldown';
|
|
1
|
+
import { Options as Options$1 } from "prettier"
|
|
2
|
+
import { Plugin } from "rolldown"
|
|
7
3
|
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
}
|
|
4
|
+
//#region src/index.d.ts
|
|
5
|
+
declare const NAME = "rolldown-plugin-prettier"
|
|
6
|
+
interface Options extends Options$1 {
|
|
7
|
+
/**
|
|
8
|
+
* Directory to look for a Prettier config file.
|
|
9
|
+
*
|
|
10
|
+
* If omitted, defaults to `process.cwd()`.
|
|
11
|
+
*/
|
|
12
|
+
cwd?: string
|
|
13
|
+
/**
|
|
14
|
+
* Whether to generate a sourcemap.
|
|
15
|
+
*
|
|
16
|
+
* Note: This may take some time because rollup-plugin-prettier diffs the
|
|
17
|
+
* output to manually generate a sourcemap.
|
|
18
|
+
*/
|
|
19
|
+
sourcemap?: boolean | "silent"
|
|
25
20
|
}
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
21
|
+
declare namespace rollupPluginPrettier {
|
|
22
|
+
export { Options }
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Create rollup plugin compatible with rollup >= 1.0.0
|
|
26
|
+
*
|
|
27
|
+
* @param options Plugin options.
|
|
28
|
+
* @return Plugin instance.
|
|
29
|
+
*/
|
|
30
|
+
declare function rollupPluginPrettier(options: Options): Plugin
|
|
31
|
+
//#endregion
|
|
32
|
+
export { NAME, Options, rollupPluginPrettier as default }
|
package/index.js
CHANGED
|
@@ -1,10 +1,36 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { diffChars } from "diff"
|
|
2
|
+
import MagicString from "magic-string"
|
|
3
|
+
import path from "node:path"
|
|
4
|
+
import * as prettier from "prettier"
|
|
5
|
+
const NAME = "rolldown-plugin-prettier"
|
|
2
6
|
function rollupPluginPrettier(options) {
|
|
3
|
-
const plugin = new RollupPluginPrettier(options)
|
|
4
7
|
return {
|
|
5
|
-
name:
|
|
6
|
-
renderChunk
|
|
7
|
-
|
|
8
|
+
name: NAME,
|
|
9
|
+
async renderChunk(source, _chunkInfo, outputOptions) {
|
|
10
|
+
const prettierConfig = await prettier.resolveConfig(path.join(options.cwd ?? process.cwd())),
|
|
11
|
+
{ sourcemap, cwd, ...prettierOptions } = options,
|
|
12
|
+
mergedOptions = { ...prettierConfig, ...prettierOptions },
|
|
13
|
+
output = await prettier.format(source, Reflect.ownKeys(mergedOptions).length ? mergedOptions : void 0),
|
|
14
|
+
defaultSourcemap = options.sourcemap ?? !1
|
|
15
|
+
if (!(outputOptions?.sourcemap ?? defaultSourcemap)) return { code: output }
|
|
16
|
+
if ("silent" !== defaultSourcemap) {
|
|
17
|
+
console.warn(`[${NAME}] Sourcemap is enabled, computing diff is required`)
|
|
18
|
+
console.warn(`[${NAME}] This may take a moment (depends on the size of your bundle)`)
|
|
19
|
+
}
|
|
20
|
+
const magicString = new MagicString(source),
|
|
21
|
+
changes = diffChars(source, output)
|
|
22
|
+
if (changes && changes.length > 0) {
|
|
23
|
+
let idx = 0
|
|
24
|
+
changes.forEach(part => {
|
|
25
|
+
if (part.added) {
|
|
26
|
+
magicString.prependLeft(idx, part.value)
|
|
27
|
+
idx -= part.count
|
|
28
|
+
} else part.removed && magicString.remove(idx, idx + part.count)
|
|
29
|
+
idx += part.count
|
|
30
|
+
})
|
|
31
|
+
}
|
|
32
|
+
return { code: magicString.toString(), map: magicString.generateMap({ hires: !0 }) }
|
|
33
|
+
}
|
|
8
34
|
}
|
|
9
35
|
}
|
|
10
|
-
export { rollupPluginPrettier as default }
|
|
36
|
+
export { NAME, rollupPluginPrettier as default }
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@samual/rolldown-plugin-prettier",
|
|
3
|
-
"version": "0.0.1-
|
|
3
|
+
"version": "0.0.1-7fd8404",
|
|
4
4
|
"description": "Rolldown plugin for code formatting using Prettier",
|
|
5
5
|
"author": "Mickael Jeanroy <mickael.jeanroy@gmail.com>",
|
|
6
6
|
"contributors": [
|
|
@@ -29,10 +29,6 @@
|
|
|
29
29
|
},
|
|
30
30
|
"dependencies": {
|
|
31
31
|
"diff": "^8",
|
|
32
|
-
"lodash.hasin": "^4",
|
|
33
|
-
"lodash.isempty": "^4",
|
|
34
|
-
"lodash.isnil": "^4",
|
|
35
|
-
"lodash.omitby": "^4",
|
|
36
32
|
"magic-string": "0.30.21",
|
|
37
33
|
"prettier": "^3",
|
|
38
34
|
"rolldown": "1.0.0-rc.6"
|
|
@@ -1,66 +0,0 @@
|
|
|
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 }
|