Skip to content

Instantly share code, notes, and snippets.

@lifeart
Created December 2, 2021 10:46
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lifeart/f493b48f227cf9566b1f41221296f1bb to your computer and use it in GitHub Desktop.
Save lifeart/f493b48f227cf9566b1f41221296f1bb to your computer and use it in GitHub Desktop.
Ember-auto-import chunk recorder (in-repo-addon)
'use strict';
// eslint-disable-next-line node/no-extraneous-require
const { MergeTrees } = require('broccoli-merge-trees');
// eslint-disable-next-line node/no-extraneous-require
const Plugin = require('broccoli-plugin');
const fs = require('fs');
const path = require('path');
class ChunksWriter extends Plugin {
constructor(inputNodes, options = {}) {
super(inputNodes, {
annotation: options.annotation,
// see `options` in the below README to see a full list of constructor options
});
this.inserter = options.inserter;
}
build() {
const content = JSON.stringify(this.inserter.categorizeChunks(), null, 2);
fs.writeFileSync(
path.join(this.outputPath, 'chunks.json'),
content,
'utf8'
);
}
}
module.exports = {
name: require('./package').name,
isDevelopingAddon() {
return true;
},
postprocessTree(which, tree) {
if (which === 'all') {
if (!tree.inputNodes) {
throw new Error(
'Unknown build tree configuration, this addon should follow exactly after ember-auto-import'
);
}
const inserter = tree.inputNodes.find(
(node) => node._name === 'Inserter'
);
const chunkWriterInstance = new ChunksWriter(['app'], { inserter });
return new MergeTrees([tree, chunkWriterInstance]);
}
return tree;
},
};
{
"name": "auto-hook",
"keywords": [
"ember-addon"
],
"ember-addon": {
"after": "ember-auto-import",
"before": "broccoli-asset-rev"
}
}
@nag5000
Copy link

nag5000 commented Dec 7, 2021

I've done something similar using the same approach (using Inserter):
https://gist.github.com/nag5000/fb55655fb9eb99c28226984048a984f0

  • custom index.html files to process
  • custom <script> transforms when using insertScriptsAt slots

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment