switch to regular object for attributes and add attribute function

This commit is contained in:
Luis Herranz
2019-04-11 15:52:36 +02:00
parent b766a9b8b7
commit 1379ee5ebd
2 changed files with 50 additions and 7 deletions
+24 -6
View File
@@ -28,6 +28,24 @@ export type ChunkExtractorOptions = {
stats: object;
});
/**
* Chunk that is received by the AttrFn function.
*/
export interface Chunk {
chunk: string;
filename: string;
linkType: string;
path: string;
scriptType: string;
type: string;
url: string;
}
/**
* Used to insert attributes in the get functions.
*/
export type AttrFn = (chunk: Chunk) => {};
/**
* Used to collect chunks server-side and get them as script tags or script elements
*/
@@ -52,32 +70,32 @@ export class ChunkExtractor {
/**
* Get scripts as a string of `<script>` tags
*/
getScriptTags(attributes?: { [key: string]: string }): string;
getScriptTags(attr?: {} | AttrFn): string;
/**
* Get scripts as an array of React `<script>` elements.
*/
getScriptElements(attributes?: { [key: string]: string }): Array<ReactElement<{}>>;
getScriptElements(attr?: {} | AttrFn): Array<ReactElement<{}>>;
/**
* Get "prefetch" and "preload" links as a string of `<link>` tags
*/
getLinkTags(attributes?: { [key: string]: string }): string;
getLinkTags(attr?: {} | AttrFn): string;
/**
* Get "prefetch" and "preload" links as an array of React `<link>` elements
*/
getLinkElements(attributes?: { [key: string]: string }): Array<ReactElement<{}>>;
getLinkElements(attr?: {} | AttrFn): Array<ReactElement<{}>>;
/**
* Get style links as a string of `<link>` tags
*/
getStyleTags(attributes?: { [key: string]: string }): string;
getStyleTags(attr?: {} | AttrFn): string;
/**
* Get style links as an array of React `<link>` elements
*/
getStyleElements(attributes?: { [key: string]: string }): Array<ReactElement<{}>>;
getStyleElements(attr?: {} | AttrFn): Array<ReactElement<{}>>;
}
export interface ChunkExtractorManagerProps {
@@ -1,5 +1,5 @@
import * as React from 'react';
import { ChunkExtractor } from '@loadable/server';
import { ChunkExtractor, AttrFn } from '@loadable/server';
// Should be satisfied by `stats` or `statsFile`
new ChunkExtractor({ stats: {} });
@@ -31,6 +31,19 @@ const attributes = {
"data-type": "loadable"
};
// An attribute function for get functions.
const attrFn: AttrFn = (chunk) => {
return {
chunk: chunk.chunk,
filename: chunk.filename,
linkType: chunk.linkType,
path: chunk.path,
scriptType: chunk.scriptType,
type: chunk.type,
url: chunk.url
};
};
// getLinkElements
{
// Should return an array of React elements
@@ -38,6 +51,9 @@ const attributes = {
const elementsWithAttrs: Array<React.ReactElement<{}>> = getLinkElements(
attributes
);
const elementsWithAttrFn: Array<React.ReactElement<{}>> = getLinkElements(
attrFn
);
}
// getLinkTags
@@ -45,6 +61,7 @@ const attributes = {
// Should return a string
const tags: string = getLinkTags();
const tagsWithAttrs: string = getLinkTags(attributes);
const tagsWithAttrFn: string = getLinkTags(attrFn);
}
// getScriptElements
@@ -54,6 +71,9 @@ const attributes = {
const elementsWithAttrs: Array<React.ReactElement<{}>> = getScriptElements(
attributes
);
const elementsWithAttrFn: Array<React.ReactElement<{}>> = getScriptElements(
attrFn
);
}
// getScriptTags
@@ -61,6 +81,7 @@ const attributes = {
// Should return a string
const tags: string = getScriptTags();
const tagsWithAttrs: string = getScriptTags(attributes);
const tagsWithAttrFn: string = getScriptTags(attrFn);
}
// getStyleElements
@@ -70,6 +91,9 @@ const attributes = {
const elementsWithAttrs: Array<React.ReactElement<{}>> = getStyleElements(
attributes
);
const elementsWithAttrFn: Array<React.ReactElement<{}>> = getStyleElements(
attrFn
);
}
// getStyleTags
@@ -77,6 +101,7 @@ const attributes = {
// Should return a string
const tags: string = getStyleTags();
const tagsWithAttrs: string = getStyleTags(attributes);
const tagsWithAttrFn: string = getStyleTags(attrFn);
}
// requireEntrypoint