-
Notifications
You must be signed in to change notification settings - Fork 779
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
chore: ability to add external libs (axios) #990
Conversation
None of these are things that impact the users, so "feat" and "fix" are inappropriate for this PR. |
Also please don't fill out the checkboxes they are "to be filled out by PR reviewer(s)" |
@WilcoFiers - In it's true sense this is a new feature, albeit unseen by users. We are creating a feature of being able to add external dependencies. What prefix/ commit standard works better? chore? |
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 think you should add a test to this to ensure axios is included in axe.js.
@WilcoFiers - I have added the test for axios. Please squash & merge, so the commit can be prefixed as |
Recording what we discussed earlier today:
|
Have hit a roadblock with this one now.
Mounting the library on return `
/* global axe, axios */
axe.imports["${library}"] = (function() {
let externalExistence = window["${library}"];
${grunt.file.read(source)}
if(window["${library}"]) {
delete window["${library}"];
}
if (externalExistence) {
window["${library}"] = externalExistence
}
return ${library};
})();
`; At this juncture, believe there are only a few ways of moving forward with this.
Alternative suggestions welcome. |
@JKODU I think I have a pretty simple solution to the axe.imports.axios = (function () {
// Clobber these globals, tricking UMD
var define = undefined
var module = undefined
var exports = undefined
// Keep a copy of the previous Axios global (if set)
var _axios = window.axios
${axios_source_code}
var axios = window.axios
// Reset window.axios (if necessary)
delete window.axios
if (_axios) {
window.axios = _axios
}
// Return "our" copy of Axios
return axios
})() |
@WilcoFiers - review again |
@@ -0,0 +1,3 @@ | |||
build/tasks/generate-imports.js | |||
!lib/core/imports/index.js | |||
lib/core/imports/*.js |
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.
You'll need to add this to gitignore as well.
}); | ||
|
||
// @wilco - need your input on this, there is no axe.source or equivalent available | ||
// it('should ensure axios source includes axios', function() { |
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.
axe.source will be available is module.exports
is defined. This test should work in umd-defined.js.
build/tasks/generate-imports.js
Outdated
// list of external dependencies, | ||
// which needs to be added to axe.imports object | ||
const LIBS_TO_IMPORT = { | ||
axios: './node_modules/axios/dist/axios.js' |
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 should come from the gruntfile.js rather than live in the task.
build/tasks/generate-imports.js
Outdated
processImport(key, LIBS_TO_IMPORT[key]); | ||
}); | ||
|
||
done(); |
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.
Looks like this function is sync. why did you need this.async()
?
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.
You are right, initially I was using fs
with promises for file read write, but got rid of it. This can go.
/*eslint-env node */ | ||
const UglifyJS = require('uglify-js'); | ||
|
||
module.exports = grunt => { |
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 going to be difficult to maintain without tests preventing regression.
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 are tests validating axios in umd integration tests. As we add multiple libraries, we can enhance the tests around each library.
build/tasks/generate-imports.js
Outdated
return ( | ||
/typeof exports/.test(sourceCode) && | ||
/typeof define/ && | ||
/typeof module/ |
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.
Did you mean to put .test()
on these?
Please review again. |
'Task for generating an axe.imports module with external dependencies.', | ||
function() { | ||
// Convenience method that utilises uglifyjs tree-transformer to unwrap umd module resolver | ||
const removeUMD = new UglifyJS.TreeTransformer(node => { |
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.
We should consider moving this to something like Babel (in the long-term). No need to change now.
} | ||
}; | ||
|
||
const hasUmdWrapper = sourceCode => { |
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 a little scary and seems brittle. I think it's fine for now, since we know Axios has a UMD wrapper, but before we start adding more "imports", we may want to revisit this.
build/tasks/generate-imports.js
Outdated
}; | ||
|
||
const writeLibrary = (libName, factory) => { | ||
const lib = `/* global axe */ axe.imports["${libName}"] = ${factory}`; |
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.
No need for this ESLint directive, since we're ignoring this file anyway 😉
lib/core/imports/index.js
Outdated
@@ -0,0 +1,10 @@ | |||
/*eslint no-unused-vars: 0*/ | |||
/* exported utils */ |
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.
What is this directive for?
lib/core/imports/index.js
Outdated
* @memberof axe | ||
*/ | ||
|
||
var imports = (axe.imports = {}); |
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 don't think we need var imports
here. Can probably just have axe.imports = {}
.
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! Great work here dude 😄
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've tested this in both Attest-node and the Attest extension to make sure we don't lose Axios somewhere while injecting it. Works like a charm!
axios
(which allows handling network requests), with some generic code written for allowing easier addition of newer libraries in the future.generate-imports
is added as a sub-task with in build step. This prepares the external assets by mounting them onaxe.imports
object as is (if not UMD wrapped), or after unwrapping UMD resolver and getting the factory to be mounted.axios
was chosen oversuperagent
based on this perf test - https://jsperf.com/axios-vs-superagent/Reviewer checks
Required fields, to be filled out by PR reviewer(s)