From 979bcc4e5f9afae04471a4cd18534ed515e3d452 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8B=92=E7=8B=92=E7=A5=9E?= Date: Thu, 21 Apr 2022 14:14:53 +0800 Subject: [PATCH] chore: use __DEV__ directly (#57) * 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 --- .github/workflows/ci.yml | 2 + jest.config.js | 7 +- package.json | 6 +- packages/pwc/package.json | 2 +- .../decorators/__tests__/attribute.test.js | 4 +- .../pwc/src/decorators/attribute/index.ts | 2 - .../pwc/src/decorators/validateAccessor.ts | 2 - packages/pwc/src/error.ts | 11 +- packages/pwc/src/utils/checkTypes.ts | 1 - pnpm-lock.yaml | 101 ++++++++++++++++++ 10 files changed, 119 insertions(+), 19 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 78a4d6f..311cfb4 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -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 diff --git a/jest.config.js b/jest.config.js index 711c2c2..ca4538c 100644 --- a/jest.config.js +++ b/jest.config.js @@ -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: ['/node_modules/'], roots: ['/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, + }, }; diff --git a/package.json b/package.json index 29dc8ad..4e9b781 100644 --- a/package.json +++ b/package.json @@ -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", @@ -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" } } diff --git a/packages/pwc/package.json b/packages/pwc/package.json index 815494c..c5e8312 100644 --- a/packages/pwc/package.json +++ b/packages/pwc/package.json @@ -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/", diff --git a/packages/pwc/src/decorators/__tests__/attribute.test.js b/packages/pwc/src/decorators/__tests__/attribute.test.js index 5879c02..d73ff1f 100644 --- a/packages/pwc/src/decorators/__tests__/attribute.test.js +++ b/packages/pwc/src/decorators/__tests__/attribute.test.js @@ -137,13 +137,13 @@ describe('attribute decorator', () => { return html`
${this.attrName}
`; } } - }).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 () => { diff --git a/packages/pwc/src/decorators/attribute/index.ts b/packages/pwc/src/decorators/attribute/index.ts index 7385efd..a1c4163 100644 --- a/packages/pwc/src/decorators/attribute/index.ts +++ b/packages/pwc/src/decorators/attribute/index.ts @@ -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 diff --git a/packages/pwc/src/decorators/validateAccessor.ts b/packages/pwc/src/decorators/validateAccessor.ts index ff249de..e3d7114 100644 --- a/packages/pwc/src/decorators/validateAccessor.ts +++ b/packages/pwc/src/decorators/validateAccessor.ts @@ -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__) { diff --git a/packages/pwc/src/error.ts b/packages/pwc/src/error.ts index 63efacf..cf26da6 100644 --- a/packages/pwc/src/error.ts +++ b/packages/pwc/src/error.ts @@ -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}.`); @@ -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}`); @@ -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) {} - }; -} + } +}; diff --git a/packages/pwc/src/utils/checkTypes.ts b/packages/pwc/src/utils/checkTypes.ts index bcbe9d1..6decd01 100644 --- a/packages/pwc/src/utils/checkTypes.ts +++ b/packages/pwc/src/utils/checkTypes.ts @@ -1,5 +1,4 @@ export const EMPTY_OBJECT = {}; -export const NOOP = () => {}; export function isArray(arg: any) { return Array.isArray(arg); diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index ff8edde..def2cde 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -19,6 +19,7 @@ importers: '@types/jest': ^27.4.1 '@types/node': ^17.0.21 chalk: ^4.1.2 + codecov: ^3.8.3 esbuild: ^0.14.27 eslint: ^7.32.0 esno: ^0.14.1 @@ -48,6 +49,7 @@ importers: '@types/jest': 27.4.1 '@types/node': 17.0.24 chalk: 4.1.2 + codecov: 3.8.3 esbuild: 0.14.36 eslint: 7.32.0 esno: 0.14.1 @@ -2814,6 +2816,11 @@ packages: sprintf-js: 1.0.3 dev: false + /argv/0.0.2: + resolution: {integrity: sha1-7L0W+JSbFXGDcRsb2jNPN4QBhas=} + engines: {node: '>=0.6.10'} + dev: false + /aria-query/4.2.2: resolution: {integrity: sha512-o/HelwhuKpTj/frsOsbNLNgnNGVIFsVP/SW2BSF14gVl7kAfMOJ6/8wUAUvG1R1NHKrfG+2sHZTu0yauT1qBrA==} engines: {node: '>=6.0'} @@ -3275,6 +3282,22 @@ packages: engines: {node: '>=0.10.0'} dev: true + /codecov/3.8.3: + resolution: {integrity: sha512-Y8Hw+V3HgR7V71xWH2vQ9lyS358CbGCldWlJFR0JirqoGtOoas3R3/OclRTvgUYFK29mmJICDPauVKmpqbwhOA==} + engines: {node: '>=4.0'} + deprecated: https://about.codecov.io/blog/codecov-uploader-deprecation-plan/ + hasBin: true + dependencies: + argv: 0.0.2 + ignore-walk: 3.0.4 + js-yaml: 3.14.1 + teeny-request: 7.1.1 + urlgrey: 1.0.0 + transitivePeerDependencies: + - encoding + - supports-color + dev: false + /collect-v8-coverage/1.0.1: resolution: {integrity: sha512-iBPtljfCNcTKNAto0KEtDfZ3qzjJvqE3aTGZsbhjSBlorqpXJlaWWtPO35D+ZImoC3KWejX64o+yPGxhWSTzfg==} dev: false @@ -4519,6 +4542,12 @@ packages: resolution: {integrity: sha1-PYpcZog6FqMMqGQ+hR8Zuqd5eRc=} dev: false + /fast-url-parser/1.1.3: + resolution: {integrity: sha1-9K8+qfNNiicc9YrSs3WfQx8LMY0=} + dependencies: + punycode: 1.4.1 + dev: false + /fastest-levenshtein/1.0.12: resolution: {integrity: sha512-On2N+BpYJ15xIC974QNVuYGMOlEVt4s0EOI3wwMqOmK1fdDY+FN/zltPV8vosq4ad4c/gJ1KHScUn/6AWIgiow==} dev: false @@ -4972,6 +5001,12 @@ packages: postcss: 8.4.12 dev: true + /ignore-walk/3.0.4: + resolution: {integrity: sha512-PY6Ii8o1jMRA1z4F2hRkH/xN59ox43DavKvD3oDpfurRlOJyAHpifIwpbdv1n4jt4ov0jSpw3kQ4GhJnpBL6WQ==} + dependencies: + minimatch: 3.1.2 + dev: false + /ignore/4.0.6: resolution: {integrity: sha512-cyFDKrqc/YdcWFniJhzI42+AzS+gNwmUzOSFcRCQYwySuBBBy/KjuxWLZ/FHEH6Moq1NizMOBWyTcv8O4OZIMg==} engines: {node: '>= 4'} @@ -6568,6 +6603,18 @@ packages: engines: {node: '>= 0.6'} dev: false + /node-fetch/2.6.7: + resolution: {integrity: sha512-ZjMPFEfVx5j+y2yF35Kzx5sF7kDzxuDj6ziH4FFbOp87zKDZNx8yExJIb05OGF4Nlt9IHFIMBkRl41VdvcNdbQ==} + engines: {node: 4.x || >=6.0.0} + peerDependencies: + encoding: ^0.1.0 + peerDependenciesMeta: + encoding: + optional: true + dependencies: + whatwg-url: 5.0.0 + dev: false + /node-int64/0.4.0: resolution: {integrity: sha1-h6kGXNs1XTGC2PlM4RGIuCXGijs=} dev: false @@ -7409,6 +7456,10 @@ packages: once: 1.4.0 dev: false + /punycode/1.4.1: + resolution: {integrity: sha1-wNWmOycYgArY4esPpSachN1BhF4=} + dev: false + /punycode/2.1.1: resolution: {integrity: sha512-XRsRjdf+j5ml+y/6GKHPZbrF/8p2Yga0JPtdqTIY2Xe5ohJPD9saDJJLPvp9+NSBprVvevdXZybnj2cv8OEd0A==} engines: {node: '>=6'} @@ -7999,6 +8050,12 @@ packages: engines: {node: '>= 0.8'} dev: false + /stream-events/1.0.5: + resolution: {integrity: sha512-E1GUzBSgvct8Jsb3v2X15pjzN1tYebtbLaMg+eBOUOAxgbLoSbT2NS91ckc5lJD1KfLjId+jXJRgo0qnV5Nerg==} + dependencies: + stubs: 3.0.0 + dev: false + /stream-log-stats/3.0.2: resolution: {integrity: sha512-393j7aeF9iRdHvyANqEQU82UQmpw2CTxgsT83caefh+lOxavVLbVrw8Mr4zjXeZLh2+xeHZMKfVx4T0rJ/EchA==} engines: {node: '>=8.0.0'} @@ -8138,6 +8195,10 @@ packages: engines: {node: '>=8'} dev: false + /stubs/3.0.0: + resolution: {integrity: sha1-6NK6H6nJBXAwPAMLaQD31fiavls=} + dev: false + /style-inject/0.3.0: resolution: {integrity: sha512-IezA2qp+vcdlhJaVm5SOdPPTUu0FCEqfNSli2vRuSIBbu5Nq5UvygTk/VzeCqfLz2Atj3dVII5QBKGZRZ0edzw==} dev: true @@ -8294,6 +8355,20 @@ packages: strip-ansi: 6.0.1 dev: false + /teeny-request/7.1.1: + resolution: {integrity: sha512-iwY6rkW5DDGq8hE2YgNQlKbptYpY5Nn2xecjQiNjOXWbKzPGUfmeUBCSQbbr306d7Z7U2N0TPl+/SwYRfua1Dg==} + engines: {node: '>=10'} + dependencies: + http-proxy-agent: 4.0.1 + https-proxy-agent: 5.0.1 + node-fetch: 2.6.7 + stream-events: 1.0.5 + uuid: 8.3.2 + transitivePeerDependencies: + - encoding + - supports-color + dev: false + /terminal-link/2.1.1: resolution: {integrity: sha512-un0FmiRUQNr5PJqy9kP7c40F5BOfpGlYTrxonDChEZB7pzZxRNp/bt+ymiy9/npwXya9KH99nJ/GXFIiUkYGFQ==} engines: {node: '>=8'} @@ -8385,6 +8460,10 @@ packages: universalify: 0.1.2 dev: false + /tr46/0.0.3: + resolution: {integrity: sha1-gYT9NH2snNwYWZLzpmIuFLnZq2o=} + dev: false + /tr46/2.1.0: resolution: {integrity: sha512-15Ih7phfcdP5YxqiB+iDtLoaTz4Nd35+IiAv0kQ5FNKHzXgdWqPoTIqEDDJmXceQt4JZk6lVPT8lnDlPpGDppw==} engines: {node: '>=8'} @@ -8573,9 +8652,20 @@ packages: punycode: 2.1.1 dev: false + /urlgrey/1.0.0: + resolution: {integrity: sha512-hJfIzMPJmI9IlLkby8QrsCykQ+SXDeO2W5Q9QTW3QpqZVTx4a/K7p8/5q+/isD8vsbVaFgql/gvAoQCRQ2Cb5w==} + dependencies: + fast-url-parser: 1.1.3 + dev: false + /util-deprecate/1.0.2: resolution: {integrity: sha1-RQ1Nyfpw3nMnYvvS1KKJgUGaDM8=} + /uuid/8.3.2: + resolution: {integrity: sha512-+NYs2QeMWy+GWFOEm9xnn6HCDp0l7QBD7ml8zLUmJ+93Q5NF0NocErnwkTkXVFNiX3/fpC6afS8Dhb/gz7R7eg==} + hasBin: true + dev: false + /v8-compile-cache/2.3.0: resolution: {integrity: sha512-l8lCEmLcLYZh4nbunNZvQCJc5pv7+RCwa8q/LdUx8u7lsWvPDKmpodJAJNwkAhJC//dFY48KuIEmjtd4RViDrA==} dev: false @@ -8677,6 +8767,10 @@ packages: makeerror: 1.0.12 dev: false + /webidl-conversions/3.0.1: + resolution: {integrity: sha1-JFNCdeKnvGvnvIZhHMFq4KVlSHE=} + dev: false + /webidl-conversions/5.0.0: resolution: {integrity: sha512-VlZwKPCkYKxQgeSbH5EyngOmRp7Ww7I9rQLERETtf5ofd9pGeswWiOtogpEO850jziPRarreGxn5QIiTqpb2wA==} engines: {node: '>=8'} @@ -8697,6 +8791,13 @@ packages: resolution: {integrity: sha512-M4yMwr6mAnQz76TbJm914+gPpB/nCwvZbJU28cUD6dR004SAxDLOOSUaB1JDRqLtaOV/vi0IC5lEAGFgrjGv/g==} dev: false + /whatwg-url/5.0.0: + resolution: {integrity: sha1-lmRU6HZUYuN2RNNib2dCzotwll0=} + dependencies: + tr46: 0.0.3 + webidl-conversions: 3.0.1 + dev: false + /whatwg-url/8.7.0: resolution: {integrity: sha512-gAojqb/m9Q8a5IV96E3fHJM70AzCkgt4uXYX2O7EmuyOnLrViCQlsEBmF9UQIu3/aeAIp2U17rtbpZWNntQqdg==} engines: {node: '>=10'}