Skip to content

Commit

Permalink
Dynamically make the config for loading can-* and steal-* modules
Browse files Browse the repository at this point in the history
This implements what Matthew suggested here: https://github.com/bitovi/documentjs/pull/273/files#r115520424
  • Loading branch information
chasenlehara committed Jul 22, 2017
1 parent 09db15f commit 7ccac1a
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 102 deletions.
10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@
},
"dependencies": {
"async": "~0.2.7",
"can-control": "3.0.10",
"can-map": "3.0.7",
"can-stache": "3.0.24",
"can-util": "3.6.1",
"can-control": "^3.0.10",
"can-map": "^3.0.7",
"can-stache": "^3.0.24",
"can-util": "^3.6.1",
"chokidar": "^1.0.0-rc5",
"cross-spawn-async": "^2.1.9",
"documentjs-github-download": "^0.3.0",
Expand All @@ -48,7 +48,7 @@
"minimatch": "~1.0.0",
"q": "~1.0.1",
"steal": "0.16.X",
"steal-stache": "3.0.7",
"steal-stache": "^3.0.7",
"steal-tools": "0.16.X",
"yargs": "^1.3.1"
},
Expand Down
70 changes: 12 additions & 58 deletions site/default/static/build.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@

var fs = require('fs');
var map = require('./map');
var stealTools = require("steal-tools"),
fsx = require('../../../../lib/fs_extras'),
Q = require('q'),
Expand All @@ -25,67 +26,20 @@ module.exports = function(options, folders){
return promise;
} else {
// manually configure Can/Steal packages for Steal build
// if can-* or steal-* packages are updated, this list will also likely need to be updated
// TODO this should be automated first

// root packages
var npmPackages = [
'can-cid',
'can-compute',
'can-construct',
'can-control',
'can-event',
'can-map',
'can-namespace',
'can-observation',
'can-simple-map',
'can-stache',
'can-stache-bindings',
'can-types',
'can-view-callbacks',
'can-view-import',
'can-view-live',
'can-view-model',
'can-view-nodelist',
'can-view-parser',
'can-view-scope',
'can-view-target',
'steal-stache',
];
var npmPackages = [];
for (var moduleName in map) {
if (map.hasOwnProperty(moduleName) && moduleName.indexOf('/') === -1) {
npmPackages.push(moduleName);
}
}

// conditional map
var map = {
'can-compute': {
'proto-compute': 'can-compute/proto-compute',
},
'can-map': {
'bubble': 'can-map/bubble',
'map-helpers': 'can-map/map-helpers',
},
'can-stache': {
'helpers/core': 'can-stache/helpers/core',
'helpers/converter': 'can-stache/helpers/converter',
'src/html_section': 'can-stache/src/html_section',
'src/intermediate_and_imports': 'can-stache/src/intermediate_and_imports',
'src/mustache_core': 'can-stache/src/mustache_core',
'src/text_section': 'can-stache/src/text_section',
},
'can-view-live': {
'lib/attr': 'can-view-live/lib/attr',
'lib/attrs': 'can-view-live/lib/attrs',
'lib/core': 'can-view-live/lib/core',
'lib/html': 'can-view-live/lib/html',
'lib/list': 'can-view-live/lib/list',
'lib/text': 'can-view-live/lib/text',
},
'can-view-scope': {
'compute_data': 'can-view-scope/compute_data',
'reference-map': 'can-view-scope/reference-map',
},
'steal-stache': {
'add-bundles': 'steal-stache/add-bundles',
},
};
// write it out for the client to consume
var mapJSON = JSON.stringify(map);
fs.writeFileSync(path.join(__dirname, 'map.json'), mapJSON);

var paths = {
'jquery': path.relative(__dirname, require.resolve('jquery')),
'can-util/*': path.dirname(path.relative(__dirname, require.resolve('can-util'))) + '/*.js',
Expand Down
12 changes: 6 additions & 6 deletions site/default/static/config.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
(function () {
var map = require('./map');
var map = require('./map.json');
var isClient = typeof window !== "undefined";

var configData = {
map: map,
ext: {
Expand All @@ -14,16 +14,16 @@
prettify: {format: "global"}
}
};

if(isClient) {
// when not a client, these values are set by build.js.
configData.paths = {
"jquery": "jquery/dist/jquery.js",
"can/*": "can/*.js"
"can/*": "can/*.js"
};
}


System.config(configData);
})();

Expand Down
90 changes: 57 additions & 33 deletions site/default/static/map.js
Original file line number Diff line number Diff line change
@@ -1,34 +1,58 @@
module.exports = {
'can-compute': {
'proto-compute': 'can-compute/proto-compute',
},
'can-map': {
'bubble': 'can-map/bubble',
'map-helpers': 'can-map/map-helpers',
},
'can-stache': {
'helpers/core': 'can-stache/helpers/core',
'helpers/converter': 'can-stache/helpers/converter',
'src/html_section': 'can-stache/src/html_section',
'src/intermediate_and_imports': 'can-stache/src/intermediate_and_imports',
'src/mustache_core': 'can-stache/src/mustache_core',
'src/text_section': 'can-stache/src/text_section',
},
'can-view-live': {
'lib/attr': 'can-view-live/lib/attr',
'lib/attrs': 'can-view-live/lib/attrs',
'lib/core': 'can-view-live/lib/core',
'lib/html': 'can-view-live/lib/html',
'lib/list': 'can-view-live/lib/list',
'lib/text': 'can-view-live/lib/text',
},
'can-view-scope': {
'compute_data': 'can-view-scope/compute_data',
'reference-map': 'can-view-scope/reference-map',
},
'steal-stache': {
'add-bundles': 'steal-stache/add-bundles',
},
"jquery/jquery": "jquery",
var fs = require('fs');
var path = require('path');

// Helper for recursively getting all the JS files in a directory
var getJSFiles = function(dir, filelist) {
var files = fs.readdirSync(dir);
filelist = filelist || [];
files.forEach(function(file) {
var fullPath = path.join(dir, file);
if (fs.statSync(fullPath).isDirectory()) {
filelist = getJSFiles(fullPath, filelist);
} else if (file.substr(-3) === '.js' && fullPath.lastIndexOf('node_modules') === 0) {
filelist.push(fullPath);
}
});
return filelist;
};

// Get all of the can-* and steal-* packages from node_modules
var fileNames = fs.readdirSync('node_modules');
var moduleNames = fileNames.filter(function(fileName) {
var isCanModule = fileName.substr(0, 4) === 'can-';
var isStealModule = fileName.substr(0, 6) === 'steal-';
return isCanModule || isStealModule;
});

// This map will be exported
var modulesMap = {
"benchmark/benchmark": "benchmark",
};
"jquery/jquery": "jquery"
};

moduleNames.forEach(function(moduleName) {
var moduleMap = {};
var modulePath = path.join('node_modules', moduleName);
var jsFiles = getJSFiles(modulePath);

// Run through all the JS files in the module and create a map like
// "can-cid": {"build": "can-cid/build"}
jsFiles.forEach(function(jsFile) {
// jsFile looks like node_modules/can-cid/build.js

// …so pathWithModuleName looks like can-cid/build
var pathWithModuleName = jsFile.substr('node_modules'.length + 1).slice(0, -3);

// pathWithoutModuleName looks like build
var pathWithoutModuleName = pathWithModuleName.substr(moduleName.length + 1);

// don’t include the main file (e.g. can-cid/can-cid)
if (pathWithoutModuleName !== moduleName) {
moduleMap[pathWithoutModuleName]= pathWithModuleName;
}
});

modulesMap[moduleName] = moduleMap;
});

module.exports = modulesMap;

0 comments on commit 7ccac1a

Please sign in to comment.