summaryrefslogtreecommitdiff
path: root/fixtures/find-unused-css.js
blob: 6804f503b6bdbd1e35b993881b5c4569f10b22f8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
const Promise = require('bluebird');
const path = require('path');
const fs = require('fs-extra');
const uncss = require('uncss');

const outputFile = Promise.promisify(fs.outputFile);

// Find `<script(.*?)<\/script>` and replace with nothing
// In `fixtures/pages`



function removeDisassociatedComments(str) {
  return str.replace(/\/\*(.*?)\*\/\n($|\/\*(.*?)\*\/)\n/gm, '');
}


const pagesDirectory = '/Users/eric/Documents/gitlab/gitlab-development-kit/gitlab/fixtures/pages/';

const files = [
  //path.join(pagesDirectory, 'Admin Broadcast Messages Live preview a customized broadcast message.html'),
  //path.join(pagesDirectory, 'Commits CI commit status is Ci Build when logged as reporter Renders header.html'),
  //path.join(pagesDirectory, 'Visual tokens editing assignee token makes value editable.html'),
  /* */
  ...fs.readdirSync(pagesDirectory).map((file) => {
    return path.join(pagesDirectory, file);
  })
  /* */
];

console.log(`Looking over ${files.length} files`);


const options = {
  report: true,
  stylesheets: [
    //'application-2abaf6ae8d375c7b1fad9dfa885d5da1b6ee69d983bd1d36a6699c880b7ca506.css',
    path.join(pagesDirectory, 'application-2abaf6ae8d375c7b1fad9dfa885d5da1b6ee69d983bd1d36a6699c880b7ca506.css')
  ],
  cacheDirectory: 'fixtures/pages/cache',
  concurrency: 10
};

uncss(files, options, (err, usedCss, report) => {
  if (err) {
    console.log('err', err, err.stack);
    return;
  }

  // TODO: Would be best to run this through a PostCSS plugin again and
  // remove any comment not above a rule or any at-rule with only comments inside
  //const resultantUsedCss = removeDisassociatedComments(usedCss);

  Promise.all([
    outputFile('output/raw-report3.json', JSON.stringify(report.selectors, null, 2)),
    //outputFile('output/raw-report3.json', report),
    //outputFile('output/used.css', resultantUsedCss)
  ])
    .then(() => {
      console.log('report saved to disk!');
    })
    .catch((err) => {
      console.log('Problem saving report', err, err.stack);
    });
});