Skip to content

Commit fc17a26

Browse files
committed
feat(standalonify) add base opt to set seajs's base for standalonify, Fix #26
1 parent 55841a6 commit fc17a26

File tree

6 files changed

+71
-11
lines changed

6 files changed

+71
-11
lines changed

lib/express.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
var path = require('path');
44
var mime = require('mime');
55
var debug = require('debug')('serve-spm:express');
6+
var urlparse = require('url').parse;
67

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

1617
return function(req, res, next) {
17-
next = req.headers['servespmexit'] ? notFound : (next || notFound);
18-
1918
if (Array.isArray(opts.paths)) {
2019
opts.paths.forEach(function(p) {
2120
req.url = req.url.replace(p[0], p[1]);
2221
});
2322
}
2423

24+
if (opts.base) {
25+
var basepath = urlparse(opts.base).pathname;
26+
basepath = basepath.replace(/\/$/, '');
27+
req.url = req.url.replace(basepath, '');
28+
}
29+
2530
debug('parse url %s', req.url);
2631
var pkg = util.getPackage(root);
2732
var rootPkg = pkg;
@@ -64,7 +69,8 @@ module.exports = function(root, opts) {
6469

6570
var opt = {
6671
pkg: pkg,
67-
ignore: ignore
72+
ignore: ignore,
73+
base: opts.base
6874
};
6975
debug('return transported file %s', file.path);
7076
transport(file, opt, function(err, file) {

lib/koa.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,12 @@ module.exports = function(root, opts) {
2323
}.bind(this));
2424
}
2525

26+
if (opts.base) {
27+
var basepath = urlparse(opts.base).pathname;
28+
basepath = basepath.replace(/\/$/, '');
29+
this.url = this.url.replace(basepath, '');
30+
}
31+
2632
debug('parse url %s', this.url);
2733
var pkg = util.getPackage(root);
2834
var rootPkg = pkg;
@@ -84,7 +90,8 @@ module.exports = function(root, opts) {
8490
debug('return transported file %s', file.path);
8591
file = yield transportThunk(file, {
8692
pkg: pkg,
87-
ignore: ignore
93+
ignore: ignore,
94+
base: opts.base
8895
});
8996
data = file.contents;
9097
ext = path.extname(file.path);

lib/parser/standalonify.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,18 @@ var join = require('path').join;
44
var fs = require('fs');
55
var through = require('through2');
66

7-
module.exports = function(url) {
7+
module.exports = function(opts) {
88
return through.obj(function(file) {
9-
this.push(parser(file, url));
9+
this.push(parser(file, opts));
1010
});
1111
};
1212

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

1920
// code = sea + seaconfig + code + init;
2021
code = sea + seaconfig + code + init;

lib/transport.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ module.exports = function transport(file, opt, cb) {
3434
gulpif(/\.tpl$/, tplParser()),
3535
gulpif(/\.json$/, jsonParser()),
3636
gulpif(/\.handlebars$/, handlebarsParser({pkg: pkg})),
37-
gulpif(useStandalone, standalonify(file.url.pathname))
37+
gulpif(useStandalone, standalonify({url:file.url.pathname,base:opt.base}))
3838
).once('data', function(file) {
3939
cb(null, file);
4040
});

test/index.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,34 @@ function wrap(server, middleware) {
365365
});
366366
});
367367

368+
describe('standalone with base', function() {
369+
370+
before(function() {
371+
app = server();
372+
app.use(middleware(join(fixtures, 'standalone'), {
373+
base: 'http://a.com/b/c'
374+
}));
375+
});
376+
377+
it('with standalone', function(done) {
378+
request(app.listen())
379+
.get('/index.js')
380+
.expect(/seajs\.config\(\{base:'http:\/\/a\.com\/b\/c'\}\);/)
381+
.expect(/\ndefine\(\'index\', function\(require, exports, module\)\{\nmodule.exports = function\(\) \{\n require\(\".\/noentry\.js\"\);\n console.log\(\'standalone\'\);\n\};\n\n\}\);\n/)
382+
.expect(/\/\*\! Init \*\/\ng_spm_init\(\'\/index.js\'\);\n$/)
383+
.expect(200, done);
384+
});
385+
386+
it('with standalone and base path', function(done) {
387+
request(app.listen())
388+
.get('/b/c/index.js')
389+
.expect(/seajs\.config\(\{base:'http:\/\/a\.com\/b\/c'\}\);/)
390+
.expect(/\ndefine\(\'index\', function\(require, exports, module\)\{\nmodule.exports = function\(\) \{\n require\(\".\/noentry\.js\"\);\n console.log\(\'standalone\'\);\n\};\n\n\}\);\n/)
391+
.expect(/\/\*\! Init \*\/\ng_spm_init\(\'\/index.js\'\);\n$/)
392+
.expect(200, done);
393+
});
394+
});
395+
368396
it('isModified disable', function(done) {
369397
app = server();
370398
app.use(middleware(join(fixtures, 'parser')));

test/standalonify.js

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,25 @@ describe('standalonify', function() {
1010
var sea = fs.readFileSync(join(__dirname, '../sea.js'), 'utf-8');
1111
var expected = sea + '\n/* Config Base */\nseajs.config({base:\'/\'});\n\nalert(1);\n\n/*! Init */\ng_spm_init(\'/a\');\n';
1212

13-
var stream = standalonifyParser('/a');
13+
var stream = standalonifyParser({url:'/a'});
14+
stream.on('data', function(newFile) {
15+
String(newFile.contents).should.be.equal(expected);
16+
done();
17+
});
18+
stream.write({
19+
url: {pathname: ''},
20+
contents: new Buffer(origin)
21+
});
22+
stream.end();
23+
});
24+
25+
it('base', function(done) {
26+
27+
var origin = 'alert(1);';
28+
var sea = fs.readFileSync(join(__dirname, '../sea.js'), 'utf-8');
29+
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';
30+
31+
var stream = standalonifyParser({url:'/a',base:'http://a.com/b/c/'});
1432
stream.on('data', function(newFile) {
1533
String(newFile.contents).should.be.equal(expected);
1634
done();

0 commit comments

Comments
 (0)