Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions lib/express.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
var path = require('path');
var mime = require('mime');
var debug = require('debug')('serve-spm:express');
var urlparse = require('url').parse;

var parse = require('./parse');
var util = require('./util');
Expand All @@ -14,14 +15,18 @@ module.exports = function(root, opts) {
var ignore = Array.isArray(opts.ignore) ? opts.ignore : [];

return function(req, res, next) {
next = req.headers['servespmexit'] ? notFound : (next || notFound);

if (Array.isArray(opts.paths)) {
opts.paths.forEach(function(p) {
req.url = req.url.replace(p[0], p[1]);
});
}

if (opts.base) {
var basepath = urlparse(opts.base).pathname;
basepath = basepath.replace(/\/$/, '');
req.url = req.url.replace(basepath, '');
}

debug('parse url %s', req.url);
var pkg = util.getPackage(root);
var rootPkg = pkg;
Expand Down Expand Up @@ -64,7 +69,8 @@ module.exports = function(root, opts) {

var opt = {
pkg: pkg,
ignore: ignore
ignore: ignore,
base: opts.base
};
debug('return transported file %s', file.path);
transport(file, opt, function(err, file) {
Expand Down
9 changes: 8 additions & 1 deletion lib/koa.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ module.exports = function(root, opts) {
}.bind(this));
}

if (opts.base) {
var basepath = urlparse(opts.base).pathname;
basepath = basepath.replace(/\/$/, '');
this.url = this.url.replace(basepath, '');
}

debug('parse url %s', this.url);
var pkg = util.getPackage(root);
var rootPkg = pkg;
Expand Down Expand Up @@ -84,7 +90,8 @@ module.exports = function(root, opts) {
debug('return transported file %s', file.path);
file = yield transportThunk(file, {
pkg: pkg,
ignore: ignore
ignore: ignore,
base: opts.base
});
data = file.contents;
ext = path.extname(file.path);
Expand Down
11 changes: 6 additions & 5 deletions lib/parser/standalonify.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,18 @@ var join = require('path').join;
var fs = require('fs');
var through = require('through2');

module.exports = function(url) {
module.exports = function(opts) {
return through.obj(function(file) {
this.push(parser(file, url));
this.push(parser(file, opts));
});
};

function parser(file, url) {
function parser(file, opts) {
var code = String(file.contents);
var sea = fs.readFileSync(join(__dirname, '../../sea.js'), 'utf-8');
var seaconfig = '\n/* Config Base */\nseajs.config({base:\'/\'});\n\n';
var init = '\n\n/*! Init */\ng_spm_init(\''+url+'\');\n';
var base = opts.base || '/';
var seaconfig = '\n/* Config Base */\nseajs.config({base:\''+base+'\'});\n\n';
var init = '\n\n/*! Init */\ng_spm_init(\''+opts.url+'\');\n';

// code = sea + seaconfig + code + init;
code = sea + seaconfig + code + init;
Expand Down
2 changes: 1 addition & 1 deletion lib/transport.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ module.exports = function transport(file, opt, cb) {
gulpif(/\.tpl$/, tplParser()),
gulpif(/\.json$/, jsonParser()),
gulpif(/\.handlebars$/, handlebarsParser({pkg: pkg})),
gulpif(useStandalone, standalonify(file.url.pathname))
gulpif(useStandalone, standalonify({url:file.url.pathname,base:opt.base}))
).once('data', function(file) {
cb(null, file);
});
Expand Down
45 changes: 45 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,23 @@ function wrap(server, middleware) {
});
});

describe('self package with base', function() {

before(function () {
app = server();
app.use(middleware(join(fixtures, 'parser'), {
base: 'http://a.com/base/'
}));
});

it('should match /base/index.js -> /index.js, wrap', function(done) {
request(app.listen())
.get('/base/index.js')
.expect(util.define('index', 'var b = require("b/0.1.0/index.js");\nconsole.log(\'a\');\n'))
.expect(200, done);
});
});

describe('dependent package', function() {

before(function() {
Expand Down Expand Up @@ -365,6 +382,34 @@ function wrap(server, middleware) {
});
});

describe('standalone with base', function() {

before(function() {
app = server();
app.use(middleware(join(fixtures, 'standalone'), {
base: 'http://a.com/b/c'
}));
});

it('with standalone', function(done) {
request(app.listen())
.get('/index.js')
.expect(/seajs\.config\(\{base:'http:\/\/a\.com\/b\/c'\}\);/)
.expect(/\ndefine\(\'index\', function\(require, exports, module\)\{\nmodule.exports = function\(\) \{\n require\(\".\/noentry\.js\"\);\n console.log\(\'standalone\'\);\n\};\n\n\}\);\n/)
.expect(/\/\*\! Init \*\/\ng_spm_init\(\'\/index.js\'\);\n$/)
.expect(200, done);
});

it('with standalone and base path', function(done) {
request(app.listen())
.get('/b/c/index.js')
.expect(/seajs\.config\(\{base:'http:\/\/a\.com\/b\/c'\}\);/)
.expect(/\ndefine\(\'index\', function\(require, exports, module\)\{\nmodule.exports = function\(\) \{\n require\(\".\/noentry\.js\"\);\n console.log\(\'standalone\'\);\n\};\n\n\}\);\n/)
.expect(/\/\*\! Init \*\/\ng_spm_init\(\'\/index.js\'\);\n$/)
.expect(200, done);
});
});

it('isModified disable', function(done) {
app = server();
app.use(middleware(join(fixtures, 'parser')));
Expand Down
20 changes: 19 additions & 1 deletion test/standalonify.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,25 @@ describe('standalonify', function() {
var sea = fs.readFileSync(join(__dirname, '../sea.js'), 'utf-8');
var expected = sea + '\n/* Config Base */\nseajs.config({base:\'/\'});\n\nalert(1);\n\n/*! Init */\ng_spm_init(\'/a\');\n';

var stream = standalonifyParser('/a');
var stream = standalonifyParser({url:'/a'});
stream.on('data', function(newFile) {
String(newFile.contents).should.be.equal(expected);
done();
});
stream.write({
url: {pathname: ''},
contents: new Buffer(origin)
});
stream.end();
});

it('base', function(done) {

var origin = 'alert(1);';
var sea = fs.readFileSync(join(__dirname, '../sea.js'), 'utf-8');
var expected = sea + '\n/* Config Base */\nseajs.config({base:\'http://a.com/b/c/\'});\n\nalert(1);\n\n/*! Init */\ng_spm_init(\'/a\');\n';

var stream = standalonifyParser({url:'/a',base:'http://a.com/b/c/'});
stream.on('data', function(newFile) {
String(newFile.contents).should.be.equal(expected);
done();
Expand Down