Skip to content

Commit

Permalink
add test
Browse files Browse the repository at this point in the history
  • Loading branch information
luncheon committed Nov 20, 2020
1 parent 184a156 commit e577e14
Show file tree
Hide file tree
Showing 15 changed files with 336 additions and 3 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
/node_modules/
node_modules/
dist/
1 change: 1 addition & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/test/**/*
2 changes: 2 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
const pluginutils_1 = require("@rollup/pluginutils");
const acorn = require("acorn");
const fs = require("fs");
const path = require("path");
const purgecss_1 = require("purgecss");
exports.default = ({ css, output, include, exclude, }) => {
const filter = pluginutils_1.createFilter(include, exclude);
Expand Down Expand Up @@ -33,6 +34,7 @@ exports.default = ({ css, output, include, exclude, }) => {
keyframes: true,
variables: true,
});
await fs.promises.mkdir(path.dirname(output), { recursive: true });
await fs.promises.writeFile(output, purged[0].css, 'utf8');
},
};
Expand Down
2 changes: 2 additions & 0 deletions index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { createFilter, FilterPattern } from '@rollup/pluginutils'
import * as acorn from 'acorn'
import * as fs from 'fs'
import * as path from 'path'
import { PurgeCSS } from 'purgecss'

export default ({
Expand Down Expand Up @@ -42,6 +43,7 @@ export default ({
keyframes: true,
variables: true,
})
await fs.promises.mkdir(path.dirname(output), { recursive: true })
await fs.promises.writeFile(output, purged[0].css, 'utf8')
},
}
Expand Down
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@luncheon/rollup-plugin-purgecss-keep-string-literals",
"version": "0.0.0",
"description": "",
"description": "An experimental rollup plugin that removes the rest of the CSS, leaving only the selectors used in the string literals in the JavaScript source code.",
"license": "WTFPL",
"repository": "luncheon/rollup-plugin-purgecss-keep-string-literals",
"main": "index.js",
Expand All @@ -11,7 +11,8 @@
"index.d.ts"
],
"scripts": {
"build": "tsc -p ."
"build": "tsc -p . && npm t",
"test": "cd test && npm t"
},
"prettier": {
"printWidth": 140,
Expand Down
212 changes: 212 additions & 0 deletions test/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions test/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"private": true,
"scripts": {
"test": "rimraf dist/ && rollup -c --silent && node src/check.js && rimraf dist/"
},
"devDependencies": {
"@rollup/plugin-typescript": "^6.1.0",
"rimraf": "^3.0.2",
"rollup": "^2.33.3",
"tslib": "^2.0.3",
"typescript": "^4.1.2"
}
}
1 change: 1 addition & 0 deletions test/parent.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const _parent = 'parent'
16 changes: 16 additions & 0 deletions test/rollup.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import typescript from '@rollup/plugin-typescript'
import purgecssKeepStringLiterals from '..'

export default {
input: 'src/index.ts',
output: { file: 'dist/index.js' },
plugins: [
typescript({ noEmit: true }),
purgecssKeepStringLiterals({
css: ['src/source.css'],
output: 'dist/utilities.css',
include: ['src/**/*.ts'],
exclude: ['**/excluded/**'],
}),
],
}
15 changes: 15 additions & 0 deletions test/src/check.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
const fs = require('fs')
const path = require('path')

const read = filename => fs.readFileSync(path.resolve(__dirname, filename), 'utf8').replace(/\s+/g, '')

const actual = read('../dist/utilities.css')
const expected = read('expected.css')
if (actual === expected) {
console.log('test success')
} else {
console.error('test failure')
console.log('expected: ', expected)
console.log(' actual: ', actual)
process.exit(1)
}
1 change: 1 addition & 0 deletions test/src/excluded/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const _excluded = 'excluded'
5 changes: 5 additions & 0 deletions test/src/expected.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.a{}
.b{}
.c{}
.included{}
.hover\:hoverable:hover{}
1 change: 1 addition & 0 deletions test/src/included/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const _included = 'included'
17 changes: 17 additions & 0 deletions test/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { _parent } from '../parent'
import { _included } from './included'
import { _excluded } from './excluded'

console.log(_parent, _included, _excluded)

const x = 'a'

const y = function (s: string) {
if (arguments.length === 0) {
return "b"
} else {
return `c ${s}`
}
}

const z = 'hover:hoverable'
45 changes: 45 additions & 0 deletions test/src/source.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
.a{}
.b{}
.c{}
.d{}
.e{}
.f{}
.g{}
.h{}
.i{}
.j{}
.k{}
.l{}
.m{}
.n{}
.o{}
.p{}
.q{}
.r{}
.s{}
.t{}
.u{}
.v{}
.w{}
.x{}
.y{}
.z{}

.parent{}
._parent{}
.included{}
._included{}
.excluded{}
._excluded{}

.export{}
.const{}
.function{}
.if{}
.else{}
.return{}
.arguments{}
.length{}
.string{}

.hover\:hoverable:hover{}

0 comments on commit e577e14

Please sign in to comment.