Skip to content

Commit

Permalink
format-specific mains for cycles
Browse files Browse the repository at this point in the history
  • Loading branch information
guybedford committed Nov 14, 2014
1 parent d12fbba commit 7a25d33
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 6 deletions.
24 changes: 23 additions & 1 deletion lib/common.js
Expand Up @@ -67,4 +67,26 @@ exports.alphabetize = function(obj) {
newObj[p] = obj[p];
});
return newObj;
}
}


exports.getRedirectContents = function(format, main) {
if (format == 'es6')
return 'export * from "' + main + '";';

else if (format == 'cjs' || format == 'global')
return 'module.exports = require("' + main + '");';

else if (format == 'amd')
return 'define(["' + main + '"], function(main) {\n return main;\n});';

else if (format == 'register')
return 'System.register(["' + main + '"], ' +
'function($__export) {\n return {\n setters: [function(m) { for (var p in m) $__export(p, m[p]); }],\n execute: function() {}\n };\n});'

else
throw 'Unknown module format ' + format + '.';
}



24 changes: 19 additions & 5 deletions lib/package.js
Expand Up @@ -28,6 +28,7 @@ var ep = require('./endpoint');
var PackageName = require('./config/package-name');
var globalConfig = require('./global-config');
var readJSON = require('./common').readJSON;
var getRedirectContents = require('./common').getRedirectContents;
var ncp = require('ncp');
var crypto = require('crypto');
var fs = require('graceful-fs');
Expand Down Expand Up @@ -503,6 +504,7 @@ exports.processPackage = function(pkg, dir, pjson) {

exports.createMain = function(pkg, pjson, downloadDir) {
var lastNamePart, main;
var mainPath;

return Promise.resolve()

Expand All @@ -522,7 +524,8 @@ exports.createMain = function(pkg, pjson, downloadDir) {

// try the package.json main
return new Promise(function(resolve, reject) {
fs.exists(path.resolve(downloadDir, main.substr(main.length - 3, 3) != '.js' ? main + '.js' : main), resolve);
mainPath = path.resolve(downloadDir, main.substr(main.length - 3, 3) != '.js' ? main + '.js' : main);
fs.exists(mainPath, resolve);
});
})
.then(function(exists) {
Expand All @@ -531,13 +534,24 @@ exports.createMain = function(pkg, pjson, downloadDir) {

main = lastNamePart;
return new Promise(function(resolve, reject) {
fs.exists(path.resolve(downloadDir, main.substr(main.length - 3, 3) != '.js' ? main + '.js' : main), resolve);
mainPath = path.resolve(downloadDir, main.substr(main.length - 3, 3) != '.js' ? main + '.js' : main);
fs.exists(mainPath, resolve);
});
})
.then(function(exists) {
// create the main pointer
var mainFile = path.resolve(downloadDir, '../' + lastNamePart + '@' + pkg.version + '.js');
return asp(fs.writeFile)(mainFile, 'export * from "' + pkg.exactName + '/' + main + '";');
// don't create a main if it doesn't exist
if (!exists)
return;

// detect the format of the main
return asp(fs.readFile)(mainPath)
.then(function(source) {
var detected = build.detectFormat(source.toString());

// create the main pointer
var mainFile = path.resolve(downloadDir, '../' + lastNamePart + '@' + pkg.version + '.js');
return asp(fs.writeFile)(mainFile, getRedirectContents(detected.format, pkg.exactName + '/' + main));
});
});
}

Expand Down

0 comments on commit 7a25d33

Please sign in to comment.