Skip to content

Commit

Permalink
Merge pull request #724 from ember-cli/inline-hbs-reform
Browse files Browse the repository at this point in the history
Inline HBS reform
  • Loading branch information
ef4 authored Oct 14, 2021
2 parents 6b69fef + 7631fca commit 2e94551
Show file tree
Hide file tree
Showing 13 changed files with 4,722 additions and 2,781 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ jobs:
strategy:
matrix:
os: [ubuntu, windows]
node-version: [10.x, 12.x, 13.x, 14.x]
node-version: [12.x, 13.x, 14.x]

steps:
- uses: actions/checkout@v1
Expand Down Expand Up @@ -89,7 +89,7 @@ jobs:
- uses: actions/checkout@v1
- uses: actions/setup-node@v1
with:
node-version: 10.x
node-version: 12.x
- name: install dependencies
run: yarn install
- name: test
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

* Ember.js v3.8 or above
* Ember CLI v3.8 or above
* Node.js v10 or above
* Node.js v12 or above

## Tagged Template Usage / Migrating from `htmlbars-inline-precompile`

Expand Down
17 changes: 0 additions & 17 deletions ember-cli-build.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,23 +40,6 @@ module.exports = function (defaults) {
],
],
},

'ember-cli-htmlbars': {
// This is an option intended to be used only be `ember-template-imports`.
// DO NOT USE THIS
_customInlineModules: {
'ember-template-imports': {
export: 'hbs',
useTemplateLiteralProposalSemantics: 1,
},

'TEMPLATE-TAG-MODULE': {
export: 'GLIMMER_TEMPLATE',
debugName: '<template>',
useTemplateTagProposalSemantics: 1,
},
},
},
});

/*
Expand Down
7 changes: 0 additions & 7 deletions lib/ember-addon-main.js
Original file line number Diff line number Diff line change
Expand Up @@ -173,11 +173,6 @@ module.exports = {
let checker = new VersionChecker(this.parent).for('ember-source', 'npm');
this._requiresModuleApiPolyfill = checker.exists() && checker.lt('3.27.0-alpha.1');

// This is an option intended to be used only be `ember-template-imports`.
// DO NOT USE THIS
let customModules =
addonOptions['ember-cli-htmlbars'] && addonOptions['ember-cli-htmlbars']._customInlineModules;

// add the babel-plugin-htmlbars-inline-precompile to the list of plugins
// used by `ember-cli-babel` addon
if (!utils.isInlinePrecompileBabelPluginRegistered(babelPlugins)) {
Expand All @@ -194,7 +189,6 @@ module.exports = {
this.projectConfig(),
templateCompilerPath,
isProduction,
customModules,
this._requiresModuleApiPolyfill
);

Expand All @@ -209,7 +203,6 @@ module.exports = {
isProduction,
projectConfig: this.projectConfig(),
templateCompilerPath,
modules: customModules,
requiresModuleApiPolyfill: this._requiresModuleApiPolyfill,
});

Expand Down
45 changes: 27 additions & 18 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,7 @@ function isInlinePrecompileBabelPluginRegistered(plugins) {
typeof plugin === 'object' &&
plugin._parallelBabel !== undefined
) {
return (
plugin._parallelBabel.requireFile === require.resolve('./require-from-worker') &&
typeof plugin._parallelBabel.params === 'object' &&
typeof plugin._parallelBabel.params.modules === 'object' &&
plugin._parallelBabel.params.modules['ember-cli-htmlbars'] === 'hbs'
);
return plugin._parallelBabel.requireFile === require.resolve('./require-from-worker');
} else {
return false;
}
Expand All @@ -62,7 +57,6 @@ function buildParalleizedBabelPlugin(
projectConfig,
templateCompilerPath,
isProduction,
customModules,
requiresModuleApiPolyfill
) {
let parallelBabelInfo = {
Expand All @@ -73,7 +67,6 @@ function buildParalleizedBabelPlugin(
isProduction,
projectConfig,
parallelConfigs: pluginInfo.parallelConfigs,
modules: Object.assign({}, customModules, INLINE_PRECOMPILE_MODULES),
requiresModuleApiPolyfill,
},
};
Expand Down Expand Up @@ -262,16 +255,32 @@ function setup(pluginInfo, options) {
cacheKey;
};

let plugin = [
require.resolve('babel-plugin-htmlbars-inline-precompile'),
{
precompile,
isProduction: options.isProduction,
ensureModuleApiPolyfill: options.requiresModuleApiPolyfill,
modules: Object.assign({}, options.modules, INLINE_PRECOMPILE_MODULES),
},
'ember-cli-htmlbars:inline-precompile',
];
let plugin;
if (options.requiresModuleApiPolyfill) {
plugin = [
require.resolve('babel-plugin-htmlbars-inline-precompile'),
{
precompile,
isProduction: options.isProduction,
ensureModuleApiPolyfill: options.requiresModuleApiPolyfill,
modules: INLINE_PRECOMPILE_MODULES,
},
'ember-cli-htmlbars:inline-precompile',
];
} else {
plugin = [
require.resolve('babel-plugin-ember-template-compilation'),
{
precompile,
enableLegacyModules: [
'ember-cli-htmlbars',
'ember-cli-htmlbars-inline-precompile',
'htmlbars-inline-precompile',
],
},
'ember-cli-htmlbars:inline-precompile',
];
}

return plugin;
}
Expand Down
12 changes: 6 additions & 6 deletions node-tests/colocated-babel-plugin-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,18 +52,18 @@ describe('ColocatedBabelPlugin', function () {
import _applyDecoratedDescriptor from "@babel/runtime/helpers/esm/applyDecoratedDescriptor";
import _initializerWarningHelper from "@babel/runtime/helpers/esm/initializerWarningHelper";
var _class, _descriptor, _temp;
var _class, _descriptor;
import Component from '@glimmer/component';
const __COLOCATED_TEMPLATE__ = 'ok';
let MyComponent = (_class = (_temp = class MyComponent extends Component {
let MyComponent = (_class = class MyComponent extends Component {
constructor(...args) {
super(...args);
_initializerDefineProperty(this, "data", _descriptor, this);
}
}, _temp), (_descriptor = _applyDecoratedDescriptor(_class.prototype, "data", [tracked], {
}, (_descriptor = _applyDecoratedDescriptor(_class.prototype, "data", [tracked], {
configurable: true,
enumerable: true,
writable: true,
Expand Down Expand Up @@ -224,19 +224,19 @@ describe('ColocatedBabelPlugin', function () {
import _applyDecoratedDescriptor from "@babel/runtime/helpers/esm/applyDecoratedDescriptor";
import _initializerWarningHelper from "@babel/runtime/helpers/esm/initializerWarningHelper";
var _class, _descriptor, _temp;
var _class, _descriptor;
import { setComponentTemplate as _setComponentTemplate } from "@ember/component";
import Component from '@glimmer/component';
const __COLOCATED_TEMPLATE__ = 'ok';
let MyComponent = (_class = (_temp = class MyComponent extends Component {
let MyComponent = (_class = class MyComponent extends Component {
constructor(...args) {
super(...args);
_initializerDefineProperty(this, "data", _descriptor, this);
}
}, _temp), (_descriptor = _applyDecoratedDescriptor(_class.prototype, "data", [tracked], {
}, (_descriptor = _applyDecoratedDescriptor(_class.prototype, "data", [tracked], {
configurable: true,
enumerable: true,
writable: true,
Expand Down
14 changes: 9 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
"test:node:debug": "mocha debug node-tests/*.js"
},
"dependencies": {
"babel-plugin-ember-template-compilation": "^1.0.0",
"@ember/edition-utils": "^1.2.0",
"babel-plugin-htmlbars-inline-precompile": "^5.3.0",
"broccoli-debug": "^0.6.5",
Expand All @@ -57,6 +58,7 @@
"@babel/plugin-transform-typescript": "^7.10.1",
"@babel/runtime": "^7.13.8",
"@ember/optional-features": "^2.0.0",
"@ember/test-helpers": "^2.4.2",
"babel-eslint": "^10.1.0",
"babel-plugin-debug-macros": "^0.3.3",
"broccoli-merge-trees": "^4.2.0",
Expand All @@ -65,16 +67,16 @@
"co": "^4.6.0",
"console-ui": "^3.1.2",
"core-object": "^3.1.5",
"ember-auto-import": "^2.2.0",
"ember-cli": "~3.25.2",
"ember-cli-app-version": "^4.0.0",
"ember-cli-babel": "^7.25.0",
"ember-cli-dependency-checker": "^3.2.0",
"ember-cli-inject-live-reload": "^2.1.0",
"ember-compatibility-helpers": "^1.2.2",
"ember-export-application-global": "^2.0.1",
"ember-load-initializers": "^2.1.1",
"ember-maybe-import-regenerator": "^0.1.6",
"ember-qunit": "^4.6.0",
"ember-qunit": "^5.1.4",
"ember-resolver": "^8.0.2",
"ember-source": "~3.25.0",
"ember-source-channel-url": "^3.0.0",
Expand All @@ -91,12 +93,14 @@
"mocha": "^8.4.0",
"module-name-inliner": "link:./tests/dummy/lib/module-name-inliner",
"prettier": "^2.3.2",
"qunit-dom": "^1.6.0",
"qunit": "^2.17.2",
"qunit-dom": "^2.0.0",
"release-it": "^14.2.1",
"release-it-lerna-changelog": "^3.1.0"
"release-it-lerna-changelog": "^3.1.0",
"webpack": "^5.58.2"
},
"engines": {
"node": "10.* || >= 12.*"
"node": ">= 12.*"
},
"publishConfig": {
"registry": "https://registry.npmjs.org"
Expand Down
39 changes: 0 additions & 39 deletions tests/dummy/lib/module-name-inliner/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ module.exports = {
setupPreprocessorRegistry(type, registry) {
// can only add the plugin with this style on newer Ember versions
registry.add('htmlbars-ast-plugin', this.buildPlugin());
registry.add('htmlbars-ast-plugin', this.buildLegacyPlugin());
},

buildPlugin() {
Expand Down Expand Up @@ -44,42 +43,4 @@ module.exports = {
},
};
},

// this type of plugin has worked since at least Ember 2.4+
buildLegacyPlugin() {
return {
name: 'module-name-inliner',
baseDir() {
return __dirname;
},
parallelBabel: {
requireFile: __filename,
buildUsing: 'buildLegacyPlugin',
params: {},
},

plugin: class LegacyPlugin {
constructor(options) {
this.options = options;
}

transform(ast) {
let { meta } = this.options;
let b = this.syntax.builders;

this.syntax.traverse(ast, {
// replacing the mustache with text, like this
// {{module-name-reverser}} -> `some-module-name`
MustacheStatement(node) {
if (node.path.original === 'module-name-reverser') {
return b.text(meta.moduleName.split('').reverse().join(''));
}
},
});

return ast;
}
},
};
},
};
7 changes: 7 additions & 0 deletions tests/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,13 @@
{{content-for "body"}}
{{content-for "test-body"}}

<div id="qunit"></div>
<div id="qunit-fixture">
<div id="ember-testing-container">
<div id="ember-testing"></div>
</div>
</div>

<script src="/testem.js" integrity=""></script>
<script src="{{rootURL}}assets/vendor.js"></script>
<script src="{{rootURL}}assets/test-support.js"></script>
Expand Down
9 changes: 0 additions & 9 deletions tests/integration/components/ast-plugins-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,6 @@ import { hbs } from 'ember-cli-htmlbars';
module('tests/integration/components/ast-plugins-test', function (hooks) {
setupRenderingTest(hooks);

test('stand alone templates have "legacy" AST plugins ran', async function (assert) {
await render(hbs`{{x-module-name-reversed-component}}`);

assert.equal(
this.element.textContent.trim(),
'sbh.tnenopmoc-desrever-eman-eludom-x/stnenopmoc/setalpmet/ymmud'
);
});

test('stand alone templates have AST plugins ran', async function (assert) {
await render(hbs`{{x-module-name-inlined-component}}`);

Expand Down
26 changes: 0 additions & 26 deletions tests/integration/components/test-inline-precompile-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import hbsOne from 'htmlbars-inline-precompile';
import hbsTwo from 'ember-cli-htmlbars-inline-precompile';
import { hbs as hbsThree } from 'ember-cli-htmlbars';
import { precompileTemplate } from '@ember/template-compilation';
import { hbs } from 'ember-template-imports';
import { gte } from 'ember-compatibility-helpers';

module('tests/integration/components/test-inline-precompile', function (hooks) {
setupRenderingTest(hooks);
Expand Down Expand Up @@ -35,30 +33,6 @@ module('tests/integration/components/test-inline-precompile', function (hooks) {
assert.equal(this.element.textContent.trim(), 'Wheeeee');
});

if (gte('3.25.0')) {
test('template literal proposal works', async function (assert) {
// eslint-disable-next-line no-undef
const Bar = [GLIMMER_TEMPLATE('world')];

const Foo = hbs`Hello`;

await render(
precompileTemplate(`<Foo/>, <Bar/>!`, {
strictMode: true,
scope: { Foo, Bar },
})
);

assert.equal(this.element.textContent.trim(), 'Hello, world!');
});
}

test('inline templates have "legacy" AST plugins ran', async function (assert) {
await render(hbsThree('{{module-name-reverser}}', { moduleName: 'hello-template.hbs' }));

assert.equal(this.element.textContent.trim(), 'sbh.etalpmet-olleh');
});

test('inline templates have AST plugins ran', async function (assert) {
await render(hbsThree('{{module-name-inliner}}', { moduleName: 'hello-template.hbs' }));

Expand Down
4 changes: 4 additions & 0 deletions tests/test-helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ import Application from '../app';
import config from '../config/environment';
import { setApplication } from '@ember/test-helpers';
import { start } from 'ember-qunit';
import * as QUnit from 'qunit';
import { setup } from 'qunit-dom';

setup(QUnit.assert);

setApplication(Application.create(config.APP));

Expand Down
Loading

0 comments on commit 2e94551

Please sign in to comment.