1
- import { acornParse } from "../../analysis/FileAnalysis" ;
2
- import { PackageAbstraction } from "./PackageAbstraction" ;
3
- import { ASTTraverse } from "../../ASTTraverse" ;
4
- import { RequireStatement } from "./nodes/RequireStatement" ;
5
1
import * as escodegen from "escodegen" ;
6
2
import * as path from "path" ;
3
+ import { acornParse } from "../../analysis/FileAnalysis" ;
4
+ import { ASTTraverse } from "../../ASTTraverse" ;
7
5
import { ensureFuseBoxPath , transpileToEs5 } from "../../Utils" ;
8
-
6
+ import { QuantumBit } from "../plugin/QuantumBit" ;
7
+ import { QuantumCore } from "../plugin/QuantumCore" ;
9
8
import {
9
+ compareStatement ,
10
+ isExportComputed ,
11
+ isExportMisused ,
12
+ isTrueRequireFunction ,
13
+ matcheObjectDefineProperty ,
10
14
matchesAssignmentExpression ,
11
- matchesLiteralStringExpression ,
12
- matchesSingleFunction ,
15
+ matchesDefinedExpression ,
13
16
matchesDoubleMemberExpression ,
14
- matcheObjectDefineProperty ,
15
17
matchesEcmaScript6 ,
18
+ matchesExportReference ,
19
+ matchesGlobalVariable ,
20
+ matchesIfStatementFuseBoxIsEnvironment ,
21
+ matchesIfStatementProcessEnv ,
22
+ matchesLiteralStringExpression ,
23
+ matchesNodeEnv ,
24
+ matchesSingleFunction ,
16
25
matchesTypeOf ,
26
+ matchNamedExport ,
17
27
matchRequireIdentifier ,
18
28
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 ,
29
31
} from "./AstUtils" ;
30
32
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" ;
32
37
import { TypeOfExportsKeyword } from "./nodes/TypeOfExportsKeyword" ;
33
38
import { TypeOfModuleKeyword } from "./nodes/TypeOfModuleKeyword" ;
34
39
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" ;
40
42
41
43
const globalNames = new Set < string > ( [ "__filename" , "__dirname" , "exports" , "module" ] ) ;
42
44
@@ -75,6 +77,9 @@ export class FileAbstraction {
75
77
public typeofGlobalKeywords = new Set < GenericAst > ( ) ;
76
78
public typeofDefineKeywords = new Set < GenericAst > ( ) ;
77
79
public typeofRequireKeywords = new Set < GenericAst > ( ) ;
80
+ public globalProcess : Set < GenericAst > ;
81
+ public globalProcessVersion : Set < GenericAst > ;
82
+ public processVariableDefined : boolean ;
78
83
79
84
public namedExports = new Map < string , NamedExport > ( ) ;
80
85
public processNodeEnv = new Set < ReplaceableBlock > ( ) ;
@@ -90,9 +95,15 @@ export class FileAbstraction {
90
95
private removalRestricted = false ;
91
96
private dependencies = new Map < FileAbstraction , Set < RequireStatement > > ( ) ;
92
97
98
+ public renderedHeaders : string [ ] ;
99
+
93
100
constructor ( public fuseBoxPath : string , public packageAbstraction : PackageAbstraction ) {
94
101
this . fuseBoxDir = ensureFuseBoxPath ( path . dirname ( fuseBoxPath ) ) ;
95
102
this . setID ( fuseBoxPath ) ;
103
+ this . globalProcess = new Set ( ) ;
104
+ this . renderedHeaders = [ ] ;
105
+ this . globalProcessVersion = new Set ( ) ;
106
+ this . processVariableDefined = false ;
96
107
packageAbstraction . registerFileAbstraction ( this ) ;
97
108
this . core = this . packageAbstraction . bundleAbstraction . producerAbstraction . quantumCore ;
98
109
@@ -269,6 +280,9 @@ export class FileAbstraction {
269
280
if ( this . isDirnameUsed ( ) ) {
270
281
fn . push ( `var __dirname = ${ JSON . stringify ( this . fuseBoxDir ) } ;` + "\n" ) ;
271
282
}
283
+ if ( this . renderedHeaders . length ) {
284
+ fn . push ( this . renderedHeaders . join ( "\n" ) + "\n" ) ;
285
+ }
272
286
if ( this . isFilenameUsed ( ) ) {
273
287
fn . push ( `var __filename = ${ JSON . stringify ( this . fuseBoxPath ) } ;` + "\n" ) ;
274
288
}
@@ -286,6 +300,7 @@ export class FileAbstraction {
286
300
*/
287
301
private onNode ( node , parent , prop , idx ) {
288
302
// process.env
303
+
289
304
if ( this . core ) {
290
305
if ( this . core . opts . definedExpressions ) {
291
306
const matchedExpression = matchesDefinedExpression ( node , this . core . opts . definedExpressions ) ;
@@ -364,6 +379,15 @@ export class FileAbstraction {
364
379
}
365
380
}
366
381
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
+ }
367
391
// detecting es6
368
392
if ( matchesEcmaScript6 ( node ) ) {
369
393
this . isEcmaScript6 = true ;
0 commit comments