Skip to content

Commit

Permalink
chore: use __DEV__ directly (#57)
Browse files Browse the repository at this point in the history
* chore: use __DEV__ directly

* chore: jest coverage (#56)

* chore: jest __DEV__ global variable

* chore: test case

* chore: add coverage

* chore: add coverage

* chore: change main entry

* chore: coverage files

* chore: coverage files

Co-authored-by: Rongyan Chen <[email protected]>
  • Loading branch information
SoloJiang and Rongyan Chen authored Apr 21, 2022
1 parent ad1d4b5 commit 979bcc4
Show file tree
Hide file tree
Showing 10 changed files with 119 additions and 19 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,5 @@ jobs:
- run: npm run setup
- run: npm run lint
- run: npm run test
- run: PRODUCTION=true npm run test
- run: npm run coverage
7 changes: 5 additions & 2 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
module.exports = {
coverageDirectory: './coverage/',
collectCoverage: true,
collectCoverageFrom: ['packages/*/lib/*.{js,jsx}'],
collectCoverageFrom: ['packages/pwc/src/**/*.{js,ts}', 'packages/pwc-compiler/src/**/*.{js,ts}', '!packages/**/*.d.ts', '!packages/**/type.ts', '!packages/*/src/index.{js,ts}'],
coveragePathIgnorePatterns: ['<rootDir>/node_modules/'],
roots: ['<rootDir>/packages'],
testPathIgnorePatterns: ['/node_modules/', '/lib/', '/es/', '/dist/', '.d.ts'],
testPathIgnorePatterns: ['/node_modules/', '/cjs/', '/esm/', '/es2017/', '/dist/', '.d.ts'],
testMatch: ['**/__tests__/**/*.[jt]s?(x)', '**/?(*.)+(spec|test).[jt]s?(x)'],
testEnvironment: 'jsdom',
transform: {
'\\.(js|ts|jsx|tsx)$': 'babel-jest',
},
globals: {
__DEV__: !process.env.PRODUCTION,
},
};
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
"benchmark": "esno ./scripts/benchmarks/index.ts",
"start": "pnpm run start --parallel --filter \"./packages\"",
"build": "pnpm run build -r --filter \"./packages\"",
"prepare-husky": "husky install"
"prepare-husky": "husky install",
"coverage": "codecov"
},
"dependencies": {
"@applint/spec": "^1.2.0",
Expand Down Expand Up @@ -45,6 +46,7 @@
"local-web-server": "^4.1.0",
"prettier": "^2.6.0",
"stylelint": "^14.6.0",
"typescript": "^4.6.2"
"typescript": "^4.6.2",
"codecov": "^3.8.3"
}
}
2 changes: 1 addition & 1 deletion packages/pwc/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"author": "Rax Team",
"homepage": "https://github.com/raxjs/pwc#readme",
"license": "MIT",
"main": "es/index.js",
"main": "esm/index.js",
"files": [
"esm/",
"es2017/",
Expand Down
4 changes: 2 additions & 2 deletions packages/pwc/src/decorators/__tests__/attribute.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,13 +137,13 @@ describe('attribute decorator', () => {
return html`<div>${this.attrName}</div>`;
}
}
}).toThrowError(`The attribute decorator should be added to the class field with accessor, like:
}).toThrowError(__DEV__ ? `The attribute decorator should be added to the class field with accessor, like:
class extends HTMLElement {
@attribute('attr-name')
accessor attrName
}
`);
`: 'Error: #0.');
});

it('should work without initialValue', async () => {
Expand Down
2 changes: 0 additions & 2 deletions packages/pwc/src/decorators/attribute/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@ import { attributeSetter } from './setter';
import { attributeGetter } from './getter';
import { validatePrivate } from './validatePrivate';

const __DEV__ = process.env.NODE_ENV !== 'production';

export function attribute(attrName: string) {
return (value, { kind, name, isPrivate }) => {
// Validate accessor operator
Expand Down
2 changes: 0 additions & 2 deletions packages/pwc/src/decorators/validateAccessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
*/
import { throwError, throwMinifiedError } from '../error';

const __DEV__ = process.env.NODE_ENV !== 'production';

export function validateAccessor(kind: string, decoratorExp: string, name: string) {
if (kind !== 'accessor') {
if (__DEV__) {
Expand Down
11 changes: 4 additions & 7 deletions packages/pwc/src/error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
* 1: repeated reflect attribute name
*/
import type { Warning } from './type';
import { NOOP } from './utils';

function createMinifiedError(type, code) {
return new Error(`${type}: #${code}.`);
Expand Down Expand Up @@ -33,10 +32,8 @@ export function throwError(message) {
throw Error(`${message}`);
}

export let warning: Warning = NOOP;

if (process.env.NODE_ENV !== 'production') {
warning = (template: string, ...args: any[]): void => {
export const warning: Warning = function (template: string, ...args: any[]): void {
if (__DEV__) {
if (typeof console !== 'undefined') {
let argsWithFormat = args.map((item) => `${item}`);
argsWithFormat.unshift(`Warning: ${template}`);
Expand All @@ -51,5 +48,5 @@ if (process.env.NODE_ENV !== 'production') {
const message = `Warning: ${template.replace(/%s/g, () => args[argIndex++])}`;
throw new Error(message);
} catch (error) {}
};
}
}
};
1 change: 0 additions & 1 deletion packages/pwc/src/utils/checkTypes.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
export const EMPTY_OBJECT = {};
export const NOOP = () => {};

export function isArray(arg: any) {
return Array.isArray(arg);
Expand Down
101 changes: 101 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 979bcc4

Please sign in to comment.