@@ -33,6 +33,7 @@ describe('htmlbars-inline-precompile', function () {
33
33
} ) ;
34
34
35
35
afterEach ( function ( ) {
36
+ sharedStateToBabel = null ;
36
37
sinon . restore ( ) ;
37
38
} ) ;
38
39
@@ -654,13 +655,23 @@ describe('htmlbars-inline-precompile', function () {
654
655
) ;
655
656
} ) ;
656
657
658
+ let sharedStateToBabel = null as any ;
659
+
657
660
let expressionTransform : ExtendedPluginBuilder = ( env ) => {
658
661
return {
659
662
name : 'expression-transform' ,
660
663
visitor : {
661
664
PathExpression ( node , path ) {
662
665
if ( node . original === 'onePlusOne' ) {
666
+ let boundName = sharedStateToBabel ?. boundName ;
667
+ if ( boundName ) {
668
+ env . meta . jsutils . bindVariable ( boundName ) ;
669
+ return env . syntax . builders . path ( boundName ) ;
670
+ }
663
671
let name = env . meta . jsutils . bindExpression ( '1+1' , path , { nameHint : 'two' } ) ;
672
+ if ( sharedStateToBabel ) {
673
+ sharedStateToBabel . boundName = name ;
674
+ }
664
675
return env . syntax . builders . path ( name ) ;
665
676
}
666
677
return undefined ;
@@ -1314,6 +1325,47 @@ describe('htmlbars-inline-precompile', function () {
1314
1325
` ) ;
1315
1326
} ) ;
1316
1327
1328
+ it ( 'can reuse bindings' , function ( ) {
1329
+ plugins = [
1330
+ [ HTMLBarsInlinePrecompile , { targetFormat : 'hbs' , transforms : [ expressionTransform ] } ] ,
1331
+ ] ;
1332
+
1333
+ sharedStateToBabel = { } ;
1334
+
1335
+ let transformed = transform ( stripIndent `
1336
+ import { precompileTemplate } from '@ember/template-compilation';
1337
+ import Message from 'message';
1338
+ const template = precompileTemplate('<Message @text={{onePlusOne}} />', {
1339
+ scope: () => ({
1340
+ Message
1341
+ })
1342
+ });
1343
+ const template2 = precompileTemplate('<Message @text={{onePlusOne}} />', {
1344
+ scope: () => ({
1345
+ Message
1346
+ })
1347
+ });
1348
+ ` ) ;
1349
+
1350
+ expect ( transformed ) . toEqualCode ( `
1351
+ import { precompileTemplate } from '@ember/template-compilation';
1352
+ import Message from 'message';
1353
+ let two = 1 + 1;
1354
+ const template = precompileTemplate("<Message @text={{two}} />", {
1355
+ scope: () => ({
1356
+ Message,
1357
+ two
1358
+ })
1359
+ });
1360
+ const template2 = precompileTemplate("<Message @text={{two}} />", {
1361
+ scope: () => ({
1362
+ Message,
1363
+ two
1364
+ })
1365
+ });
1366
+ ` ) ;
1367
+ } ) ;
1368
+
1317
1369
it ( 'adds new locals to preexisting renamed scope' , function ( ) {
1318
1370
plugins = [
1319
1371
[ HTMLBarsInlinePrecompile , { targetFormat : 'hbs' , transforms : [ expressionTransform ] } ] ,
@@ -1323,6 +1375,12 @@ describe('htmlbars-inline-precompile', function () {
1323
1375
import { precompileTemplate } from '@ember/template-compilation';
1324
1376
import Message$ from 'message';
1325
1377
import Label from 'label';
1378
+ const template2 = precompileTemplate('<Label /><Message />', {
1379
+ scope: () => ({
1380
+ Label,
1381
+ Message: Message$
1382
+ })
1383
+ });
1326
1384
const template = precompileTemplate('<Label /><Message @text={{onePlusOne}} />', {
1327
1385
scope: () => ({
1328
1386
Label,
@@ -1336,6 +1394,12 @@ describe('htmlbars-inline-precompile', function () {
1336
1394
import Message$ from 'message';
1337
1395
import Label from 'label';
1338
1396
let two = 1 + 1;
1397
+ const template2 = precompileTemplate("<Label /><Message />", {
1398
+ scope: () => ({
1399
+ Label,
1400
+ Message: Message$,
1401
+ }),
1402
+ });
1339
1403
const template = precompileTemplate("<Label /><Message @text={{two}} />", {
1340
1404
scope: () => ({
1341
1405
Label,
@@ -1862,6 +1926,16 @@ describe('htmlbars-inline-precompile', function () {
1862
1926
expect ( spy . firstCall . lastArg ) . toHaveProperty ( 'locals' , [ 'foo' , 'bar' ] ) ;
1863
1927
} ) ;
1864
1928
1929
+ it ( 'correctly removes not used scope' , function ( ) {
1930
+ let spy = sinon . spy ( compiler , 'precompile' ) ;
1931
+ transform ( `
1932
+ import { precompileTemplate } from '@ember/template-compilation';
1933
+ let foo, bar;
1934
+ var compiled = precompileTemplate('<foo /><bar/>', { scope: () => ({ foo, bar, baz }) });
1935
+ ` ) ;
1936
+ expect ( spy . firstCall . lastArg ) . toHaveProperty ( 'locals' , [ 'foo' , 'bar' ] ) ;
1937
+ } ) ;
1938
+
1865
1939
it ( 'does not automagically add to scope when not using implicit-scope-form' , function ( ) {
1866
1940
let spy = sinon . spy ( compiler , 'precompile' ) ;
1867
1941
transform ( `
@@ -2205,6 +2279,63 @@ describe('htmlbars-inline-precompile', function () {
2205
2279
` ) ;
2206
2280
} ) ;
2207
2281
2282
+ it ( 'works with two components' , function ( ) {
2283
+ plugins = [
2284
+ [
2285
+ HTMLBarsInlinePrecompile ,
2286
+ {
2287
+ targetFormat : 'hbs' ,
2288
+ } ,
2289
+ ] ,
2290
+ ] ;
2291
+
2292
+ let p = new Preprocessor ( ) ;
2293
+ let processed = p . process (
2294
+ `
2295
+ import Component from '@glimmer/component';
2296
+
2297
+ export default class Test extends Component {
2298
+ foo = 1;
2299
+
2300
+ <template>
2301
+ <Icon />
2302
+ </template>
2303
+ }
2304
+
2305
+ const Icon = <template>Icon</template>;
2306
+ `
2307
+ ) ;
2308
+
2309
+ let transformed = transform ( processed ) ;
2310
+
2311
+ expect ( transformed ) . toEqualCode ( `
2312
+ import Component from "@glimmer/component";
2313
+ import { precompileTemplate } from "@ember/template-compilation";
2314
+ import { setComponentTemplate } from "@ember/component";
2315
+ import templateOnly from "@ember/component/template-only";
2316
+ export default class Test extends Component {
2317
+ foo = 1;
2318
+ static {
2319
+ setComponentTemplate(
2320
+ precompileTemplate("\\n <Icon />\\n ", {
2321
+ strictMode: true,
2322
+ scope: () => ({
2323
+ Icon,
2324
+ }),
2325
+ }),
2326
+ this
2327
+ );
2328
+ }
2329
+ }
2330
+ const Icon = setComponentTemplate(
2331
+ precompileTemplate("Icon", {
2332
+ strictMode: true,
2333
+ }),
2334
+ templateOnly()
2335
+ );
2336
+ ` ) ;
2337
+ } ) ;
2338
+
2208
2339
it ( 'works for class member form with `this` references' , function ( ) {
2209
2340
plugins = [
2210
2341
[
0 commit comments