8000 fix: Quantum breaks on more than one configured globals (#1223) · fuse-box/fuse-box@f509d19 · 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 f509d19

Browse files
Josh Pikenchanged
authored andcommitted
fix: Quantum breaks on more than one configured globals (#1223)
* Fix typo AllowedExtenstions > AllowedExtensions * setEnryPoint -> setEntryPoint * fix: quantum tweaks * fix: quantum working properly with globals configuration * fix: reinstate use of * for server (and browser if desired)
1 parent 3c6631f commit f509d19

File tree

4 files changed

+51
-34
lines changed

4 files changed

+51
-34
lines changed

src/quantum/core/FileAbstraction.ts

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ export class FileAbstraction {
3737

3838
public isEcmaScript6 = false;
3939
public shakable = false;
40-
public globalsName: string;
4140
public amountOfReferences = 0;
4241
public canBeRemoved = false;
4342

@@ -135,6 +134,7 @@ export class FileAbstraction {
135134
this.ast = acornParse​​(contents);
136135
this.analyse();
137136
}
137+
138138
public setID(id: any) {
139139
this.id = id;
140140
}
@@ -246,9 +246,8 @@ export class FileAbstraction {
246246
return this.globalVariables.has("exports") || this.globalVariables.has("module");
247247
}
248248

249-
public setEntryPoint(globalsName?: string) {
249+
public setEntryPoint() {
250250
this.isEntryPoint = true;
251-
this.globalsName = globalsName;
252251
this.treeShakingRestricted = true;
253252
}
254253

@@ -282,17 +281,17 @@ export class FileAbstraction {
282281

283282
// process.env
284283
if (this.core) {
285-
if( this.core.opts.definedExpressions){
284+
if (this.core.opts.definedExpressions) {
286285
const matchedExpression = matchesDefinedExpression(node, this.core.opts.definedExpressions)
287-
if ( matchedExpression ){
288-
if( matchedExpression.isConditional ){
286+
if (matchedExpression) {
287+
if (matchedExpression.isConditional) {
289288
const result = compareStatement(node, matchedExpression.value);
290289
const block = new ReplaceableBlock(node.test, "left", node.test.left);
291290
this.processNodeEnv.add(block);
292291
return block.conditionalAnalysis(node, result);
293292
} else {
294293
const block = new ReplaceableBlock(parent, prop, node);
295-
if(block === undefined){
294+
if (block === undefined) {
296295
block.setUndefinedValue()
297296
} else {
298297
block.setValue(matchedExpression.value)

src/quantum/plugin/FlatFileGenerator.ts

Lines changed: 37 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,11 @@ import { BundleAbstraction } from "../core/BundleAbstraction";
55
export class FlatFileGenerator {
66
public contents = [];
77
public entryId;
8-
public globalsName: string;
8+
public globals = new Map<string, string>();
99
constructor(public core: QuantumCore, public bundleAbstraction?: BundleAbstraction​​) { }
10-
public addGlobal(code: string) {
11-
this.contents.push(code);
10+
11+
public setGlobals(packageName: string, fileID: string) {
12+
this.globals.set(packageName, fileID);
1213
}
1314

1415
public init() {
@@ -43,7 +44,6 @@ export class FlatFileGenerator {
4344
let fileId = file.getID();
4445
if (file.isEntryPoint) {
4546
this.entryId = fileId;
46-
this.globalsName = file.globalsName;
4747
}
4848
this.contents.push(`// ${file.packageAbstraction.name}/${file.fuseBoxPath}`);
4949
this.contents.push(`${this.core.opts.quantumVariableName}.f[${JSON.stringify(fileId)}] = ${file.generate(ensureES5)}`);
@@ -59,35 +59,56 @@ export class FlatFileGenerator {
5959
public render() {
6060
if (this.bundleAbstraction) {
6161
this.addHoistedVariables();
62-
}
63-
if (this.bundleAbstraction) {
62+
6463
if (this.bundleAbstraction.globalVariableRequired) {
6564
const defineGlobalFn = "var global = window";
6665
if (this.core.opts.isTargetBrowser()) {
6766
this.contents.push(defineGlobalFn);
6867
}
6968
}
69+
7070
}
71+
72+
if (this.core.opts.isTargetBrowser()) {
73+
this.globals.forEach((fileID, globalName) => {
74+
const req = `${this.core.opts.quantumVariableName}.r(${JSON.stringify(fileID)})`;
75+
if (globalName == '*') {
76+
this.contents.push(`var r = ${req}`);
77+
this.contents.push(`if (r){for(var i in r){ window[i] = r[i] }}`);
78+
} else {
79+
this.contents.push(`window['${globalName}']=${req}`);
80+
}
81+
})
82+
}
83+
7184
if (this.entryId !== undefined) {
85+
7286
const req = `${this.core.opts.quantumVariableName}.r(${JSON.stringify(this.entryId)})`;
7387

74-
if (this.globalsName) {
75-
if (this.core.opts.isTargetNpm() || this.core.opts.isTargetServer()) {
76-
this.contents.push(`module.exports = ${req}`);
77-
}
88+
if (this.core.opts.isTargetNpm() || this.core.opts.isTargetServer()) {
7889

79-
if (this.core.opts.isTargetBrowser()) {
80-
if (this.globalsName === "*") {
81-
this.contents.push(`var r = ${req}`);
82-
this.contents.push(`if (r){for(var i in r){ window[i] = r[i] }}`);
83-
} else {
84-
this.contents.push(`window['${this.globalsName}']=${req}`);
90+
// look for a global mention of the entry package, ignore other settings. this could use some improvement.
91+
var dirtyCheck = false;
92+
this.globals.forEach((fileID, globalName) => {
93+
if (fileID == this.entryId && globalName == '*') {
94+
dirtyCheck = true;
8595
}
96+
});
97+
98+
if (dirtyCheck) {
99+
this.contents.push(`module.exports = ${req}`);
100+
} else {
101+
this.contents.push(req);
86102
}
103+
87104
} else {
105+
88106
this.contents.push(req);
107+
89108
}
109+
90110
}
111+
91112
// finish wrapping
92113
if (this.core.opts.isTargetBrowser() || this.core.opts.isTargetUniveral()) {
93114
if (this.core.opts.isContained()) {

src/quantum/plugin/QuantumCore.ts

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -218,30 +218,22 @@ export class QuantumCore {
218218
entryId = `${this.producer.entryPackageName}/${this.producer.entryPackageFile}`;
219219
}
220220

221-
// define globals
222-
const globals = this.producer.fuse.context.globals;
223-
let globalsName;
224-
if (globals) {
225-
for (let i in globals) { globalsName = globals[i]; }
226-
}
227-
//console.log("here", bundleAbstraction.packageAbstractions);
228221
bundleAbstraction.packageAbstractions.forEach(packageAbstraction => {
229222
packageAbstraction.fileAbstractions.forEach((fileAbstraction, key: string) => {
230223
let fileId = fileAbstraction.getFuseBoxFullPath();
231224
const id = this.index;
232225
this.handleMappings(fileId, id);
233226
this.index++;
234227
if (fileId === entryId) {
235-
fileAbstraction.setEntryPoint(globalsName);
228+
fileAbstraction.setEntryPoint();
236229
}
237230
fileAbstraction.setID(id);
238231
});
239232
});
240233
}
241-
242234
public async processBundle(bundleAbstraction: BundleAbstraction) {
243235
this.log.echoInfo(`Process bundle ${bundleAbstraction.name}`);
244-
await each(bundleAbstraction.packageAbstractions, (packageAbstraction: PackageAbstraction) => {
236+
await each(bundleAbstraction.packageAbstractions, (packageAbstraction: PackageAbstraction) => {
245237
const fileSize = packageAbstraction.fileAbstractions.size;
246238
this.log.echoInfo(`Process package ${packageAbstraction.name} `);
247239
this.log.echoInfo(` Files: ${fileSize} `);
@@ -261,11 +253,14 @@ export class QuantumCore {
261253
}
262254
public render() {
263255
return each(this.producerAbstraction.bundleAbstractions, (bundleAbstraction: BundleAbstraction​​) => {
264-
256+
const globals = this.producer.fuse.context.globals;
265257
const generator = new FlatFileGenerator(this, bundleAbstraction);
266258
generator.init();
267259
return each(bundleAbstraction.packageAbstractions, (packageAbstraction: PackageAbstraction) => {
268260
return each(packageAbstraction.fileAbstractions, (fileAbstraction: FileAbstraction) => {
261+
if (fileAbstraction.fuseBoxPath == packageAbstraction.entryFile && globals && Object.keys(globals).indexOf(packageAbstraction.name) != -1) {
262+
generator.setGlobals(globals[packageAbstraction.name], fileAbstraction.getID());
263+
}
269264
return generator.addFile(fileAbstraction, this.opts.shouldEnsureES5());
270265
});
271266

src/tsconfig.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@
1919
"preserveConstEnums": true,
2020
"suppressImplicitAnyIndexErrors": true
2121
},
22+
"include": [ "**/*.ts", ],
2223
"exclude": [
24+
"**/*.d.ts",
2325
"loader/**/*",
2426
"modules/**/*"
2527
]

0 commit comments

Comments
 (0)
0