8000 fix(Quantum): Quantum file resolution of conflicting libraries · fuse-box/fuse-box@9cea9e5 · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content
This repository was archived by the owner on Jun 20, 2023. It is now read-only.

Commit 9cea9e5

Browse files
committed
fix(Quantum): Quantum file resolution of conflicting libraries
1 parent dd664d5 commit 9cea9e5

File tree

6 files changed

+77
-7
lines changed

6 files changed

+77
-7
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,5 +123,5 @@
123123
"test": "node test.js"
124124
},
125125
"typings": "index.d.ts",
126-
"version": "3.5.0-next.5"
126+
"version": "3.5.0-next.8"
127127
}

src/quantum/core/BundleAbstraction.ts

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,17 +54,29 @@ export class BundleAbstraction {
5454
parent.arguments &&
5555
parent.arguments.length === 3
5656
) {
57-
let pkgName = parent.arguments[0].value;
58-
if (pkgName.charAt(0) !== "@") {
59-
pkgName = pkgName.split("@")[0];
57+
const conflictingLibraries: { [key: string]: string } = {};
58+
// handle the following scenario
59+
// FuseBox.pkg("aafoo", {"aaboo":"1.0.9"}, function(___scope___){
60+
// Second arguments in original FuseBox bundle is responsible for
61+
// conflicting libraries
62+
if (parent.arguments[1] && parent.arguments[1].type === "ObjectExpression") {
63+
const versionsNode = parent.arguments[1];
64+
if (versionsNode.properties.length) {
65+
versionsNode.properties.forEach(prop => {
66+
conflictingLibraries[prop.key.value] = prop.value.value;
67+
});
68+
}
6069
}
70+
const pkgName = parent.arguments[0].value;
6171
const packageAst = parent.arguments[2].body;
6272
let packageAbstraction;
6373
if (this.packageAbstractions.get(pkgName)) {
6474
packageAbstraction = this.packageAbstractions.get(pkgName);
6575
} else {
6676
packageAbstraction = new PackageAbstraction(pkgName, this);
6777
}
78+
// store it so we can get to it from anywhere
79+
packageAbstraction.conflictingLibraries = conflictingLibraries;
6880
packageAbstraction.loadAst(packageAst);
6981
return false;
7082
}

src/quantum/core/PackageAbstraction.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ export class PackageAbstraction {
99
public entries = new Map<string, FileAbstraction>();
1010

1111
public quantumBit: QuantumBit;
12+
public conflictingLibraries: { [key: string]: string };
1213
public quantumBitBanned = false;
1314
public quantumDynamic = false;
1415
constructor(public name: string, public bundleAbstraction: BundleAbstraction) {

src/quantum/core/nodes/RequireStatement.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -176,8 +176,16 @@ export class RequireStatement {
176176
if (this.isComputed) {
177177
return;
178178
}
179-
const pkgName = !this.isNodeModule ? this.file.packageAbstraction.name : this.nodeModuleName;
180-
179+
let pkgName = !this.isNodeModule ? this.file.packageAbstraction.name : this.nodeModuleName;
180+
const conflictingVersion =
181+
this.file.packageAbstraction &&
182+
this.file.packageAbstraction.conflictingLibraries &&
183+
this.file.packageAbstraction.conflictingLibraries[pkgName];
184+
// if an abstraction contains the override we need to consider it
185+
// and reference with `{name}@{version}` as it's registered as such
186+
if (conflictingVersion) {
187+
pkgName += `@${conflictingVersion}`;
188+
}
181189
let resolvedName;
182190
const producerAbstraction = this.file.packageAbstraction.bundleAbstraction.producerAbstraction;
183191
if (!this.isNodeModule) {

src/quantum/plugin/QuantumBit.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,11 @@ export class QuantumBit {
8989
const dependency = item[0];
9090
if (dependency.belongsToProject()) {
9191
if (dependency.quantumBit && dependency.quantumBit !== this) {
92-
dependency.quantumBitBanned = true;
92+
dependency.referencedRequireStatements.forEach(ref => {
93+
if (!ref.isDynamicImport) {
94+
dependency.quantumBitBanned = true;
95+
}
96+
});
9397
} else {
9498
dependency.quantumBit = this;
9599
if (!this.candidates.has(dependency.getFuseBoxFullPath())) {
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import { should } from "fuse-test-runner";
2+
import { QuantumPlugin } from "../../index";
3+
import { FuseTestEnv, createRealNodeModule } from "../../tests/stubs/FuseTestEnv";
4+
5+
export class QuantumConflictingVersionTest {
6+
"Should work with 2 version of the same library"() {
7+
createRealNodeModule("aafoo", {
8+
"package.json": `{ "name" : "aafoo", "version" : "1"}`,
9+
"index.js": `module.exports = require("aaboo")`,
10+
});
11+
12+
createRealNodeModule("aafoo/node_modules/aaboo", {
13+
"package.json": `{ "name" : "aafoo", "version" : "1.0.9"}`,
14+
"index.js": `module.exports = {version : "1.0.9"}`,
15+
});
16+
createRealNodeModule("aaboo", {
17+
"package.json": `{ "name" : "aafoo", "version" : "1.1.0"}`,
18+
"index.js": `module.exports = {version : "1.1.0"}`,
19+
});
20+
21+
return FuseTestEnv.create({
22+
project: {
23+
files: {
24+
"index.ts": `
25+
import * as aafoo from "aafoo";
26+
import * as aaboo from "aaboo";
27+
module.exports = [aafoo, aaboo]
28+
`,
29+
},
30+
plugins: [
31+
QuantumPlugin({
32+
target: "browser",
33+
}),
34+
],
35+
},
36+
})
37+
.simple()
38+
.then(test =>
39+
test.browser(window => {
40+
const res = window.$fsx.r(0);
41+
should(res).deepEqual([{ version: "1.0.9" }, { version: "1.1.0" }]);
42+
}),
43+
);
44+
}
45+
}

0 commit comments

Comments
 (0)
0