Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Try out bundlemon #897

Draft
wants to merge 7 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions .bundlemonrc.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// @ts-check

const path = require('node:path');
const { mkdirSync, existsSync } = require('node:fs');
const { Compression } = require('bundlemon-utils');

/** @typedef {import("bundlemon/lib/main/types").Config} Config */

const packagePath = path.join(__dirname, 'ember-resources');
const manifest = require(path.join(packagePath, 'package.json'));

const jsFiles = Object.values(manifest.exports)
.map(distFile => distFile.replace(/^\.\/dist\//, ''))
.filter(distFile => distFile !== 'addon-main.cjs')
.filter(distFile => distFile !== 'index.js')
.filter(distFile => !distFile.includes('core/'));

/** @type {Config} */
module.exports = {
baseDir: path.join(__dirname, './ember-resources/dist'),
groups: [
{ path: 'index.bundled.js', friendlyName: 'index.js' },
],
files: jsFiles.map(distFile => ({ path: distFile })),
defaultCompression: Compression.Brotli,
includeCommitMessage: true,
reportOutput: [
['json', { fileName: 'bundlemon.json' }],
['github', { checkRun: true, commitStatus: true, prComment: true }],
],
};
20 changes: 20 additions & 0 deletions .github/workflows/size.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
name: Bundle Size

on:
push:
branches: [main]
pull_request:
types: [synchronize, opened, reopened]

jobs:
bundle-size:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: NullVoxPopuli/[email protected]
- name: Production Build (not shipped to npm)
run: TERSER=1 pnpm build
- name: Bundle the index.js
run: node ./dev/estimate-bytes/bundle.js
- name: Run BundleMon
run: pnpm bundlemon
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
dist/
tmp/
tsconfig.tsbuildinfo
bundlemon.json

# dependencies
node_modules/
Expand Down
12 changes: 12 additions & 0 deletions dev/estimate-bytes/bundle.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import fs from 'node:fs/promises';
import path from 'node:path';
import { fileURLToPath } from 'node:url';

import { bundle } from './index.js';

const __dirname = path.dirname(fileURLToPath(import.meta.url));
const packagePath = path.join(__dirname, '../../ember-resources');
const packageJsonPath = path.join(packagePath, 'package.json');
const packageJson = JSON.parse((await fs.readFile(packageJsonPath)).toString());

await bundle(path.join(packagePath, 'dist/index.js'), path.join(packagePath, 'dist/index.bundled.js'));
16 changes: 14 additions & 2 deletions dev/estimate-bytes/index.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
import fs from 'fs/promises';
import path from 'path';
import { fileURLToPath } from 'url';
import url from 'url';
import * as terser from 'terser';
import { globby } from 'globby';
import esbuild from 'esbuild';
import { createRequire } from "node:module"
import { dir as tmpDir } from 'tmp-promise';
import { gzip } from 'gzip-cli';
import { filesize } from 'filesize';

const require = createRequire(import.meta.url)
const __dirname = path.dirname(fileURLToPath(import.meta.url));
const root = path.join(__dirname, '../..');
const dist = path.join(root, 'ember-resources/dist');
Expand Down Expand Up @@ -99,7 +102,9 @@ async function collectStats() {
await fs.writeFile(path.join(__dirname, 'comment.txt'), output);
}

async function bundle(entry, outFile) {
export async function bundle(entry, outFile) {
packageJson ||= JSON.parse((await fs.readFile(packageJsonPath)).toString());

let externals = [
...Object.keys(packageJson.dependencies || {}),
...Object.keys(packageJson.peerDependencies || {})
Expand Down Expand Up @@ -162,4 +167,11 @@ async function statsFor(outFile, { tmp }) {
return result;
}

collectStats();

if (import.meta.url.startsWith('file:')) { // (A)
const modulePath = url.fileURLToPath(import.meta.url);
if (process.argv[1] === modulePath) { // (B)
// Main ESM module
collectStats();
}
}
1 change: 1 addition & 0 deletions ember-resources/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@
"@glimmer/tracking": "^1.1.2",
"@glint/template": "^1.0.0-beta.3",
"@nullvoxpopuli/eslint-configs": "^3.0.0",
"@rollup/plugin-terser": "^0.4.2",
"@tsconfig/ember": "^2.0.0",
"@types/ember__application": "^4.0.0",
"@types/ember__component": "^4.0.8",
Expand Down
13 changes: 13 additions & 0 deletions ember-resources/rollup.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import ts from 'rollup-plugin-ts';
import { Addon } from '@embroider/addon-dev/rollup';
import copy from 'rollup-plugin-copy';
import { defineConfig } from 'rollup';
import terser from '@rollup/plugin-terser';

const addon = new Addon({
srcDir: 'src',
Expand All @@ -26,6 +27,18 @@ export default defineConfig({
hoistTransitiveImports: false,
},
plugins: [
// Used for bundle impact estimation
// not shipped to npm.
...(process.env['TERSER']
? [
terser({
ecma: 2016,
module: true,
toplevel: true,
}),
]
: []),

// These are the modules that users should be able to import from your
// addon. Anything not listed here may get optimized away.
addon.publicEntrypoints(['**/*.ts']),
Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
"@changesets/changelog-github": "^0.4.8",
"@changesets/cli": "^2.26.0",
"@nullvoxpopuli/eslint-configs": "^3.1.3",
"bundlemon": "^2.0.1",
"bundlemon-utils": "^1.2.0",
"concurrently": "^8.0.0",
"eslint": "^8.35.0",
"loader.js": "^4.7.0",
Expand Down
Loading