Socket
Socket
Sign inDemoInstall

babel-plugin-htmlbars-inline-precompile

Package Overview
Dependencies
Maintainers
4
Versions
42
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

babel-plugin-htmlbars-inline-precompile - npm Package Compare versions

Comparing version 4.3.0 to 4.4.0

__tests__/template-literal-tests.js

423

__tests__/tests.js

@@ -53,3 +53,5 @@ 'use strict';

expect(transpiled).toMatchInlineSnapshot(`
"var compiled = Ember.HTMLBars.template(
"import { createTemplateFactory as _createTemplateFactory } from \\"@ember/template-factory\\";
var compiled = _createTemplateFactory(
/*

@@ -202,4 +204,6 @@ hello

expect(transformed).toEqual(stripIndent`
import { createTemplateFactory as _createTemplateFactory } from "@ember/template-factory";
if ('foo') {
const template = Ember.HTMLBars.template(
const template = _createTemplateFactory(
/*

@@ -223,3 +227,5 @@ hello

expect(transformed).toMatchInlineSnapshot(`
"var compiled = function () {
"import { createTemplateFactory as _createTemplateFactory } from \\"@ember/template-factory\\";
var compiled = function () {
throw new Error(\\"NOOOOOOOOOOOOOOOOOOOOOO\\");

@@ -239,4 +245,6 @@ }();"

expect(transformed).toEqual(stripIndent`
import { createTemplateFactory as _createTemplateFactory } from "@ember/template-factory";
if ('foo') {
const template = Ember.HTMLBars.template(
const template = _createTemplateFactory(
/*

@@ -275,6 +283,11 @@ hello *\\/

expect(transformed).toEqual(
'var compiled = Ember.HTMLBars.template(\n/*\n hello\n*/\n"precompiled(hello)");',
'tagged template is replaced'
);
expect(transformed).toMatchInlineSnapshot(`
"import { createTemplateFactory as _createTemplateFactory } from \\"@ember/template-factory\\";
var compiled = _createTemplateFactory(
/*
hello
*/
\\"precompiled(hello)\\");"
`);
});

@@ -289,5 +302,11 @@

expect(transformed).toEqual(
'var compiled = Ember.HTMLBars.template(\n/*\n hello\n*/\n"precompiled(hello)");'
);
expect(transformed).toMatchInlineSnapshot(`
"import { createTemplateFactory as _createTemplateFactory } from \\"@ember/template-factory\\";
var compiled = _createTemplateFactory(
/*
hello
*/
\\"precompiled(hello)\\");"
`);
});

@@ -302,5 +321,11 @@

expect(transformed).toEqual(
'var compiled = Ember.HTMLBars.template(\n/*\n hello\n*/\n"precompiled(hello)");'
);
expect(transformed).toMatchInlineSnapshot(`
"import { createTemplateFactory as _createTemplateFactory } from \\"@ember/template-factory\\";
var compiled = _createTemplateFactory(
/*
hello
*/
\\"precompiled(hello)\\");"
`);
});

@@ -321,5 +346,17 @@

let expected = `let a = Ember.HTMLBars.template(\n/*\n hello\n*/\n"precompiled(hello)");\nlet b = Ember.HTMLBars.template(\n/*\n hello\n*/\n"precompiled(hello)");`;
expect(transformed).toMatchInlineSnapshot(`
"import { createTemplateFactory as _createTemplateFactory } from \\"@ember/template-factory\\";
expect(transformed).toEqual(expected, 'tagged template is replaced');
let a = _createTemplateFactory(
/*
hello
*/
\\"precompiled(hello)\\");
let b = _createTemplateFactory(
/*
hello
*/
\\"precompiled(hello)\\");"
`);
});

@@ -344,6 +381,8 @@

expect(transformed).toMatchInlineSnapshot(`
"import { foo } from 'precompile1';
"import { createTemplateFactory as _createTemplateFactory } from \\"@ember/template-factory\\";
import { foo } from 'precompile1';
import { bar } from 'precompile2';
import baz from 'precompile3';
let a = Ember.HTMLBars.template(
let a = _createTemplateFactory(
/*

@@ -353,3 +392,4 @@ hello

\\"precompiled(hello)\\");
let b = Ember.HTMLBars.template(
let b = _createTemplateFactory(
/*

@@ -359,3 +399,4 @@ hello

\\"precompiled(hello)\\");
let c = Ember.HTMLBars.template(
let c = _createTemplateFactory(
/*

@@ -394,5 +435,17 @@ hello

let expected = `let a = Ember.HTMLBars.template(\n/*\n hello\n*/\n"precompiled(hello)");\nlet b = Ember.HTMLBars.template(\n/*\n hello\n*/\n"precompiled(hello)");`;
expect(transformed).toMatchInlineSnapshot(`
"import { createTemplateFactory as _createTemplateFactory } from \\"@ember/template-factory\\";
expect(transformed).toEqual(expected, 'tagged template is replaced');
let a = _createTemplateFactory(
/*
hello
*/
\\"precompiled(hello)\\");
let b = _createTemplateFactory(
/*
hello
*/
\\"precompiled(hello)\\");"
`);
});

@@ -464,6 +517,13 @@

expect(transformed).toEqual(
`define([], function () {\n "use strict";\n\n var compiled = Ember.HTMLBars.template(\n /*\n hello\n */\n "precompiled(hello)");\n});`,
'tagged template is replaced'
);
expect(transformed).toMatchInlineSnapshot(`
"define([\\"@ember/template-factory\\"], function (_templateFactory) {
\\"use strict\\";
var compiled = (0, _templateFactory.createTemplateFactory)(
/*
hello
*/
\\"precompiled(hello)\\");
});"
`);
});

@@ -477,6 +537,13 @@

expect(transformed).toEqual(
`define([], function () {\n "use strict";\n\n var compiled = Ember.HTMLBars.template(\n /*\n hello\n */\n "precompiled(hello)");\n});`,
'tagged template is replaced'
);
expect(transformed).toMatchInlineSnapshot(`
"define([\\"@ember/template-factory\\"], function (_templateFactory) {
\\"use strict\\";
var compiled = (0, _templateFactory.createTemplateFactory)(
/*
hello
*/
\\"precompiled(hello)\\");
});"
`);
});

@@ -491,3 +558,5 @@

expect(transformed).toMatchInlineSnapshot(`
"var compiled = Ember.HTMLBars.template(
"import { createTemplateFactory as _createTemplateFactory } from \\"@ember/template-factory\\";
var compiled = _createTemplateFactory(
/*

@@ -506,6 +575,11 @@ some emoji goes 💥

expect(transformed).toEqual(
'var compiled = Ember.HTMLBars.template(\n/*\n hello\n*/\n"precompiled(hello)");',
'tagged template is replaced'
);
expect(transformed).toMatchInlineSnapshot(`
"import { createTemplateFactory as _createTemplateFactory } from \\"@ember/template-factory\\";
var compiled = _createTemplateFactory(
/*
hello
*/
\\"precompiled(hello)\\");"
`);
});

@@ -532,2 +606,32 @@

it('works with glimmer modules', function () {
plugins[0][1].moduleOverrides = {
'@ember/component/template-only': {
default: ['templateOnlyComponent', '@glimmer/core'],
},
'@ember/template-factory': {
createTemplateFactory: ['createTemplateFactory', '@glimmer/core'],
},
'@ember/component': {
setComponentTemplate: ['setComponentTemplate', '@glimmer/core'],
},
};
let transformed = transform(stripIndent`
import hbs from 'htmlbars-inline-precompile';
const template = hbs\`hello\`;
`);
expect(transformed).toEqual(stripIndent`
import { createTemplateFactory as _createTemplateFactory } from "@glimmer/core";
const template = _createTemplateFactory(
/*
hello
*/
"precompiled(hello)");
`);
});
describe('caching', function () {

@@ -545,6 +649,11 @@ it('include `baseDir` function for caching', function () {

expect(transformed).toEqual(
'var compiled = Ember.HTMLBars.template(\n/*\n hello\n*/\n"precompiled(hello)");',
'tagged template is replaced'
);
expect(transformed).toMatchInlineSnapshot(`
"import { createTemplateFactory as _createTemplateFactory } from \\"@ember/template-factory\\";
var compiled = _createTemplateFactory(
/*
hello
*/
\\"precompiled(hello)\\");"
`);
});

@@ -573,2 +682,24 @@

});
it('works with babel-plugin-ember-modules-api-polyfill', function () {
plugins.push('babel-plugin-ember-modules-api-polyfill');
precompile = (template) => {
return `function() { return "${template}"; }`;
};
let transpiled = transform(
"import hbs from 'htmlbars-inline-precompile';\nvar compiled = hbs`hello`;"
);
expect(transpiled).toMatchInlineSnapshot(`
"var compiled = Ember.HTMLBars.template(
/*
hello
*/
function () {
return \\"hello\\";
});"
`);
});
});

@@ -596,61 +727,2 @@

describe('with Ember imports', function () {
it('adds an Ember import if useEmberModule is set to true', function () {
plugins = [
[
HTMLBarsInlinePrecompile,
{
precompile() {
return precompile.apply(this, arguments);
},
useEmberModule: true,
},
],
];
let transpiled = transform(
"import hbs from 'htmlbars-inline-precompile';\nvar compiled = hbs`hello`;"
);
expect(transpiled).toMatchInlineSnapshot(`
"import _ember from \\"ember\\";
var compiled = _ember.HTMLBars.template(
/*
hello
*/
\\"precompiled(hello)\\");"
`);
});
it('Uses existing Ember import if one exists', function () {
plugins = [
[
HTMLBarsInlinePrecompile,
{
precompile() {
return precompile.apply(this, arguments);
},
useEmberModule: true,
},
],
];
let transpiled = transform(
"import Foo from 'ember';\nimport hbs from 'htmlbars-inline-precompile';\nvar compiled = hbs`hello`;"
);
expect(transpiled).toMatchInlineSnapshot(`
"import Foo from 'ember';
var compiled = Foo.HTMLBars.template(
/*
hello
*/
\\"precompiled(hello)\\");"
`);
});
});
describe('with transformScope: true', function () {

@@ -719,157 +791,2 @@ beforeEach(() => {

});
describe('with useTemplateLiteralProposalSemantics', function () {
beforeEach(() => {
plugins = [
[
HTMLBarsInlinePrecompile,
{
precompile() {
return precompile.apply(this, arguments);
},
modules: {
'ember-template-imports': {
export: 'hbs',
useTemplateLiteralProposalSemantics: 1,
},
},
},
],
'@babel/plugin-proposal-class-properties',
];
});
it('works with templates assigned to variables', function () {
let transpiled = transform(
`
import { hbs } from 'ember-template-imports';
const Foo = hbs\`hello\`;
`
);
expect(transpiled).toMatchInlineSnapshot(`
"import { templateOnly as _templateOnly } from \\"@ember/component/template-only\\";
import { setComponentTemplate as _setComponentTemplate } from \\"@ember/component\\";
const Foo = _templateOnly(\\"foo-bar\\", \\"Foo\\");
_setComponentTemplate(Ember.HTMLBars.template(
/*
hello
*/
\\"precompiled(hello)\\"), Foo);"
`);
});
it('works with templates exported as the default', function () {
let transpiled = transform(
`
import { hbs } from 'ember-template-imports';
export default hbs\`hello\`;
`
);
expect(transpiled).toMatchInlineSnapshot(`
"import { setComponentTemplate as _setComponentTemplate } from \\"@ember/component\\";
import { templateOnly as _templateOnly } from \\"@ember/component/template-only\\";
const _fooBar = _templateOnly(\\"foo-bar\\", \\"_fooBar\\");
_setComponentTemplate(Ember.HTMLBars.template(
/*
hello
*/
\\"precompiled(hello)\\"), _fooBar);
export default _fooBar;"
`);
});
it('works with templates assigned to classes', function () {
let transpiled = transform(
`
import { hbs } from 'ember-template-imports';
class Foo {
static template = hbs\`hello\`;
}
`
);
expect(transpiled).toMatchInlineSnapshot(`
"import { setComponentTemplate as _setComponentTemplate } from \\"@ember/component\\";
class Foo {}
_setComponentTemplate(Ember.HTMLBars.template(
/*
hello
*/
\\"precompiled(hello)\\"), Foo);"
`);
});
it('correctly handles scope', function () {
let source = 'hello';
transform(
`
import { hbs } from 'ember-template-imports';
import baz from 'qux';
let foo = 123;
const bar = 456;
export default hbs\`${source}\`;
`
);
expect(optionsReceived).toEqual({
contents: source,
isProduction: undefined,
scope: ['baz', 'foo', 'bar'],
strict: true,
});
});
it('errors if used in an incorrect positions', function () {
expect(() => {
transform("import { hbs } from 'ember-template-imports';\nhbs`hello`;");
}).toThrow(
/Attempted to use `hbs` to define a template in an unsupported way. Templates defined using this helper must be:/
);
expect(() => {
transform("import { hbs } from 'ember-template-imports';\nfunc(hbs`hello`);");
}).toThrow(
/Attempted to use `hbs` to define a template in an unsupported way. Templates defined using this helper must be:/
);
expect(() => {
transform(
"import { hbs } from 'ember-template-imports';\n let Foo = class { static template = hbs`hello`; }"
);
}).toThrow(
/Attempted to use `hbs` to define a template in an unsupported way. Templates defined using this helper must be:/
);
});
it('errors if passed incorrect useTemplateLiteralProposalSemantics version', function () {
plugins[0][1].modules['ember-template-imports'].useTemplateLiteralProposalSemantics = true;
expect(() => {
transform(
`
import { hbs } from 'ember-template-imports';
const Foo = hbs\`hello\`;
`
);
}).toThrow(
/Passed an invalid version for useTemplateLiteralProposalSemantics. This option must be assign a version number. The current valid version numbers are: 1/
);
});
});
});

@@ -0,1 +1,12 @@

## v4.4.0 (2021-02-23)
#### :rocket: Enhancement
* [#339](https://github.com/ember-cli/babel-plugin-htmlbars-inline-precompile/pull/339) Add `moduleOverrides` options ([@pzuraq](https://github.com/pzuraq))
* [#338](https://github.com/ember-cli/babel-plugin-htmlbars-inline-precompile/pull/338) Refactor to use `createTemplateFactory` ([@pzuraq](https://github.com/pzuraq))
* [#336](https://github.com/ember-cli/babel-plugin-htmlbars-inline-precompile/pull/336) Implements an option to support the template tag imports proposal ([@pzuraq](https://github.com/pzuraq))
#### Committers: 1
- Chris Garrett ([@pzuraq](https://github.com/pzuraq))
## v4.3.0 (2021-02-22)

@@ -2,0 +13,0 @@

'use strict';
const { replaceTemplateLiteralProposal } = require('./src/template-literal-transform');
const { replaceTemplateTagProposal } = require('./src/template-tag-transform');
const { registerRefs } = require('./src/util');

@@ -80,3 +82,3 @@ module.exports = function (babel) {

function compileTemplate(precompile, template, emberIdentifier, _options) {
function compileTemplate(precompile, template, templateCompilerIdentifier, _options) {
let options = Object.assign({ contents: template }, _options);

@@ -107,9 +109,3 @@

return t.callExpression(
t.memberExpression(
t.memberExpression(emberIdentifier, t.identifier('HTMLBars')),
t.identifier('template')
),
[templateExpression]
);
return t.callExpression(templateCompilerIdentifier, [templateExpression]);
}

@@ -131,7 +127,23 @@

function shouldUseAutomaticScope(options) {
return options.useTemplateLiteralProposalSemantics || options.useTemplateTagProposalSemantics;
}
function shouldUseStrictMode(options) {
return (
Boolean(options.useTemplateLiteralProposalSemantics) ||
Boolean(options.useTemplateTagProposalSemantics)
);
}
function replacePath(path, state, compiled, options) {
if (options.useTemplateLiteralProposalSemantics) {
replaceTemplateLiteralProposal(t, path, state, compiled, options);
} else if (options.useTemplateTagProposalSemantics) {
replaceTemplateTagProposal(t, path, state, compiled, options);
} else {
path.replaceWith(compiled);
registerRefs(path.replaceWith(compiled), (newPath) => {
// If we use `insertRuntimeErrors` then the node won't exist
return newPath.node ? [newPath.get('callee')] : [];
});
}

@@ -146,2 +158,4 @@ }

let useEmberModule = Boolean(options.useEmberModule);
let moduleOverrides = options.moduleOverrides;
let allAddedImports = {};

@@ -154,2 +168,12 @@

if (moduleOverrides) {
let glimmerModule = moduleOverrides[moduleName];
let glimmerExport = glimmerModule && glimmerModule[exportName];
if (glimmerExport) {
exportName = glimmerExport[0];
moduleName = glimmerExport[1];
}
}
if (exportName === 'default' && moduleName === 'ember' && !useEmberModule) {

@@ -191,2 +215,3 @@ addedImports[exportName] = t.identifier('Ember');

path.unshiftContainer('body', newImport);
path.scope.registerBinding('module', path.get('body.0.specifiers.0'));
}

@@ -212,2 +237,23 @@

for (let module in modules) {
let options = modules[module];
if (options.useTemplateTagProposalSemantics) {
if (options.useTemplateLiteralProposalSemantics) {
throw path.buildCodeFrameError(
'Cannot use both the template literal and template tag syntax proposals together'
);
}
// template tags don't have an import
presentModules.set(
options.export,
Object.assign({}, options, {
modulePath: module,
originalName: options.export,
})
);
continue;
}
let paths = importDeclarations.filter(

@@ -262,3 +308,3 @@ (path) => !path.removed && path.get('source').get('value').node === module

ClassDeclaration(path, state) {
Class(path, state) {
// Processing classes this way allows us to process ClassProperty nodes

@@ -305,6 +351,6 @@ // before other transforms, such as the class-properties transform

let { precompile, isProduction } = state.opts;
let scope = options.useTemplateLiteralProposalSemantics ? getScope(path.scope) : null;
let strict = Boolean(options.useTemplateLiteralProposalSemantics);
let scope = shouldUseAutomaticScope(options) ? getScope(path.scope) : null;
let strict = shouldUseStrictMode(options);
let emberIdentifier = state.ensureImport('default', 'ember');
let emberIdentifier = state.ensureImport('createTemplateFactory', '@ember/template-factory');

@@ -395,2 +441,14 @@ replacePath(

if (shouldUseAutomaticScope(options)) {
// If using the transform semantics, then users are not expected to pass
// options, so we override any existing scope
compilerOptions.scope = getScope(path.scope);
}
if (shouldUseStrictMode(options)) {
// If using the transform semantics, then users are not expected to pass
// options, so we override any existing strict option
compilerOptions.strict = true;
}
replacePath(

@@ -402,3 +460,3 @@ path,

template,
state.ensureImport('default', 'ember'),
state.ensureImport('createTemplateFactory', '@ember/template-factory'),
compilerOptions

@@ -405,0 +463,0 @@ ),

{
"name": "babel-plugin-htmlbars-inline-precompile",
"version": "4.3.0",
"version": "4.4.0",
"description": "Babel plugin to replace tagged template strings with precompiled HTMLBars templates",

@@ -18,2 +18,3 @@ "repository": "https://github.com/ember-cli/babel-plugin-htmlbars-inline-precompile",

"@babel/plugin-transform-unicode-escapes": "^7.12.13",
"babel-plugin-ember-modules-api-polyfill": "^3.3.0",
"common-tags": "^1.8.0",

@@ -20,0 +21,0 @@ "ember-source": "^3.25.1",

const filePath = require('path');
const { registerRefs } = require('./util');

@@ -19,38 +20,53 @@ module.exports.replaceTemplateLiteralProposal = function (t, path, state, compiled, options) {

varDeclaration.insertAfter(
t.expressionStatement(
t.callExpression(state.ensureImport('setComponentTemplate', '@ember/component'), [
compiled,
varId,
registerRefs(
varDeclaration.insertAfter(
t.expressionStatement(
t.callExpression(state.ensureImport('setComponentTemplate', '@ember/component'), [
compiled,
varId,
])
)
),
(newPath) => [newPath.get('expression.callee'), newPath.get('expression.arguments.0.callee')]
);
registerRefs(
path.replaceWith(
t.callExpression(state.ensureImport('default', '@ember/component/template-only'), [
t.stringLiteral(filename),
t.stringLiteral(varId.name),
])
)
),
(newPath) => [newPath.get('callee')]
);
path.replaceWith(
t.callExpression(state.ensureImport('templateOnly', '@ember/component/template-only'), [
t.stringLiteral(filename),
t.stringLiteral(varId.name),
])
);
} else if (parentPath.node.type === 'ExportDefaultDeclaration') {
let varId = path.scope.generateUidIdentifier(filename);
parentPath.insertBefore(
t.variableDeclaration('const', [
t.variableDeclarator(
varId,
t.callExpression(state.ensureImport('templateOnly', '@ember/component/template-only'), [
t.stringLiteral(filename),
t.stringLiteral(varId.name),
registerRefs(
parentPath.insertBefore(
t.variableDeclaration('const', [
t.variableDeclarator(
varId,
t.callExpression(state.ensureImport('default', '@ember/component/template-only'), [
t.stringLiteral(filename),
t.stringLiteral(varId.name),
])
),
])
),
(newPath) => [newPath.get('declarations.0.init.callee')]
);
registerRefs(
parentPath.insertBefore(
t.expressionStatement(
t.callExpression(state.ensureImport('setComponentTemplate', '@ember/component'), [
compiled,
varId,
])
),
])
)
),
(newPath) => [newPath.get('expression.callee'), newPath.get('expression.arguments.0.callee')]
);
parentPath.insertBefore(
t.expressionStatement(
t.callExpression(state.ensureImport('setComponentTemplate', '@ember/component'), [
compiled,
varId,
])
)
);
path.replaceWith(varId);

@@ -70,19 +86,36 @@ } else if (parentPath.node.type === 'ClassProperty') {

let classDeclaration = parentPath.parentPath.parentPath;
let classPath = parentPath.parentPath.parentPath;
if (classDeclaration.node.type !== 'ClassDeclaration') {
throw path.buildCodeFrameError(
`Attempted to use \`${options.originalName}\` to define a template for an anonymous class. Templates declared with this helper must be assigned to classes which have a name.`
if (classPath.node.type === 'ClassDeclaration') {
registerRefs(
classPath.insertAfter(
t.expressionStatement(
t.callExpression(state.ensureImport('setComponentTemplate', '@ember/component'), [
compiled,
classPath.node.id,
])
)
),
(newPath) => [
newPath.get('expression.callee'),
newPath.get('expression.arguments.0.callee'),
]
);
} else {
registerRefs(
classPath.replaceWith(
t.expressionStatement(
t.callExpression(state.ensureImport('setComponentTemplate', '@ember/component'), [
compiled,
classPath.node,
])
)
),
(newPath) => [
newPath.parentPath.get('callee'),
newPath.parentPath.get('arguments.0.callee'),
]
);
}
classDeclaration.insertAfter(
t.expressionStatement(
t.callExpression(state.ensureImport('setComponentTemplate', '@ember/component'), [
compiled,
classDeclaration.node.id,
])
)
);
parentPath.remove();

@@ -89,0 +122,0 @@ } else {

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc