Skip to content

Commit f4abc62

Browse files
authored
✨ Add metadata on the generated sources (dubzzz#526) (dubzzz#540)
1 parent c2de4fc commit f4abc62

File tree

5 files changed

+51
-12
lines changed

5 files changed

+51
-12
lines changed

.codesandbox/ci.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11

22

33
{
4-
"buildCommand": "build",
4+
"buildCommand": "build:prod",
55
"sandboxes": ["vanilla", "/example"]
66
}
77

.travis.yml

+2-10
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,7 @@ jobs:
4141
- stage: test
4242
env: STEP=EXAMPLES_ROLLUP_LEGACY_TYPES
4343
script:
44-
- yarn prebuild
45-
- yarn build:publish-types
46-
- yarn build:publish-cjs
47-
- yarn build:publish-esm
48-
- yarn webbuild
44+
- yarn build:prod
4945
- yarn link
5046
- cd example ;
5147
yarn --ignore-engines ;
@@ -84,11 +80,7 @@ jobs:
8480
- stage: deploy
8581
if: tag =~ ^v\d+\.
8682
script:
87-
- yarn prebuild
88-
- yarn build:publish-types
89-
- yarn build:publish-cjs
90-
- yarn build:publish-esm
91-
- yarn webbuild
83+
- yarn build:prod
9284
deploy:
9385
provider: npm
9486

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,9 @@
2121
"build:watch": "tsc -w",
2222
"build:publish-types": "tsc -p tsconfig.publish.types.json && node ./buildTypes.js",
2323
"build:publish-cjs": "tsc -p tsconfig.publish.json",
24-
"build:publish-esm": "tsc -p tsconfig.publish.json --module es2015 --moduleResolution node --outDir lib/esm && node postbuild/main.js",
24+
"build:publish-esm": "tsc -p tsconfig.publish.json --module es2015 --moduleResolution node --outDir lib/esm",
2525
"webbuild": "browserify lib/fast-check.js --s fastcheck | uglifyjs -cm > lib/bundle.js",
26+
"build:prod": "yarn prebuild && yarn build:publish-cjs && yarn build:publish-esm && yarn build:publish-types && yarn webbuild && node postbuild/main.js",
2627
"watch": "tsc -w",
2728
"test": "jest --config jest.unit.config.js --coverage",
2829
"test:watch": "jest --config jest.unit.config.js --watch",

postbuild/main.js

+39
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
// eslint-disable-next-line
2+
const fs = require('fs');
3+
// eslint-disable-next-line
4+
const path = require('path');
5+
// eslint-disable-next-line
26
const replace = require('replace-in-file');
37

8+
// Append *.js file extension on all local imports
9+
410
const options = {
511
files: 'lib/**/*.js',
612
from: [/from '\.(.*)(?<!\.js)'/g, /from "\.(.*)(?<!\.js)"/g],
@@ -14,3 +20,36 @@ for (const { file, hasChanged } of results) {
1420
console.info(`Extensions added to: ${file}`);
1521
}
1622
}
23+
24+
// Fill metas related to the package
25+
26+
// eslint-disable-next-line
27+
fs.readFile(path.join(__dirname, '../package.json'), (err, data) => {
28+
if (err) {
29+
// eslint-disable-next-line
30+
console.error(err.message);
31+
return;
32+
}
33+
34+
const packageVersion = JSON.parse(data.toString()).version;
35+
36+
const commonJsReplacement = replace.sync({
37+
files: 'lib/fast-check-default.js',
38+
from: [/__PACKAGE_TYPE__/g, /__PACKAGE_VERSION__/g],
39+
to: ['commonjs', packageVersion]
40+
});
41+
if (commonJsReplacement.length === 1 && commonJsReplacement[0].hasChanged) {
42+
// eslint-disable-next-line
43+
console.info(`Package details added onto commonjs version`);
44+
}
45+
46+
const moduleReplacement = replace.sync({
47+
files: 'lib/esm/fast-check-default.js',
48+
from: [/__PACKAGE_TYPE__/g, /__PACKAGE_VERSION__/g],
49+
to: ['module', packageVersion]
50+
});
51+
if (moduleReplacement.length === 1 && moduleReplacement[0].hasChanged) {
52+
// eslint-disable-next-line
53+
console.info(`Package details added onto module version`);
54+
}
55+
});

src/fast-check-default.ts

+7
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,10 @@ import { Stream, stream } from './stream/Stream';
8989
import { stringify } from './utils/stringify';
9090
import { scheduler, Scheduler, SchedulerSequenceItem } from './check/arbitrary/AsyncSchedulerArbitrary';
9191

92+
// Explicit cast into string to avoid to have __type: "__PACKAGE_TYPE__"
93+
const __type = '__PACKAGE_TYPE__' as string;
94+
const __version = '__PACKAGE_VERSION__' as string;
95+
9296
// boolean
9397
// floating point types
9498
// integer types
@@ -97,6 +101,9 @@ import { scheduler, Scheduler, SchedulerSequenceItem } from './check/arbitrary/A
97101
// combination of others
98102
// complex combinations
99103
export {
104+
// meta
105+
__type,
106+
__version,
100107
// assess the property
101108
sample,
102109
statistics,

0 commit comments

Comments
 (0)