ember-scoped-css 0.2.5 → 0.2.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ember-scoped-css",
3
- "version": "0.2.5",
3
+ "version": "0.2.7",
4
4
  "description": "",
5
5
  "type": "commonjs",
6
6
  "main": "index.js",
@@ -1,9 +1,32 @@
1
1
  const template = require('@babel/template').default;
2
2
  const replaceScopedClass = require('./replaceScopedClass');
3
+ const nodePath = require('path');
4
+ const renameClass = require('./renameClass');
5
+ const getPostfix = require('./getPostfix');
3
6
 
4
- const scopedClass = () => {
7
+ const scopedClass = (babel) => {
8
+ let scopedClassName = '';
5
9
  return {
6
10
  visitor: {
11
+ ImportDeclaration(path) {
12
+ if (path.node.source.value === 'ember-scoped-css') {
13
+ // find import scopedClass
14
+ let sc = path.node.specifiers.find(
15
+ (s) => s.imported.name === 'scopedClass'
16
+ );
17
+
18
+ // store scopedClass local name
19
+ scopedClassName = sc.local.name;
20
+
21
+ // remove import scopedClass
22
+ if (path.node.specifiers.length === 1) {
23
+ path.remove();
24
+ } else {
25
+ path.node.specifiers = path.node.specifiers.filter((s) => s !== sc);
26
+ }
27
+ }
28
+ },
29
+
7
30
  CallExpression(path, state) {
8
31
  if (path.node?.callee?.name === 'precompileTemplate') {
9
32
  let source = '';
@@ -22,6 +45,31 @@ const scopedClass = () => {
22
45
  );
23
46
  }
24
47
  }
48
+
49
+ // scopedClass helper
50
+ if (
51
+ scopedClassName &&
52
+ path.node?.callee?.name === scopedClassName &&
53
+ path.node.arguments.length === 2 &&
54
+ path.node.arguments[0].type === 'StringLiteral' &&
55
+ path.node.arguments[1].type === 'StringLiteral'
56
+ ) {
57
+ // get class name and css file path from scopedClass helper
58
+ let [className, relativeCssFilePath] = path.node.arguments.map(
59
+ (a) => a.value
60
+ );
61
+
62
+ // rename class
63
+ let cssFilePath = nodePath.resolve(
64
+ nodePath.dirname(state.filename),
65
+ relativeCssFilePath
66
+ );
67
+ let postfix = getPostfix(cssFilePath);
68
+ let renamedClass = renameClass(className, postfix);
69
+
70
+ // replace scopedClass helper with renamed class
71
+ path.replaceWith(babel.types.stringLiteral(renamedClass));
72
+ }
25
73
  },
26
74
  },
27
75
  };
@@ -1,7 +1,14 @@
1
- const getPostfix = require('./getPostfix');
2
-
3
- module.exports = function (className, projectCssPath) {
1
+ module.exports = function (className, postfix, classesInCss) {
4
2
  const classes = className.split(/\s+/);
5
- const postfix = getPostfix(projectCssPath);
6
- return classes.map((c) => c + '_' + postfix).join(' ');
3
+ const renamedClasses = classes
4
+ .filter((c) => c)
5
+ .map((c) => c.trim())
6
+ .map((c) => (!classesInCss || classesInCss.has(c) ? c + '_' + postfix : c))
7
+ .join(' ');
8
+
9
+ const renamedWithPreservedSpaces = className.replace(
10
+ className.trimStart().trimEnd(),
11
+ renamedClasses
12
+ );
13
+ return renamedWithPreservedSpaces;
7
14
  };
@@ -1,11 +1,12 @@
1
1
  const recast = require('ember-template-recast');
2
2
  const renameClass = require('./renameClass');
3
+ const getPostfix = require('./getPostfix');
3
4
 
4
5
  module.exports = function (hbs, templatePath, basePath) {
5
6
  let ast = recast.parse(hbs);
6
7
  let stack = [];
7
8
  const cssPath = templatePath.replace(/(\.hbs)?\.js$/, '.css');
8
- const projectCssPath = cssPath; // cssPath.replace(basePath, '');
9
+ const postfix = getPostfix(cssPath);
9
10
 
10
11
  recast.traverse(ast, {
11
12
  All: {
@@ -36,9 +37,7 @@ module.exports = function (hbs, templatePath, basePath) {
36
37
  }
37
38
 
38
39
  if (cssClass) {
39
- const textNode = recast.builders.text(
40
- renameClass(cssClass, projectCssPath)
41
- );
40
+ const textNode = recast.builders.text(renameClass(cssClass, postfix));
42
41
  const parent = stack[stack.length - 1];
43
42
  if (parent.type === 'AttrNode') {
44
43
  parent.quoteType = '"';
@@ -55,7 +54,7 @@ module.exports = function (hbs, templatePath, basePath) {
55
54
  const cssClass = node.params[0].value;
56
55
  const textNode = recast.builders.literal(
57
56
  'StringLiteral',
58
- renameClass(cssClass, projectCssPath)
57
+ renameClass(cssClass, postfix)
59
58
  );
60
59
  return textNode;
61
60
  }
package/src/rewriteHbs.js CHANGED
@@ -1,4 +1,5 @@
1
1
  const recast = require('ember-template-recast');
2
+ const renameClass = require('./renameClass');
2
3
 
3
4
  module.exports = function rewriteHbs(hbs, classes, tags, postfix) {
4
5
  let ast = recast.parse(hbs);
@@ -7,26 +8,13 @@ module.exports = function rewriteHbs(hbs, classes, tags, postfix) {
7
8
  AttrNode(node) {
8
9
  if (node.name === 'class') {
9
10
  if (node.value.type === 'TextNode' && node.value.chars) {
10
- const newClasses = node.value.chars.split(' ').map((c) => {
11
- if (c.trim() && classes.has(c.trim())) {
12
- return c.trim() + '_' + postfix;
13
- } else {
14
- return c;
15
- }
16
- });
17
-
18
- node.value.chars = newClasses.join(' ');
11
+ const renamedClass = renameClass(node.value.chars, postfix, classes);
12
+ node.value.chars = renamedClass;
19
13
  } else if (node.value.type === 'ConcatStatement') {
20
14
  for (let part of node.value.parts) {
21
15
  if (part.type === 'TextNode' && part.chars) {
22
- const newClasses = part.chars.split(' ').map((c) => {
23
- if (c.trim() && classes.has(c.trim())) {
24
- return c.trim() + '_' + postfix;
25
- } else {
26
- return c;
27
- }
28
- });
29
- part.chars = newClasses.join(' ');
16
+ const renamedClass = renameClass(part.chars, postfix, classes);
17
+ part.chars = renamedClass;
30
18
  }
31
19
  }
32
20
  }