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

chore(internal): copy over estimate-bytes improvements from glint/v5 … #536

Merged
merged 1 commit into from
Jun 18, 2022
Merged
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ tsconfig.tsbuildinfo
node_modules/

# misc
.vscode
.env*
.pnp*
.sass-cache
Expand Down
55 changes: 50 additions & 5 deletions build/estimate-bytes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,32 @@ import filesize from 'filesize';
const __dirname = path.dirname(fileURLToPath(import.meta.url));
const root = path.join(__dirname, '../..');
const dist = path.join(root, 'ember-resources/dist');

const bundlePatterns = ['core/index.js', 'util/*.js'];

const packageJsonPath = path.join(root, 'ember-resources/package.json');

let packageJson;

const config = {
bundles: {
'index.js': {
alias: '.',
// Uncomment in v5
// nest: ['core/index.js', 'util/function-resource.js']
},
'core/index.js': { alias: 'core' },
'util/*.js': {},
}
}
/**
* 1. Create bundles
* 2. Minify
* 3. Find gzip + brotli sizes
*/
async function collectStats() {
packageJson = JSON.parse((await fs.readFile(packageJsonPath)).toString());

let { path: tmp } = await tmpDir();

let bundlePatterns = Object.keys(config.bundles);
let originalDistPaths = await globby(bundlePatterns.map((p) => path.join(dist, p)));
let stats = {};

Expand All @@ -44,10 +59,28 @@ async function collectStats() {
output += '| | js | min | min + gzip | min + brotli |\n';
output += '|--| -- | --- | ---------- | ------------ |\n';

for (let [file, fileStats] of Object.entries(stats)) {
let rowFor = (file, fileStats, indent = '') => {
let { js, 'js.min': min, 'js.min.br': brotli, 'js.min.gz': gzip } = fileStats;

output += `| ${file} | ${js} | ${min} | ${gzip} | ${brotli} |\n`;
return `| ${indent}${file} | ${js} | ${min} | ${gzip} | ${brotli} |\n`;
}

for (let [file, fileStats] of Object.entries(stats)) {
output += rowFor(file, fileStats);

let relativeFile = file.replace(/^\//, '');
if (config.bundles[relativeFile]?.nest) {
let nest = config.bundles[relativeFile].nest;

for (let nested of nest) {
if (stats['/' + nested]) {
let isLast = nest.indexOf(nested) === nest.length - 1;
let glyph = isLast ? '└── ' : '├── ';

output += rowFor(nested, stats['/' + nested], glyph);
}
}
}
}

console.debug(output);
Expand All @@ -56,6 +89,11 @@ async function collectStats() {
}

async function bundle(entry, outFile) {
let externals = [
...Object.keys(packageJson.dependencies || {}),
...Object.keys(packageJson.peerDependencies || {})
];

/**
* Utils are one file
*/
Expand All @@ -67,10 +105,17 @@ async function bundle(entry, outFile) {
outfile: outFile,
bundle: true,
external: [
...externals,
'ember',
'@ember/application',
'@ember/debug',
'@ember/helper',
'@ember/destroyable',
'@ember/object',
'@ember/runloop',
'@ember/test',
'@embroider/macros',
'@glimmer/component',
'@glimmer/tracking',
],
});
Expand Down