Skip to content

Commit

Permalink
Move configuration to "steal" option
Browse files Browse the repository at this point in the history
This moves configuration to a "steal" object in package.json and
deprecates the use of "system". "system" will still be allowed going
forward for backwards compatibility the same way "jam" and whatnot is.

Closes #164
  • Loading branch information
matthewp committed Oct 20, 2016
1 parent 0340e9c commit ca4e9de
Show file tree
Hide file tree
Showing 11 changed files with 150 additions and 78 deletions.
45 changes: 25 additions & 20 deletions npm-convert.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
var crawl = require('./npm-crawl');
var utils = require("./npm-utils");

exports.system = convertSystem;
exports.steal = convertSteal;
exports.propertyNames = convertPropertyNames;
exports.propertyNamesAndValues = convertPropertyNamesAndValues;
exports.name = convertName;
Expand Down Expand Up @@ -34,50 +34,54 @@ exports.forPackage = convertForPackage;

// Translate helpers ===============
// Given all the package.json data, these helpers help convert it to a source.
function convertSystem(context, pkg, system, root, ignoreWaiting) {
if(!system) {
return system;
function convertSteal(context, pkg, steal, root, ignoreWaiting) {
if(!steal) {
return steal;
}
var copy = utils.extend({}, system, true);
var copy = utils.extend({}, steal, true);
var waiting = utils.isArray(ignoreWaiting) ? ignoreWaiting : [];
if(system.meta) {
system.meta = convertPropertyNames(context, pkg, system.meta, root, waiting);
if(steal.meta) {
steal.meta = convertPropertyNames(context, pkg, steal.meta, root,
waiting);
}
if(system.map) {
system.map = convertPropertyNamesAndValues(context, pkg, system.map, root, waiting);
if(steal.map) {
steal.map = convertPropertyNamesAndValues(context, pkg, steal.map,
root, waiting);
}
if(system.paths) {
system.paths = convertPropertyNames(context, pkg, system.paths, root, waiting);
if(steal.paths) {
steal.paths = convertPropertyNames(context, pkg, steal.paths, root,
waiting);
}
// needed for builds
if(system.buildConfig) {
system.buildConfig = convertSystem(context, pkg, system.buildConfig, root, waiting);
if(steal.buildConfig) {
steal.buildConfig = convertSteal(context, pkg, steal.buildConfig,
root, waiting);
}

// Push the waiting conversions down.
if(ignoreWaiting !== true && waiting.length) {
convertLater(context, waiting, function(){
var context = this;
var local = utils.extend({}, copy, true);
var config = convertSystem(context, pkg, local, root, true);
var config = convertSteal(context, pkg, local, root, true);

// If we are building we need to resave the package's system
// configuration so that it will be written out into the build.
if(context.resavePackageInfo) {
var info = utils.pkg.findPackageInfo(context, pkg);
info.system = config;
info.steal = info.system = config;
}

// Temporarily remove system.main so that it doesn't set System.main
var systemMain = config.main;
// Temporarily remove steal.main so that it doesn't set System.main
var stealMain = config.main;
delete config.main;
delete config.transpiler;
context.loader.config(config);
config.main = systemMain;
config.main = stealMain;
});
}

return system;
return steal;
}

// converts only the property name
Expand Down Expand Up @@ -289,6 +293,7 @@ function convertToPackage(context, pkg, index) {
name: pkg.fileUrl.split('/').pop(),
metadata: {}
}, pkg);
var steal = utils.pkg.config(pkg);

localPkg = {
name: pkg.name,
Expand All @@ -297,7 +302,7 @@ function convertToPackage(context, pkg, index) {
pkg.fileUrl :
utils.relativeURI(context.loader.baseURL, pkg.fileUrl),
main: pkg.main,
system: convertSystem(context, pkg, pkg.system, index === 0),
steal: convertSteal(context, pkg, steal, index === 0),
globalBrowser: convertBrowser(pkg, pkg.globalBrowser),
browser: convertBrowser(pkg, pkg.browser || pkg.browserify),
jspm: convertJspm(pkg, pkg.jspm),
Expand Down
35 changes: 20 additions & 15 deletions npm-crawl.js
Original file line number Diff line number Diff line change
Expand Up @@ -158,9 +158,11 @@ var crawl = {
* @return {Object<String,Range>} A map of dependency names and requested version ranges.
*/
getDependencyMap: function(loader, packageJSON, isRoot){
var system = packageJSON.system;
var config = utils.pkg.config(packageJSON);
var hasConfig = !!config;

// convert npmIgnore
var npmIgnore = system && system.npmIgnore;
var npmIgnore = hasConfig && config.npmIgnore;
function convertToMap(arr) {
var npmMap = {};
for(var i = 0; i < arr.length; i++) {
Expand All @@ -169,12 +171,12 @@ var crawl = {
return npmMap;
}
if(npmIgnore && typeof npmIgnore.length === 'number') {
npmIgnore = packageJSON.system.npmIgnore = convertToMap(npmIgnore);
npmIgnore = config.npmIgnore = convertToMap(npmIgnore);
}
// convert npmDependencies
var npmDependencies = system && system.npmDependencies;
var npmDependencies = hasConfig && config.npmDependencies;
if(npmDependencies && typeof npmDependencies.length === "number") {
packageJSON.system.npmDependencies = convertToMap(npmDependencies);
config.npmDependencies = convertToMap(npmDependencies);
}
npmIgnore = npmIgnore || {};

Expand Down Expand Up @@ -285,23 +287,24 @@ var crawl = {
}
loader.npm[name+"@"+pkg.version] = pkg;
};
if(pkg.system) {
var config = utils.pkg.config(pkg);
if(config) {
var ignoredConfig = ["bundle", "configDependencies", "transpiler"];

// don't set system.main
var main = pkg.system.main;
delete pkg.system.main;
// don't set steal.main
var main = config.main;
delete config.main;
utils.forEach(ignoredConfig, function(name){
delete pkg.system[name];
delete config[name];
});
loader.config(pkg.system);
pkg.system.main = main;
loader.config(config);
config.main = main;

}
if(pkg.globalBrowser) {
setGlobalBrowser(pkg.globalBrowser, pkg);
}
var systemName = pkg.system && pkg.system.name;
var systemName = config && config.name;
if(systemName) {
setInNpm(systemName, pkg);
} else {
Expand Down Expand Up @@ -343,9 +346,11 @@ function truthy(x) {
var alwaysIgnore = {"steal-tools":1,"bower":1,"grunt":1,"grunt-cli":1};

function addDeps(packageJSON, dependencies, deps, type, defaultProps){
var config = utils.pkg.config(packageJSON);

// convert an array to a map
var npmIgnore = packageJSON.system && packageJSON.system.npmIgnore;
var npmDependencies = packageJSON.system && packageJSON.system.npmDependencies;
var npmIgnore = config && config.npmIgnore;
var npmDependencies = config && config.npmDependencies;
var ignoreType = npmIgnore && npmIgnore[type];

function includeDep(name) {
Expand Down
7 changes: 4 additions & 3 deletions npm-extension.js
Original file line number Diff line number Diff line change
Expand Up @@ -211,9 +211,10 @@ exports.addExtension = function(System){
}
var moduleName = utils.moduleName.create(parsedModuleName);
// Apply mappings, if they exist in the refPkg
if(refPkg.system && refPkg.system.map &&
typeof refPkg.system.map[moduleName] === "string") {
moduleName = refPkg.system.map[moduleName];
var steal = utils.pkg.config(refPkg);
if(steal && steal.map &&
typeof steal.map[moduleName] === "string") {
moduleName = steal.map[moduleName];
}
var p = oldNormalize.call(loader, moduleName, parentName,
parentAddress, pluginNormalize);
Expand Down
31 changes: 16 additions & 15 deletions npm-load.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,9 @@ exports.makeSource = function(context, pkg){
*/
exports.configDeps = function(context, pkg){
var deps = [];
if(pkg.system && pkg.system.configDependencies) {
deps = deps.concat(pkg.system.configDependencies);
var config = utils.pkg.config(pkg);
if(config && config.configDependencies) {
deps = deps.concat(config.configDependencies);
}
if(context.loader.configDependencies) {
deps = deps.concat(context.loader.configDependencies);
Expand Down Expand Up @@ -124,8 +125,8 @@ var translateConfig = function(loader, packages, options){
loader.npmParentMap = options.npmParentMap || {};
}
var rootPkg = loader.npmPaths.__default = packages[0];
var lib = packages[0].system && packages[0].system.directories && packages[0].system.directories.lib;

var steal = rootPkg.steal || rootPkg.system;
var lib = steal && steal.directories && steal.directories.lib;

var setGlobalBrowser = function(globals, pkg){
for(var name in globals) {
Expand Down Expand Up @@ -163,23 +164,23 @@ var translateConfig = function(loader, packages, options){
};
var ignoredConfig = ["bundle", "configDependencies", "transpiler"];
forEach(packages, function(pkg){
if(pkg.system) {
var system = pkg.system;
// don't set system.main
var main = system.main;
delete system.main;
var configDeps = system.configDependencies;
var steal = pkg.steal || pkg.system;
if(steal) {
// don't set steal.main
var main = steal.main;
delete steal.main;
var configDeps = steal.configDependencies;
if(pkg !== rootPkg) {
forEach(ignoredConfig, function(name){
delete system[name];
delete steal[name];
});
}

loader.config(system);
loader.config(steal);
if(pkg === rootPkg) {
system.configDependencies = configDeps;
steal.configDependencies = configDeps;
}
system.main = main;
steal.main = main;
}
if(pkg.globalBrowser) {
var doNotApplyGlobalBrowser = pkg.name === "steal" &&
Expand All @@ -188,7 +189,7 @@ var translateConfig = function(loader, packages, options){
setGlobalBrowser(pkg.globalBrowser, pkg);
}
}
var systemName = system && system.name;
var systemName = steal && steal.name;
if(systemName) {
setInNpm(systemName, pkg);
} else {
Expand Down
33 changes: 22 additions & 11 deletions npm-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -268,17 +268,23 @@ var utils = {
// we have the moduleName without the version
// we check this against various configs
var mapName = utils.moduleName.create(parsedModuleName),
refSteal = utils.pkg.config(refPkg),
mappedName;

// The refPkg might have a browser [https://github.com/substack/node-browserify#browser-field] mapping.
// Perform that mapping here.
if(refPkg.browser && (typeof refPkg.browser !== "string") && (mapName in refPkg.browser) && (!refPkg.system || !refPkg.system.ignoreBrowser)) {
mappedName = refPkg.browser[mapName] === false ? "@empty" : refPkg.browser[mapName];
if(refPkg.browser && (typeof refPkg.browser !== "string") &&
(mapName in refPkg.browser) &&
(!refSteal || !refSteal.ignoreBrowser)) {
mappedName = refPkg.browser[mapName] === false ?
"@empty" : refPkg.browser[mapName];
}
// globalBrowser looks like: {moduleName: aliasName, pgk: aliasingPkg}
var global = loader && loader.globalBrowser && loader.globalBrowser[mapName];
var global = loader && loader.globalBrowser &&
loader.globalBrowser[mapName];
if(global) {
mappedName = global.moduleName === false ? "@empty" : global.moduleName;
mappedName = global.moduleName === false ? "@empty" :
global.moduleName;
}

if(mappedName) {
Expand All @@ -298,12 +304,14 @@ var utils = {
* @return {String}
*/
name: function(pkg){
return (pkg.system && pkg.system.name) || pkg.name;
var steal = utils.pkg.config(pkg);
return (steal && steal.name) || pkg.name;
},
main: function(pkg) {
var main;
if(pkg.system && pkg.system.main) {
main = pkg.system.main;
var steal = utils.pkg.config(pkg);
if(steal && steal.main) {
main = steal.main;
} else if(typeof pkg.browser === "string") {
if(utils.path.endsWithSlash(pkg.browser)) {
main = pkg.browser + "index";
Expand Down Expand Up @@ -446,8 +454,8 @@ var utils = {
}
},
directoriesLib: function(pkg) {
var system = pkg.system;
var lib = system && system.directories && system.directories.lib;
var steal = utils.pkg.config(pkg);
var lib = steal && steal.directories && steal.directories.lib;
var ignores = [".", "/"], ignore;

if(!lib) return undefined;
Expand All @@ -460,8 +468,8 @@ var utils = {
return lib;
},
hasDirectoriesLib: function(pkg) {
var system = pkg.system;
return system && system.directories && !!system.directories.lib;
var steal = utils.pkg.config(pkg);
return steal && steal.directories && !!steal.directories.lib;
},
findPackageInfo: function(context, pkg){
var pkgInfo = context.pkgInfo;
Expand All @@ -479,6 +487,9 @@ var utils = {
var npmPkg = utils.pkg.findPackageInfo(context, refPkg);
npmPkg.resolutions[pkg.name] = refPkg.resolutions[pkg.name] =
pkg.version;
},
config: function(pkg){
return pkg.steal || pkg.system;
}
},
path: {
Expand Down
13 changes: 6 additions & 7 deletions npm.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
"format cjs";

// TODO: cleanup removing package.json
var utils = require('./npm-utils');
var convert = require("./npm-convert");
var crawl = require('./npm-crawl');
var npmLoad = require("./npm-load");
var isNode = typeof process === "object" &&
{}.toString.call(process) === "[object process]";

// SYSTEMJS PLUGIN EXPORTS =================

/**
* @function translate
* @description Convert the package.json file into a System.config call.
Expand Down Expand Up @@ -50,11 +47,12 @@ exports.translate = function(load){
crawl.processPkgSource(context, pkg, load.source);

// backwards compatible for < npm 3
if(pkg.system && pkg.system.npmAlgorithm === "nested") {
var steal = utils.pkg.config(pkg);
if(steal && steal.npmAlgorithm === "nested") {
context.isFlatFileStructure = false;
} else {
pkg.system = pkg.system || {};
pkg.system.npmAlgorithm = "flat";
pkg.steal = steal = steal || {};
steal.npmAlgorithm = "flat";
}

return crawl.deps(context, pkg, true).then(function(){
Expand All @@ -67,6 +65,7 @@ exports.translate = function(load){
delete pkg.browser.transform;
}
pkg = utils.json.transform(loader, load, pkg);
var steal = utils.pkg.config(pkg);

packages.push({
name: pkg.name,
Expand All @@ -75,7 +74,7 @@ exports.translate = function(load){
pkg.fileUrl :
utils.relativeURI(context.loader.baseURL, pkg.fileUrl),
main: pkg.main,
system: convert.system(context, pkg, pkg.system, index === 0),
steal: convert.steal(context, pkg, steal, index === 0),
globalBrowser: convert.browser(pkg, pkg.globalBrowser),
browser: convert.browser(pkg, pkg.browser || pkg.browserify),
jspm: convert.jspm(pkg, pkg.jspm),
Expand Down
2 changes: 1 addition & 1 deletion test/config_deps/dev.html
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
if(hasQUnit()) {
QUnit.equal(window.DEP, "this is a config dep");
QUnit.notEqual(System.configDependencies[0], "my-dep", "An npm dependency's configDependencies should not be part of the configDependencies array.");
QUnit.ok(rootPkg.system.configDependencies, "there are config deps in the pkg");
QUnit.ok(rootPkg.steal.configDependencies, "there are config deps in the pkg");
} else {
console.log(window.DEP);
}
Expand Down

0 comments on commit ca4e9de

Please sign in to comment.