Skip to content
Open
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
node_modules
.husky
.DS_Store
dist
# Exclude node_modules in e2e dir as we need it for testing importers
Expand Down
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,16 @@

## Quick setup

Run via API

```
import { NgxUnusedCSS } from 'ngx-unused-css';

const result = new NgxUnusedCSS(config).instance.run();
```

Run via CLI

`npx ngx-unused-css --init`

`npm i -D ngx-unused-css`
Expand Down
2 changes: 1 addition & 1 deletion e2e/__snapshots__/test.ts.snap
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`e2e should throw error with the list of unused css classes 1`] = `
exports[`e2e should test CLI 1`] = `
"Unused CSS classes were found for the following files:

src/app/app.component.html
Expand Down
32 changes: 30 additions & 2 deletions e2e/test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { exec } from 'child_process';
import { exec, execSync } from 'child_process';
import { Config } from '../src/config';
import { Main } from './../src/main';

describe('e2e', () => {
it('should throw error with the list of unused css classes', (done) => {
it('should test CLI', (done) => {
exec(
'node ./../dist/index.js',
{ cwd: 'e2e' },
Expand All @@ -13,4 +15,30 @@ describe('e2e', () => {
}
);
});

it('should test API', async () => {
const config: Config = require('./.ngx-unused-css.json');

// Copy test libs to root node modules to make tests not failing
execSync('cp -R e2e/node_modules/test-css-lib node_modules/test-css-lib');

// Override paths
config.path = 'e2e/src/app';
config.globalStyles = 'e2e/src/styles.scss';
config.includePaths = ['e2e/node_modules/test-css-lib-load-paths'];

const instance = new Main(config);

const result = await instance.run();

expect(result).toEqual({
css: [
[
['.test-lib-class', '.test-css-lib-load-paths', '.unused'],
'e2e/src/app/app.component.html'
]
],
globalCss: ['.i-am-global-class']
});
});
});
9 changes: 7 additions & 2 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,16 @@ if (cli.flags.init) {
);
}

config.cli = true;

// Use dynamic import so config is initialized on every import
const mainPromise = import('./src/main');
mainPromise.then((res) => {
// Bootstrap library
// eslint-disable-next-line
new res.default(config);
/* eslint-disable no-new */
new res.Main(config);
});
}

export { Config } from './src/config';
export { Main as NgxUnusedCSS } from './src/main';
3 changes: 2 additions & 1 deletion jest.config.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
module.exports = {
preset: 'ts-jest',
testEnvironment: 'node'
// testEnvironment: 'node' https://github.com/sass/sass/issues/3235#issuecomment-1005195249
testEnvironment: 'jest-environment-node-single-context'
};
11 changes: 10 additions & 1 deletion package-lock.json

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

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
{
"name": "ngx-unused-css",
"version": "3.0.4",
"version": "4.0.0-1",
"description": "Detect unused CSS in angular components",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"files": [
"dist/*"
],
Expand Down Expand Up @@ -67,6 +68,7 @@
"eslint-plugin-promise": "^4.3.1",
"husky": "^5.2.0",
"jest": "27.4.7",
"jest-environment-node-single-context": "^27.2.1",
"mock-fs": "^4.14.0",
"nodemon": "^2.0.15",
"prettier": "^2.5.1",
Expand Down
1 change: 1 addition & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@ export interface Config {
importer?: Importer<'sync'> | FileImporter<'sync'>;
includePaths?: string[];
globalStyles?: string;
cli: boolean; // Determine if running via CLI or by using programmic API
}
3 changes: 2 additions & 1 deletion src/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ export default async function init() {

const config: Config = {
path: '',
ignore: []
ignore: [],
cli: true
};

if (result.isDefaultSrc) {
Expand Down
43 changes: 24 additions & 19 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import chalk from 'chalk';
import path from 'path';
import { table } from 'table';
import { Config } from './config';
import { DEFAULT_STYLE_EXTENSION } from './constants';
import { UnusedClassesMap } from './helpers/unusedClassMapper';
import UnusedClasses from './main/getUnusedClasses';

export default class Main {
export class Main {
private config: Config;

constructor(config: Config) {
Expand All @@ -15,26 +16,29 @@ export default class Main {
config.styleExt = DEFAULT_STYLE_EXTENSION;
}

this.run()
.then((r) => {
const res = r.css;

if (r.globalCss.length > 0) {
res.push([r.globalCss, '***** GLOBAL UNUSED CSS *****']);
}

if (res.length > 0) {
this.log(res);
} else {
console.log('No duplicate classes were found!');
}
})
.catch((err) => {
throw new Error(err);
});
// Execute run immediately if running via CLI
if (this.config.cli) {
this.run()
.then((r) => {
const res = r.css;

if (r.globalCss.length > 0) {
res.push([r.globalCss, '***** GLOBAL UNUSED CSS *****']);
}

if (res.length > 0) {
this.log(res);
} else {
console.log('No duplicate classes were found!');
}
})
.catch((err) => {
throw new Error(err);
});
}
}

private async run(): Promise<{
public async run(): Promise<{
css: UnusedClassesMap[];
globalCss: string[];
}> {
Expand All @@ -52,6 +56,7 @@ export default class Main {

return { css, globalCss };
} catch (e) {
console.log('e', e, path.resolve(''));
throw new Error(e as string);
}
}
Expand Down
4 changes: 2 additions & 2 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"sourceMap": true,
"esModuleInterop": true,
"strict": true,
"declaration": false,
"declaration": true,
"allowSyntheticDefaultImports": true,
"downlevelIteration": true,
"experimentalDecorators": true,
Expand All @@ -16,5 +16,5 @@
"typeRoots": ["node_modules/@types"],
"lib": ["es2018", "dom"]
},
"exclude": ["./e2e", "./node_modules", "**/*.test.ts"]
"exclude": ["./e2e", "./node_modules", "**/*.test.ts", "./dist"]
}