From 7ccac1a85efa6d6572981182cc0ea7cd8692e9a8 Mon Sep 17 00:00:00 2001 From: Chasen Le Hara Date: Fri, 21 Jul 2017 17:01:38 -0700 Subject: [PATCH] Dynamically make the config for loading can-* and steal-* modules This implements what Matthew suggested here: https://github.com/bitovi/documentjs/pull/273/files#r115520424 --- package.json | 10 ++-- site/default/static/build.js | 70 +++++---------------------- site/default/static/config.js | 12 ++--- site/default/static/map.js | 90 ++++++++++++++++++++++------------- 4 files changed, 80 insertions(+), 102 deletions(-) diff --git a/package.json b/package.json index d7278375c..348beb8d5 100644 --- a/package.json +++ b/package.json @@ -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", @@ -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" }, diff --git a/site/default/static/build.js b/site/default/static/build.js index 46a78d3ac..6a667efc5 100644 --- a/site/default/static/build.js +++ b/site/default/static/build.js @@ -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'), @@ -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', diff --git a/site/default/static/config.js b/site/default/static/config.js index c555c92e4..d5b5184dd 100644 --- a/site/default/static/config.js +++ b/site/default/static/config.js @@ -1,7 +1,7 @@ (function () { - var map = require('./map'); + var map = require('./map.json'); var isClient = typeof window !== "undefined"; - + var configData = { map: map, ext: { @@ -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); })(); diff --git a/site/default/static/map.js b/site/default/static/map.js index 5a7c15131..2f2e4f914 100644 --- a/site/default/static/map.js +++ b/site/default/static/map.js @@ -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", -}; \ No newline at end of file + "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;