ember-scoped-css 0.1.1 → 0.2.0

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/index.js CHANGED
@@ -8,6 +8,7 @@ const appCssLoader = require('./src/app-css-loader');
8
8
  const appDependencyLoader = require('./src/app-dependency-loader');
9
9
  const appScopedcssWebpack = require('./src/app-scopedcss-webpack');
10
10
  const addonRewritecssRollup = require('./src/addon-rewritecss-rollup');
11
+ const babelPluginScopedClass = require('./src/babel-plugin-scoped-class');
11
12
 
12
13
  module.exports = {
13
14
  rollupEmberTemplateImportsPlugin,
@@ -20,4 +21,5 @@ module.exports = {
20
21
  appDependencyLoader,
21
22
  appScopedcssWebpack,
22
23
  addonRewritecssRollup,
24
+ babelPluginScopedClass,
23
25
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ember-scoped-css",
3
- "version": "0.1.1",
3
+ "version": "0.2.0",
4
4
  "description": "",
5
5
  "type": "commonjs",
6
6
  "main": "index.js",
@@ -0,0 +1,30 @@
1
+ const template = require('@babel/template').default;
2
+ const replaceScopedClass = require('./replaceScopedClass');
3
+
4
+ const scopedClass = () => {
5
+ return {
6
+ visitor: {
7
+ CallExpression(path, state) {
8
+ if (path.node?.callee?.name === 'precompileTemplate') {
9
+ let source = '';
10
+ if (path.node.arguments[0].type === 'StringLiteral') {
11
+ source = path.node.arguments[0].value;
12
+ path.node.arguments[0].value = replaceScopedClass(
13
+ path.node.arguments[0].value,
14
+ state.filename,
15
+ state.cwd
16
+ );
17
+ } else if (path.node.arguments[0].type === 'TemplateLiteral') {
18
+ path.node.arguments[0].quasis[0].value.raw = replaceScopedClass(
19
+ path.node.arguments[0].quasis[0].value.raw,
20
+ state.filename,
21
+ state.cwd
22
+ );
23
+ }
24
+ }
25
+ },
26
+ },
27
+ };
28
+ };
29
+
30
+ module.exports = scopedClass;
@@ -0,0 +1,5 @@
1
+ const getPostfix = require('./getPostfix');
2
+
3
+ module.exports = function (className, projectCssPath) {
4
+ return className + '_' + getPostfix(projectCssPath);
5
+ };
@@ -0,0 +1,69 @@
1
+ const recast = require('ember-template-recast');
2
+ const renameClass = require('./renameClass');
3
+
4
+ module.exports = function (hbs, templatePath, basePath) {
5
+ let ast = recast.parse(hbs);
6
+ let stack = [];
7
+ const cssPath = templatePath
8
+ .replace(basePath, '')
9
+ .replace(/(\.hbs)?(\.js)?/, '.css');
10
+ const projectCssPath = cssPath; // cssPath.replace(basePath, '');
11
+
12
+ recast.traverse(ast, {
13
+ All: {
14
+ enter(node) {
15
+ stack.push(node);
16
+ },
17
+ exit(node) {
18
+ stack.pop();
19
+ },
20
+ },
21
+ MustacheStatement(node) {
22
+ let cssClass;
23
+
24
+ if (
25
+ node.path?.original === 'scoped-class' &&
26
+ node.params?.length === 1 &&
27
+ node.params[0].type === 'StringLiteral'
28
+ ) {
29
+ cssClass = node.params[0].value;
30
+ }
31
+
32
+ if (
33
+ node.path?.path?.original === 'scoped-class' &&
34
+ node.path?.params?.length === 1 &&
35
+ node.path?.params[0].type === 'StringLiteral'
36
+ ) {
37
+ cssClass = node.path.params[0].value;
38
+ }
39
+
40
+ if (cssClass) {
41
+ const textNode = recast.builders.text(
42
+ renameClass(cssClass, projectCssPath)
43
+ );
44
+ const parent = stack[stack.length - 1];
45
+ if (parent.type === 'AttrNode') {
46
+ parent.quoteType = '"';
47
+ }
48
+ return textNode;
49
+ }
50
+ },
51
+ SubExpression(node) {
52
+ if (
53
+ node.path?.original === 'scoped-class' &&
54
+ node.params?.length === 1 &&
55
+ node.params[0].type === 'StringLiteral'
56
+ ) {
57
+ const cssClass = node.params[0].value;
58
+ const textNode = recast.builders.literal(
59
+ 'StringLiteral',
60
+ renameClass(cssClass, projectCssPath)
61
+ );
62
+ return textNode;
63
+ }
64
+ },
65
+ });
66
+
67
+ let result = recast.print(ast);
68
+ return result;
69
+ };