ember-scoped-css 0.2.6 → 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.6",
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
  };