Skip to content

Commit bf19e48

Browse files
committed
Format debug build using prettier
1 parent 1e852df commit bf19e48

File tree

4 files changed

+22
-10
lines changed

4 files changed

+22
-10
lines changed

package-lock.json

Lines changed: 2 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@
9999
"lodash.merge": "^4.6.2",
100100
"magic-string": "^0.25.9",
101101
"postcss": "^8.2.1",
102+
"prettier": "^2.2.1",
102103
"pretty-bytes": "^5.4.1",
103104
"rollup": "^2.35.1",
104105
"rollup-plugin-bundle-size": "^1.0.3",
@@ -131,7 +132,6 @@
131132
"jest": "^26.6.3",
132133
"lint-staged": "^10.5.3",
133134
"npm-merge-driver-install": "^1.1.1",
134-
"prettier": "^2.2.1",
135135
"regenerator-runtime": "^0.13.7",
136136
"rimraf": "^3.0.2",
137137
"shell-quote": "^1.7.2",

src/index.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ import { getConfigFromPkgJson, getName } from './lib/package-info';
4040
import { shouldCssModules, cssModulesConfig } from './lib/css-modules';
4141
import { EOL } from 'os';
4242
import MagicString from 'magic-string';
43+
import prettier from 'prettier';
4344

4445
// Extensions to use when resolving modules
4546
const EXTENSIONS = ['.ts', '.tsx', '.js', '.jsx', '.es6', '.es', '.mjs'];
@@ -660,7 +661,19 @@ function createConfig(options, entry, format, writeMeta) {
660661
// Rollup 2 injects globalThis, which is nice, but doesn't really make sense for Microbundle.
661662
// Only ESM environments necessitate globalThis, and UMD bundles can't be properly loaded as ESM.
662663
// So we remove the globalThis check, replacing it with `this||self` to match Rollup 1's output:
663-
renderChunk(code, chunk, opts) {
664+
async renderChunk(code, chunk, opts) {
665+
// Format debug builds with Prettier for better readability
666+
if (format === 'debug') {
667+
const formatted = prettier.format(code, {
668+
parser: 'babel',
669+
...pkg.prettier,
670+
});
671+
return {
672+
code: formatted,
673+
map: null,
674+
};
675+
}
676+
664677
if (opts.format === 'umd') {
665678
// Can swap this out with MagicString.replace() when we bump it:
666679
// https://github.com/developit/microbundle/blob/f815a01cb63d90b9f847a4dcad2a64e6b2f8596f/src/index.js#L657-L671

test/__snapshots__/index.test.js.snap

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1750,8 +1750,8 @@ Build \\"debug-lib\\" to dist:
17501750
279 B: debug-lib.esm.mjs.br
17511751
415 B: debug-lib.umd.js.gz
17521752
343 B: debug-lib.umd.js.br
1753-
385 B: debug-lib.debug.js.gz
1754-
320 B: debug-lib.debug.js.br"
1753+
389 B: debug-lib.debug.js.gz
1754+
323 B: debug-lib.debug.js.br"
17551755
`;
17561756
17571757
exports[`fixtures build debug with microbundle 2`] = `8`;
@@ -1779,10 +1779,11 @@ class UserManager {
17791779
}
17801780
17811781
async loadUsers() {
1782-
const allUsers = await Promise.all(this.users.map(u => fetchUserData(u.id)));
1782+
const allUsers = await Promise.all(
1783+
this.users.map((u) => fetchUserData(u.id))
1784+
);
17831785
return allUsers;
17841786
}
1785-
17861787
}
17871788
17881789
export { UserManager, createGreeting, fetchUserData };

0 commit comments

Comments
 (0)