Skip to content

Commit 2c1ca76

Browse files
committed
Clean up template plugin compat code for Ember < 3.24
1 parent 404646f commit 2c1ca76

File tree

4 files changed

+12
-73
lines changed

4 files changed

+12
-73
lines changed

packages/ember-css-modules/index.js

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,8 @@ module.exports = {
6666
this.parentPreprocessorRegistry.add(
6767
'htmlbars-ast-plugin',
6868
HtmlbarsPlugin.instantiate({
69-
emberVersion: this.checker.for('ember-source').version,
70-
options: {
71-
fileExtension: this.getFileExtension(),
72-
includeExtensionInModulePath: this.includeExtensionInModulePath(),
73-
},
69+
fileExtension: this.getFileExtension(),
70+
includeExtensionInModulePath: this.includeExtensionInModulePath(),
7471
})
7572
);
7673
},

packages/ember-css-modules/lib/htmlbars-plugin/index.js

Lines changed: 8 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,24 @@
11
'use strict';
22

33
const utils = require('./utils');
4-
const semver = require('semver');
54

65
module.exports = class ClassTransformPlugin {
76
constructor(env, options) {
87
this.syntax = env.syntax;
98
this.builders = env.syntax.builders;
109
this.options = options;
1110
this.stylesModule = this.determineStylesModule(env);
12-
this.isGlimmer = this.detectGlimmer();
1311
this.visitor = this.buildVisitor(env);
14-
15-
// Alias for 2.15 <= Ember < 3.1
16-
this.visitors = this.visitor;
1712
}
1813

19-
static instantiate({ emberVersion, options }) {
14+
static instantiate(options) {
2015
return {
2116
name: 'ember-css-modules',
22-
plugin: semver.lt(emberVersion, '2.15.0-alpha')
23-
? LegacyAdapter.bind(null, this, options)
24-
: (env) => new this(env, options),
17+
plugin: (env) => new this(env, options),
2518
parallelBabel: {
2619
requireFile: __filename,
2720
buildUsing: 'instantiate',
28-
params: { emberVersion, options },
21+
params: options,
2922
},
3023
baseDir() {
3124
return `${__dirname}/../..`;
@@ -54,17 +47,6 @@ module.exports = class ClassTransformPlugin {
5447
return name;
5548
}
5649

57-
detectGlimmer() {
58-
if (!this.syntax.parse) {
59-
return false;
60-
}
61-
62-
// HTMLBars builds ConcatStatements with StringLiterals + raw PathExpressions
63-
// Glimmer builds ConcatStatements with TextNodes + MustacheStatements
64-
let ast = this.syntax.parse('<div class="foo {{bar}}"></div>');
65-
return ast.body[0].attributes[0].value.parts[0].type === 'TextNode';
66-
}
67-
6850
buildVisitor(env) {
6951
if (env.moduleName === env.filename) {
7052
// No-op for the stage 1 Embroider pass (which only contains relative paths)
@@ -133,7 +115,6 @@ module.exports = class ClassTransformPlugin {
133115

134116
utils.removeAttr(node, localClassAttr);
135117

136-
let stringBuilder = this.isGlimmer ? 'text' : 'string';
137118
let classAttr = utils.getAttr(node, 'class');
138119
let parts = [];
139120
let classAttrValue;
@@ -149,26 +130,18 @@ module.exports = class ClassTransformPlugin {
149130
parts.push(
150131
this.builders.mustache(
151132
this.builders.path('concat'),
152-
utils.concatStatementToParams(
153-
this.builders,
154-
classAttrValue,
155-
this.isGlimmer
156-
)
133+
utils.concatStatementToParams(this.builders, classAttrValue)
157134
)
158135
);
159136
} else if (classAttrValue.type === 'TextNode') {
160-
parts.push(this.builders[stringBuilder](classAttrValue.chars));
137+
parts.push(this.builders.text(classAttrValue.chars));
161138
} else if (classAttrValue.type === 'MustacheStatement') {
162-
if (classAttrValue.params.length || this.isGlimmer) {
163-
parts.push(classAttrValue);
164-
} else {
165-
parts.push(this.builders.path(classAttrValue.path.original));
166-
}
139+
parts.push(classAttrValue);
167140
}
168141
}
169142

170143
utils.pushAll(parts, this.localToPath(localClassAttr.value));
171-
this.divide(parts, this.isGlimmer ? 'text' : 'string');
144+
this.divide(parts, 'text');
172145
node.attributes.unshift(
173146
this.builders.attr('class', this.builders.concat(parts))
174147
);
@@ -225,11 +198,7 @@ module.exports = class ClassTransformPlugin {
225198

226199
concatLocalPath(node) {
227200
let concatPath = this.builders.path('concat');
228-
let concatParts = utils.concatStatementToParams(
229-
this.builders,
230-
node,
231-
this.isGlimmer
232-
);
201+
let concatParts = utils.concatStatementToParams(this.builders, node);
233202
let concatStatement = this.builders.mustache(concatPath, concatParts);
234203
return this.dynamicLocalPath(concatStatement);
235204
}
@@ -268,22 +237,3 @@ module.exports = class ClassTransformPlugin {
268237
return parts;
269238
}
270239
};
271-
272-
// For Ember < 2.15
273-
class LegacyAdapter {
274-
constructor(plugin, options, env) {
275-
this.plugin = plugin;
276-
this.options = options;
277-
this.meta = env.meta;
278-
this.syntax = null;
279-
}
280-
281-
transform(ast) {
282-
let plugin = new this.plugin(
283-
Object.assign({ syntax: this.syntax }, this.meta),
284-
this.options
285-
);
286-
this.syntax.traverse(ast, plugin.visitor);
287-
return ast;
288-
}
289-
}

packages/ember-css-modules/lib/htmlbars-plugin/utils.js

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,7 @@ function textToString(builders, node) {
4444
}
4545
}
4646

47-
function concatStatementToParams(builders, node, isGlimmer) {
48-
if (!isGlimmer) {
49-
return node.parts;
50-
}
51-
47+
function concatStatementToParams(builders, node) {
5248
return node.parts.map(function (part) {
5349
if (part.type === 'MustacheStatement') {
5450
if (!part.params.length && !part.hash.pairs.length) {

packages/ember-css-modules/tests/helpers/render-with-styles.js

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
/* global require, define */
22

33
import Ember from 'ember';
4-
import { VERSION } from '@ember/version';
54
import ClassTransformPlugin from 'ecm-template-transform';
65

76
const { compile } = Ember.__loader.require('ember-template-compiler');
@@ -22,10 +21,7 @@ export default function registerStyles(styles) {
2221
}
2322

2423
const { plugin } = ClassTransformPlugin.instantiate({
25-
emberVersion: VERSION,
26-
options: {
27-
includeExtensionInModulePath: false,
28-
},
24+
includeExtensionInModulePath: false,
2925
});
3026

3127
const plugins = { ast: [plugin] };

0 commit comments

Comments
 (0)