|
| 1 | +/* |
| 2 | + MIT License http://www.opensource.org/licenses/mit-license.php |
| 3 | + Author Tobias Koppers @sokra |
| 4 | +*/ |
| 5 | +var RawSource = require("./RawSource"); |
| 6 | +var path = require("path"); |
| 7 | + |
| 8 | +var ModuleBuildError = require("./ModuleBuildError"); |
| 9 | +var ModuleError = require("./ModuleError"); |
| 10 | +var ModuleWarning = require("./ModuleWarning"); |
| 11 | + |
| 12 | +function NormalModuleMixin(request, preLoaders, postLoaders) { |
| 13 | + var splittedRequest = request.split("!"); |
| 14 | + this.resource = splittedRequest.pop(); |
| 15 | + this.loaders = splittedRequest; |
| 16 | + var resourcePath = this.splitQuery(this.resource)[0]; |
| 17 | + this.context = resourcePath && path.dirname(resourcePath); |
| 18 | + this.request = request; |
| 19 | + this.preLoaders = preLoaders; |
| 20 | + this.postLoaders = postLoaders; |
| 21 | + this.fileDependencies = []; |
| 22 | + this.contextDependencies = []; |
| 23 | +} |
| 24 | +module.exports = NormalModuleMixin; |
| 25 | + |
| 26 | +NormalModuleMixin.mixin = function(pt) { |
| 27 | + for(var name in NormalModuleMixin.prototype) |
| 28 | + pt[name] = NormalModuleMixin.prototype[name]; |
| 29 | +}; |
| 30 | + |
| 31 | +NormalModuleMixin.prototype.splitQuery = function splitQuery(req) { |
| 32 | + var i = req.indexOf("?"); |
| 33 | + if(i < 0) return [req, ""]; |
| 34 | + return [req.substr(0, i), req.substr(i)]; |
| 35 | +}; |
| 36 | + |
| 37 | +NormalModuleMixin.prototype.build = function(options, resolver, fs, callback) { |
| 38 | + var splitQuery = this.splitQuery.bind(this); |
| 39 | + |
| 40 | + // Prepare context |
| 41 | + var loaders = []; |
| 42 | + function addLoaderToList(loader) { |
| 43 | + var l = splitQuery(loader); |
| 44 | + loaders.push({ |
| 45 | + request: loader, |
| 46 | + path: l[0], |
| 47 | + query: l[1], |
| 48 | + module: null |
| 49 | + }); |
| 50 | + } |
| 51 | + this.preLoaders.forEach(addLoaderToList); |
| 52 | + this.loaders.forEach(addLoaderToList); |
| 53 | + this.postLoaders.forEach(addLoaderToList); |
| 54 | + var loaderContext = { |
| 55 | + version: 1, |
| 56 | + context: this.context, |
| 57 | + loaders: loaders, |
| 58 | + loaderIndex: 0, |
| 59 | + resource: this.resource, |
| 60 | + resourcePath: splitQuery(this.resource)[0], |
| 61 | + resourceQuery: this.resource ? splitQuery(this.resource)[1] : null, |
| 62 | + emitWarning: function(warning) { |
| 63 | + this.warnings.push(new ModuleWarning(warning)); |
| 64 | + }.bind(this), |
| 65 | + emitError: function(error) { |
| 66 | + this.errors.push(new ModuleError(error)); |
| 67 | + }.bind(this), |
| 68 | + resolve: function(context, request, callback) { |
| 69 | + resolver.resolve(context, request, callback); |
| 70 | + }, |
| 71 | + resolveSync: function(context, request, callback) { |
| 72 | + return resolver.resolveSync(context, request); |
| 73 | + }, |
| 74 | + addDependency: function(file) { |
| 75 | + this.fileDependencies.push(file); |
| 76 | + }.bind(this), |
| 77 | + addContextDependency: function(context) { |
| 78 | + this.contextDependencies.push(context); |
| 79 | + }.bind(this), |
| 80 | + clearDependencies: function() { |
| 81 | + this.fileDependencies.length = 0; |
| 82 | + this.contextDependencies.length = 0; |
| 83 | + }.bind(this), |
| 84 | + options: options, |
| 85 | + debug: options.debug, |
| 86 | + }; |
| 87 | + this.fillLoaderContext(loaderContext, options); |
| 88 | + if(options.loader) for(var key in options.loader) |
| 89 | + loaderContext[key] = options.loader[key]; |
| 90 | + |
| 91 | + |
| 92 | + function runSyncOrAsync(fn, context, args, callback) { |
| 93 | + var isSync = true; |
| 94 | + var isDone = false; |
| 95 | + var isError = false; // internal error |
| 96 | + context.async = function() { |
| 97 | + if(isDone) throw new Error("async(): The callback was already called."); |
| 98 | + isSync = false; |
| 99 | + return context.callback; |
| 100 | + }; |
| 101 | + context.callback = function() { |
| 102 | + if(isDone) throw new Error("callback(): The callback was already called."); |
| 103 | + isDone = true; |
| 104 | + isSync = false; |
| 105 | + try { |
| 106 | + callback.apply(null, arguments); |
| 107 | + } catch(e) { |
| 108 | + isError = true; |
| 109 | + throw e; |
| 110 | + } |
| 111 | + }; |
| 112 | + try { |
| 113 | + var result = fn.apply(context, args); |
| 114 | + if(isSync) { |
| 115 | + isDone = true; |
| 116 | + if(result === undefined) |
| 117 | + return callback(); |
| 118 | + return callback(null, result); |
| 119 | + } |
| 120 | + } catch(e) { |
| 121 | + if(isError) throw e; |
| 122 | + if(isDone) { |
| 123 | + // loader is already "done", so we cannot use the callback function |
| 124 | + // for better debugging we print the error on the console |
| 125 | + if(typeof e === "object" && e.stack) console.error(e.stack); |
| 126 | + else console.error(e); |
| 127 | + return; |
| 128 | + } |
| 129 | + isDone = true; |
| 130 | + callback(e); |
| 131 | + } |
| 132 | + |
| 133 | + } |
| 134 | + |
| 135 | + // Load and pitch loaders |
| 136 | + (function loadPitch() { |
| 137 | + var l = loaderContext.loaders[loaderContext.loaderIndex]; |
| 138 | + if(!l) { |
| 139 | + return onLoadPitchDone.call(this); |
| 140 | + } |
| 141 | + if(l.module) { |
| 142 | + loaderContext.loaderIndex++; |
| 143 | + return loadPitch.call(this); |
| 144 | + } |
| 145 | + if(require.supportQuery) { |
| 146 | + l.module = require(l.request); |
| 147 | + } else { |
| 148 | + l.module = require(l.path); |
| 149 | + } |
| 150 | + if(typeof l.module !== "function") |
| 151 | + return callback(new Error("Loader " + l.request + " didn't returned a function")); |
| 152 | + var remaining = []; |
| 153 | + for(var i = loaderContext.loaderIndex; i < loaderContext.loaders.length; i++) |
| 154 | + remaining.push(loaderContext.loaders[i].request); |
| 155 | + remaining.push(loaderContext.resource); |
| 156 | + if(typeof l.module.pitch !== "function") return loadPitch.call(this); |
| 157 | + runSyncOrAsync(l.module.pitch, loaderContext, [remaining.join("!"), l.data = {}], function(err) { |
| 158 | + if(err) return onModuleBuildFailed.call(this, err); |
| 159 | + var args = Array.prototype.slice.call(arguments, 1); |
| 160 | + if(args.length > 0) { |
| 161 | + onModuleBuild.apply(this, args); |
| 162 | + } else { |
| 163 | + loadPitch.call(this); |
| 164 | + } |
| 165 | + }.bind(this)); |
| 166 | + }.call(this)); |
| 167 | + |
| 168 | + |
| 169 | + function onLoadPitchDone() { |
| 170 | + loaderContext.loaderIndex++; |
| 171 | + var resourcePath = loaderContext.resourcePath; |
| 172 | + if(resourcePath) { |
| 173 | + loaderContext.addDependency(resourcePath); |
| 174 | + fs.readFile(resourcePath, nextLoader.bind(this)); |
| 175 | + } else |
| 176 | + nextLoader.call(this, null, null); |
| 177 | + } |
| 178 | + |
| 179 | + function nextLoader(err/*, paramBuffer1, paramBuffer2, ...*/) { |
| 180 | + var args = Array.prototype.slice.apply(arguments, 1); |
| 181 | + if(err) { |
| 182 | + // a loader emitted an error |
| 183 | + return onModuleBuildFailed.call(this, err); |
| 184 | + } |
| 185 | + if(loaderContext.loaderIndex === 0) { |
| 186 | + if(Buffer.isBuffer(args[0])) |
| 187 | + args[0] = args[0].toString("utf-8"); |
| 188 | + return onModuleBuild.apply(this, args); |
| 189 | + } |
| 190 | + loaderContext.loaderIndex--; |
| 191 | + var l = loaderContext.loaders[loaderContext.loaderIndex]; |
| 192 | + loaderContext.data = l.data; |
| 193 | + if(!l.module.raw && Buffer.isBuffer(args[0])) { |
| 194 | + args[0] = args[0].toString("utf-8"); |
| 195 | + } else if(l.module.raw && typeof args[0] === "string") { |
| 196 | + args[0] = new Buffer(args[0], "utf-8"); |
| 197 | + } |
| 198 | + runSyncOrAsync(l.module, loaderContext, args, nextLoader); |
| 199 | + } |
| 200 | + |
| 201 | + |
| 202 | + function onModuleBuild(source) { |
| 203 | + this._source = new RawSource(source); |
| 204 | + return callback(); |
| 205 | + } |
| 206 | + |
| 207 | + function onModuleBuildFailed(err) { |
| 208 | + this.error = err; |
| 209 | + return callback(err); |
| 210 | + } |
| 211 | +}; |
| 212 | + |
| 213 | +NormalModuleMixin.prototype.fillLoaderContext = function fillLoaderContext() {}; |
0 commit comments