This project is meant to help assist you find resource files within parsed JavaScript. It currently has very limited support for syntactic structures where the resource files can be found. See the list below.
It also allows some optional basic scanning of HTML for scripts, images, and stylesheets, and for CSS URLs.
A main use case is to supply it to workbox-build so as to be able to build a service-worker which caches the files that your application fetches, etc.
Because we wish to avoid grabbing strings which are not file resources, we need to whitelist the JavaScript structures where we can expect to find files (and we can't follow variables everywhere--literals are expected to be found at a predictable location).
The intent is to expand this gradually as we find a need ourselves. PRs are welcome (with tests) for supporting other types of structures.
You should just need to add a method to src/queries.js
.
-
fetch
(e.g., for polyglot usage, alanode-fetch
) - Literals within an array directly supplied to amap
call which returns the result of afetch
call:
await Promise.all([
'./test1.json',
'./test2.json'
].map((path) => {
return fetch(path);
}));
npm i find-es-resources
import {findESResources} from 'find-es-resources';
const arrayOfFileStrings = await findESResources({
// File as a string path
input: filePath,
// See the `es-file-traverse` package:
// https://github.com/brettz9/es-file-traverse
// Can be an empty object
esFileTraverseOptions,
// Point to a CJS file exporting an object with string selectors as keys
// to functions which accept a node and return the string resources.
queryOptions: {
queryModule: moduleString
}
});
// Module pointed to by `moduleString`:
const queries = {
'an > AST > Selector' (node) {
return getResourceStringsOutOfNode(node);
},
'another AST Selector' (node) {
return getResourceStringsAlso(node);
}
};
export default queries;
- find-resources - For resource discovery within parsed HTML and CSS
The changelog can be found on CHANGES.md.
Brett Zamir and contributors.
MIT License, see the included LICENSE-MIT.tx file.