8000 fix(Quantum): global process and process.env are handled gracefully · fuse-box/fuse-box@341d5ff · 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 341d5ff

Browse files
committed
fix(Quantum): global process and process.env are handled gracefully
1 parent 81b404e commit 341d5ff

File tree

6 files changed

+190
-25
lines changed

6 files changed

+190
-25
lines changed

src/quantum/core/AstUtils.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,35 @@ export function isTrueRequireFunction(node) {
297297
export function matchesSingleFunction(node: any, name: string) {
298298
return node.callee && node.callee.type === "Identifier" && node.callee.name === name;
299299
}
300+
export function matchesGlobalVariable(node: any, name: string) {
301+
const parent = node.$parent;
302+
const isMemberExpression = parent && parent.type === "MemberExpression";
303+
const isProperty = parent && parent.type === "Property";
304+
const isDeclarator = parent && parent.type === "VariableDeclarator";
305+
return node.type === "Identifier" && node.name === name && !isMemberExpression && !isProperty && !isDeclarator;
306+
}
307+
308+
export function matchesGlobalVariableReference(node: any, name: string) {
309+
const [first, second] = name.split(".");
310+
const parent = node.$parent;
311+
const isMemberExpression = parent && parent.type === "MemberExpression";
312+
const isProperty = parent && parent.type === "Property";
313+
const isDeclarator = parent && parent.type === "VariableDeclarator";
314+
315+
const initial =
316+
node.type === "Identifier" && node.name === first && !isProperty && !isDeclarator && isMemberExpression;
317+
let secondCondition = true;
318+
if (second && initial) {
319+
secondCondition = parent.property && parent.property.type === "Identifier" && parent.property.name === second;
320+
}
321+
return initial && secondCondition;
322+
}
323+
324+
export function matchesVariableDeclarator(node: any, name: string) {
325+
const parent = node.$parent;
326+
const isDeclarator = parent && parent.type === "VariableDeclarator";
327+
return node.type === "Identifier" && node.name === name && isDeclarator;
328+
}
300329

301330
export function trackRequireMember(node: any, name: string): string {
302331
if (node && node.type === "MemberExpression") {

src/quantum/core/FileAbstraction.ts

Lines changed: 48 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,44 @@
1-
import { acornParse } from "../../analysis/FileAnalysis";
2-
import { PackageAbstraction } from "./PackageAbstraction";
3-
import { ASTTraverse } from "../../ASTTraverse";
4-
import { RequireStatement } from "./nodes/RequireStatement";
51
import * as escodegen from "escodegen";
62
import * as path from "path";
3+
import { acornParse } from "../../analysis/FileAnalysis";
4+
import { ASTTraverse } from "../../ASTTraverse";
75
import { ensureFuseBoxPath, transpileToEs5 } from "../../Utils";
8-
6+
import { QuantumBit } from "../plugin/QuantumBit";
7+
import { QuantumCore } from "../plugin/QuantumCore";
98
import {
9+
compareStatement,
10+
isExportComputed,
11+
isExportMisused,
12+
isTrueRequireFunction,
13+
matcheObjectDefineProperty,
1014
matchesAssignmentExpression,
11-
matchesLiteralStringExpression,
12-
matchesSingleFunction,
15+
matchesDefinedExpression,
1316
matchesDoubleMemberExpression,
14-
matcheObjectDefineProperty,
1517
matchesEcmaScript6,
18+
matchesExportReference,
19+
matchesGlobalVariable,
20+
matchesIfStatementFuseBoxIsEnvironment,
21+
matchesIfStatementProcessEnv,
22+
matchesLiteralStringExpression,
23+
matchesNodeEnv,
24+
matchesSingleFunction,
1625
matchesTypeOf,
26+
matchNamedExport,
1727
matchRequireIdentifier,
1828
trackRequireMember,
19-
matchNamedExport,
20-
isExportMisused,
21-
matchesNodeEnv,
22-
matchesExportReference,
23-
matchesIfStatementProcessEnv,
24-
compareStatement,
25-
matchesIfStatementFuseBoxIsEnvironment,
26-
isExportComputed,
27-
isTrueRequireFunction,
28-
matchesDefinedExpression,
29+
matchesVariableDeclarator,
30+
matchesGlobalVariableReference,
2931
} from "./AstUtils";
3032
import { ExportsInterop } from "./nodes/ExportsInterop";
31-
import { UseStrict } from "./nodes/UseStrict";
33+
import { GenericAst } from "./nodes/GenericAst";
34+
import { NamedExport } from "./nodes/NamedExport";
35+
import { ReplaceableBlock } from "./nodes/ReplaceableBlock";
36+
import { RequireStatement } from "./nodes/RequireStatement";
3237
import { TypeOfExportsKeyword } from "./nodes/TypeOfExportsKeyword";
3338
import { TypeOfModuleKeyword } from "./nodes/TypeOfModuleKeyword";
3439
import { TypeOfWindowKeyword } from "./nodes/TypeOfWindowKeyword";
35-
import { NamedExport } from "./nodes/NamedExport";
36-
import { GenericAst } from "./nodes/GenericAst";
37-
import { QuantumCore } from "../plugin/QuantumCore";
38-
import { ReplaceableBlock } from "./nodes/ReplaceableBlock";
39-
import { QuantumBit } from "../plugin/QuantumBit";
40+
import { UseStrict } from "./nodes/UseStrict";
41+
import { PackageAbstraction } from "./PackageAbstraction";
4042

4143
const globalNames = new Set<string>(["__filename", "__dirname", "exports", "module"]);
4244

@@ -75,6 +77,9 @@ export class FileAbstraction {
7577
public typeofGlobalKeywords = new Set<GenericAst>();
7678
public typeofDefineKeywords = new Set<GenericAst>();
7779
public typeofRequireKeywords = new Set<GenericAst>();
80+
public globalProcess: Set<GenericAst>;
81+
public globalProcessVersion: Set<GenericAst>;
82+
public processVariableDefined: boolean;
7883

7984
public namedExports = new Map<string, NamedExport>();
8085
public processNodeEnv = new Set<ReplaceableBlock>();
@@ -90,9 +95,15 @@ export class FileAbstraction {
9095
private removalRestricted = false;
9196
private dependencies = new Map<FileAbstraction, Set<RequireStatement>>();
9297

98+
public renderedHeaders: string[];
99+
93100
constructor(public fuseBoxPath: string, public packageAbstraction: PackageAbstraction) {
94101
this.fuseBoxDir = ensureFuseBoxPath(path.dirname(fuseBoxPath));
95102
this.setID(fuseBoxPath);
103+
this.globalProcess = new Set();
104+
this.renderedHeaders = [];
105+
this.globalProcessVersion = new Set();
106+
this.processVariableDefined = false;
96107
packageAbstraction.registerFileAbstraction(this);
97108
this.core = this.packageAbstraction.bundleAbstraction.producerAbstraction.quantumCore;
98109

@@ -269,6 +280,9 @@ export class FileAbstraction {
269280
if (this.isDirnameUsed()) {
270281
fn.push(`var __dirname = ${JSON.stringify(this.fuseBoxDir)};` + "\n");
271282
}
283+
if (this.renderedHeaders.length) {
284+
fn.push(this.renderedHeaders.join("\n") + "\n");
285+
}
272286
if (this.isFilenameUsed()) {
273287
fn.push(`var __filename = ${JSON.stringify(this.fuseBoxPath)};` + "\n");
274288
}
@@ -286,6 +300,7 @@ export class FileAbstraction {
286300
*/
287301
private onNode(node, parent, prop, idx) {
288302
// process.env
303+
289304
if (this.core) {
290305
if (this.core.opts.definedExpressions) {
291306
const matchedExpression = matchesDefinedExpression(node, this.core.opts.definedExpressions);
@@ -364,6 +379,15 @@ export class FileAbstraction {
364379
}
365380
}
366381

382+
if (matchesGlobalVariable(node, "process")) {
383+
this.globalProcess.add(new GenericAst(parent, prop, node));
384+
}
385+
if (matchesGlobalVariableReference(node, "process.version")) {
386+
this.globalProcessVersion.add(new GenericAst(parent, prop, node));
387+
}
388+
if (matchesVariableDeclarator(node, "process")) {
389+
this.processVariableDefined = true;
390+
}
367391
// detecting es6
368392
if (matchesEcmaScript6(node)) {
369393
this.isEcmaScript6 = true;

src/quantum/plugin/QuantumCore.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ import { QuantumBit } from "./QuantumBit";
3030
import { CSSModifications } from "./modifications/CSSModifications";
3131
import { CSSCollection } from "../core/CSSCollection";
3232
import { QuantumTask } from "../core/QuantumTask";
33+
import { GlobalProcessReplacement } from "./modifications/GlobalProcessReplacement";
34+
import { GlobalProcessVersionReplacement } from "./modifications/GlobalProcessVersionReplacement";
3335

3436
export interface QuantumStatementMapping {
3537
statement: RequireStatement;
@@ -211,7 +213,7 @@ export class QuantumCore {
211213

212214
public prepareFiles(bundleAbstraction: BundleAbstraction) {
213215
bundleAbstraction.packageAbstractions.forEach(packageAbstraction => {
214-
let entryId = `${this.producer.entryPackageName}/${packageAbstraction.entryFile}`
216+
let entryId = `${this.producer.entryPackageName}/${packageAbstraction.entryFile}`;
215217
packageAbstraction.fileAbstractions.forEach((fileAbstraction, key: string) => {
216218
let fileId = fileAbstraction.getFuseBoxFullPath();
217219
const id = this.index;
@@ -318,6 +320,10 @@ export class QuantumCore {
318320
TypeOfModifications,
319321
// process.env removal
320322
ProcessEnvModification,
323+
// process global replacement with "undefined"
324+
GlobalProcessReplacement,
325+
// process.version global replacement with ""
326+
GlobalProcessVersionReplacement,
321327
];
322328
return each(modifications, (modification: IPerformable) => modification.perform(this, file));
323329
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { FileAbstraction } from "../../core/FileAbstraction";
2+
import { QuantumCore } from "../QuantumCore";
3+
4+
export class GlobalProcessReplacement {
5+
public static perform(core: QuantumCore, file: FileAbstraction) {
6+
if (!file.processVariableDefined) {
7+
file.globalProcess.forEach(item => {
8+
item.replaceWithString(undefined);
9+
});
10+
}
11+
}
12+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { FileAbstraction } from "../../core/FileAbstraction";
2+
import { QuantumCore } from "../QuantumCore";
3+
4+
export class GlobalProcessVersionReplacement {
5+
public static perform(core: QuantumCore, file: FileAbstraction) {
6+
if (!file.processVariableDefined) {
7+
if (file.globalProcessVersion.size) {
8+
file.renderedHeaders.push(`var process = { version : "" };`);
9+
}
10+
}
11+
}
12+
}
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
import { should } from "fuse-test-runner";
2+
import { createOptimisedBundleEnv } from "../../tests/stubs/TestEnvironment";
3+
4+
export class RemoveProcess {
5+
"Should replace process with undefined"() {
6+
return createOptimisedBundleEnv({
7+
stubs: true,
8+
options: {
9+
removeExportsInterop: false,
10+
},
11+
project: {
12+
natives: {
13+
process: false,
14+
},
15+
files: {
16+
"index.ts": `
17+
if ( process ) {
18+
console.log("hello")
19+
}
20+
`,
21+
"dev.ts": ``,
22+
},
23+
instructions: "> index.ts",
24+
},
25+
}).then(result => {
26+
const contents = result.contents["index.js"];
27+
should(contents).notFindString("process");
28+
should(contents).findString("undefined");
29+
});
30+
}
31+
32+
"Should not process with undefined"() {
33+
return createOptimisedBundleEnv({
34+
stubs: true,
35+
options: {
36+
removeExportsInterop: false,
37+
},
38+
project: {
39+
natives: {
40+
process: false,
41+
},
42+
files: {
43+
"index.ts": `
44+
var process = {};
45+
if ( process ) {
46+
console.log("hello")
47+
}
48+
`,
49+
"dev.ts": ``,
50+
},
51+
instructions: "> index.ts",
52+
},
53+
}).then(result => {
54+
const contents = result.contents["index.js"];
55+
should(contents).findString("if (process) {");
56+
});
57+
}
58+
59+
"Should replace process.version with a string"() {
60+
return createOptimisedBundleEnv({
61+
stubs: true,
62+
options: {
63+
removeExportsInterop: false,
64+
},
65+
project: {
66+
natives: {
67+
process: false,
68+
},
69+
files: {
70+
"index.ts": `
71+
var version = process.version;
72+
`,
73+
"dev.ts": ``,
74+
},
75+
instructions: "> index.ts",
76+
},
77+
}).then(result => {
78+
const contents = result.contents["index.js"];
79+
should(contents).findString(`var process = { version : "" };`);
80+
});
81+
}
82+
}

0 commit comments

Comments
 (0)
0