Skip to content

Commit

Permalink
remove typescript and simplify even more
Browse files Browse the repository at this point in the history
  • Loading branch information
billouboq committed Aug 2, 2018
1 parent 0cdef78 commit cf25295
Show file tree
Hide file tree
Showing 9 changed files with 65 additions and 53 deletions.
3 changes: 0 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
# dist files
dist

# Logs
logs
*.log
Expand Down
10 changes: 5 additions & 5 deletions __tests__/index.test.ts → __tests__/index.test.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
"use strict";

import postcss from "postcss";
import removeDuplicateCSS from "../src/index";
const postcss = require("postcss");
const slashCSS = require("../index");

function run(input: string, output: string, opts: object) {
return postcss([ removeDuplicateCSS(opts) ])
function run(input, output, opts) {
return postcss([ slashCSS(opts) ])
.process(input)
.then(result => {
expect(result.css.replace(/\s/g,'')).toEqual(output);
Expand All @@ -30,6 +30,6 @@ describe("Test main functions", () => {
});

it("Should throw an error since we dont pass targets option", async () => {
expect(() => removeDuplicateCSS(null)).toThrow("This plugins needs an option object with a targets propertie");
expect(() => slashCSS(null)).toThrow("This plugins needs an option object with a targets propertie");
});
})
3 changes: 3 additions & 0 deletions dist/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import postcss from "postcss";
declare const _default: postcss.Plugin<SlashCSS.Options>;
export default _default;
45 changes: 45 additions & 0 deletions dist/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const fs_1 = __importDefault(require("fs"));
const util_1 = __importDefault(require("util"));
const postcss_1 = __importDefault(require("postcss"));
const fast_glob_1 = __importDefault(require("fast-glob"));
const getFileContent = util_1.default.promisify(fs_1.default.readFile);
exports.default = postcss_1.default.plugin('slashcss', (opts) => {
if (!opts || !opts.targets) {
throw new Error("This plugins needs an option object with a targets propertie");
}
return async (root) => {
try {
// get all external targets files
const cssFilesPath = await fast_glob_1.default(opts.targets);
const getFileContentPromises = cssFilesPath.map(filePath => getFileContent(filePath, "utf-8"));
const cssFilesContent = await Promise.all(getFileContentPromises);
cssFilesContent.forEach(targetCSSContent => {
const targetsAST = postcss_1.default.parse(targetCSSContent).nodes;
root.walkRules((rule) => {
// search for duplicate selector
const findedAst = targetsAST.find(ast => ast.selector === rule.selector);
if (findedAst) {
rule.walkDecls(function (decl) {
// if css properties are the sames (props and value) remove it
if (findedAst.nodes.some(prop => prop.prop === decl.prop && prop.value === decl.value)) {
decl.remove();
}
});
// if selector doesn't have any props then remove selector
if (!rule.nodes || !rule.nodes.length) {
rule.remove();
}
}
});
});
}
catch (err) {
throw err;
}
};
});
5 changes: 0 additions & 5 deletions index.d.ts

This file was deleted.

17 changes: 10 additions & 7 deletions src/index.ts → index.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import fs from "fs";
import util from "util";
import postcss from "postcss";
import glob from "fast-glob";
"use strict";

const getFileContent = util.promisify(fs.readFile);
const fs = require("fs");
const util = require("util");
const postcss = require("postcss");
const glob = require("fast-glob");

export default postcss.plugin<SlashCSS.Options>('slashcss', (opts) => {
const getFileContent = util.promisify(fs.readFile);

function slashCSSPlugin (opts = {}) {
if (!opts || !opts.targets) {
throw new Error("This plugins needs an option object with a targets propertie");
}
Expand Down Expand Up @@ -44,4 +45,6 @@ export default postcss.plugin<SlashCSS.Options>('slashcss', (opts) => {
throw err;
}
};
});
}

module.exports = postcss.plugin('slashcss', slashCSSPlugin);
7 changes: 1 addition & 6 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
module.exports = {
transform: {
"^.+\\.tsx?$": "ts-jest",
},
testRegex: "(/__tests__/.*|(\\.|/)(test|spec))\\.(jsx?|tsx?)$",
testPathIgnorePatterns: ["/lib/", "/node_modules/"],
moduleFileExtensions: ["ts", "tsx", "js", "jsx", "json", "node"],
testPathIgnorePatterns: ["/node_modules/"],
collectCoverage: true,
verbose: true,
testURL: "http://localhost/"
Expand Down
7 changes: 1 addition & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,10 @@
"repository": "https://github.com/billouboq/postcss-slashcss.git",
"main": "./dist/index.js",
"scripts": {
"build": "jest tests && tsc",
"test": "jest tests"
},
"devDependencies": {
"@types/jest": "^23.3.1",
"@types/node": "^10.5.5",
"jest": "^23.4.2",
"ts-jest": "^23.0.1",
"typescript": "^3.0.1"
"jest": "^23.4.2"
},
"dependencies": {
"fast-glob": "^2.2.2",
Expand Down
21 changes: 0 additions & 21 deletions tsconfig.json

This file was deleted.

0 comments on commit cf25295

Please sign in to comment.