Skip to content

Commit

Permalink
Fix support for npm 2
Browse files Browse the repository at this point in the history
  • Loading branch information
chasenlehara committed Jul 27, 2017
1 parent 858ed08 commit 2bee9d7
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 55 deletions.
46 changes: 28 additions & 18 deletions site/default/static/build.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
var assign = require("can-util/js/assign/assign");
var fs = require('fs');
var map = require('./map');
var stealTools = require("steal-tools"),
Expand Down Expand Up @@ -26,40 +27,49 @@ module.exports = function(options, folders){
return promise;
} else {
// manually configure Can/Steal packages for Steal build
var paths = {
'jquery': path.relative(__dirname, require.resolve('jquery'))
};

// root packages
var npmPackages = [];
// generate the remaining paths
var mapCopy = {};
for (var moduleName in map) {
if (map.hasOwnProperty(moduleName) && moduleName.indexOf('/') === -1) {
npmPackages.push(moduleName);
if (map.hasOwnProperty(moduleName)) {
// map[moduleName] can either be just a string (e.g. jquery) or
// an object, so we want the path for the module, not an object
var resolvePath = map[moduleName][moduleName] || map[moduleName];

// Get the path relative to the build folder
var moduleRelativePath = path.relative(__dirname, resolvePath);

// Update the paths object with the AMD configuration
paths[moduleName + '/*'] = path.dirname(moduleRelativePath) + '/*.js';
paths[moduleName] = moduleRelativePath;

// Make a copy of the object without the key
// that was used to locate the module
if (map[moduleName][moduleName]) {
mapCopy[moduleName] = assign({}, map[moduleName]);
delete mapCopy[moduleName][moduleName];
} else {
mapCopy[moduleName] = map[moduleName];
}
}
}

// conditional map
// write it out for the client to consume
var mapJSON = JSON.stringify(map);
var mapJSON = JSON.stringify(mapCopy);
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',
'steal-stache': path.relative(__dirname, require.resolve('steal-stache'))
};

// generate the remaining paths
npmPackages.forEach(function(pkg) {
paths[pkg + '/*'] = path.dirname(path.relative(__dirname, require.resolve(pkg))) + '/*.js';
paths[pkg] = path.relative(__dirname, require.resolve(pkg));
});

// makes sure can is not added to the global so we can build nicely.
global.GLOBALCAN = false;
return stealTools.build({
main: "static",
config: __dirname + "/config.js",
bundlesPath: __dirname+"/bundles",
paths: paths,
map: map,
map: mapCopy,
ext: {
'stache': 'steal-stache'
}
Expand Down
108 changes: 71 additions & 37 deletions site/default/static/map.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,59 +4,93 @@ 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;
filelist = filelist || [];
files.forEach(function(file) {
if (file !== 'node_modules') {
var fullPath = path.join(dir, file);
if (fs.statSync(fullPath).isDirectory()) {
filelist = getJSFiles(fullPath, filelist);
} else if (file.substr(-3) === '.js') {
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;
});
// Get most of the can-* and steal-* packages from node_modules
var getCanAndStealModules = function(moduleFolder) {
try {
var fileNames = fs.readdirSync(path.join(moduleFolder, 'node_modules'));
return fileNames.filter(function(fileName) {
var isCanModule = fileName.substr(0, 4) === 'can-';
var isStealModule = fileName.substr(0, 6) === 'steal-' && fileName !== 'steal-tools';
return isCanModule || isStealModule;
});
} catch (error) {
return;
}
};
var moduleNames = getCanAndStealModules('');

// 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);
// This function is used recursively to update the exported modulesMap
var updateMapWithModules = function(moduleNames, prefix) {
moduleNames.forEach(function(moduleName) {

// If the module has already been defined, skip it
if (modulesMap[moduleName]) {
return;
}

var moduleMap = {};
var modulePath = path.join(prefix, 'node_modules', moduleName);

// Get all of the .js files in this module’s directory
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
// or node_modules\can-cid\build.js on Windows

// 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
// or node_modules\can-cid\build.js on Windows
// …normalize to use forward slashes for the module names
var jsFileModule = jsFile.replace(new RegExp('\\\\', 'g'), '/');

// …normalize to use forward slashes for the module names
var jsFileModule = jsFile.replace(new RegExp('\\\\', 'g'), '/');
// pathWithModuleName will look like “can-cid/build”
var pathWithModuleName = jsFileModule.substr((prefix.length ? prefix.length + 1 : 0) + 'node_modules'.length + 1).slice(0, -3);

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

// pathWithoutModuleName looks like build
var pathWithoutModuleName = pathWithModuleName.substr(moduleName.length + 1);
// for the main file (e.g. can-cid/can-cid), include the full path;
// otherwise, used the short module name (e.g. can-cid/build)
if (pathWithoutModuleName === moduleName) {
moduleMap[pathWithoutModuleName]= jsFileModule;
} else {
moduleMap[pathWithoutModuleName]= pathWithModuleName;
}
});

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

// If this module has node_modules that are can-* or steal-*,
// then go through them recursively
var subModuleNames = getCanAndStealModules(modulePath);
if (subModuleNames) {
updateMapWithModules(subModuleNames, modulePath);
}
});
};

modulesMap[moduleName] = moduleMap;
});
// Start at the root folder
updateMapWithModules(moduleNames, '');

module.exports = modulesMap;

0 comments on commit 2bee9d7

Please sign in to comment.