-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcjsc-module.js
115 lines (93 loc) · 3.13 KB
/
cjsc-module.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/**
* js-import-compiler
*
* @author sheiko
* @license MIT
* jscs standard:Jquery
*/
/**
* Executing cjsc cli
* @module cjsc-module
*/
/**
* @typedef requireConfig
* @type {Object}
* @property {module:DependencyConfig} depId
*/
"use strict";
/** @type {module:fs} nodejs File I/O api */
var fs = require( "fs" ),
/** @type {module:path} nodejs api for handling and transforming file paths */
npath = require( "path" ),
/*
* @type {module:lib/SrcMapGenerator}
*/
SrcMapGenerator = require( "./lib/SrcMapGenerator" ),
/*
* @type {module:Compiler} Compiler constructor
*/
Compiler = require( "./lib/Compiler" ),
/**
* @type {module:Parser} Parser constructor
*/
Parser = require( "./lib/Parser" ),
/**
* @type {module:Replacer} Replacer constructor
*/
Replacer = require( "./lib/Replacer" ),
/**
* @type {module:DependencyEntity} DependencyEntity constructor
*/
DependencyEntity = require( "./lib/Entity/Dependency" ),
/**
* @type {module:Config} Config constructor
*/
Config = require( "./lib/Config" );
/**
* Runner
* @param {Array || Object} rawArgv - CLI arguments
* @param {requireConfig} config - Depnedency configuration
* @param {Function} [done]
*/
module.exports = function( rawArgv, config, done ) {
(function(){
var
/** @type {Compiler} */
compiler,
/** @type {Parser} */
parser,
/** @type {module:lib/FileSystem} */
fSys,
/** @type {module:lLib/Cli} Cli constructor */
cli = new require( "./lib/Cli" )(),
/** @type {SourceMapGenerator} */
srcMapGen;
cli.printHeader();
cli.run( rawArgv );
fSys = new require( "./lib/FileSystem" )( cli );
config = new Config( config || cli.options[ "config" ], fSys, cli );
parser = new Parser( DependencyEntity );
srcMapGen = new SrcMapGenerator( cli, fSys );
compiler = new Compiler( parser, fSys, config, srcMapGen, cli );
cli.srcPath = fSys.resolveFilename( cli.srcPath );
if ( cli.options[ "source-map" ] ) {
cli.options[ "source-map" ] = cli.options[ "source-map" ].replace( /\*/, npath.basename( cli.destPath ) );
fSys.setSourceMapRoot( cli.options[ "source-map-root" ] || "", cli.options[ "source-map" ] );
}
compiler.start( cli.srcPath, function( map, output ){
if ( !map ) {
return;
}
if ( cli.options[ "minify" ] ) {
output = require( "uglify-js" ).minify( output, { fromString: true }).code;
}
if ( cli.options[ "source-map" ] ) {
output += "\n//# sourceMappingURL=" + ( cli.options[ "source-map-url" ] || "./" ) + npath.basename( cli.options[ "source-map" ] );
}
fSys.writeJs( cli.destPath, cli.options.banner + output );
cli.options[ "source-map" ] && fSys.writeJs( cli.options[ "source-map" ], srcMapGen.get() );
map[ cli.srcPath ].length && cli.printBody( Object.keys( map ).length );
done && done( cli.options.banner + output );
});
}());
};