Skip to content

Commit bc29103

Browse files
committed
release: release version
1 parent 8ff81bc commit bc29103

3 files changed

Lines changed: 192 additions & 2 deletions

File tree

README.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ Rollup plugin that can be used to run [prettier](http://npmjs.com/package/pretti
1010

1111
Install the plugin with NPM:
1212

13-
`npm install --save-dev rollup-plugin-prettier`
13+
`npm install --save-dev prettier rollup-plugin-prettier`
1414

1515
Then add it to your rollup configuration:
1616

@@ -65,6 +65,11 @@ module.exports = {
6565

6666
## ChangeLogs
6767

68+
- 1.0.0
69+
- **Breaking Change**: `prettier` dependency is now a peer dependency instead of a "direct" dependency: user of the plugin can choose to use prettier 1.x.x or prettier 2.x.x (note that this plugin should be compatible with all versions of prettier).
70+
- Support node >= 6.
71+
- Support rollup >= 1.0.0
72+
- Remove support of deprecated option (`sourceMap` was deprecated in favor of `sourcemap`).
6873
- 0.7.0
6974
- Dependency updates.
7075
- Update rollup peer dependency version.

dist/index.js

Lines changed: 185 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,185 @@
1+
/**
2+
* The MIT License (MIT)
3+
*
4+
* Copyright (c) 2017-2020 Mickael Jeanroy
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in all
14+
* copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
* SOFTWARE.
23+
*/
24+
25+
"use strict";
26+
27+
function _interopDefault(ex) {
28+
return ex && typeof ex === "object" && "default" in ex ? ex["default"] : ex;
29+
}
30+
31+
var hasIn = _interopDefault(require("lodash.hasin"));
32+
var isEmpty = _interopDefault(require("lodash.isempty"));
33+
var isNil = _interopDefault(require("lodash.isnil"));
34+
var omitBy = _interopDefault(require("lodash.omitby"));
35+
var MagicString = _interopDefault(require("magic-string"));
36+
var diff = require("diff");
37+
var prettier = _interopDefault(require("prettier"));
38+
39+
/**
40+
* The plugin options that are currently supported.
41+
* @type {Set<string>}
42+
*/
43+
44+
const OPTIONS = new Set(["sourcemap", "cwd"]);
45+
/**
46+
* The plugin.
47+
*
48+
* @class
49+
*/
50+
51+
class RollupPluginPrettier {
52+
/**
53+
* Initialize plugin & prettier.
54+
*
55+
* @param {Object} options Initalization option.
56+
*/
57+
constructor(options = {}) {
58+
// Initialize plugin name.
59+
this.name = "rollup-plugin-prettier"; // Initialize main options.
60+
61+
this._options = omitBy(options, (value, key) => OPTIONS.has(key)); // Try to resolve config file it it exists
62+
// Be careful, `resolveConfig` function does not exist on old version of prettier.
63+
64+
if (prettier.resolveConfig) {
65+
const cwd = hasIn(options, "cwd") ? options.cwd : process.cwd();
66+
const configOptions = prettier.resolveConfig.sync(cwd);
67+
68+
if (configOptions != null) {
69+
this._options = Object.assign(configOptions, this._options || {});
70+
}
71+
} // Reset empty options.
72+
73+
if (isEmpty(this._options)) {
74+
this._options = undefined;
75+
} // Check if sourcemap is enabled by default.
76+
77+
if (hasIn(options, "sourcemap")) {
78+
this._sourcemap = options.sourcemap;
79+
} else {
80+
this._sourcemap = null;
81+
}
82+
}
83+
/**
84+
* Get the `sourcemap` value.
85+
*
86+
* @return {boolean} The `sourcemap` flag value.
87+
*/
88+
89+
getSourcemap() {
90+
return this._sourcemap;
91+
}
92+
/**
93+
* Disable sourcemap.
94+
*
95+
* @return {void}
96+
*/
97+
98+
enableSourcemap() {
99+
this._sourcemap = true;
100+
}
101+
/**
102+
* Reformat source code using prettier.
103+
*
104+
* @param {string} source The source code to reformat.
105+
* @param {boolean} sourcemap If sourcemap should be generated or not.
106+
* @return {Object} The transformation result.
107+
*/
108+
109+
reformat(source, sourcemap) {
110+
const output = prettier.format(source, this._options); // Should we generate sourcemap?
111+
// The sourcemap option may be a boolean or any truthy value (such as a `string`).
112+
// Note that this option should be false by default as it may take a (very) long time.
113+
114+
const defaultSourcemap = isNil(this._sourcemap) ? false : this._sourcemap;
115+
const outputSourcemap = isNil(sourcemap) ? defaultSourcemap : sourcemap;
116+
117+
if (!outputSourcemap) {
118+
return {
119+
code: output
120+
};
121+
}
122+
123+
console.warn(
124+
`[${this.name}] Sourcemap is enabled, computing diff is required`
125+
);
126+
console.warn(
127+
`[${this.name}] This may take a moment (depends on the size of your bundle)`
128+
);
129+
const magicString = new MagicString(source);
130+
const changes = diff.diffChars(source, output);
131+
132+
if (changes && changes.length > 0) {
133+
let idx = 0;
134+
changes.forEach(part => {
135+
if (part.added) {
136+
magicString.prependLeft(idx, part.value);
137+
idx -= part.count;
138+
} else if (part.removed) {
139+
magicString.remove(idx, idx + part.count);
140+
}
141+
142+
idx += part.count;
143+
});
144+
}
145+
146+
return {
147+
code: magicString.toString(),
148+
map: magicString.generateMap({
149+
hires: true
150+
})
151+
};
152+
}
153+
}
154+
155+
/**
156+
* Create rollup plugin compatible with rollup >= 1.0.0
157+
*
158+
* @param {Object} options Plugin options.
159+
* @return {Object} Plugin instance.
160+
*/
161+
162+
function rollupPluginPrettier(options) {
163+
const plugin = new RollupPluginPrettier(options);
164+
return {
165+
/**
166+
* Plugin name (used by rollup for error messages and warnings).
167+
* @type {string}
168+
*/
169+
name: plugin.name,
170+
171+
/**
172+
* Function called by `rollup` before generating final bundle.
173+
*
174+
* @param {string} source Souce code of the final bundle.
175+
* @param {Object} chunkInfo Chunk info.
176+
* @param {Object} outputOptions Output option.
177+
* @return {Object} The result containing a `code` property and, if a enabled, a `map` property.
178+
*/
179+
renderChunk(source, chunkInfo, outputOptions) {
180+
return plugin.reformat(source, outputOptions.sourcemap);
181+
}
182+
};
183+
}
184+
185+
module.exports = rollupPluginPrettier;

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "rollup-plugin-prettier",
3-
"version": "0.7.0",
3+
"version": "1.0.0",
44
"description": "Run prettier formatter with rollup",
55
"main": "dist/index.js",
66
"author": "Mickael Jeanroy <mickael.jeanroy@gmail.com>",

0 commit comments

Comments
 (0)