Employing loader utilities

Why not take advantage of the loader-utils package? It provides a variety of useful tools, but one of the most common of these is the ability to retrieve the options being passed to any loader in use. Along with loader-utils, the schema-utils package should be used for consistent JSON Schema-based validation. The following code block shows an example that utilizes both packages, using loader.js:

import { getOptions } from 'loader-utils';
import validateOptions from 'schema-utils';
const schema = {
type: 'object',
properties: {
test: {
type: 'string'
}
}
};
export default function(source) {
const options = getOptions(this);
validateOptions(schema, options, 'Example Loader');

Now, we can apply some transformations to the source, like this:

return `export default ${ JSON.stringify(source) }`;
}

This transformation will stringify the code—essentially, outputting the contents into a single line of code that is difficult to read by humans but ideal for computers. This also often helps with privacy issues, should anyone wish to manually copy the code. With that understood, let's move on to loader dependency guidelines.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.217.182.45