Skip to content

Commit

Permalink
Feature: Add ignoreExtensions option. (#23)
Browse files Browse the repository at this point in the history
- Add `ignoreExtensions` option. Fixes #18
  • Loading branch information
ryan-roemer authored Nov 18, 2022
1 parent 6a8ceba commit aa98a36
Show file tree
Hide file tree
Showing 8 changed files with 366 additions and 97 deletions.
6 changes: 6 additions & 0 deletions .changeset/wise-trees-collect.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"trace-deps": patch
"trace-pkg": patch
---

Add `ignoreExtensions` option to `traceFile`/`traceFiles` in `trace-deps` and `trace-pkg` options (see #18).
2 changes: 2 additions & 0 deletions packages/trace-deps/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ Trace and return on-disk locations of all file dependencies from a source file.
_Parameters_:

* `srcPath` (`string`): source file path to trace
* `ignoreExtensions` (`Array<string>`): A set of file extensions (e.g., `.map` or `.graphql`) to skip tracing on. These files will still be included in the bundle. These are added to our built-in extensions to skip of `.json` and `.node`.
* `ignores` (`Array<string>`): list of package prefixes to ignore tracing entirely
* `conditions` (`Array<string>`): list of Node.js runtime import [user conditions](https://nodejs.org/api/packages.html#resolving-user-conditions) to trace in addition to our default built-in Node.js conditions of `import`, `require`, `node`, and `default`.
* `allowMissing` (`Object.<string, Array<string>`): Mapping of (1) absolute source file paths and (2) package name or relative file path keys to permitted missing module prefixes values.
Expand Down Expand Up @@ -69,6 +70,7 @@ Trace and return on-disk locations of all file dependencies from source files.
_Parameters_:

* `srcPaths` (`Array<string>`): source file paths to trace
* `ignoreExtensions` (`Array<string>`): set of file extensions to skip tracing on
* `ignores` (`Array<string>`): list of package prefixes to ignore
* `conditions` (`Array<string>`): list of Node.js runtime import user conditions to trace.
* `allowMissing` (`Object.<string, Array<string>`): Mapping of source file paths and package names/paths to permitted missing module prefixes.
Expand Down
26 changes: 22 additions & 4 deletions packages/trace-deps/lib/trace.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const { toPosixPath } = require("./path");

// File extensions that we know are or are not JavaScript (but still
// require-able in Node.js)
const NOT_JS_EXTS = new Set([".json", ".node"]);
const NOT_JS_EXTS = [".json", ".node"];

// Extensions to infer for resolving. Follow Node.js algorithm.
const RESOLVE_EXTS = [".js", ".json"];
Expand Down Expand Up @@ -184,11 +184,13 @@ const isAllowedMiss = ({ depName, srcPath, allowMissing }) => {
const _recurseDeps = async ({
srcPaths,
depPaths = [],
ignoreExtensions = [],
ignores = [],
conditions = [],
allowMissing = {},
bailOnMissing = true,
includeSourceMaps = false,
_ignoreExtensions,
_extraImports,
_tracedDepPaths
}) => {
Expand All @@ -209,17 +211,19 @@ const _recurseDeps = async ({
_tracedDepPaths.add(depPath);

// Short-circuit: Is it a known non-traceable extension?
if (NOT_JS_EXTS.has(path.extname(depPath))) { continue; }
if (_ignoreExtensions.some((ext) => depPath.endsWith(ext))) { continue; }

// Recurse.
// eslint-disable-next-line no-use-before-define
const traced = await traceFile({
srcPath: depPath,
ignoreExtensions,
ignores,
conditions,
allowMissing,
bailOnMissing,
includeSourceMaps,
_ignoreExtensions,
_extraImports,
_tracedDepPaths
});
Expand Down Expand Up @@ -503,25 +507,29 @@ const _resolveDep = async ({
*
* @param {*} opts options object
* @param {string} opts.srcPath source file path to trace
* @param {Array<string>} opts.ignoreExtensions list of extensions to skip tracing on
* @param {Array<string>} opts.ignores list of package prefixes to ignore
* @param {Array<string>} opts.conditions list of additional user conditions to trace
* @param {Object} opts.allowMissing map packages to list of allowed missing package
* @param {boolean} opts.bailOnMissing allow static dependencies to be missing
* @param {boolean} opts.includeSourceMaps include source map paths in output
* @param {Object} opts.extraImports map files to additional imports to trace
* @param {Object} opts._extraImports (internal) normalized map
* @param {Array<string>} opts._ignoreExtensions (internal) ignored extensions
* @param {Object} opts._extraImports (internal) normalized imports
* @param {Set} opts._tracedDepPaths (internal) tracked dependencies
* @returns {Promise<Object>} dependencies and other information
*/
// eslint-disable-next-line max-statements,complexity
const traceFile = async ({
srcPath,
ignoreExtensions = [],
ignores = [],
conditions = [],
allowMissing = {},
bailOnMissing = true,
includeSourceMaps = false,
extraImports = {},
_ignoreExtensions,
_extraImports,
_tracedDepPaths = new Set()
} = {}) => {
Expand All @@ -530,6 +538,7 @@ const traceFile = async ({
}

// Parameters
_ignoreExtensions = _ignoreExtensions || NOT_JS_EXTS.concat(ignoreExtensions);
_extraImports = _extraImports || normalizeExtraImports(extraImports);

// Get source.
Expand Down Expand Up @@ -647,11 +656,13 @@ const traceFile = async ({
_tracedDepPaths.add(srcPath);
const recursed = await _recurseDeps({
depPaths,
ignoreExtensions,
ignores,
conditions,
allowMissing,
bailOnMissing,
includeSourceMaps,
_ignoreExtensions,
_extraImports,
_tracedDepPaths
});
Expand All @@ -678,37 +689,44 @@ const traceFile = async ({
*
* @param {*} opts options object
* @param {Array<string>} opts.srcPaths source file paths to trace
* @param {Array<string>} opts.ignoreExtensions list of extensions to skip tracing on
* @param {Array<string>} opts.ignores list of package prefixes to ignore
* @param {Array<string>} opts.conditions list of additional user conditions to trace
* @param {Object} opts.allowMissing map packages to list of allowed missing package
* @param {boolean} opts.bailOnMissing allow static dependencies to be missing
* @param {boolean} opts.includeSourceMaps include source map paths in output
* @param {Object} opts.extraImports map files to additional imports to trace
* @param {Object} opts._extraImports (internal) normalized map
* @param {Array<string>} opts._ignoreExtensions (internal) ignored extensions
* @param {Object} opts._extraImports (internal) normalized imports
* @param {Set} opts._tracedDepPaths (internal) tracked dependencies
* @returns {Promise<Object>} dependencies and other information
*/
const traceFiles = async ({
srcPaths,
ignoreExtensions = [],
ignores = [],
conditions = [],
allowMissing = {},
bailOnMissing = true,
includeSourceMaps = false,
extraImports = {},
_ignoreExtensions,
_extraImports,
_tracedDepPaths = new Set()
} = {}) => {
_ignoreExtensions = _ignoreExtensions || NOT_JS_EXTS.concat(ignoreExtensions);
_extraImports = _extraImports || normalizeExtraImports(extraImports);

// Recurse all source files.
const results = await _recurseDeps({
srcPaths,
ignoreExtensions,
ignores,
conditions,
allowMissing,
bailOnMissing,
includeSourceMaps,
_ignoreExtensions,
_extraImports,
_tracedDepPaths
});
Expand Down
Loading

0 comments on commit aa98a36

Please sign in to comment.