Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Emit .d.ts declaration files for @finos/perspective-viewer #1649

Merged
merged 2 commits into from
Dec 3, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions examples/react/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import "@finos/perspective-viewer";
import "@finos/perspective-viewer-datagrid";
import "@finos/perspective-viewer-d3fc";
import {
HTMLPerspectiveViewerConfig,
PerspectiveViewerConfig,
HTMLPerspectiveViewerElement,
} from "@finos/perspective-viewer";

Expand All @@ -30,7 +30,7 @@ const getTable = async (): Promise<perspective.Table> => {
return await worker.table(buffer as any);
};

const config: HTMLPerspectiveViewerConfig = {
const config: PerspectiveViewerConfig = {
row_pivots: ["State"],
};

Expand Down
16 changes: 0 additions & 16 deletions packages/perspective-webpack-plugin/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,22 +68,6 @@ class PerspectiveWebpackPlugin {
if (this.options.inline || this.options.inlineWorker) {
rules[rules.length - 2].use.options.inline = "no-fallback";
rules[rules.length - 1].use.options.inline = "no-fallback";
} else {
rules.push({
test: /\.js$/,
include: path.dirname(
require.resolve("@finos/perspective-viewer/package.json")
),
use: [
{
loader: require.resolve("string-replace-loader"),
options: {
search: /webpackMode:\s*?"eager"/g,
replace: "",
},
},
],
});
}

if (!(this.options.inline || this.options.inlineWasm)) {
Expand Down
1 change: 1 addition & 0 deletions packages/perspective/src/js/perspective.node.js
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,7 @@ const websocket = (url) => {
module.exports.get_type_config = get_type_config;
module.exports.get_config = get_config;
module.exports.worker = () => module.exports;
module.exports.shared_worker = () => module.exports;
module.exports.websocket = websocket;
module.exports.perspective_assets = perspective_assets;
module.exports.WebSocketServer = WebSocketServer;
Expand Down
27 changes: 27 additions & 0 deletions rust/perspective-viewer/build.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
const {lessLoader} = require("esbuild-plugin-less");
const {execSync} = require("child_process");
const util = require("util");
const exec = util.promisify(require("child_process").exec);
const fs = require("fs");

const {IgnoreCSSPlugin} = require("@finos/perspective-build/ignore_css");
Expand All @@ -10,6 +12,18 @@ const {NodeModulesExternal} = require("@finos/perspective-build/external");
const {ReplacePlugin} = require("@finos/perspective-build/replace");
const {build} = require("@finos/perspective-build/build");

function _compile(fileNames) {
const ts = require("typescript");
const path = require.resolve("@finos/perspective-viewer/tsconfig.json");
const {compilerOptions} = JSON.parse(fs.readFileSync(path).toString());
const {options} = ts.convertCompilerOptionsFromJson(compilerOptions, "");
const host = ts.createCompilerHost(options);
fs.mkdirSync("dist/esm", {recursive: true});
host.writeFile = (path, contents) => fs.writeFileSync(path, contents);
const program = ts.createProgram(fileNames, options, host);
program.emit();
}

const PREBUILD = [
{
entryPoints: [
Expand Down Expand Up @@ -93,6 +107,12 @@ const POSTBUILD = [
];

async function build_all() {
// generate declaration in parallel because tsc is sloooow.
let tsc;
if (fs.existsSync("dist/pkg/perspective_viewer.js")) {
tsc = exec("yarn tsc --emitDeclarationOnly --outDir dist/esm");
}

await Promise.all(PREBUILD.map(build)).catch(() => process.exit(1));

const debug = process.env.PSP_DEBUG ? "--debug" : "";
Expand All @@ -104,11 +124,18 @@ async function build_all() {
}
);

if (typeof tsc === "undefined") {
tsc = exec("yarn tsc --emitDeclarationOnly --outDir dist/esm");
}

await Promise.all(BUILD.map(build)).catch(() => process.exit(1));
await Promise.all(POSTBUILD.map(build)).catch(() => process.exit(1));

// legacy compat
execSync("cpy dist/css/* dist/umd");

const {stdout} = await tsc;
console.log(stdout);
}

build_all();
5 changes: 3 additions & 2 deletions rust/perspective-viewer/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,14 @@
"./umd/": "./dist/umd/",
"./pkg/": "./dist/pkg/",
"./src/": "./src/",
"./package.json": "./package.json"
"./package.json": "./package.json",
"./tsconfig.json": "./tsconfig.json"
},
"files": [
"dist/**/*",
"src/**/*"
],
"types": "dist/esm/index.d.ts",
"types": "dist/esm/perspective-viewer.d.ts",
"scripts": {
"build": "node ./build.js",
"clean": "rimraf dist && rimraf pkg && rimraf build",
Expand Down
11 changes: 7 additions & 4 deletions rust/perspective-viewer/src/rust/js/monaco.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,15 @@ pub enum KeyCode {

#[cfg_attr(
not(test),
wasm_bindgen(module = "monaco-editor/esm/vs/editor/editor.worker.js")
wasm_bindgen(inline_js = "
import monaco from 'monaco-editor/esm/vs/editor/editor.worker.js';
export default async function () {
return await monaco();
}
")
)]
#[cfg_attr(test, wasm_bindgen(inline_js = "export default async function() {}"))]
extern "C" {

#[wasm_bindgen(js_name = "default")]
pub async fn new_worker() -> JsValue;
}
Expand All @@ -36,9 +40,8 @@ extern "C" {
not(test),
wasm_bindgen(inline_js = "
export async function monaco_module() {
return import(
return await import(
/* webpackChunkName: \"monaco\" */
/* webpackMode: \"eager\" */
'monaco-editor/esm/vs/editor/edcore.main.js'
);
}
Expand Down
1 change: 1 addition & 0 deletions rust/perspective-viewer/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"module": "esnext",
"target": "es2018",
"declaration": true,
"emitDeclarationOnly": true,
"outDir": "dist/esm",
"rootDir": "src/ts",
"allowSyntheticDefaultImports": true,
Expand Down