Skip to content

Commit

Permalink
enable strict ts && remove tslib && build es module
Browse files Browse the repository at this point in the history
  • Loading branch information
imhele committed Jun 9, 2019
1 parent cdb56c5 commit 526695e
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 14 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
# production
/dist
/lib
/es
/coverage
.npmignore

Expand Down
15 changes: 9 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
{
"name": "react-media-hook2",
"version": "1.0.5",
"main": "./lib/index.js",
"types": "./lib/index.d.ts",
"typings": "./lib/index.d.ts",
"version": "1.1.0",
"main": "lib/index.js",
"module": "es/index.js",
"types": "lib/index.d.ts",
"typings": "lib/index.d.ts",
"scripts": {
"build": "tsc -p .",
"build": "npm run build:es && npm run build:lib",
"build:lib": "tsc --build tsconfig.json",
"build:es": "tsc --build tsconfig.es.json",
"coverage": "cat ./coverage/lcov.info | coveralls",
"lint": "tslint \"src/**/*.ts\" \"test/**/*.ts\"",
"precommit": "lint-staged",
Expand Down Expand Up @@ -38,7 +41,7 @@
},
"files": [
"lib",
"src"
"es"
],
"license": "MIT",
"peerDependencies": {
Expand Down
7 changes: 4 additions & 3 deletions src/format.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@ const Camel2CSS = (camel: string): string =>
camel.replace(/[A-Z]/g, str => `-${str.toLowerCase()}`).toLowerCase();

const CSSProperties2MediaQuery = (css: MediaQueryProperties): string => {
return Object.entries(css)
.map(([key, value]) => {
return Object.keys(css)
.map(key => {
let value = css[key as keyof typeof css];
key = Camel2CSS(key);
switch (typeof value) {
case 'boolean':
return value ? `(${key})` : `(not ${key})`;
case 'number':
if (key.endsWith('height') || key.endsWith('width')) value = `${value}px`;
if (/(?:height|width)$/.test(key)) value = `${value}px`;
default:
return `(${key}: ${value})`;
}
Expand Down
6 changes: 4 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ const createMatchMedia = (
ref: MutableRefObject<MediaQueryList>,
prevRef: MutableRefObject<MediaQueryList>,
): void => {
const { defaultMatches = false, query = '', targetWindow = window } = props;
const defaultMatches = props.defaultMatches || false;
const query = props.query || '';
const targetWindow = props.targetWindow || window;
const warn =
// tslint:disable-next-line no-console
['production', 'test'].includes(process && process.env.NODE_ENV!) && console && console.warn;
Expand All @@ -57,7 +59,7 @@ const useMedia = (initialProps: UseMediaProps = {}): [boolean, SetUseMediaProps]
const setPropsRef = useRef<SetUseMediaProps>();
const mediaQueryListRef = useRef<MediaQueryList>(void 0 as any);
const prevMediaQueryListRef = useRef<MediaQueryList>(mediaQueryListRef.current);
const useMediaPropsRef = useRef<UseMediaProps>({ ...initialProps });
const useMediaPropsRef = useRef<UseMediaProps>(Object.assign({}, initialProps));
useState(() => {
createMatchMedia(useMediaPropsRef.current, mediaQueryListRef, prevMediaQueryListRef);
useMediaPropsRef.current.defaultMatches = mediaQueryListRef.current!.matches;
Expand Down
18 changes: 18 additions & 0 deletions tsconfig.es.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"compilerOptions": {
"target": "esnext",
"module": "esnext",
"declaration": true,
"moduleResolution": "node",
"esModuleInterop": true,
"jsx": "react",
"experimentalDecorators": true,
"noImplicitAny": true,
"preserveConstEnums": true,
"outDir": "es/",
"baseUrl": ".",
"strict": true
},
"include": ["src/**/*"],
"exclude": ["node_modules", "**/*.spec.ts"]
}
5 changes: 2 additions & 3 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,14 @@
"module": "commonjs",
"declaration": true,
"moduleResolution": "node",
"importHelpers": true,
"esModuleInterop": true,
"jsx": "react",
"experimentalDecorators": true,
"noImplicitAny": true,
"preserveConstEnums": true,
"lib": ["esnext", "dom"],
"outDir": "lib/",
"strictNullChecks": true
"baseUrl": ".",
"strict": true
},
"include": ["src/**/*"],
"exclude": ["node_modules", "**/*.spec.ts"]
Expand Down

0 comments on commit 526695e

Please sign in to comment.