Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
*.js
node_modules
/node_modules/
/lib/**/*.js
6 changes: 6 additions & 0 deletions babel.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module.exports = {
presets: [
['@babel/preset-env', {targets: {node: 'current'}}],
'@babel/preset-typescript',
]
};
Empty file added d.ts
Empty file.
2 changes: 1 addition & 1 deletion lib/convertScopeToModuleName.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as camelCase from "camelcase";
import camelCase from "camelcase";

export function convertScopeToModuleName(scope: string) {
return camelCase(scope)
Expand Down
11 changes: 5 additions & 6 deletions lib/convertScopedCssForEmotion.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as postcss from "postcss";
import * as Stringifier from "postcss/lib/stringifier";
import {parse} from "postcss";
import {stringify} from "postcss";
import type {AnyNode} from "postcss";

import { convertSelectorForEmotion } from "./convertSelectorForEmotion";
import { escapeScopedCss } from "./escapeScopedCss";
Expand All @@ -13,7 +14,7 @@ export function convertScopedCssForEmotion(

function builder(
output: string,
node?: postcss.Node,
node?: AnyNode,
flag?: "start" | "end",
) {
if (
Expand Down Expand Up @@ -50,9 +51,7 @@ export function convertScopedCssForEmotion(
scopedCssForEmotion += output;
}

(new Stringifier(builder) as postcss.Stringifier).stringify(
postcss.parse(scopedCss),
);
stringify(parse(scopedCss), builder)

return escapeScopedCss(scopedCssForEmotion);
}
3 changes: 1 addition & 2 deletions lib/convertSelectorForEmotion.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import * as parseSelector from "postcss-selector-parser";

import parseSelector from "postcss-selector-parser";
import { convertScopeToModuleName } from "./convertScopeToModuleName";

export function convertSelectorForEmotion(
Expand Down
15 changes: 7 additions & 8 deletions lib/getCssIndexedByScope.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as postcss from "postcss";
import * as Stringifier from "postcss/lib/stringifier";
import {parse} from "postcss";
import {stringify} from "postcss";
import type {AnyNode} from "postcss";

import { getNodeScopes } from "./getNodeScopes";
import { getSelectorScope } from "./getSelectorScope";
Expand All @@ -11,7 +12,7 @@ export function getCssIndexedByScope(css: string): Map<string, string> {

function builder(
output: string,
node?: postcss.Node,
node?: AnyNode,
flag?: "start" | "end",
) {
if (flag === "start" && node) {
Expand All @@ -31,8 +32,8 @@ export function getCssIndexedByScope(css: string): Map<string, string> {
flag === "start" &&
node &&
node.type === "rule" &&
(node.parent.type !== "atrule" ||
/keyframes$/.test(node.parent.name) === false)
(node.parent && (node.parent.type !== "atrule" ||
/keyframes$/.test(node.parent.name) === false))
) {
output = `${(node.selectors || [])
.filter(
Expand All @@ -48,9 +49,7 @@ export function getCssIndexedByScope(css: string): Map<string, string> {
}
}

(new Stringifier(builder) as postcss.Stringifier).stringify(
postcss.parse(css),
);
stringify(parse(css), builder)

return cssIndexedByScope;
}
9 changes: 4 additions & 5 deletions lib/getNodeScopes.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
import * as postcss from "postcss";

import type {AnyNode} from "postcss";
import { getSelectorScope } from "./getSelectorScope";

export function getNodeScopes(node: postcss.Node): Set<string> {
export function getNodeScopes(node: AnyNode): Set<string> {
const nodeScopes = new Set();

if (
node.type === "rule" &&
(node.parent.type !== "atrule" ||
/keyframes/.test(node.parent.name) === false)
(node.parent && (node.parent.type !== "atrule" ||
/keyframes/.test(node.parent.name) === false))
) {
(node.selectors || []).forEach((selector) => {
nodeScopes.add(getSelectorScope(selector));
Expand Down
14 changes: 8 additions & 6 deletions lib/getRequiredScopes.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
import * as postcss from "postcss";
import * as parseSelector from "postcss-selector-parser";
import { parse } from "postcss";
import parse from "postcss-selector-parser";
import { Container, ClassName } from "postcss-selector-parser";

import { getSelectorScope } from "./getSelectorScope";


export function getRequiredScopes(
css: string,
scope: string,
knownScopes: Set<string>,
): Set<string> {
const requiredScopes = new Set();

const root = postcss.parse(css);
const root = parse(css);
root.walkRules((rule) => {
parseSelector((nodes: any) => {
nodes.walkClasses((node: any) => {
const selectorScope = getSelectorScope(node.toString());
parse((selectors: Container) => {
selectors.walkClasses((className: ClassName) => {
const selectorScope = getSelectorScope(className.toString());
if (selectorScope === scope) {
return;
}
Expand Down
2 changes: 1 addition & 1 deletion lib/getSelectorScope.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as parseSelector from "postcss-selector-parser";
import parseSelector from "postcss-selector-parser";

export function getSelectorScope(selector: string): string {
let selectorScope = "root";
Expand Down
Loading