From 2c1ca760f2a3743c5e44d3a6e414286f7ff93046 Mon Sep 17 00:00:00 2001 From: Dan Freeman Date: Mon, 22 Nov 2021 13:42:56 +0100 Subject: [PATCH 1/3] Clean up template plugin compat code for Ember < 3.24 --- packages/ember-css-modules/index.js | 7 +- .../lib/htmlbars-plugin/index.js | 66 +++---------------- .../lib/htmlbars-plugin/utils.js | 6 +- .../tests/helpers/render-with-styles.js | 6 +- 4 files changed, 12 insertions(+), 73 deletions(-) diff --git a/packages/ember-css-modules/index.js b/packages/ember-css-modules/index.js index 448ce89..b637cc3 100644 --- a/packages/ember-css-modules/index.js +++ b/packages/ember-css-modules/index.js @@ -66,11 +66,8 @@ module.exports = { this.parentPreprocessorRegistry.add( 'htmlbars-ast-plugin', HtmlbarsPlugin.instantiate({ - emberVersion: this.checker.for('ember-source').version, - options: { - fileExtension: this.getFileExtension(), - includeExtensionInModulePath: this.includeExtensionInModulePath(), - }, + fileExtension: this.getFileExtension(), + includeExtensionInModulePath: this.includeExtensionInModulePath(), }) ); }, diff --git a/packages/ember-css-modules/lib/htmlbars-plugin/index.js b/packages/ember-css-modules/lib/htmlbars-plugin/index.js index 21ca165..e6f03c1 100644 --- a/packages/ember-css-modules/lib/htmlbars-plugin/index.js +++ b/packages/ember-css-modules/lib/htmlbars-plugin/index.js @@ -1,7 +1,6 @@ 'use strict'; const utils = require('./utils'); -const semver = require('semver'); module.exports = class ClassTransformPlugin { constructor(env, options) { @@ -9,23 +8,17 @@ module.exports = class ClassTransformPlugin { this.builders = env.syntax.builders; this.options = options; this.stylesModule = this.determineStylesModule(env); - this.isGlimmer = this.detectGlimmer(); this.visitor = this.buildVisitor(env); - - // Alias for 2.15 <= Ember < 3.1 - this.visitors = this.visitor; } - static instantiate({ emberVersion, options }) { + static instantiate(options) { return { name: 'ember-css-modules', - plugin: semver.lt(emberVersion, '2.15.0-alpha') - ? LegacyAdapter.bind(null, this, options) - : (env) => new this(env, options), + plugin: (env) => new this(env, options), parallelBabel: { requireFile: __filename, buildUsing: 'instantiate', - params: { emberVersion, options }, + params: options, }, baseDir() { return `${__dirname}/../..`; @@ -54,17 +47,6 @@ module.exports = class ClassTransformPlugin { return name; } - detectGlimmer() { - if (!this.syntax.parse) { - return false; - } - - // HTMLBars builds ConcatStatements with StringLiterals + raw PathExpressions - // Glimmer builds ConcatStatements with TextNodes + MustacheStatements - let ast = this.syntax.parse('
'); - return ast.body[0].attributes[0].value.parts[0].type === 'TextNode'; - } - buildVisitor(env) { if (env.moduleName === env.filename) { // No-op for the stage 1 Embroider pass (which only contains relative paths) @@ -133,7 +115,6 @@ module.exports = class ClassTransformPlugin { utils.removeAttr(node, localClassAttr); - let stringBuilder = this.isGlimmer ? 'text' : 'string'; let classAttr = utils.getAttr(node, 'class'); let parts = []; let classAttrValue; @@ -149,26 +130,18 @@ module.exports = class ClassTransformPlugin { parts.push( this.builders.mustache( this.builders.path('concat'), - utils.concatStatementToParams( - this.builders, - classAttrValue, - this.isGlimmer - ) + utils.concatStatementToParams(this.builders, classAttrValue) ) ); } else if (classAttrValue.type === 'TextNode') { - parts.push(this.builders[stringBuilder](classAttrValue.chars)); + parts.push(this.builders.text(classAttrValue.chars)); } else if (classAttrValue.type === 'MustacheStatement') { - if (classAttrValue.params.length || this.isGlimmer) { - parts.push(classAttrValue); - } else { - parts.push(this.builders.path(classAttrValue.path.original)); - } + parts.push(classAttrValue); } } utils.pushAll(parts, this.localToPath(localClassAttr.value)); - this.divide(parts, this.isGlimmer ? 'text' : 'string'); + this.divide(parts, 'text'); node.attributes.unshift( this.builders.attr('class', this.builders.concat(parts)) ); @@ -225,11 +198,7 @@ module.exports = class ClassTransformPlugin { concatLocalPath(node) { let concatPath = this.builders.path('concat'); - let concatParts = utils.concatStatementToParams( - this.builders, - node, - this.isGlimmer - ); + let concatParts = utils.concatStatementToParams(this.builders, node); let concatStatement = this.builders.mustache(concatPath, concatParts); return this.dynamicLocalPath(concatStatement); } @@ -268,22 +237,3 @@ module.exports = class ClassTransformPlugin { return parts; } }; - -// For Ember < 2.15 -class LegacyAdapter { - constructor(plugin, options, env) { - this.plugin = plugin; - this.options = options; - this.meta = env.meta; - this.syntax = null; - } - - transform(ast) { - let plugin = new this.plugin( - Object.assign({ syntax: this.syntax }, this.meta), - this.options - ); - this.syntax.traverse(ast, plugin.visitor); - return ast; - } -} diff --git a/packages/ember-css-modules/lib/htmlbars-plugin/utils.js b/packages/ember-css-modules/lib/htmlbars-plugin/utils.js index 78041a5..f3277e3 100644 --- a/packages/ember-css-modules/lib/htmlbars-plugin/utils.js +++ b/packages/ember-css-modules/lib/htmlbars-plugin/utils.js @@ -44,11 +44,7 @@ function textToString(builders, node) { } } -function concatStatementToParams(builders, node, isGlimmer) { - if (!isGlimmer) { - return node.parts; - } - +function concatStatementToParams(builders, node) { return node.parts.map(function (part) { if (part.type === 'MustacheStatement') { if (!part.params.length && !part.hash.pairs.length) { diff --git a/packages/ember-css-modules/tests/helpers/render-with-styles.js b/packages/ember-css-modules/tests/helpers/render-with-styles.js index 85aece9..f5886cc 100644 --- a/packages/ember-css-modules/tests/helpers/render-with-styles.js +++ b/packages/ember-css-modules/tests/helpers/render-with-styles.js @@ -1,7 +1,6 @@ /* global require, define */ import Ember from 'ember'; -import { VERSION } from '@ember/version'; import ClassTransformPlugin from 'ecm-template-transform'; const { compile } = Ember.__loader.require('ember-template-compiler'); @@ -22,10 +21,7 @@ export default function registerStyles(styles) { } const { plugin } = ClassTransformPlugin.instantiate({ - emberVersion: VERSION, - options: { - includeExtensionInModulePath: false, - }, + includeExtensionInModulePath: false, }); const plugins = { ast: [plugin] }; From 39e0312496833ddb169e963e851fbb18f7320b86 Mon Sep 17 00:00:00 2001 From: Dan Freeman Date: Mon, 22 Nov 2021 14:22:29 +0100 Subject: [PATCH 2/3] Remove compat shim for PostCSS < 8 --- .../lib/make-postcss-plugin.js | 18 ------------------ .../lib/modules-preprocessor.js | 12 +++++++++--- packages/ember-css-modules/package.json | 1 - 3 files changed, 9 insertions(+), 22 deletions(-) delete mode 100644 packages/ember-css-modules/lib/make-postcss-plugin.js diff --git a/packages/ember-css-modules/lib/make-postcss-plugin.js b/packages/ember-css-modules/lib/make-postcss-plugin.js deleted file mode 100644 index 3def85f..0000000 --- a/packages/ember-css-modules/lib/make-postcss-plugin.js +++ /dev/null @@ -1,18 +0,0 @@ -const semver = require('semver'); -const postcssVersion = require('postcss/package.json').version; - -if (semver.lt(postcssVersion, '8.0.0')) { - module.exports = require('postcss').plugin; -} else { - module.exports = function (name, callback) { - let plugin = (options = {}) => { - let handler = callback(options); - return { - postcssPlugin: name, - Once: handler, - }; - }; - plugin.postcss = true; - return plugin; - }; -} diff --git a/packages/ember-css-modules/lib/modules-preprocessor.js b/packages/ember-css-modules/lib/modules-preprocessor.js index 8e94cfa..0dfb3a7 100644 --- a/packages/ember-css-modules/lib/modules-preprocessor.js +++ b/packages/ember-css-modules/lib/modules-preprocessor.js @@ -199,9 +199,15 @@ module.exports = class ModulesPreprocessor { } rootPathPlugin() { - return require('./make-postcss-plugin')('root-path-tag', () => (css) => { - css.source.input.rootPath = this._modulesTree.inputPaths[0]; - }); + return Object.assign( + () => ({ + postcssPlugin: 'root-path-tag', + Once: (css) => { + css.source.input.rootPath = this._modulesTree.inputPaths[0]; + }, + }), + { postcss: true } + ); } resolvePath(importPath, fromFile) { diff --git a/packages/ember-css-modules/package.json b/packages/ember-css-modules/package.json index 8014dd6..31d4b7e 100644 --- a/packages/ember-css-modules/package.json +++ b/packages/ember-css-modules/package.json @@ -86,7 +86,6 @@ "hash-string": "^1.0.0", "lodash.merge": "^4.6.1", "postcss": "^8.0.0", - "semver": "^7.3.5", "toposort": "^2.0.2" }, "ember": { From 82072f6eabbe897d74b979216220d789ccbe07b6 Mon Sep 17 00:00:00 2001 From: Dan Freeman Date: Mon, 22 Nov 2021 14:26:39 +0100 Subject: [PATCH 3/3] Clean up compat shims in the build for old CLI --- packages/ember-css-modules/index.js | 11 ----------- .../lib/modules-preprocessor.js | 16 +--------------- packages/ember-css-modules/lib/resolve-path.js | 2 +- packages/ember-css-modules/package.json | 1 - 4 files changed, 2 insertions(+), 28 deletions(-) diff --git a/packages/ember-css-modules/index.js b/packages/ember-css-modules/index.js index b637cc3..43010fd 100644 --- a/packages/ember-css-modules/index.js +++ b/packages/ember-css-modules/index.js @@ -3,7 +3,6 @@ const path = require('path'); const fs = require('fs'); const debug = require('debug')('ember-css-modules:addon'); -const VersionChecker = require('ember-cli-version-checker'); const HtmlbarsPlugin = require('./lib/htmlbars-plugin'); const ModulesPreprocessor = require('./lib/modules-preprocessor'); @@ -19,7 +18,6 @@ module.exports = { this.outputStylesPreprocessor = new OutputStylesPreprocessor({ owner: this, }); - this.checker = new VersionChecker(this.project); this.plugins = new PluginRegistry(this.parent); }, @@ -166,15 +164,6 @@ module.exports = { return this.cssModulesOptions.postcssOptions; }, - getAddonModulesRoot() { - // CLI 2.12 stopped exposing addon stuff nested under `modules/` - if (this.checker.for('ember-cli', 'npm').satisfies('< 2.12')) { - return 'modules/'; - } else { - return ''; - } - }, - getParentAddonTree() { return path.join(this.parentAddon.root, this.parentAddon.treePaths.addon); }, diff --git a/packages/ember-css-modules/lib/modules-preprocessor.js b/packages/ember-css-modules/lib/modules-preprocessor.js index 0dfb3a7..ce1013c 100644 --- a/packages/ember-css-modules/lib/modules-preprocessor.js +++ b/packages/ember-css-modules/lib/modules-preprocessor.js @@ -50,15 +50,6 @@ module.exports = class ModulesPreprocessor { let inputRoot = this.owner.belongsToAddon() ? this.owner.getParentAddonTree() : this.owner.app.trees.app; - let outputRoot = this.owner.belongsToAddon() - ? this.owner.getAddonModulesRoot() - : ''; - - if (outputRoot) { - inputRoot = new Funnel(inputRoot, { - destDir: outputRoot, - }); - } // If moduleName is defined, that should override the parent's name. // Otherwise, the template and generated module will disagree as to what the path should be. @@ -71,7 +62,6 @@ module.exports = class ModulesPreprocessor { let modulesSources = new ModuleSourceFunnel(inputRoot, modulesInput, { include: ['**/*.' + this.owner.getFileExtension()], - outputRoot, parentName: ownerName, }); @@ -79,9 +69,7 @@ module.exports = class ModulesPreprocessor { extension: this.owner.getFileExtension(), plugins: this.getPostcssPlugins(), enableSourceMaps: this.owner.enableSourceMaps(), - sourceMapBaseDir: this.owner.belongsToAddon() - ? this.owner.getAddonModulesRoot() - : '', + sourceMapBaseDir: '', postcssOptions: this.owner.getPostcssOptions(), virtualModules: this.owner.getVirtualModules(), generateScopedName: this.scopedNameGenerator(), @@ -217,7 +205,6 @@ module.exports = class ModulesPreprocessor { defaultExtension: this.owner.getFileExtension(), ownerName: this._ownerName, parentName: this._parentName, - addonModulesRoot: this.owner.getAddonModulesRoot(), root: ensurePosixPath(this._modulesTree.inputPaths[0]), parent: this.owner.getParent(), ui: this.owner.ui, @@ -236,7 +223,6 @@ class ModuleSourceFunnel extends Funnel { constructor(input, stylesTree, options) { super([input, stylesTree], options); this.parentName = options.parentName; - this.destDir = options.outputRoot; this.inputHasParentName = null; } diff --git a/packages/ember-css-modules/lib/resolve-path.js b/packages/ember-css-modules/lib/resolve-path.js index 62170c1..52cb498 100644 --- a/packages/ember-css-modules/lib/resolve-path.js +++ b/packages/ember-css-modules/lib/resolve-path.js @@ -103,7 +103,7 @@ function resolveExternalPath(importPath, originalPath, fromFile, options) { let absolutePath = ensurePosixPath( path.resolve(addonTreePath, pathWithinAddon) ); - let keyPath = options.addonModulesRoot + addonName + '/' + pathWithinAddon; + let keyPath = addonName + '/' + pathWithinAddon; return new DependencyPath('external', absolutePath, keyPath); } diff --git a/packages/ember-css-modules/package.json b/packages/ember-css-modules/package.json index 31d4b7e..d248a8d 100644 --- a/packages/ember-css-modules/package.json +++ b/packages/ember-css-modules/package.json @@ -81,7 +81,6 @@ "debug": "^4.3.2", "ember-cli-babel": "^7.26.6", "ember-cli-htmlbars": "^6.0.0", - "ember-cli-version-checker": "^5.1.2", "ensure-posix-path": "^1.0.2", "hash-string": "^1.0.0", "lodash.merge": "^4.6.1",