Skip to content

Commit 521c28c

Browse files
Bart Venemanbartveneman
authored andcommitted
expose ./core to tree-shake out the css-analyzer
1 parent e398bd3 commit 521c28c

File tree

10 files changed

+97
-65
lines changed

10 files changed

+97
-65
lines changed

.github/workflows/release.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ jobs:
1616
with:
1717
node-version: 16
1818
- run: npm ci
19+
- run: npm run build
1920
- run: npm test
2021

2122
publish-npm:

.github/workflows/test.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,5 @@ jobs:
3030
node-version: ${{ matrix.node-version }}
3131
cache: npm
3232
- run: npm ci
33+
- run: npm run build
3334
- run: npm test

package.json

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,22 @@
1717
"maintainability"
1818
],
1919
"files": [
20-
"dist"
20+
"dist",
21+
"src"
2122
],
2223
"type": "module",
2324
"source": "src/index.js",
2425
"main": "./dist/css-code-quality.cjs",
2526
"module": "./dist/css-code-quality.module.js",
2627
"unpkg": "./dist/css-code-quality.umd.js",
2728
"exports": {
28-
"require": "./dist/css-code-quality.cjs",
29-
"default": "./dist/css-code-quality.modern.js"
29+
".": {
30+
"import": "./dist/css-code-quality.modern.js",
31+
"require": "./dist/css-code-quality.cjs"
32+
},
33+
"./core": {
34+
"import": "./src/core.js"
35+
}
3036
},
3137
"types": "./dist/index.d.ts",
3238
"scripts": {
@@ -40,4 +46,4 @@
4046
"microbundle": "^0.15.1",
4147
"uvu": "^0.5.6"
4248
}
43-
}
49+
}

src/complexity.js

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { compareSpecificity } from '@projectwallace/css-analyzer'
22

3-
const guards = [
3+
export const guards = [
44

55
// Complexity per Selector should not differ too much from the most common Complexity
66
result => {
@@ -124,7 +124,3 @@ const guards = [
124124
return outcome
125125
},
126126
]
127-
128-
export {
129-
guards
130-
}

src/core.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import { guards as performanceGuards } from './performance.js'
2+
import { guards as maintainabilityGuards } from './maintainability.js'
3+
import { guards as complexityGuards } from './complexity.js'
4+
5+
function calculateScore({ result, guards }) {
6+
let score = 100
7+
let violations = []
8+
let passes = []
9+
10+
for (const guard of guards) {
11+
/** @type {{score: number, actual: number, id: string}} */
12+
const outcome = guard(result)
13+
14+
if (outcome.score > 0) {
15+
score -= outcome.score
16+
violations.push(outcome)
17+
} else {
18+
passes.push(outcome)
19+
}
20+
}
21+
22+
return {
23+
score: Math.max(score, 0),
24+
violations,
25+
passes,
26+
}
27+
}
28+
29+
export function calculate(analysis) {
30+
const performance = calculateScore({ result: analysis, guards: performanceGuards })
31+
const maintainability = calculateScore({ result: analysis, guards: maintainabilityGuards })
32+
const complexity = calculateScore({ result: analysis, guards: complexityGuards })
33+
34+
return {
35+
/** @deprecated */
36+
score: 0,
37+
violations: performance.violations
38+
.concat(maintainability.violations)
39+
.concat(complexity.violations),
40+
passes: performance.passes
41+
.concat(maintainability.passes)
42+
.concat(complexity.passes),
43+
performance,
44+
maintainability,
45+
complexity,
46+
}
47+
}

src/core.test.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import * as assert from 'uvu/assert'
2+
import { suite } from 'uvu'
3+
import { calculate } from './core.js'
4+
5+
const Core = suite('Core')
6+
7+
Core('exports calculate', () => {
8+
assert.is(typeof calculate, 'function')
9+
})
10+
11+
Core.run()

src/index.js

Lines changed: 4 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,7 @@
11
import { analyze } from '@projectwallace/css-analyzer'
2-
import { guards as performanceGuards } from './performance.js'
3-
import { guards as maintainabilityGuards } from './maintainability.js'
4-
import { guards as complexityGuards } from './complexity.js'
2+
import { calculate as calculateFromAnalysis } from './core.js'
53

6-
function calculateScore({ result, guards }) {
7-
let score = 100
8-
let violations = []
9-
let passes = []
10-
11-
for (const guard of guards) {
12-
/** @type {{score: number, actual: number, id: string}} */
13-
const outcome = guard(result)
14-
15-
if (outcome.score > 0) {
16-
score -= outcome.score
17-
violations.push(outcome)
18-
} else {
19-
passes.push(outcome)
20-
}
21-
}
22-
23-
return {
24-
score: Math.max(score, 0),
25-
violations,
26-
passes,
27-
}
28-
}
29-
30-
function calculate(css) {
31-
const result = analyze(css)
32-
33-
const performance = calculateScore({ result, guards: performanceGuards })
34-
const maintainability = calculateScore({ result, guards: maintainabilityGuards })
35-
const complexity = calculateScore({ result, guards: complexityGuards })
36-
37-
return {
38-
score: 0,
39-
violations: performance.violations
40-
.concat(maintainability.violations)
41-
.concat(complexity.violations),
42-
passes: performance.passes
43-
.concat(maintainability.passes)
44-
.concat(complexity.passes),
45-
performance,
46-
maintainability,
47-
complexity,
48-
}
4+
export function calculate(css) {
5+
const analysis = analyze(css)
6+
return calculateFromAnalysis(analysis)
497
}
50-
51-
export {
52-
calculate
53-
}

src/index.test.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import * as assert from 'uvu/assert'
2+
import { suite } from 'uvu'
3+
import { calculate } from './index.js'
4+
import { calculate as pkgCalculate } from '../dist/css-code-quality.modern.js'
5+
6+
const Index = suite('Index')
7+
8+
Index('exposes a calculcate function', () => {
9+
assert.is(typeof calculate, 'function')
10+
})
11+
12+
Index.run()
13+
14+
const Package = suite('NPM Package')
15+
16+
Package('exposes a calculate function', () => {
17+
assert.is(typeof pkgCalculate, 'function')
18+
})
19+
20+
Package.run()

src/maintainability.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const guards = [
1+
export const guards = [
22

33
// Source Lines of Code should be low'
44
result => {
@@ -148,5 +148,3 @@ const guards = [
148148
return outcome
149149
},
150150
]
151-
152-
export { guards }

src/performance.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const guards = [
1+
export const guards = [
22

33
// Should not contain @import rules
44
result => ({
@@ -75,5 +75,3 @@ const guards = [
7575
}
7676
},
7777
]
78-
79-
export { guards }

0 commit comments

Comments
 (0)