ember-scoped-css 0.1.0 → 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 +2 -0
- package/package.json +1 -1
- package/src/babel-plugin-scoped-class.js +30 -0
- package/src/renameClass.js +5 -0
- package/src/replaceScopedClass.js +69 -0
- package/src/rewriteCss.js +8 -1
- package/test/rewriteCss.js +18 -0
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
|
@@ -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,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
|
+
};
|
package/src/rewriteCss.js
CHANGED
|
@@ -26,10 +26,17 @@ function rewriteSelector(sel, postfix) {
|
|
|
26
26
|
return transformed;
|
|
27
27
|
}
|
|
28
28
|
|
|
29
|
+
function isInsideKeyframes(node) {
|
|
30
|
+
const parent = node.parent;
|
|
31
|
+
if (!parent) return false;
|
|
32
|
+
if (parent.type === 'atrule' && parent.name === 'keyframes') return true;
|
|
33
|
+
return isInsideKeyframes(parent);
|
|
34
|
+
}
|
|
35
|
+
|
|
29
36
|
module.exports = function rewriteCss(css, postfix, fileName) {
|
|
30
37
|
const ast = postcss.parse(css);
|
|
31
38
|
ast.walk((node) => {
|
|
32
|
-
if (node.type === 'rule') {
|
|
39
|
+
if (node.type === 'rule' && !isInsideKeyframes(node)) {
|
|
33
40
|
node.selector = rewriteSelector(node.selector, postfix);
|
|
34
41
|
}
|
|
35
42
|
});
|
package/test/rewriteCss.js
CHANGED
|
@@ -24,4 +24,22 @@ describe('rewriteCss', function () {
|
|
|
24
24
|
`/* foo.css */\n@layer components {\n\n.baz_postfix .foo p .bar_postfix { color: red; }\n}\n`
|
|
25
25
|
);
|
|
26
26
|
});
|
|
27
|
+
|
|
28
|
+
it.only('sudnt rewrite keyframes', function () {
|
|
29
|
+
const css = `
|
|
30
|
+
@keyframes luna-view-navigation {
|
|
31
|
+
100% {
|
|
32
|
+
padding-top: 1rem;
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
`;
|
|
36
|
+
|
|
37
|
+
const postfix = 'postfix';
|
|
38
|
+
const fileName = 'foo.css';
|
|
39
|
+
const rewritten = rewriteCss(css, postfix, fileName);
|
|
40
|
+
|
|
41
|
+
expect(rewritten).to.equal(
|
|
42
|
+
`/* foo.css */\n@layer components {\n\n${css}\n}\n`
|
|
43
|
+
);
|
|
44
|
+
});
|
|
27
45
|
});
|