Skip to content

Commit

Permalink
Add optional types entry point (visgl#6946)
Browse files Browse the repository at this point in the history
  • Loading branch information
Pessimistress authored May 26, 2022
1 parent d42c4e0 commit bc30a77
Show file tree
Hide file tree
Showing 37 changed files with 103 additions and 24 deletions.
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
dist/
dist-es6/
modules/*/typed
node_modules/
workers/
dist.js
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
build/
dist/
modules/*/typed
**/*.min.js
node_modules/
coverage/
Expand Down
1 change: 1 addition & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
workers/
**/dist*/**/*
dist.js
modules/*/typed
*.min.js
**/mapbox-gl-dev.js
**/mapbox-gl.js
Expand Down
1 change: 1 addition & 0 deletions modules/aggregation-layers/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"files": [
"dist",
"src",
"typed",
"dist.min.js"
],
"sideEffects": false,
Expand Down
2 changes: 1 addition & 1 deletion modules/aggregation-layers/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"compilerOptions": {
"composite": true,
"rootDir": "src",
"outDir": "dist/typed"
"outDir": "typed"
},
"references": [
{"path": "../core"},
Expand Down
1 change: 1 addition & 0 deletions modules/arcgis/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"files": [
"dist",
"src",
"typed",
"dist.min.js"
],
"sideEffects": false,
Expand Down
2 changes: 1 addition & 1 deletion modules/arcgis/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"compilerOptions": {
"composite": true,
"rootDir": "src",
"outDir": "dist/typed"
"outDir": "typed"
},
"references": [
{"path": "../core"}
Expand Down
1 change: 1 addition & 0 deletions modules/carto/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"files": [
"dist",
"src",
"typed",
"dist.min.js"
],
"sideEffects": false,
Expand Down
2 changes: 1 addition & 1 deletion modules/carto/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"compilerOptions": {
"composite": true,
"rootDir": "src",
"outDir": "dist/typed"
"outDir": "typed"
},
"references": [
{"path": "../core"},
Expand Down
1 change: 1 addition & 0 deletions modules/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"files": [
"dist",
"src",
"typed",
"debug.min.js",
"dist.min.js"
],
Expand Down
2 changes: 1 addition & 1 deletion modules/core/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"compilerOptions": {
"composite": true,
"rootDir": "src",
"outDir": "dist/typed"
"outDir": "typed"
},
"references": [
]
Expand Down
1 change: 1 addition & 0 deletions modules/extensions/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"files": [
"dist",
"src",
"typed",
"dist.min.js"
],
"sideEffects": false,
Expand Down
2 changes: 1 addition & 1 deletion modules/extensions/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"compilerOptions": {
"composite": true,
"rootDir": "src",
"outDir": "dist/typed"
"outDir": "typed"
},
"references": [
{"path": "../core"}
Expand Down
1 change: 1 addition & 0 deletions modules/geo-layers/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"files": [
"dist",
"src",
"typed",
"dist.min.js"
],
"sideEffects": false,
Expand Down
2 changes: 1 addition & 1 deletion modules/geo-layers/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"compilerOptions": {
"composite": true,
"rootDir": "src",
"outDir": "dist/typed"
"outDir": "typed"
},
"references": [
{"path": "../core"},
Expand Down
1 change: 1 addition & 0 deletions modules/google-maps/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"files": [
"dist",
"src",
"typed",
"dist.min.js"
],
"sideEffects": false,
Expand Down
2 changes: 1 addition & 1 deletion modules/google-maps/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"compilerOptions": {
"composite": true,
"rootDir": "src",
"outDir": "dist/typed"
"outDir": "typed"
},
"references": [
{"path": "../core"}
Expand Down
1 change: 1 addition & 0 deletions modules/json/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"files": [
"dist",
"src",
"typed",
"dist.min.js"
],
"sideEffects": false,
Expand Down
2 changes: 1 addition & 1 deletion modules/json/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"compilerOptions": {
"composite": true,
"rootDir": "src",
"outDir": "dist/typed"
"outDir": "typed"
},
"references": [
{"path": "../core"}
Expand Down
2 changes: 1 addition & 1 deletion modules/jupyter-widget/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"compilerOptions": {
"composite": true,
"rootDir": "src",
"outDir": "dist/typed"
"outDir": "typed"
},
"references": [
{"path": "../aggregation-layers"},
Expand Down
1 change: 1 addition & 0 deletions modules/layers/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"files": [
"dist",
"src",
"typed",
"dist.min.js"
],
"sideEffects": false,
Expand Down
2 changes: 0 additions & 2 deletions modules/layers/src/polygon-layer/polygon-layer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,6 @@ export default class PolygonLayer<DataT = any, ExtraProps = {}> extends Composit
(changeFlags.updateTriggersChanged.all || changeFlags.updateTriggersChanged.getPolygon));

if (geometryChanged && Array.isArray(changeFlags.dataChanged)) {
// @ts-expect-error state is always initialized on instantiated layer
const paths = this.state.paths.slice();
const pathsDiff = changeFlags.dataChanged.map(dataRange =>
replaceInRange({
Expand Down Expand Up @@ -357,7 +356,6 @@ export default class PolygonLayer<DataT = any, ExtraProps = {}> extends Composit
material
} = this.props;

// @ts-expect-error state is always initialized on instantiated layer
const {paths, pathsDiff} = this.state;

const FillLayer = this.getSubLayerClass('fill', SolidPolygonLayer);
Expand Down
2 changes: 1 addition & 1 deletion modules/layers/src/scatterplot-layer/scatterplot-layer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ export default class ScatterplotLayer<DataT = any, ExtraPropsT = {}> extends Lay
});
}

updateState(params: UpdateParameters<ScatterplotLayer<DataT, ExtraPropsT>>) {
updateState(params: UpdateParameters<this>) {
super.updateState(params);

if (params.changeFlags.extensionsChanged) {
Expand Down
2 changes: 1 addition & 1 deletion modules/layers/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"compilerOptions": {
"composite": true,
"rootDir": "src",
"outDir": "dist/typed"
"outDir": "typed"
},
"references": [
{"path": "../core"}
Expand Down
1 change: 1 addition & 0 deletions modules/main/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"files": [
"dist",
"src",
"typed",
"dist.min.js"
],
"sideEffects": false,
Expand Down
2 changes: 1 addition & 1 deletion modules/main/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"compilerOptions": {
"composite": true,
"rootDir": "src",
"outDir": "dist/typed"
"outDir": "typed"
},
"references": [
{"path": "../aggregation-layers"},
Expand Down
1 change: 1 addition & 0 deletions modules/mapbox/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"files": [
"dist",
"src",
"typed",
"dist.min.js"
],
"sideEffects": false,
Expand Down
2 changes: 1 addition & 1 deletion modules/mapbox/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"compilerOptions": {
"composite": true,
"rootDir": "src",
"outDir": "dist/typed"
"outDir": "typed"
},
"references": [
{"path": "../core"}
Expand Down
1 change: 1 addition & 0 deletions modules/mesh-layers/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"files": [
"dist",
"src",
"typed",
"dist.min.js"
],
"sideEffects": false,
Expand Down
2 changes: 1 addition & 1 deletion modules/mesh-layers/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"compilerOptions": {
"composite": true,
"rootDir": "src",
"outDir": "dist/typed"
"outDir": "typed"
},
"references": [
{"path": "../core"}
Expand Down
3 changes: 2 additions & 1 deletion modules/react/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
"module": "dist/esm/index.js",
"files": [
"dist",
"src"
"src",
"typed"
],
"sideEffects": false,
"scripts": {},
Expand Down
2 changes: 1 addition & 1 deletion modules/react/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"compilerOptions": {
"composite": true,
"rootDir": "src",
"outDir": "dist/typed"
"outDir": "typed"
},
"references": [
{"path": "../core"}
Expand Down
3 changes: 2 additions & 1 deletion modules/test-utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
"module": "dist/esm/index.js",
"files": [
"dist",
"src"
"src",
"typed"
],
"peerDependencies": {
"@deck.gl/core": "^8.0.0",
Expand Down
2 changes: 1 addition & 1 deletion modules/test-utils/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"compilerOptions": {
"composite": true,
"rootDir": "src",
"outDir": "dist/typed"
"outDir": "typed"
},
"references": [
{"path": "../core"}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
"test-node": "ocular-test node",
"test-browser": "ocular-test browser",
"tsclean": "find . -name tsconfig.tsbuildinfo -exec rm {} \\;",
"tsbuild": "tsc -b tsconfig.build.json",
"tsbuild": "tsc -b tsconfig.build.json && node scripts/typed-entry.js",
"bench": "ocular-test bench",
"bench-browser": "ocular-test bench-browser",
"metrics": "ocular-metrics",
Expand Down
64 changes: 64 additions & 0 deletions scripts/typed-entry.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/**
* This script enables a temporary, opt-in only entry point in each module while TypeScript is being developed.
*/

const fs = require('fs');
const path = require('path');

const MODULES_DIR = path.resolve(__dirname, '../modules');
const IMPORT_PATTERN = /\bfrom ('|")(@deck.gl\/[\w\-]+)('|")/g;

for (const moduleName of fs.readdirSync(MODULES_DIR)) {
if (!isDirectory(`${MODULES_DIR}/${moduleName}`)) {
continue;
}

const distDir = `${MODULES_DIR}/${moduleName}/typed`;
if (isDirectory(distDir)) {
// Create package.json
const entryPoint = {
internal: true,
main: "../dist/es5/index.js",
module: "../dist/esm/index.js",
types: "index.d.ts"
};

fs.writeFileSync(`${distDir}/package.json`, JSON.stringify(entryPoint, null, 2), 'utf8');

// Update all internal imports
const stats = updateImports(distDir);
console.log(`${stats.fileCount} files updated in module ${moduleName}`)

} else {
console.log(`No types found for module ${moduleName}`);
}
}

function isDirectory(path) {
try {
const stats = fs.lstatSync(path);
return stats.isDirectory();
} catch {
// Does not exist
return false;
}
}

// Replace all internal imports (@deck.gl/*) with '@deck.gl/*/typed'
function updateImports(path, stats = {fileCount: 0}) {
for (const item of fs.readdirSync(path)) {
const subPath = `${path}/${item}`;
if (item.endsWith('.d.ts')) {
// find imports
let fileContent = fs.readFileSync(subPath, 'utf8');
if (IMPORT_PATTERN.test(fileContent)) {
fileContent = fileContent.replace(IMPORT_PATTERN, (_, $1, $2) => `from '${$2}/typed'`);
stats.fileCount++;
fs.writeFileSync(subPath, fileContent, 'utf8');
}
} else if (isDirectory(subPath)) {
updateImports(subPath, stats);
}
}
return stats;
}
6 changes: 3 additions & 3 deletions tsconfig.build.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,16 @@
// {"path": "modules/arcgis"},
// {"path": "modules/carto"},
{"path": "modules/core"},
// {"path": "modules/extensions"},
{"path": "modules/extensions"},
// {"path": "modules/geo-layers"},
// {"path": "modules/google-maps"},
// {"path": "modules/json"},
// {"path": "modules/jupyter-widget"},
// {"path": "modules/layers"},
{"path": "modules/layers"},
// {"path": "modules/main"},
// {"path": "modules/mapbox"},
// {"path": "modules/mesh-layers"},
// {"path": "modules/react"},
{"path": "modules/react"},
// {"path": "modules/test-utils"}
],
"include": [
Expand Down

0 comments on commit bc30a77

Please sign in to comment.