babel-plugin-htmlbars-inline-precompile
Advanced tools
Comparing version 4.4.0 to 4.4.1
@@ -5,2 +5,3 @@ 'use strict'; | ||
const HTMLBarsInlinePrecompile = require('../index'); | ||
const TransformModules = require('@babel/plugin-transform-modules-amd'); | ||
@@ -412,3 +413,39 @@ describe('htmlbars-inline-precompile: useTemplateLiteralProposalSemantics', function () { | ||
}); | ||
it('works when used alongside modules transform', function () { | ||
plugins[0][1].ensureModuleApiPolyfill = true; | ||
plugins.push([TransformModules]); | ||
let transpiled = transform( | ||
` | ||
import { hbs } from 'ember-template-imports'; | ||
const Foo = hbs\`hello\`; | ||
const Bar = hbs\`hello\`; | ||
` | ||
); | ||
expect(transpiled).toMatchInlineSnapshot(` | ||
"define([], function () { | ||
\\"use strict\\"; | ||
const Foo = Ember._templateOnlyComponent(\\"foo-bar\\", \\"Foo\\"); | ||
Ember._setComponentTemplate(Ember.HTMLBars.template( | ||
/* | ||
hello | ||
*/ | ||
\\"precompiled(hello)\\"), Foo); | ||
const Bar = Ember._templateOnlyComponent(\\"foo-bar\\", \\"Bar\\"); | ||
Ember._setComponentTemplate(Ember.HTMLBars.template( | ||
/* | ||
hello | ||
*/ | ||
\\"precompiled(hello)\\"), Bar); | ||
});" | ||
`); | ||
}); | ||
}); | ||
}); |
@@ -5,2 +5,3 @@ 'use strict'; | ||
const HTMLBarsInlinePrecompile = require('../index'); | ||
const TransformModules = require('@babel/plugin-transform-modules-amd'); | ||
@@ -507,3 +508,37 @@ describe('htmlbars-inline-precompile: useTemplateTagProposalSemantics', function () { | ||
}); | ||
it('works when used alongside modules transform', function () { | ||
plugins[0][1].ensureModuleApiPolyfill = true; | ||
plugins.push([TransformModules]); | ||
let transpiled = transform( | ||
` | ||
const Foo = [GLIMMER_TEMPLATE(\`hello\`)]; | ||
const Bar = [GLIMMER_TEMPLATE(\`hello\`)]; | ||
` | ||
); | ||
expect(transpiled).toMatchInlineSnapshot(` | ||
"define([], function () { | ||
\\"use strict\\"; | ||
const Foo = Ember._templateOnlyComponent(\\"foo-bar\\", \\"Foo\\"); | ||
Ember._setComponentTemplate(Ember.HTMLBars.template( | ||
/* | ||
hello | ||
*/ | ||
\\"precompiled(hello)\\"), Foo); | ||
const Bar = Ember._templateOnlyComponent(\\"foo-bar\\", \\"Bar\\"); | ||
Ember._setComponentTemplate(Ember.HTMLBars.template( | ||
/* | ||
hello | ||
*/ | ||
\\"precompiled(hello)\\"), Bar); | ||
});" | ||
`); | ||
}); | ||
}); | ||
}); |
@@ -686,2 +686,24 @@ 'use strict'; | ||
}); | ||
it('works with ensureModuleApiPolyfill', function () { | ||
plugins[0][1].ensureModuleApiPolyfill = true; | ||
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\\"; | ||
});" | ||
`); | ||
}); | ||
}); | ||
@@ -688,0 +710,0 @@ |
@@ -0,1 +1,10 @@ | ||
## v4.4.1 (2021-02-24) | ||
#### :bug: Bug Fix | ||
* [#346](https://github.com/ember-cli/babel-plugin-htmlbars-inline-precompile/pull/346) Ensure proposal syntaxes work with Ember module API polyfill ([@pzuraq](https://github.com/pzuraq)) | ||
#### Committers: 1 | ||
- Chris Garrett ([@pzuraq](https://github.com/pzuraq)) | ||
## v4.4.0 (2021-02-23) | ||
@@ -2,0 +11,0 @@ |
48
index.js
@@ -5,2 +5,3 @@ 'use strict'; | ||
const { registerRefs } = require('./src/util'); | ||
const { setupState, processImportDeclaration } = require('babel-plugin-ember-modules-api-polyfill'); | ||
@@ -148,6 +149,35 @@ module.exports = function (babel) { | ||
} | ||
if (state.opts.ensureModuleApiPolyfill) { | ||
processModuleApiPolyfill(state); | ||
} | ||
} | ||
let allAddedImports = Object.create(null); | ||
function processModuleApiPolyfill(state) { | ||
for (let module in allAddedImports) { | ||
let addedImports = allAddedImports[module]; | ||
for (let addedImport in addedImports) { | ||
let { path } = addedImports[addedImport]; | ||
if (path && path.node) { | ||
processImportDeclaration(t, path, state); | ||
if (path.removed) { | ||
delete addedImports[addedImport]; | ||
} | ||
} | ||
} | ||
} | ||
} | ||
let visitor = { | ||
Program(path, state) { | ||
if (state.opts.ensureModuleApiPolyfill) { | ||
// Setup state for the module API polyfill | ||
setupState(t, path, state); | ||
} | ||
let options = state.opts || {}; | ||
@@ -159,8 +189,6 @@ | ||
let allAddedImports = {}; | ||
state.ensureImport = (exportName, moduleName) => { | ||
let addedImports = (allAddedImports[moduleName] = allAddedImports[moduleName] || {}); | ||
if (addedImports[exportName]) return addedImports[exportName]; | ||
if (addedImports[exportName]) return addedImports[exportName].id; | ||
@@ -178,4 +206,4 @@ if (moduleOverrides) { | ||
if (exportName === 'default' && moduleName === 'ember' && !useEmberModule) { | ||
addedImports[exportName] = t.identifier('Ember'); | ||
return addedImports[exportName]; | ||
addedImports[exportName] = { id: t.identifier('Ember') }; | ||
return addedImports[exportName].id; | ||
} | ||
@@ -197,3 +225,3 @@ | ||
if (importSpecifier) { | ||
addedImports[exportName] = importSpecifier.node.local; | ||
addedImports[exportName] = [importSpecifier.node.local]; | ||
} | ||
@@ -206,3 +234,2 @@ } | ||
); | ||
addedImports[exportName] = uid; | ||
@@ -217,5 +244,10 @@ let newImportSpecifier = | ||
path.scope.registerBinding('module', path.get('body.0.specifiers.0')); | ||
addedImports[exportName] = { | ||
id: uid, | ||
path: path.get('body.0'), | ||
}; | ||
} | ||
return addedImports[exportName]; | ||
return addedImports[exportName].id; | ||
}; | ||
@@ -222,0 +254,0 @@ |
{ | ||
"name": "babel-plugin-htmlbars-inline-precompile", | ||
"version": "4.4.0", | ||
"version": "4.4.1", | ||
"description": "Babel plugin to replace tagged template strings with precompiled HTMLBars templates", | ||
@@ -12,9 +12,11 @@ "repository": "https://github.com/ember-cli/babel-plugin-htmlbars-inline-precompile", | ||
}, | ||
"dependencies": { | ||
"babel-plugin-ember-modules-api-polyfill": "^3.4.0" | ||
}, | ||
"devDependencies": { | ||
"@babel/core": "^7.12.16", | ||
"@babel/plugin-proposal-class-properties": "^7.12.13", | ||
"@babel/plugin-transform-modules-amd": "^7.12.13", | ||
"@babel/plugin-transform-template-literals": "^7.12.13", | ||
"@babel/core": "^7.13.1", | ||
"@babel/plugin-proposal-class-properties": "^7.13.0", | ||
"@babel/plugin-transform-modules-amd": "^7.13.0", | ||
"@babel/plugin-transform-template-literals": "^7.13.0", | ||
"@babel/plugin-transform-unicode-escapes": "^7.12.13", | ||
"babel-plugin-ember-modules-api-polyfill": "^3.3.0", | ||
"common-tags": "^1.8.0", | ||
@@ -28,3 +30,3 @@ "ember-source": "^3.25.1", | ||
"prettier": "^2.2.1", | ||
"release-it": "^14.4.0", | ||
"release-it": "^14.4.1", | ||
"release-it-lerna-changelog": "^3.1.0" | ||
@@ -31,0 +33,0 @@ }, |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
100155
15
2199
1
+ Addedbabel-plugin-ember-modules-api-polyfill@3.5.0(transitive)
+ Addedember-rfc176-data@0.3.18(transitive)