find-es-resources
v4.0.0
Published
Find resource files within JavaScript files
Downloads
7
Readme
find-es-resources
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.
Presently supported AST for detection
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);
}));
Install
npm i find-es-resources
API
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;
CLI Usage
See also
- find-resources - For resource discovery within parsed HTML and CSS
Changelog
The changelog can be found on CHANGES.md.
Authors and license
Brett Zamir and contributors.
MIT License, see the included LICENSE-MIT.tx file.