Skip to content
Merged
Show file tree
Hide file tree
Changes from 6 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
44 changes: 44 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -160,3 +160,47 @@ var templateTree = new HtmlbarsCompiler('app/templates', {
templateCompiler: require('./bower_components/ember/ember-template-compiler')
});
```

## Extending or Wrapping the Broccoli Plugin within an Ember CLI Addon

In some rare circumstances, it may be necessary for an ember-cli addon to extend the broccoli plugin used by this ember-cli addon to compile templates. For instance, the addon may want to write additional output files based on processing that was done by one of the AST plugins.

To accomplish this, the `getTemplateCompiler(inputTree, htmlbarsOptions)` method provided by the ember-cli addon can be overridden. For example:

```js
const Plugin = require('broccoli-plugin');

class MyTemplateCompilerWrapper extends Plugin {
build() { /* implementation */ }
}

module.exports = {
name: require('../package').name,

getTemplateCompiler(inputTree, htmlbarsOptions) {
const TemplateCompiler = require('./template-compiler-plugin');
return new TemplateCompiler(inputTree, htmlbarsOptions);
},

findSiblingAddon(name) {
if (this.parent.findOwnAddonByName) {
return this.parent.findOwnAddonByName(name);
} else {
return this.project.findAddonByName(name);
}
},

included() {
this._super.included.apply(this, arguments);
const htmlBarsAddon = this.findSiblingAddon('ember-cli-htmlbars');
if (!htmlBarsAddon) {
throw new Error('Projects and addons that depend on my-fancy-addon must also depend on ember-cli-htmlbars.');
}
let getRealTemplateCompiler = htmlBarsAddon.getTemplateCompiler;
htmlBarsAddon.getTemplateCompiler = function(inputTree, htmlbarsOptions) {
let htmlbarsTree = getRealTemplateCompiler.call(htmlBarsAddon, inputTree, htmlbarsOptions);
return new MyTemplateCompilerWrapper(htmlbarsTree);
}
},
};
```
9 changes: 6 additions & 3 deletions lib/ember-addon-main.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ module.exports = {
return this._cachedShouldColocateTemplates;
},

getTemplateCompiler(inputTree, htmlbarsOptions) {
const TemplateCompiler = require('./template-compiler-plugin');
return new TemplateCompiler(inputTree, htmlbarsOptions);
},

setupPreprocessorRegistry(type, registry) {
// ensure that broccoli-ember-hbs-template-compiler is not processing hbs files
registry.remove('template', 'broccoli-ember-hbs-template-compiler');
Expand All @@ -83,10 +88,8 @@ module.exports = {

inputTree = debugTree(new ColocatedTemplateProcessor(inputTree), '02-colocated-output');
}

this._addon.logger.debug(`setup *.hbs compiler with ${htmlbarsOptions.pluginNames}`);
const TemplateCompiler = require('./template-compiler-plugin');
return debugTree(new TemplateCompiler(inputTree, htmlbarsOptions), '03-output');
return debugTree(this._addon.getTemplateCompiler(inputTree, htmlbarsOptions), '03-output');
},

precompile(string, options) {
Expand Down
23 changes: 23 additions & 0 deletions lib/template-compiler-plugin.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import Filter = require('broccoli-persistent-filter');
import type { ASTPluginBuilder } from '@glimmer/syntax';
import type { InputNode } from 'broccoli-node-api';
// This is a hack to get access to the abstract Filter parameters which aren't exported.
declare class Foo extends Filter { }
type PluginOptions = ConstructorParameters<typeof Foo>[1]; // this should be exported by the main import.
declare namespace TemplateCompiler {
interface HtmlBarsOptions extends PluginOptions {
plugins?: {
ast?: Array<ASTPluginBuilder>
};
}
}
declare class TemplateCompiler extends Filter {
public extensions: ['hbs', 'handlebars'];
public targetExtension: 'js';
constructor(inputTree: InputNode, options: TemplateCompiler.HtmlBarsOptions);
registeredASTPlugins(): Array<ASTPluginBuilder>;
unregisterPlugins(): void;
baseDir(): string;
optionsHash(): string;
}
export = TemplateCompiler;
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
"@ember/edition-utils": "^1.2.0",
"babel-plugin-htmlbars-inline-precompile": "^4.1.0",
"broccoli-debug": "^0.6.5",
"broccoli-persistent-filter": "^2.3.1",
"broccoli-persistent-filter": "^3.0.0",
"broccoli-plugin": "^4.0.3",
"common-tags": "^1.8.0",
"ember-cli-babel-plugin-helpers": "^1.1.0",
Expand Down
104 changes: 102 additions & 2 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1541,6 +1541,19 @@ async-disk-cache@^1.2.1:
rsvp "^3.0.18"
username-sync "^1.0.2"

async-disk-cache@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/async-disk-cache/-/async-disk-cache-2.0.0.tgz#71bd47706ffa323de3677ebe1f55f8c92e90768f"
integrity sha512-lczEl6qJmyZXfTM4K9WG7s6vpjfJpdl1T5tbYeT5Q8Jp4z5TMjixYdFkIYmH4Y+6ZsG8ZYYdT6PSwkoVvGE5ag==
dependencies:
debug "^4.1.1"
heimdalljs "^0.2.3"
istextorbinary "^2.5.1"
mkdirp "^0.5.0"
rimraf "^3.0.0"
rsvp "^4.8.5"
username-sync "^1.0.2"

async-limiter@~1.0.0:
version "1.0.1"
resolved "https://registry.yarnpkg.com/async-limiter/-/async-limiter-1.0.1.tgz#dd379e94f0db8310b08291f9d64c3209766617fd"
Expand Down Expand Up @@ -2271,6 +2284,11 @@ binary-extensions@^2.0.0:
resolved "https://registry.yarnpkg.com/binaryextensions/-/binaryextensions-2.1.2.tgz#c83c3d74233ba7674e4f313cb2a2b70f54e94b7c"
integrity sha512-xVNN69YGDghOqCCtA6FI7avYrr02mTJjOgB0/f1VPD3pJC8QEvjTKWc4epDx8AqxxA75NI0QpVM2gPJXUbE4Tg==

binaryextensions@^2.1.2:
version "2.2.0"
resolved "https://registry.yarnpkg.com/binaryextensions/-/binaryextensions-2.2.0.tgz#e7c6ba82d4f5f5758c26078fe8eea28881233311"
integrity sha512-bHhs98rj/7i/RZpCSJ3uk55pLXOItjIrh2sRQZSM6OoktScX+LxJzvlU+FELp9j3TdcddTmmYArLSGptCTwjuw==

blank-object@^1.0.1:
version "1.0.2"
resolved "https://registry.yarnpkg.com/blank-object/-/blank-object-1.0.2.tgz#f990793fbe9a8c8dd013fb3219420bec81d5f4b9"
Expand Down Expand Up @@ -2663,7 +2681,7 @@ broccoli-persistent-filter@^1.1.6, broccoli-persistent-filter@^1.4.3:
symlink-or-copy "^1.0.1"
walk-sync "^0.3.1"

broccoli-persistent-filter@^2.1.0, broccoli-persistent-filter@^2.2.1, broccoli-persistent-filter@^2.3.0, broccoli-persistent-filter@^2.3.1:
broccoli-persistent-filter@^2.1.0, broccoli-persistent-filter@^2.2.1, broccoli-persistent-filter@^2.3.0:
version "2.3.1"
resolved "https://registry.yarnpkg.com/broccoli-persistent-filter/-/broccoli-persistent-filter-2.3.1.tgz#4a052e0e0868b344c3a2977e35a3d497aa9eca72"
integrity sha512-hVsmIgCDrl2NFM+3Gs4Cr2TA6UPaIZip99hN8mtkaUPgM8UeVnCbxelCvBjUBHo0oaaqP5jzqqnRVvb568Yu5g==
Expand All @@ -2683,6 +2701,24 @@ broccoli-persistent-filter@^2.1.0, broccoli-persistent-filter@^2.2.1, broccoli-p
sync-disk-cache "^1.3.3"
walk-sync "^1.0.0"

broccoli-persistent-filter@^3.0.0:
version "3.0.0"
resolved "https://registry.yarnpkg.com/broccoli-persistent-filter/-/broccoli-persistent-filter-3.0.0.tgz#497e054e2c3bf9b7b360b8a50e7707078d39fed6"
integrity sha512-XdrtZ7K3zY0cyCg8XNTqoESdVqtFQUh1lSn7xkzaJxL4PTFjmzEtscvm6AoKQJ7rTzBBhER4rAzgqPKsy9/VIA==
dependencies:
async-disk-cache "^2.0.0"
async-promise-queue "^1.0.3"
broccoli-plugin "^4.0.3"
fs-tree-diff "^2.0.0"
hash-for-dep "^1.5.0"
heimdalljs "^0.2.1"
heimdalljs-logger "^0.1.7"
mkdirp "^0.5.1"
promise-map-series "^0.2.1"
rimraf "^3.0.0"
symlink-or-copy "^1.0.1"
sync-disk-cache "^2.0.0"

broccoli-plugin@^1.0.0, broccoli-plugin@^1.2.0, broccoli-plugin@^1.2.1, broccoli-plugin@^1.3.0:
version "1.3.1"
resolved "https://registry.yarnpkg.com/broccoli-plugin/-/broccoli-plugin-1.3.1.tgz#a26315732fb99ed2d9fb58f12a1e14e986b4fabd"
Expand Down Expand Up @@ -2716,6 +2752,19 @@ broccoli-plugin@^4.0.1, broccoli-plugin@^4.0.2, broccoli-plugin@^4.0.3:
rimraf "^3.0.0"
symlink-or-copy "^1.3.0"

broccoli-plugin@^4.0.3:
version "4.0.3"
resolved "https://registry.yarnpkg.com/broccoli-plugin/-/broccoli-plugin-4.0.3.tgz#9dcfbfb6a1b27a37cc22e65c071719ce9f92bc1e"
integrity sha512-CtAIEYq5K+4yQv8c/BHymOteuyjDAJfvy/asu4LudIWcMSS7dTn3yGI5gNBkwHG+qlRangYkHJNVAcDZMQbSVQ==
dependencies:
broccoli-node-api "^1.6.0"
broccoli-output-wrapper "^3.2.1"
fs-merger "^3.1.0"
promise-map-series "^0.2.1"
quick-temp "^0.1.3"
rimraf "^3.0.0"
symlink-or-copy "^1.3.0"

broccoli-slow-trees@^3.0.1:
version "3.0.1"
resolved "https://registry.yarnpkg.com/broccoli-slow-trees/-/broccoli-slow-trees-3.0.1.tgz#9bf2a9e2f8eb3ed3a3f2abdde988da437ccdc9b4"
Expand Down Expand Up @@ -3791,6 +3840,14 @@ editions@^1.1.1:
resolved "https://registry.yarnpkg.com/editions/-/editions-1.3.4.tgz#3662cb592347c3168eb8e498a0ff73271d67f50b"
integrity sha512-gzao+mxnYDzIysXKMQi/+M1mjy/rjestjg6OPoYTtI+3Izp23oiGZitsl9lPDPiTGXbcSIk1iJWhliSaglxnUg==

editions@^2.2.0:
version "2.3.0"
resolved "https://registry.yarnpkg.com/editions/-/editions-2.3.0.tgz#47f2d5309340bce93ab5eb6ad755b9e90ff825e4"
integrity sha512-jeXYwHPKbitU1l14dWlsl5Nm+b1Hsm7VX73BsrQ4RVwEcAQQIPFHTZAbVtuIGxZBrpdT2FXd8lbtrNBrzZxIsA==
dependencies:
errlop "^2.0.0"
semver "^6.3.0"

[email protected]:
version "1.1.1"
resolved "https://registry.yarnpkg.com/ee-first/-/ee-first-1.1.1.tgz#590c61156b0ae2f4f0255732a158b266bc56b21d"
Expand Down Expand Up @@ -4365,6 +4422,11 @@ err-code@^1.0.0:
resolved "https://registry.yarnpkg.com/err-code/-/err-code-1.1.2.tgz#06e0116d3028f6aef4806849eb0ea6a748ae6960"
integrity sha1-BuARbTAo9q70gGhJ6w6mp0iuaWA=

errlop@^2.0.0:
version "2.2.0"
resolved "https://registry.yarnpkg.com/errlop/-/errlop-2.2.0.tgz#1ff383f8f917ae328bebb802d6ca69666a42d21b"
integrity sha512-e64Qj9+4aZzjzzFpZC7p5kmm/ccCrbLhAJplhsDXQFs87XTsXwOpH4s1Io2s90Tau/8r2j9f4l/thhDevRjzxw==

error-ex@^1.3.1:
version "1.3.2"
resolved "https://registry.yarnpkg.com/error-ex/-/error-ex-1.3.2.tgz#b4ac40648107fdcdcfae242f428bea8a14d4f1bf"
Expand Down Expand Up @@ -5170,6 +5232,18 @@ fs-merger@^3.1.0:
rimraf "^2.6.3"
walk-sync "^2.0.2"

fs-merger@^3.1.0:
version "3.1.0"
resolved "https://registry.yarnpkg.com/fs-merger/-/fs-merger-3.1.0.tgz#f30f74f6c70b2ff7333ec074f3d2f22298152f3b"
integrity sha512-RZ9JtqugaE8Rkt7idO5NSwcxEGSDZpLmVFjtVQUm3f+bWun7JAU6fKyU6ZJUeUnKdJwGx8uaro+K4QQfOR7vpA==
dependencies:
broccoli-node-api "^1.7.0"
broccoli-node-info "^2.1.0"
fs-extra "^8.0.1"
fs-tree-diff "^2.0.1"
rimraf "^2.6.3"
walk-sync "^2.0.2"

fs-minipass@^2.0.0:
version "2.1.0"
resolved "https://registry.yarnpkg.com/fs-minipass/-/fs-minipass-2.1.0.tgz#7f5036fdbf12c63c169190cbe4199c852271f9fb"
Expand Down Expand Up @@ -6424,6 +6498,15 @@ [email protected]:
editions "^1.1.1"
textextensions "1 || 2"

istextorbinary@^2.5.1:
version "2.6.0"
resolved "https://registry.yarnpkg.com/istextorbinary/-/istextorbinary-2.6.0.tgz#60776315fb0fa3999add276c02c69557b9ca28ab"
integrity sha512-+XRlFseT8B3L9KyjxxLjfXSLMuErKDsd8DBNrsaxoViABMEZlOSCstwmw0qpoFX3+U6yWU1yhLudAe6/lETGGA==
dependencies:
binaryextensions "^2.1.2"
editions "^2.2.0"
textextensions "^2.5.0"

isurl@^1.0.0-alpha5:
version "1.0.0"
resolved "https://registry.yarnpkg.com/isurl/-/isurl-1.0.0.tgz#b27f4f49f3cdaa3ea44a0a5b7f3462e6edc39d67"
Expand Down Expand Up @@ -7279,7 +7362,8 @@ mocha@^7.1.2:
yargs-unparser "1.6.0"

"module-name-inliner@link:./tests/dummy/lib/module-name-inliner":
version "0.1.0"
version "0.0.0"
uid ""

morgan@^1.9.1:
version "1.9.1"
Expand Down Expand Up @@ -9345,6 +9429,17 @@ sync-disk-cache@^1.3.3:
rimraf "^2.2.8"
username-sync "^1.0.2"

sync-disk-cache@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/sync-disk-cache/-/sync-disk-cache-2.0.0.tgz#27a6490132424d6b7a766788176168e74a14dde5"
integrity sha512-2WtxRtXHoOxK7V48pOZaZnSBktAm1IDWZUJoK6fAeRzkjJkvBp8H3uHNjnBmhWYirfFZulJ+wuDv71znbfFGSA==
dependencies:
debug "^4.1.1"
heimdalljs "^0.2.6"
mkdirp "^0.5.0"
rimraf "^3.0.0"
username-sync "^1.0.2"

table@^5.2.3:
version "5.4.6"
resolved "https://registry.yarnpkg.com/table/-/table-5.4.6.tgz#1292d19500ce3f86053b05f0e8e7e4a3bb21079e"
Expand Down Expand Up @@ -9433,6 +9528,11 @@ text-table@^0.2.0:
resolved "https://registry.yarnpkg.com/textextensions/-/textextensions-2.5.0.tgz#e21d3831dafa37513dd80666dff541414e314293"
integrity sha512-1IkVr355eHcomgK7fgj1Xsokturx6L5S2JRT5WcRdA6v5shk9sxWuO/w/VbpQexwkXJMQIa/j1dBi3oo7+HhcA==

textextensions@^2.5.0:
version "2.6.0"
resolved "https://registry.yarnpkg.com/textextensions/-/textextensions-2.6.0.tgz#d7e4ab13fe54e32e08873be40d51b74229b00fc4"
integrity sha512-49WtAWS+tcsy93dRt6P0P3AMD2m5PvXRhuEA0kaXos5ZLlujtYmpmFsB+QvWUSxE1ZsstmYXfQ7L40+EcQgpAQ==

thenify-all@^1.0.0:
version "1.6.0"
resolved "https://registry.yarnpkg.com/thenify-all/-/thenify-all-1.6.0.tgz#1a1918d402d8fc3f98fbf234db0bcc8cc10e9726"
Expand Down