Skip to content

Commit

Permalink
Merge pull request #611 from preactjs/chore/bump-rollup-plugin-visual…
Browse files Browse the repository at this point in the history
…izer
  • Loading branch information
marvinhagemeister committed Aug 30, 2021
2 parents 6e8d710 + 3b1f456 commit 494f933
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 33 deletions.
5 changes: 5 additions & 0 deletions .changeset/eleven-parents-beg.md
@@ -0,0 +1,5 @@
---
'wmr': patch
---

Updates the bundle visualizer to latest, allowing for some useful output exclusions
4 changes: 2 additions & 2 deletions packages/wmr/package.json
Expand Up @@ -76,9 +76,9 @@
"posthtml": "0.13.1",
"puppeteer": "^10.0.0",
"resolve.exports": "^1.0.2",
"rollup": "^2.43.1",
"rollup": "^2.52.7",
"rollup-plugin-preserve-shebang": "^1.0.1",
"rollup-plugin-visualizer": "^4.2.2",
"rollup-plugin-visualizer": "^5.5.2",
"sade": "^1.7.3",
"sass": "^1.34.1",
"semver": "^7.3.2",
Expand Down
35 changes: 16 additions & 19 deletions packages/wmr/rollup.config.js
Expand Up @@ -119,26 +119,23 @@ const config = {
// rather than bundleds as fs.readFile()
name: 'fix-visualizer',
transform(code, id) {
if (/rollup-plugin-visualizer[/\\]plugin[/\\]build-stats\.js$/.test(id)) {
code = code.replace(
/fs\.readFile\(path\.join\(__dirname,\s*(.+?)\)\s*,\s*"utf8"\s*\)/g,
(str, stringifiedJoin) => {
const path = require('path');
const fs = require('fs');
const filePathParts = stringifiedJoin
.replace(/['"`]+/g, '')
.replace(/\$\{template\}/g, 'treemap')
.split(', ');
const filepath = path.resolve(path.dirname(id), ...filePathParts);
try {
const text = fs.readFileSync(filepath, 'utf-8');
return `Promise.resolve(${JSON.stringify(text)})`;
} catch (err) {
this.warn(`Failed to inline ${filepath} into ${id}:\n${err.message}`);
return `Promise.reject(Error(${JSON.stringify(err.message)}))`;
}
if (/rollup-plugin-visualizer[/\\]dist[/\\]plugin[/\\]build-stats\.js$/.test(id)) {
code = code.replace(/fs.*readFile.*\(__dirname,\s*(.+?)\)\s*,\s*"utf8"\s*\)/g, (_str, stringifiedJoin) => {
const path = require('path');
const fs = require('fs');
const filePathParts = stringifiedJoin
.replace(/['"`]+/g, '')
.replace(/\$\{template\}/g, 'treemap')
.split(', ');
const filepath = path.resolve(path.dirname(id), ...filePathParts);
try {
const text = fs.readFileSync(filepath, 'utf-8');
return `Promise.resolve(${JSON.stringify(text)})`;
} catch (err) {
this.warn(`Failed to inline ${filepath} into ${id}:\n${err.message}`);
return `Promise.reject(Error(${JSON.stringify(err.message)}))`;
}
);
});
return { code, map: null };
}
}
Expand Down
9 changes: 8 additions & 1 deletion packages/wmr/src/lib/plugins.js
Expand Up @@ -94,6 +94,13 @@ export function getPlugins(options) {
production && optimizeGraphPlugin({ publicPath }),
minify && minifyCssPlugin({ sourcemap }),
production && copyAssetsPlugin({ root, mergedAssets }),
production && visualize && visualizer({ open: true, gzipSize: true, brotliSize: true })
production &&
visualize &&
(visualizer.default || visualizer)({
// Don't open in unit tests
open: process.env.NODE_ENV !== 'test',
gzipSize: true,
brotliSize: true
})
].filter(Boolean);
}
2 changes: 1 addition & 1 deletion packages/wmr/src/plugins/npm-plugin/index.js
Expand Up @@ -103,7 +103,7 @@ export default function npmPlugin({ publicPath = '/@npm', prefix = 'npm/', exter
const versionTag = emitVersion && meta.version ? '@' + meta.version : '';
id = `${meta.module}${versionTag}${meta.path ? '/' + meta.path : ''}`;

return { id: `${publicPath}/${id}`, external: true };
return { id: `${publicPath}/${id}`, external: 'absolute' };
}

// Compute the final path
Expand Down
22 changes: 22 additions & 0 deletions packages/wmr/test/production.test.js
Expand Up @@ -2,6 +2,7 @@
import path from 'path';
import { promises as fs } from 'fs';
import { setupTest, teardown, runWmr, loadFixture, serveStatic, withLog, updateFile } from './test-helpers.js';
import { pathToFileURL } from 'url';
import { printCoverage, analyzeTrace } from './tracing-helpers.js';

jest.setTimeout(30000);
Expand Down Expand Up @@ -910,4 +911,25 @@ describe('production', () => {
expect(chunks).toEqual(['index.html']);
});
});

describe('--visualize', () => {
it('should create stats.html', async () => {
await loadFixture('simple', env);
instance = await runWmr(env.tmp.path, 'build', '--visualize');
const code = await instance.done;

await withLog(instance.output, async () => {
expect(code).toBe(0);

const stats = path.join(env.tmp.path, 'stats.html');
const statsUrl = pathToFileURL(stats);

await env.page.goto(statsUrl.href, {
waitUntil: ['networkidle0', 'load']
});

expect(await env.page.content()).toMatch(/RollUp Visualizer/);
});
});
});
});
20 changes: 10 additions & 10 deletions yarn.lock
Expand Up @@ -4023,7 +4023,7 @@ fs.realpath@^1.0.0:
resolved "https://registry.yarnpkg.com/fs.realpath/-/fs.realpath-1.0.0.tgz#1504ad2523158caa40db4a2787cb01411994ea4f"
integrity sha1-FQStJSMVjKpA20onh8sBQRmU6k8=

fsevents@^2.1.2, fsevents@^2.1.3, fsevents@~2.3.1:
fsevents@^2.1.2, fsevents@^2.1.3, fsevents@~2.3.1, fsevents@~2.3.2:
version "2.3.2"
resolved "https://registry.yarnpkg.com/fsevents/-/fsevents-2.3.2.tgz#8a526f78b8fdf4623b709e0b975c52c24c02fd1a"
integrity sha512-xiqMQR4xAeHTuB9uWm+fFRcIOgKBMiOBP+eXiyT7jsgVCq1bkVygt00oASowB7EdtpOHaaPgKt812P9ab+DDKA==
Expand Down Expand Up @@ -7953,10 +7953,10 @@ rollup-plugin-preserve-shebang@^1.0.1:
dependencies:
magic-string "^0.25.7"

rollup-plugin-visualizer@^4.2.2:
version "4.2.2"
resolved "https://registry.yarnpkg.com/rollup-plugin-visualizer/-/rollup-plugin-visualizer-4.2.2.tgz#edeb8b3fc6f49b3c95f6cc668f4eba57c6112099"
integrity sha512-10/TsugsaQL5rdynl0lrklBngTtkRBESZdxUJy+3fN+xKqNdg5cr7JQU1OoPx4p5mhQ+nspa6EvX3qc8SsBvnA==
rollup-plugin-visualizer@^5.5.2:
version "5.5.2"
resolved "https://registry.yarnpkg.com/rollup-plugin-visualizer/-/rollup-plugin-visualizer-5.5.2.tgz#ae2130ee5ae4a2d901e764e492b71357cb95eed7"
integrity sha512-sh+P9KhuWTzeStyRA5yNZpoEFGuj5Ph34JLMa9+muhU8CneFf9L0XE4fmAwAojJLWp//uLUEyytBPSCdZEg5AA==
dependencies:
nanoid "^3.1.22"
open "^7.4.2"
Expand All @@ -7970,12 +7970,12 @@ rollup@^2.0.0, rollup@^2.39.0, rollup@^2.41.0:
optionalDependencies:
fsevents "~2.3.1"

rollup@^2.43.1:
version "2.43.1"
resolved "https://registry.yarnpkg.com/rollup/-/rollup-2.43.1.tgz#9e5c9208c2011de227ac6c93101e11dc12e88e04"
integrity sha512-kvRE6VJbiv4d8m2nGeccc3qRpzOMghAhu2KeITjyZVCjneIFLPQ3zm2Wmqnl0LcUg3FvDaV0MfKnG4NCMbiSfw==
rollup@^2.52.7:
version "2.52.7"
resolved "https://registry.yarnpkg.com/rollup/-/rollup-2.52.7.tgz#e15a8bf734f6e4c204b7cdf33521151310250cb2"
integrity sha512-55cSH4CCU6MaPr9TAOyrIC+7qFCHscL7tkNsm1MBfIJRRqRbCEY0mmeFn4Wg8FKsHtEH8r389Fz38r/o+kgXLg==
optionalDependencies:
fsevents "~2.3.1"
fsevents "~2.3.2"

rsvp@^4.8.4, rsvp@^4.8.5:
version "4.8.5"
Expand Down

0 comments on commit 494f933

Please sign in to comment.