Skip to content

Commit

Permalink
feat: active files report includes style files (#1190)
Browse files Browse the repository at this point in the history
  • Loading branch information
dhhyi authored Jul 25, 2022
1 parent 4a71df7 commit 2f7c765
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 24 deletions.
19 changes: 19 additions & 0 deletions scripts/dead-files.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
const _ = require('lodash');
const glob = require('glob');
const fs = require('fs');

const activeFiles = _.flatten(
glob.sync('dist/**/active-files.json').map(activeFilesPath => {
console.log('loading', activeFilesPath);
return JSON.parse(fs.readFileSync(activeFilesPath, { encoding: 'utf-8' }));
})
).filter((v, i, a) => a.indexOf(v) === i);

const filesToBeSearched = glob.sync('{src,projects}/**/!(*.spec).{ts,html,scss}');

filesToBeSearched
.filter(file => !activeFiles.includes(file))
.filter(file => !file.includes('/dev/') && !file.endsWith('.model.ts') && !file.endsWith('.interface.ts'))
.forEach(file => {
console.log(file);
});
85 changes: 61 additions & 24 deletions templates/webpack/webpack.custom.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { CustomWebpackBrowserSchema, TargetOptions } from '@angular-builders/custom-webpack';
import { tsquery } from '@phenomnomnominal/tsquery';
import * as fs from 'fs';
import { flatten } from 'lodash';
import { basename, join, resolve } from 'path';
import { flattenDeep } from 'lodash';
import { basename, dirname, join, normalize, resolve } from 'path';
import * as ts from 'typescript';
import { Configuration, DefinePlugin, WebpackPluginInstance } from 'webpack';

/* eslint-disable no-console */
Expand Down Expand Up @@ -367,32 +369,67 @@ export default (config: Configuration, angularJsonConfig: CustomWebpackBrowserSc
const outputFolder = fs.readdirSync(config.output.path);
const sourceMaps = outputFolder.filter(f => f.endsWith('.js.map'));
if (sourceMaps.length) {
const traverseStyleFile = (file: string): string[] => {
const fileWithExt = file.endsWith('.scss') ? file : `${file}.scss`;
const path = ['', 'src/styles/', `src/styles/themes/${theme}/`]
.map(p => normalize(p + fileWithExt))
.find(fs.existsSync);
if (!path) {
return [];
}

const paths = [path];

const regex = /@import '(.*?)'/g;
const content = fs.readFileSync(path, { encoding: 'utf-8' });
for (let match: RegExpExecArray; (match = regex.exec(content)); ) {
paths.push(...traverseStyleFile(match[1]));
}

return paths;
};

const activeFilesPath = join(config.output.path, 'active-files.json');
logger.log('writing', logOutputFile(activeFilesPath));

// write active file report
const activeFiles = flatten(
sourceMaps.map(sourceMapPath => {
const sourceMap: { sources: string[] } = JSON.parse(
fs.readFileSync(join(config.output.path, sourceMapPath), { encoding: 'utf-8' })
);
// replacements needed because html overrides are not swapped in source maps
return (
sourceMap.sources
// source map entries start with './
.map(path => path.substring(2))
.filter(path => path.startsWith('src') || path.startsWith('projects'))
.map(path => relativeReplacements[path] ?? path)
.filter(path => {
// TODO: handle lazy sources whenever this becomes a problem
if (path.includes('|lazy|')) {
logger.warn('cannot handle lazy source:', path);
return false;
}
return true;
})
);
})
const activeFiles = flattenDeep(
sourceMaps
.map(sourceMapPath => {
const sourceMap: { sources: string[] } = JSON.parse(
fs.readFileSync(join(config.output.path, sourceMapPath), { encoding: 'utf-8' })
);
return (
sourceMap.sources
// source map entries start with './
.map(path => path.substring(2))
.filter(path => path.startsWith('src') || path.startsWith('projects'))
.filter(path => {
// TODO: handle lazy sources whenever this becomes a problem
if (path.includes(' lazy ')) {
logger.warn('cannot handle lazy source:', path);
return false;
}
return true;
})
.map(path => relativeReplacements[path] ?? path)
.map(path => {
if (basename(path).includes('.component.') && path.endsWith('.ts')) {
return tsquery(
tsquery.ast(fs.readFileSync(path, { encoding: 'utf-8' })),
'CallExpression:has(Identifier[name=Component]) PropertyAssignment:has(Identifier[name=styleUrls]) ArrayLiteralExpression > StringLiteral'
)
.map((styleUrl: ts.StringLiteral) => `${dirname(path)}/${styleUrl.text.substring(2)}`)
.map(path => relativeReplacements[path] ?? path)
.map(traverseStyleFile)
.concat([path]);
} else {
return path;
}
})
);
})
.concat(traverseStyleFile(`src/styles/themes/${theme}/style`))
)
.filter((v, i, a) => a.indexOf(v) === i)
.sort();
Expand Down

0 comments on commit 2f7c765

Please sign in to comment.