From 1d5729e8b82682b50fc0e531a83eda3224098dc2 Mon Sep 17 00:00:00 2001 From: Yanko Valera Date: Tue, 21 Dec 2021 15:04:57 +0100 Subject: [PATCH 1/4] Start user conditions --- lib/trace.js | 38 +++++++++++++++++++++++++++++++------- 1 file changed, 31 insertions(+), 7 deletions(-) diff --git a/lib/trace.js b/lib/trace.js index eeaed16..86a1451 100644 --- a/lib/trace.js +++ b/lib/trace.js @@ -151,7 +151,9 @@ const _recurseDeps = async ({ bailOnMissing = true, includeSourceMaps = false, _extraImports, - _tracedDepPaths + _tracedDepPaths, + userConditions, + replaceConditions }) => { // Start **only** with depPaths. let dependencies = Array.from(depPaths); @@ -181,7 +183,9 @@ const _recurseDeps = async ({ bailOnMissing, includeSourceMaps, _extraImports, - _tracedDepPaths + _tracedDepPaths, + userConditions, + replaceConditions }); // Aggregate. @@ -208,6 +212,8 @@ const _resolveDep = async ({ addMisses, allowMissing, bailOnMissing, + userConditions, + replaceConditions, addRootPackagePaths = false }) => { // Capture all package.json files encountered in node resolution. @@ -298,7 +304,13 @@ const _resolveDep = async ({ } }); - CONDITIONS.forEach((cond) => { + const conditions = replaceConditions + ? userConditions + : [...CONDITIONS, ...userConditions]; + + console.log("conditions", conditions) + + conditions.forEach((cond) => { let resolveOpts; if (Array.isArray(cond)) { resolveOpts = cond[1]; @@ -452,6 +464,8 @@ const _resolveDep = async ({ * @param {Object} opts.extraImports map files to additional imports to trace * @param {Object} opts._extraImports (internal) normalized map * @param {Set} opts._tracedDepPaths (internal) tracked dependencies + * @param {Array} opts.userConditions list of conditional exports https://nodejs.org/api/packages.html#packages_conditional_exports + * @param {boolean} opts.replaceConditions flag to replace conditions with userConditions * @returns {Promise} dependencies and other information */ // eslint-disable-next-line max-statements,complexity @@ -463,7 +477,9 @@ const traceFile = async ({ includeSourceMaps = false, extraImports = {}, _extraImports, - _tracedDepPaths = new Set() + _tracedDepPaths = new Set(), + userConditions = [], + replaceConditions = false } = {}) => { if (!srcPath) { throw new Error("Empty source file path"); @@ -553,7 +569,9 @@ const traceFile = async ({ extraDepKeys, addMisses, allowMissing, - bailOnMissing + bailOnMissing, + userConditions, + replaceConditions }))) // Post-resolution processing. .then((allPaths) => allPaths @@ -614,6 +632,8 @@ const traceFile = async ({ * @param {Object} opts.extraImports map files to additional imports to trace * @param {Object} opts._extraImports (internal) normalized map * @param {Set} opts._tracedDepPaths (internal) tracked dependencies + * @param {Array} opts.userConditions list of conditional exports https://nodejs.org/api/packages.html#packages_conditional_exports + * @param {boolean} opts.replaceConditions flag to replace conditions with userConditions * @returns {Promise} dependencies and other information */ const traceFiles = async ({ @@ -624,7 +644,9 @@ const traceFiles = async ({ includeSourceMaps = false, extraImports = {}, _extraImports, - _tracedDepPaths = new Set() + _tracedDepPaths = new Set(), + userConditions = [], + replaceConditions = false } = {}) => { _extraImports = _extraImports || normalizeExtraImports(extraImports); @@ -636,7 +658,9 @@ const traceFiles = async ({ bailOnMissing, includeSourceMaps, _extraImports, - _tracedDepPaths + _tracedDepPaths, + userConditions, + replaceConditions }); // Make unique and return. From f68765626cfa9989facc75095d5a940bca7d8e5f Mon Sep 17 00:00:00 2001 From: Yanko Valera Date: Tue, 21 Dec 2021 16:41:05 +0100 Subject: [PATCH 2/4] Add user conditions with flag to replace --- lib/trace.js | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/lib/trace.js b/lib/trace.js index 86a1451..4716477 100644 --- a/lib/trace.js +++ b/lib/trace.js @@ -304,11 +304,7 @@ const _resolveDep = async ({ } }); - const conditions = replaceConditions - ? userConditions - : [...CONDITIONS, ...userConditions]; - - console.log("conditions", conditions) + const conditions = replaceConditions ? userConditions : CONDITIONS.concat(userConditions); conditions.forEach((cond) => { let resolveOpts; From d2fedda0617cd3b0a90f8ba910bb6802adf20258 Mon Sep 17 00:00:00 2001 From: Yanko Valera Date: Fri, 7 Jan 2022 12:27:09 +0100 Subject: [PATCH 3/4] Add tests for userConditions --- test/lib/trace.spec.js | 52 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) diff --git a/test/lib/trace.spec.js b/test/lib/trace.spec.js index 34213e1..d00495a 100644 --- a/test/lib/trace.spec.js +++ b/test/lib/trace.spec.js @@ -2793,5 +2793,57 @@ describe("lib/trace", () => { } }); }); + + it("Use userConditions when tracing files", async () => { + mock({ + "hi.js": ` + const core = require("trace/core"); + `, + node_modules: { + trace: { + "package.json": stringify({ + name: "trace", + main: "index.cjs", + exports: { + ".": { + require: "index.cjs" + }, + "./core": { + lite: { + node: "./core-lite.cjs" + }, + require: "./core.cjs" + } + } + }), + "index.cjs": ` + const hello = () => {}; + module.exports = hello; + `, + "core.cjs": ` + const core = () => {}; + + module.exports = core; + `, + "core-lite.cjs": ` + const coreLight = () => {}; + + module.exports = coreLight; + ` + } + } + }); + + const { dependencies } = await traceFiles({ + srcPaths: ["hi.js"], + userConditions: ["lite"], + replaceConditions: true + }); + + expect(dependencies).to.eql(fullPaths([ + "node_modules/trace/core-lite.cjs", + "node_modules/trace/package.json" + ])); + }); }); }); From ae2795c052174d5998a320a85f8e14cc3988715d Mon Sep 17 00:00:00 2001 From: Yanko Valera Date: Fri, 7 Jan 2022 12:29:36 +0100 Subject: [PATCH 4/4] no message --- test/lib/trace.spec.js | 55 +++++++++++++++++++++++++++++++++++++++++- 1 file changed, 54 insertions(+), 1 deletion(-) diff --git a/test/lib/trace.spec.js b/test/lib/trace.spec.js index d00495a..428f007 100644 --- a/test/lib/trace.spec.js +++ b/test/lib/trace.spec.js @@ -2794,7 +2794,7 @@ describe("lib/trace", () => { }); }); - it("Use userConditions when tracing files", async () => { + it("Replace conditions with supplied userConditions when tracing files", async () => { mock({ "hi.js": ` const core = require("trace/core"); @@ -2845,5 +2845,58 @@ describe("lib/trace", () => { "node_modules/trace/package.json" ])); }); + + it("Append supplied userConditions to conditions when tracing files", async () => { + mock({ + "hi.js": ` + const core = require("trace/core"); + `, + node_modules: { + trace: { + "package.json": stringify({ + name: "trace", + main: "index.cjs", + exports: { + ".": { + require: "index.cjs" + }, + "./core": { + lite: { + node: "./core-lite.cjs" + }, + require: "./core.cjs" + } + } + }), + "index.cjs": ` + const hello = () => {}; + module.exports = hello; + `, + "core.cjs": ` + const core = () => {}; + + module.exports = core; + `, + "core-lite.cjs": ` + const coreLight = () => {}; + + module.exports = coreLight; + ` + } + } + }); + + const { dependencies } = await traceFiles({ + srcPaths: ["hi.js"], + userConditions: ["lite"], + replaceConditions: false + }); + + expect(dependencies).to.eql(fullPaths([ + "node_modules/trace/core-lite.cjs", + "node_modules/trace/core.cjs", + "node_modules/trace/package.json" + ])); + }); }); });