Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow node core deps to be used and whitelisted #479

Merged
merged 13 commits into from
May 5, 2017
Merged

Allow node core deps to be used and whitelisted #479

merged 13 commits into from
May 5, 2017

Conversation

matteofigus
Copy link
Member

Fixes #462

@matteofigus matteofigus changed the title [wip] Allow node core deps to be used and whitelisted Allow node core deps to be used and whitelisted May 4, 2017
@@ -58,6 +58,7 @@
"babili-webpack-plugin": "0.0.11",
"basic-auth-connect": "^1.0.0",
"body-parser": "^1.16.0",
"builtin-modules": "^1.1.1",
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

a little convenience module with a list of all the core node dependencies

@@ -17,7 +19,7 @@ module.exports = function(components){
components.forEach((c) => {
const pkg = fs.readJsonSync(path.join(c, 'package.json'));
const type = pkg.oc.files.template.type;
const dependencies = pkg.dependencies;
const dependencies = pkg.dependencies || {};
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this comes from package.json. After testing, I found a component with no dependencies props could actually generate some troubles

@@ -43,7 +43,7 @@ module.exports = function(components){
});

return {
modules: _.keys(deps.modules),
modules: _.union(coreModules, _.keys(deps.modules)).sort(),
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is used for the CLI. Now the cli by default allows any core module to be used. When running in the registry, still, the dependency will need to be whitelisted as all the other deps in order to be usable.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are we sorting them?

Copy link
Member Author

@matteofigus matteofigus May 5, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point. I think it was to make a test green but I can sort the result in the test instead as this is not necessary here.

return !_.includes(_.keys(dependencies), dep);
};
const missingExternalDependency = (dep, dependencies) =>
!_.includes(_.keys(dependencies), dep) && !_.includes(coreModules, dep);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This part is used for the CLI bundler. This is for making the bundler bypass the dependency in case is a core, by being treated in the same way normal deps are.

@@ -3,7 +3,7 @@

const webpack = require('webpack');
const path = require('path');
const externalDependenciesHandlers = require('./externalDependenciesHandlers');
const externalDependenciesHandlers = require('./external-dependencies-handlers');
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just a little renaming for consistency

code: strings.errors.registry.DEPENDENCY_NOT_FOUND_CODE,
missing: [modulePath]
};
throw getError(requirePath);
}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So the flow here is 1) try to require the module normally 2) if it fails and is a core dep, try to require it 3) if it fails, return the error at the end

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

Copy link
Contributor

@nickbalestra nickbalestra left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All good, thanks for this!
I've just added a couple of minor questions

@@ -43,7 +43,7 @@ module.exports = function(components){
});

return {
modules: _.keys(deps.modules),
modules: _.union(coreModules, _.keys(deps.modules)).sort(),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are we sorting them?

const requireDependency = (requirePath) => {
const nodeModulesPath = path.resolve('.', 'node_modules');
const modulePath = path.resolve(nodeModulesPath, requirePath);
return require(modulePath);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll perhaps move the try catch logic inside each requiring function requireCoreDependency and requireCoreDependency .

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok done. I found a try-require module for that and made some more cleanup around.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice we should then use it also for the templates as we are doing similar stuff in multiple places, perhaps once we work on allowing multiple-version deps

code: strings.errors.registry.DEPENDENCY_NOT_FOUND_CODE,
missing: [modulePath]
};
throw getError(requirePath);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

getError accept a param called moduleName and here you are passing a path, perhaps worth renaming such param to accomodate both cases better?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok done

code: strings.errors.registry.DEPENDENCY_NOT_FOUND_CODE,
missing: [modulePath]
};
throw getError(requirePath);
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

Copy link
Contributor

@nickbalestra nickbalestra left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!
I'll merge this once all tests are green

throw getError(requirePath);
}
}
return requireDependency(requirePath) || requireCoreDependency(requirePath) || throwError(requirePath);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

love it

@nickbalestra nickbalestra merged commit e7509ba into master May 5, 2017
@nickbalestra nickbalestra deleted the url branch May 5, 2017 10:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants