@guanghechen/invariant 3.0.0-alpha.1 → 3.0.0-alpha.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/cjs/index.js CHANGED
@@ -16,3 +16,4 @@ function invariant(condition, message) {
16
16
 
17
17
  exports.default = invariant;
18
18
  exports.invariant = invariant;
19
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sources":["../../src/index.ts"],"sourcesContent":[null],"names":[],"mappings":";;;;AAAA,MAAM,YAAY,GAAY,OAAO,CAAC,GAAG,CAAC,QAAQ,KAAK,YAAY,CAAA;AACnE,MAAM,MAAM,GAAG,kBAAkB,CAAA;AASjB,SAAA,SAAS,CACvB,SAAkB,EAClB,OAAwC,EAAA;AAExC,IAAA,IAAI,SAAS;QAAE,OAAM;AAGrB,IAAA,IAAI,YAAY;AAAE,QAAA,MAAM,IAAI,KAAK,CAAC,MAAM,CAAC,CAAA;IAGzC,IAAI,OAAO,IAAI,IAAI;AAAE,QAAA,MAAM,IAAI,KAAK,CAAC,MAAM,GAAG,IAAI,CAAC,CAAA;IAEnD,MAAM,IAAI,KAAK,CAAC,MAAM,GAAG,IAAI,IAAI,OAAO,YAAY,QAAQ,GAAG,OAAO,EAAE,GAAG,OAAO,CAAC,CAAC,CAAA;AACtF;;;;;"}
package/lib/esm/index.js CHANGED
@@ -11,3 +11,4 @@ function invariant(condition, message) {
11
11
  }
12
12
 
13
13
  export { invariant as default, invariant };
14
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sources":["../../src/index.ts"],"sourcesContent":[null],"names":[],"mappings":"AAAA,MAAM,YAAY,GAAY,OAAO,CAAC,GAAG,CAAC,QAAQ,KAAK,YAAY,CAAA;AACnE,MAAM,MAAM,GAAG,kBAAkB,CAAA;AASjB,SAAA,SAAS,CACvB,SAAkB,EAClB,OAAwC,EAAA;AAExC,IAAA,IAAI,SAAS;QAAE,OAAM;AAGrB,IAAA,IAAI,YAAY;AAAE,QAAA,MAAM,IAAI,KAAK,CAAC,MAAM,CAAC,CAAA;IAGzC,IAAI,OAAO,IAAI,IAAI;AAAE,QAAA,MAAM,IAAI,KAAK,CAAC,MAAM,GAAG,IAAI,CAAC,CAAA;IAEnD,MAAM,IAAI,KAAK,CAAC,MAAM,GAAG,IAAI,IAAI,OAAO,YAAY,QAAQ,GAAG,OAAO,EAAE,GAAG,OAAO,CAAC,CAAC,CAAA;AACtF;;;;"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@guanghechen/invariant",
3
- "version": "3.0.0-alpha.1",
3
+ "version": "3.0.0-alpha.2",
4
4
  "description": "Invariant function",
5
5
  "author": {
6
6
  "name": "guanghechen",
@@ -12,8 +12,8 @@
12
12
  "directory": "packages/invariant"
13
13
  },
14
14
  "homepage": "https://github.com/guanghechen/node-scaffolds/tree/release-3.x.x/packages/invariant#readme",
15
- "main": "lib/cjs/index.js",
16
- "module": "lib/esm/index.js",
15
+ "type": "module",
16
+ "exports": "./lib/esm/index.js",
17
17
  "types": "lib/types/index.d.ts",
18
18
  "source": "src/index.ts",
19
19
  "license": "MIT",
@@ -22,8 +22,7 @@
22
22
  },
23
23
  "files": [
24
24
  "lib/",
25
- "!lib/**/*.js.map",
26
- "!lib/**/*.d.ts.map",
25
+ "src/",
27
26
  "package.json",
28
27
  "CHANGELOG.md",
29
28
  "LICENSE",
@@ -32,8 +31,8 @@
32
31
  "scripts": {
33
32
  "build": "cross-env NODE_ENV=production rollup -c ../../rollup.config.mjs",
34
33
  "prebuild": "rimraf lib/",
35
- "prepublishOnly": "cross-env ROLLUP_SHOULD_SOURCEMAP=false yarn build",
34
+ "prepublishOnly": "yarn build",
36
35
  "test": "cross-env TS_NODE_FILES=true NODE_OPTIONS=--experimental-vm-modules jest --config ../../jest.config.js --rootDir ."
37
36
  },
38
- "gitHead": "69dd141feb9fae875cf252ac6f6404542425a147"
37
+ "gitHead": "76c56b76cb6b1fb368d47a8c20d2c06398f0009b"
39
38
  }
package/src/index.ts ADDED
@@ -0,0 +1,26 @@
1
+ const isProduction: boolean = process.env.NODE_ENV === 'production'
2
+ const prefix = 'Invariant failed'
3
+
4
+ /**
5
+ * Assert the condition is true
6
+ *
7
+ * @param condition
8
+ * @param message
9
+ * @returns
10
+ */
11
+ export function invariant(
12
+ condition: boolean,
13
+ message?: (() => string) | string | null,
14
+ ): asserts condition {
15
+ if (condition) return
16
+
17
+ // Strip out errors messages in production environment.
18
+ if (isProduction) throw new Error(prefix)
19
+
20
+ // Throw an error when the condition fails.
21
+ if (message == null) throw new Error(prefix + ': ')
22
+
23
+ throw new Error(prefix + ': ' + (message instanceof Function ? message() : message))
24
+ }
25
+
26
+ export default invariant