Skip to content

Commit

Permalink
chore: add ts to workflow
Browse files Browse the repository at this point in the history
  • Loading branch information
Dattaya committed Feb 7, 2021
1 parent 9c841a7 commit dcc7894
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 10 deletions.
4 changes: 3 additions & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ jobs:
npm rm react react-dom
npm i react@${{ matrix.react-version }} react-dom@${{ matrix.react-version }}
- name: Validate
run: npm run lint
run: |
npm run tsc
npm run lint
- name: Run tests
run: npm run cy:ci
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
"dev": "next",
"test": "cypress open -b chrome",
"lint": "eslint --fix --ext .ts,.tsx src components pages",
"tsc": "tsc",
"tsc": "tsc -p tsconfig.build.json",
"cy:run": "cypress run",
"cy:ci": "start-server-and-test dev http://localhost:3000 cy:run"
},
Expand Down
14 changes: 8 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,19 +38,21 @@ const getLazyInitImages = (): string[] => {
}
return normInitImages;
};
const isInitImage = (src: string | undefined): boolean => getLazyInitImages().some((initImgSrc) => initImgSrc?.endsWith(src));
const isInitImage = (src: string | undefined): boolean => getLazyInitImages().some(
(initImgSrc) => src && initImgSrc?.endsWith(src),
);
const isInitSsr = (isSsrMode = false, src?: string): boolean => {
if (isSsrMode && src && isBrowser && document.readyState !== 'complete' && isInitImage(src)) {
return true;
}
return false;
};

function normArg(obj: ImgArg | string): Omit<ImgArg, 'srcSet'> | { 'srcset': ImgArg['srcSet'] }
function normArg(obj: ImgArg): Omit<ImgArg, 'srcSet'> | { 'srcset': ImgArg['srcSet'] }
function normArg(obj: SourceArg): Omit<SourceArg, 'srcSet'> | { 'srcset': SourceArg['srcSet'] }[]
function normArg(obj: SourceArg): Omit<SourceArg, 'srcSet'> | { 'srcset': SourceArg['srcSet'] }[]
function normArg(obj: any): any {
const nObj = typeof obj === 'string' ? { src: obj } : obj;
const normObj = { ...nObj, srcset: nObj.srcSet };
const normObj = { ...obj, srcset: obj.srcSet };
delete normObj.srcSet;
Object.keys(normObj).forEach((key) => {
if (normObj[key] === undefined) {
Expand Down Expand Up @@ -78,7 +80,7 @@ const useProgressiveImage = ({

// eslint-disable-next-line consistent-return
const image = useDeepCompareMemo<HTMLImageElement | undefined>(() => {
const src = typeof imgArg === 'string' ? imgArg : imgArg.src;
const src = typeof imgArg === 'string' ? imgArg : imgArg?.src;
if (src && isBrowser && !isInitSsr(ssr, src)) {
const img = document.createElement('img');

Expand All @@ -94,7 +96,7 @@ const useProgressiveImage = ({

img.onload = rerender;
img.onerror = handleError;
Object.assign(img, normArg(imgArg));
Object.assign(img, normArg(typeof imgArg === 'string' ? { src: imgArg } : imgArg!));
return img;
}
}, [imgArg, sourcesArg, rerender, handleError, ssr]);
Expand Down
3 changes: 2 additions & 1 deletion tsconfig.build.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
// "noUncheckedIndexedAccess": true, /* Include 'undefined' in index signature results */

/* Module Resolution Options */
// "moduleResolution": "node", /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */
"moduleResolution": "node", /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */
// "baseUrl": "./", /* Base directory to resolve non-absolute module names. */
// "paths": {}, /* A series of entries which re-map imports to lookup locations relative to the 'baseUrl'. */
// "rootDirs": [], /* List of root folders whose combined content represents the structure of the project at runtime. */
Expand All @@ -67,6 +67,7 @@
"skipLibCheck": true, /* Skip type checking of declaration files. */
"forceConsistentCasingInFileNames": true /* Disallow inconsistently-cased references to the same file. */
},
"include": ["src"],
"exclude": [
"node_modules"
]
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
],
"allowJs": true,
"skipLibCheck": true,
"strict": false,
"strict": true,
"forceConsistentCasingInFileNames": true,
"noEmit": true,
"esModuleInterop": true,
Expand Down

0 comments on commit dcc7894

Please sign in to comment.