UNPKG

6.92 kBJavaScriptView Raw
1var makeGraph = require("../graph/make_graph"),
2 makeBundleFromModuleName = require("../graph/get"),
3 concatSource = require("../bundle/concat_source"),
4 mapDependencies = require("../graph/map_dependencies"),
5
6 logging = require('../logger'),
7 hasES6 = require('../graph/has_es6'),
8 _ = require('lodash'),
9 hasES6 = require("../graph/has_es6"),
10 addTraceurRuntime = require("../bundle/add_traceur_runtime"),
11 addGlobalShim = require("../bundle/add_global_shim"),
12 addProcessShim = require("../bundle/add_process_shim"),
13 transpile = require('../graph/transpile'),
14 clean = require("../graph/clean"),
15 minify = require("../bundle/minify_source"),
16 winston = require('winston'),
17 removeActiveSourceKeys = require("../graph/remove_active_source_keys"),
18 nameBundle = require("../bundle/name"),
19 makeConfiguration = require("../configuration/make"),
20 addSourceMapUrl = require("../bundle/add_source_map_url");
21
22var transformImport = function(config, transformOptions){
23 transformOptions = _.assign(transformOptions || {}, {
24 useNormalizedDependencies: true
25 });
26 if(transformOptions.sourceMaps) {
27 _.assign(config, {
28 lessOptions: _.assign({}, transformOptions.lessOptions, {
29 sourceMap: {}
30 })
31 });
32 }
33 transformOptions.exports = transformOptions.exports || {};
34
35 // Setup logging
36 logging.setup(transformOptions, config);
37
38 return makeGraph(config, transformOptions).then(function(data){
39
40 return transformImport.normalizeExports(data.steal.System,
41 transformOptions)
42 .then(function(){
43 return data;
44 });
45 }).then(function(data){
46
47 var transform = function(moduleNames, options){
48 options = _.extend({
49 ignore: [],
50 minify: false,
51 removeDevelopmentCode: true,
52 format: "global",
53 concatFormat: "amd",
54 noGlobalShim: false,
55 includeTraceurRuntime: true,
56 ignoreAllDependencies: false,
57 removeSourceNodes: true,
58 addProcessShim: false
59 },transformOptions, options);
60 var configuration = data.configuration =
61 makeConfiguration(data.loader, data.buildLoader, options);
62
63 if(!moduleNames) {
64 moduleNames = data.loader.main;
65 }
66 if(typeof moduleNames === "string"){
67 moduleNames = [moduleNames];
68 }
69
70 moduleNames.forEach(function(moduleName){
71 if(!data.graph[moduleName]){
72 throw new Error("Can't find module '" + moduleName + "' in graph.");
73 }
74 });
75 var nodesInBundle;
76
77 // get nodes
78 if(options.ignoreAllDependencies) {
79
80 nodesInBundle = moduleNames.map(function(moduleName){
81 return data.graph[moduleName];
82 });
83
84 } else {
85 // get all nodes that are dependencies of moduleName
86 var nodes = makeBundleFromModuleName(data.graph, moduleNames);
87 // figure ot what modules should be ignored
88 var ignores = transformImport.getAllIgnores(options.ignore, data.graph);
89 // get only the nodes that should be in the bundle.
90 nodesInBundle = transformImport.notIgnored(nodes, ignores);
91 }
92
93 // If there's nothing in this bundle, just give them an empty
94 // source object.
95 if(nodesInBundle.length === 0) {
96 return Promise.resolve({code: ""});
97 }
98
99 // resets the active source to be worked from.
100 removeActiveSourceKeys(nodesInBundle, options);
101
102 // #### clean
103 // Clean first
104 if(options.removeDevelopmentCode) {
105 winston.debug('Cleaning...');
106 clean(nodesInBundle, options);
107 }
108 // Minify would make sense to do next as it is expensive. But it
109 // makes transpile hard to debug.
110
111 // #### transpile
112 winston.debug('Transpiling...');
113 options.sourceMapPath = configuration.bundlesPath;
114
115 transpile(nodesInBundle,
116 options.format === "global" ? "amd" : options.format,
117 options.format === "global" ? _.assign(_.clone(options),{namedDefines: true}) : options,
118 data);
119
120 var bundle = {
121 bundles: moduleNames,
122 nodes: nodesInBundle,
123 buildType: nodesInBundle[0].load.metadata.buildType || "js"
124 };
125 bundle.name = nameBundle.getName(bundle);
126
127 // add shim if global
128 if(options.format === "global" && !options.noGlobalShim) {
129 addGlobalShim(bundle, options);
130 }
131 if(options.format === "global" && !options.noProcessShim) {
132 addProcessShim(bundle, options);
133 }
134 if(hasES6(nodesInBundle) && options.includeTraceurRuntime){
135 addTraceurRuntime(bundle);
136 }
137 // Add shim for `process`
138 if(options.addProcessShim) {
139 addProcessShim(bundle, options);
140 }
141 winston.debug('Output Modules:');
142 bundle.nodes.forEach(function(node) {
143 winston.debug("+ %s", node.load.name);
144 });
145
146 return Promise.resolve()
147 .then(function(){
148 var concatOptions = {
149 excludePlugins: options.format === "global",
150 sourceProp: "activeSource",
151 format: options.concatFormat,
152 removeDevelopmentCode: options.removeDevelopmentCode
153 };
154 return concatSource(bundle, concatOptions);
155 })
156 .then(function(){
157 if(options.sourceMaps) {
158 addSourceMapUrl(bundle);
159 }
160
161 return bundle;
162 })
163 .then(function(bundle) {
164 // #### minify
165 if(options.minify) {
166 winston.debug('Minifying...');
167 return minify(bundle);
168 } else {
169 return bundle.source;
170 }
171 });
172 };
173
174 // Set the graph on transform in case anyone needs to use it.
175 transform.graph = data.graph;
176 transform.loader = data.loader;
177 transform.data = data;
178 return transform;
179
180
181 });
182
183};
184
185
186
187var matches = function(rules, name, load){
188 if(rules === name) {
189 return true;
190 }else if( Array.isArray(rules) ) {
191 for(var i =0; i < rules.length; i++) {
192 if( matches(rules[i], name, load) ) {
193 return true;
194 }
195 }
196 } else if( rules instanceof RegExp) {
197 return rules.test(name);
198 } else if( typeof rules === "function") {
199 return rules(name, load);
200 }
201
202};
203
204transformImport.getAllIgnores = function(baseIgnores, graph) {
205 var ignores = [];
206 baseIgnores = Array.isArray(baseIgnores) ? baseIgnores : [ baseIgnores ];
207
208 baseIgnores.forEach(function(moduleName){
209
210 // An ignore could be a regular expression, this only applies to strings.
211 if(typeof moduleName === "string") {
212 // Add this ignore's dependencies
213 ignores = ignores.concat(mapDependencies(graph, moduleName, function(name){
214 return name;
215 }));
216 } else {
217 ignores.push(moduleName);
218 }
219
220 });
221
222 return ignores;
223};
224
225transformImport.notIgnored = function( bundle, rules ) {
226 var notIgnored = [];
227 bundle.filter(function(b) { return !!b; })
228 .forEach(function(node){
229 if( !matches(rules, node.load.name, node.load ) ) {
230 notIgnored.push(node);
231 }
232 });
233 return notIgnored;
234};
235
236transformImport.normalizeExports = function(loader, options) {
237 var exports = options.exports;
238 var main = loader.main;
239
240 var promises = Object.keys(exports).map(function(oldName){
241 return loader.normalize(oldName, main).then(function(name){
242 if(name !== oldName) {
243 exports[name] = exports[oldName];
244 delete exports[oldName];
245 }
246 });
247 });
248
249 return Promise.all(promises);
250};
251
252transformImport.matches = matches;
253
254
255module.exports = transformImport;