Skip to content

Commit

Permalink
added baseDir option for normalizing the relative source path
Browse files Browse the repository at this point in the history
  • Loading branch information
cenfun committed May 11, 2024
1 parent 10e525b commit 6063085
Show file tree
Hide file tree
Showing 9 changed files with 71 additions and 56 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
## Changelog

- 2.8.1
- added `baseDir` option for normalizing the relative source path

- 2.8.0
- added `filter` option as combined filter for `entryFilter` and `sourceFilter`
- added `--all` option for CLI
Expand Down
1 change: 1 addition & 0 deletions lib/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@ program

.option('-o, --outputDir <dir>', 'output dir for reports')
.option('-i, --inputDir <dir>', 'input dir for merging raw files')
.option('-b, --baseDir <dir>', 'base dir for normalizing path')

.option('-a, --all <dir>', 'include all files from dir')

Expand Down
7 changes: 2 additions & 5 deletions lib/converter/converter.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@ const { getIgnoredRanges } = require('./ignore.js');
const {
sortRanges, dedupeCountRanges, mergeRangesWith
} = require('../utils/dedupe.js');
const {
getSourceType, getSourcePathHandler, initSourceMapSourcePath
} = require('../utils/source-path.js');
const { getSourceType, initSourceMapSourcesPath } = require('../utils/source-path.js');
const { decode, minimatch } = require('../packages/monocart-coverage-vendor.js');

const InfoBranch = require('./info-branch.js');
Expand Down Expand Up @@ -1111,8 +1109,7 @@ const unpackSourceMap = (state, options) => {

// keep original urls
const fileUrls = {};
const sourcePathHandler = getSourcePathHandler(options);
initSourceMapSourcePath(sourceMap, fileUrls, sourcePath, sourcePathHandler);
initSourceMapSourcesPath(fileUrls, sourceMap, sourcePath, options);
state.fileUrls = fileUrls;

// ===============================================
Expand Down
3 changes: 3 additions & 0 deletions lib/default/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ module.exports = {
// {string|string[]} input raw dir(s)
inputDir: null,

// {string} base dir for normalizing the relative source path, defaults to cwd
baseDir: null,

// (V8 only) {function} A filter function to execute for each element in the V8 list.
// entryFilter: (entry) => {
// if (entry.url.indexOf('googleapis.com') !== -1) {
Expand Down
3 changes: 3 additions & 0 deletions lib/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,9 @@ declare namespace MCR {
/** {string|string[]} input raw dir(s) */
inputDir?: string | string[];

/** {string} base dir for normalizing the relative source path, defaults to cwd */
baseDir?: string;

/** (V8 only)
*
* {string} `minimatch` pattern for entry url;
Expand Down
6 changes: 2 additions & 4 deletions lib/istanbul/istanbul.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const istanbulLibReport = require('istanbul-lib-report');
const Util = require('../utils/util.js');
const IstanbulSummary = require('./istanbul-summary.js');

const { getSourcePathHandler, initIstanbulSourcePath } = require('../utils/source-path.js');
const { initIstanbulSourcePath } = require('../utils/source-path.js');

const findHtmlPath = (outputDir) => {
const defaultHtml = path.resolve(outputDir, 'index.html');
Expand Down Expand Up @@ -129,9 +129,7 @@ const mergeIstanbulCoverage = (dataList) => {
const initIstanbulData = (istanbulData, options) => {

const fileSources = options.fileSources || {};
const sourcePathHandler = getSourcePathHandler(options);

const coverageData = initIstanbulSourcePath(istanbulData, fileSources, sourcePathHandler);
const coverageData = initIstanbulSourcePath(istanbulData, fileSources, options);

return {
fileSources,
Expand Down
36 changes: 22 additions & 14 deletions lib/utils/source-path.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ const filterPath = (str) => {
return ls.join('/');
};

const getSourcePath = (url, index = '', type = '') => {
// eslint-disable-next-line complexity
const normalizeSourcePath = (url, baseDir, index = '', type = '') => {

// no url for anonymous
if (!url) {
Expand All @@ -32,7 +33,7 @@ const getSourcePath = (url, index = '', type = '') => {

// already is file url
if (url.startsWith('file:')) {
const relPath = Util.relativePath(fileURLToPath(url));
const relPath = Util.relativePath(fileURLToPath(url), baseDir);
return filterPath(relPath);
}

Expand All @@ -54,7 +55,7 @@ const getSourcePath = (url, index = '', type = '') => {

// could be path
const fileUrl = pathToFileURL(url).toString();
const relPath = Util.relativePath(fileURLToPath(fileUrl));
const relPath = Util.relativePath(fileURLToPath(fileUrl), baseDir);
return filterPath(relPath);

};
Expand All @@ -75,7 +76,7 @@ const getSourceType = (sourcePath) => {

// ========================================================================================================

const getSourcePathHandler = (options) => {
const getSourcePathReplacer = (options) => {
const input = options.sourcePath;
if (typeof input === 'function') {
return input;
Expand All @@ -96,8 +97,10 @@ const getSourcePathHandler = (options) => {

// ========================================================================================================

const initIstanbulSourcePath = (coverageData, fileSources, sourcePathHandler) => {
if (!sourcePathHandler) {
const initIstanbulSourcePath = (coverageData, fileSources, options) => {

const sourcePathReplacer = getSourcePathReplacer(options);
if (!sourcePathReplacer) {
return coverageData;
}

Expand All @@ -119,7 +122,7 @@ const initIstanbulSourcePath = (coverageData, fileSources, sourcePathHandler) =>
}

// new source path, second is source url
const newSourcePath = sourcePathHandler(sourcePath, item);
const newSourcePath = sourcePathReplacer(sourcePath, item);
if (typeof newSourcePath === 'string' && newSourcePath) {
sourcePath = newSourcePath;
}
Expand Down Expand Up @@ -180,16 +183,21 @@ const resolveSourceUrl = (sourceName, sourceRoot) => {
return sourceUrl;
};

const initSourceMapSourcePath = (sourceMap, fileUrls, distFile, sourcePathHandler) => {
const initSourceMapSourcesPath = (fileUrls, sourceMap, distFile, options) => {

const baseDir = options.baseDir;
const sourcePathReplacer = getSourcePathReplacer(options);

sourceMap.sources = sourceMap.sources.map((sourceName, i) => {

const sourceUrl = resolveSourceUrl(sourceName, sourceMap.sourceRoot);

let sourcePath = getSourcePath(sourceUrl, i + 1);
if (sourcePathHandler) {
let sourcePath = normalizeSourcePath(sourceUrl, baseDir, i + 1);
// console.log(sourceUrl, sourcePath);

if (sourcePathReplacer) {
// sourcePath could be a filename only, may using distFile to get full path
const newSourcePath = sourcePathHandler(sourcePath, {
const newSourcePath = sourcePathReplacer(sourcePath, {
url: sourceUrl,
distFile
});
Expand All @@ -208,9 +216,9 @@ module.exports = {
// for unit test
resolveSourceUrl,

getSourcePath,
normalizeSourcePath,
getSourceType,
getSourcePathHandler,
getSourcePathReplacer,
initIstanbulSourcePath,
initSourceMapSourcePath
initSourceMapSourcesPath
};
12 changes: 7 additions & 5 deletions lib/v8/v8.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const EC = require('eight-colors');
const Util = require('../utils/util.js');
const { getV8Summary } = require('./v8-summary.js');
const { dedupeFlatRanges } = require('../utils/dedupe.js');
const { getSourcePath, getSourcePathHandler } = require('../utils/source-path.js');
const { normalizeSourcePath, getSourcePathReplacer } = require('../utils/source-path.js');
const { mergeScriptCovs, minimatch } = require('../packages/monocart-coverage-vendor.js');
const { collectSourceMaps } = require('../converter/collect-source-maps.js');
const version = require('../../package.json').version;
Expand Down Expand Up @@ -109,7 +109,9 @@ const initV8ListAndSourcemap = async (v8list, options) => {
}
}

const sourcePathHandler = getSourcePathHandler(options);
const sourcePathReplacer = getSourcePathReplacer(options);

const baseDir = options.baseDir;

// init id, sourcePath
v8list = v8list.map((entry, i) => {
Expand Down Expand Up @@ -149,9 +151,9 @@ const initV8ListAndSourcemap = async (v8list, options) => {
}

// resolve source path
let sourcePath = getSourcePath(data.url, i + 1, data.type);
if (sourcePathHandler) {
const newSourcePath = sourcePathHandler(sourcePath, data);
let sourcePath = normalizeSourcePath(data.url, baseDir, i + 1, data.type);
if (sourcePathReplacer) {
const newSourcePath = sourcePathReplacer(sourcePath, data);
if (typeof newSourcePath === 'string' && newSourcePath) {
sourcePath = newSourcePath;
}
Expand Down
56 changes: 28 additions & 28 deletions test/unit/source-path.test.js
Original file line number Diff line number Diff line change
@@ -1,63 +1,63 @@
const platform = process.platform;
const assert = require('assert');
const { resolveSourceUrl, getSourcePath } = require('../../lib/utils/source-path.js');
const { resolveSourceUrl, normalizeSourcePath } = require('../../lib/utils/source-path.js');

it('resolveSourceUrl', () => {
it('normalizeSourcePath + resolveSourceUrl', () => {

// windows absolute path
if (platform === 'win32') {
console.log('test win32 path');
assert.equal(getSourcePath(resolveSourceUrl('C://a.js')), 'C/a.js');
assert.equal(getSourcePath(resolveSourceUrl('C:\\\\a.js')), 'C/a.js');
assert.equal(getSourcePath(resolveSourceUrl('c://workspace/test.spec.js')), 'c/workspace/test.spec.js');
assert.equal(getSourcePath(resolveSourceUrl('c:/workspace/test.spec.js')), 'c/workspace/test.spec.js');
assert.equal(getSourcePath(resolveSourceUrl('c:\\workspace\\test.spec.js')), 'c/workspace/test.spec.js');
assert.equal(normalizeSourcePath(resolveSourceUrl('C://a.js')), 'C/a.js');
assert.equal(normalizeSourcePath(resolveSourceUrl('C:\\\\a.js')), 'C/a.js');
assert.equal(normalizeSourcePath(resolveSourceUrl('c://workspace/test.spec.js')), 'c/workspace/test.spec.js');
assert.equal(normalizeSourcePath(resolveSourceUrl('c:/workspace/test.spec.js')), 'c/workspace/test.spec.js');
assert.equal(normalizeSourcePath(resolveSourceUrl('c:\\workspace\\test.spec.js')), 'c/workspace/test.spec.js');
}

// linux absolute path
assert.equal(getSourcePath(resolveSourceUrl('/dir/./src/a.js')), 'dir/src/a.js');
assert.equal(normalizeSourcePath(resolveSourceUrl('/dir/./src/a.js')), 'dir/src/a.js');

// protocol
assert.equal(getSourcePath(resolveSourceUrl('ws://a.js')), 'a.js');
assert.equal(getSourcePath(resolveSourceUrl('webpack://coverage-v8/./src/branch.js')), 'coverage-v8/src/branch.js');
assert.equal(normalizeSourcePath(resolveSourceUrl('ws://a.js')), 'a.js');
assert.equal(normalizeSourcePath(resolveSourceUrl('webpack://coverage-v8/./src/branch.js')), 'coverage-v8/src/branch.js');

// file:// protocol
if (platform === 'win32') {
// absolute
assert.equal(getSourcePath(resolveSourceUrl('file:///C:/path/src/a.js')), 'C/path/src/a.js');
assert.equal(normalizeSourcePath(resolveSourceUrl('file:///C:/path/src/a.js')), 'C/path/src/a.js');
// relative
assert.equal(getSourcePath(resolveSourceUrl('file://src/a.js')), 'src/a.js');
assert.equal(normalizeSourcePath(resolveSourceUrl('file://src/a.js')), 'src/a.js');
} else {
// absolute ///
assert.equal(getSourcePath(resolveSourceUrl('file:///src/a.js')), 'src/a.js');
assert.equal(normalizeSourcePath(resolveSourceUrl('file:///src/a.js')), 'src/a.js');
// relative localhost or empty
assert.equal(getSourcePath(resolveSourceUrl('file://localhost/src/a.js')), 'src/a.js');
assert.equal(getSourcePath(resolveSourceUrl('file:/src/a.js')), 'src/a.js');
assert.equal(normalizeSourcePath(resolveSourceUrl('file://localhost/src/a.js')), 'src/a.js');
assert.equal(normalizeSourcePath(resolveSourceUrl('file:/src/a.js')), 'src/a.js');
}

// relative
assert.equal(getSourcePath(resolveSourceUrl('../../src/a.js')), 'src/a.js');
assert.equal(getSourcePath(resolveSourceUrl('a.js')), 'a.js');
assert.equal(getSourcePath(resolveSourceUrl('./src/a.js')), 'src/a.js');
assert.equal(normalizeSourcePath(resolveSourceUrl('../../src/a.js')), 'src/a.js');
assert.equal(normalizeSourcePath(resolveSourceUrl('a.js')), 'a.js');
assert.equal(normalizeSourcePath(resolveSourceUrl('./src/a.js')), 'src/a.js');

// sourceRoot
assert.equal(getSourcePath(resolveSourceUrl('../a.js', 'webpack://')), 'a.js');
assert.equal(getSourcePath(resolveSourceUrl('a.js', 'dist')), 'dist/a.js');
assert.equal(getSourcePath(resolveSourceUrl('../a.js', 'dist')), 'a.js');
assert.equal(normalizeSourcePath(resolveSourceUrl('../a.js', 'webpack://')), 'a.js');
assert.equal(normalizeSourcePath(resolveSourceUrl('a.js', 'dist')), 'dist/a.js');
assert.equal(normalizeSourcePath(resolveSourceUrl('../a.js', 'dist')), 'a.js');

if (platform === 'win32') {
assert.equal(getSourcePath(resolveSourceUrl('./a.js', 'file:///C:/path/src')), 'C/path/src/a.js');
assert.equal(getSourcePath(resolveSourceUrl('../a.js', 'file:///C:/path/src')), 'C/path/a.js');
assert.equal(normalizeSourcePath(resolveSourceUrl('./a.js', 'file:///C:/path/src')), 'C/path/src/a.js');
assert.equal(normalizeSourcePath(resolveSourceUrl('../a.js', 'file:///C:/path/src')), 'C/path/a.js');

assert.equal(getSourcePath(resolveSourceUrl('./a.js', 'file://path/src')), 'path/src/a.js');
assert.equal(getSourcePath(resolveSourceUrl('../a.js', 'file://path/src')), 'path/a.js');
assert.equal(normalizeSourcePath(resolveSourceUrl('./a.js', 'file://path/src')), 'path/src/a.js');
assert.equal(normalizeSourcePath(resolveSourceUrl('../a.js', 'file://path/src')), 'path/a.js');

} else {
// absolute ///
assert.equal(getSourcePath(resolveSourceUrl('./a.js', 'file:///path/src')), 'path/src/a.js');
assert.equal(getSourcePath(resolveSourceUrl('../a.js', 'file:///path/src')), 'path/a.js');
assert.equal(normalizeSourcePath(resolveSourceUrl('./a.js', 'file:///path/src')), 'path/src/a.js');
assert.equal(normalizeSourcePath(resolveSourceUrl('../a.js', 'file:///path/src')), 'path/a.js');
}

assert.equal(getSourcePath(resolveSourceUrl('a.js', '/root')), 'root/a.js');
assert.equal(normalizeSourcePath(resolveSourceUrl('a.js', '/root')), 'root/a.js');

});

0 comments on commit 6063085

Please sign in to comment.