-
Notifications
You must be signed in to change notification settings - Fork 122
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
Conversation
@@ -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", |
There was a problem hiding this comment.
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 || {}; |
There was a problem hiding this comment.
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(), |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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); |
There was a problem hiding this comment.
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'); |
There was a problem hiding this comment.
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); | ||
} |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
There was a problem hiding this 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(), |
There was a problem hiding this comment.
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); |
There was a problem hiding this comment.
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
.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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); |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
There was a problem hiding this 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); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
love it
Fixes #462