Skip to content
This repository was archived by the owner on Jan 16, 2018. It is now read-only.

Commit ea1e218

Browse files
committed
Initial commit
0 parents  commit ea1e218

10 files changed

+353
-0
lines changed

Diff for: .gitattributes

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* text=auto

Diff for: .gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/node_modules

Diff for: README.md

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# webpack-core
2+
3+
The shared core of [webpack](https://github.com/webpack/webpack) and [enhanced-require](https://github.com/webpack/enhanced-require).
4+
5+
It mainly encapsulate
6+
7+
* the loader stuff
8+
9+
Not useable as standalone module, but this may change in the future.
10+
11+
# License
12+
13+
Copyright (c) 2012 - 2013 Tobias Koppers
14+
15+
MIT (http://www.opensource.org/licenses/mit-license.php)

Diff for: lib/ModuleBuildError.js

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/*
2+
MIT License http://www.opensource.org/licenses/mit-license.php
3+
Author Tobias Koppers @sokra
4+
*/
5+
function ModuleBuildError(module, err) {
6+
Error.call(this);
7+
Error.captureStackTrace(this, ModuleBuildError);
8+
this.name = "ModuleBuildError";
9+
this.message = "Module build failed: " + err.toString();
10+
this.module = module;
11+
this.error = err;
12+
}
13+
module.exports = ModuleBuildError;
14+
15+
ModuleBuildError.prototype = Object.create(Error.prototype);

Diff for: lib/ModuleError.js

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/*
2+
MIT License http://www.opensource.org/licenses/mit-license.php
3+
Author Tobias Koppers @sokra
4+
*/
5+
function ModuleError(err) {
6+
Error.call(this);
7+
Error.captureStackTrace(this, ModuleError);
8+
this.name = "ModuleError";
9+
this.message = err;
10+
this.error = err;
11+
}
12+
module.exports = ModuleError;
13+
14+
ModuleError.prototype = Object.create(Error.prototype);

Diff for: lib/ModuleWarning.js

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/*
2+
MIT License http://www.opensource.org/licenses/mit-license.php
3+
Author Tobias Koppers @sokra
4+
*/
5+
function ModuleWarning(warning) {
6+
Error.call(this);
7+
Error.captureStackTrace(this, ModuleWarning);
8+
this.name = "ModuleWarning";
9+
this.message = warning;
10+
this.warning = warning;
11+
}
12+
module.exports = ModuleWarning;
13+
14+
ModuleWarning.prototype = Object.create(Error.prototype);

Diff for: lib/NormalModuleMixin.js

+213
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,213 @@
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() {};

Diff for: lib/RawSource.js

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/*
2+
MIT License http://www.opensource.org/licenses/mit-license.php
3+
Author Tobias Koppers @sokra
4+
*/
5+
var Source = require("./Source");
6+
7+
function RawSource(value) {
8+
Source.call(this);
9+
this._value = value;
10+
}
11+
module.exports = RawSource;
12+
13+
RawSource.prototype = Object.create(Source.prototype);
14+
RawSource.prototype._bake = function() {
15+
return {
16+
source: this._value
17+
};
18+
};

Diff for: lib/Source.js

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
MIT License http://www.opensource.org/licenses/mit-license.php
3+
Author Tobias Koppers @sokra
4+
*/
5+
function Source() {
6+
this._result = null;
7+
}
8+
module.exports = Source;
9+
10+
Source.prototype.source = function() {
11+
if(!this._result)
12+
this._result = this._bake();
13+
return this._result.source;
14+
};
15+
Source.prototype.size = function() {
16+
if(!this._result)
17+
this._result = this._bake();
18+
return this._result.source.length;
19+
};
20+
Source.prototype.map = function() {
21+
if(!this._result)
22+
this._result = this._bake();
23+
return this._result.map;
24+
};
25+
Source.prototype.origins = function() {
26+
if(!this._result)
27+
this._result = this._bake();
28+
return this._result.origins;
29+
};
30+
Source.prototype.updateHash = function(hash) {
31+
if(!this._result)
32+
this._result = this._bake();
33+
hash.update(this._result.source || "");
34+
// TODO
35+
// hash.update(this._result.map || "");
36+
// hash.update(this._result.origins || "");
37+
};

Diff for: package.json

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
"name": "webpack-core",
3+
"version": "0.1.0",
4+
"author": "Tobias Koppers @sokra",
5+
"description": "The core of webpack and enhanced-require.",
6+
"dependencies": {
7+
},
8+
"licenses": [
9+
{
10+
"type": "MIT",
11+
"url": "http://www.opensource.org/licenses/mit-license.php"
12+
}
13+
],
14+
"devDependencies": {
15+
"mocha": "1.3.x",
16+
"should": "1.1.x"
17+
},
18+
"engines": {
19+
"node": ">=0.6"
20+
},
21+
"homepage": "http://github.com/webpack/core",
22+
"scripts": {
23+
"test": "mocha --reporter spec"
24+
}
25+
}

0 commit comments

Comments
 (0)