Skip to content

Commit

Permalink
fix(types): Fix plugin's type definition
Browse files Browse the repository at this point in the history
Generate type.d file on dist folder to make plugin modules
to resolve corresponding type definition file

Fix #2483
  • Loading branch information
netil authored Jan 5, 2022
1 parent d3d2e05 commit f3690f9
Show file tree
Hide file tree
Showing 15 changed files with 82 additions and 30 deletions.
4 changes: 1 addition & 3 deletions config/cjs.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,4 @@ const content = {
"type": "commonjs"
};

writeJson(resolvePath("../dist/package.json", false), content, e => {
console.error(e);
});
writeJson(resolvePath("../dist/package.json", false), content);
3 changes: 3 additions & 0 deletions config/const.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"pluginPrefix": "billboardjs-plugin"
}
3 changes: 2 additions & 1 deletion config/rollup/esm.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import del from "rollup-plugin-delete";
import {getBanner, readJson, resolvePath} from "../util.js";

const pkg = readJson("package.json");
const prefix = readJson("./const.json").pluginPrefix;
const distPath = "dist-esm";

const {plugin, production} = getBanner();
Expand Down Expand Up @@ -44,7 +45,7 @@ const bbPlugins = readdirSync(resolvePath("../src/Plugin/"), {
.map(({name}) => ({
input: `src/Plugin/${name}/index.ts`,
output: {
file: `${distPath}/plugin/billboardjs-plugin-${name}.js`,
file: `${distPath}/plugin/${prefix}-${name}.js`,
format: "es",
banner: getBannerStr(true)
},
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion config/banner.js → config/template/banner.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {readJson} from "./util.js";
import {readJson} from "../util.js";

const pkg = readJson("package.json");

Expand Down
7 changes: 7 additions & 0 deletions config/template/plugin.d.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/**
* Copyright (c) 2017 ~ present NAVER Corp.
* billboard.js project is licensed under the MIT license
*/
import PluginClass from "../../types/plugin/{=PLUGIN-NAME}";

export default PluginClass;
29 changes: 29 additions & 0 deletions config/type.d-plugin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/**
* Generate plugin type definition file for dist folder.
* Note: Need to be executed after 'dist', 'dist-esm' folders are generated.
*/
import fs from "fs";
import {resolvePath, readFile, readJson, writeFile} from "./util.js";

const srcPath = resolvePath("../../src/Plugin/");
const distPath = resolvePath("../../dist/plugin/");
const distEsmPath = resolvePath("../../dist-esm/plugin/");

// read plugin type template
const template = readFile("./template/plugin.d.txt");

// read the const to get plugin filename prefix
const prefix = readJson("./const.json").pluginPrefix;

// read the plugin directory
fs.readdirSync(srcPath, {
withFileTypes: true
})
.filter(dirent => dirent.isDirectory())
.forEach(({name}) => {
const fileName = `${prefix}-${name}.d.ts`;
const content = template.replace(/{=PLUGIN-NAME}/, name)

writeFile(`${distPath}/${fileName}`, content);
writeFile(`${distEsmPath}/${fileName}`, content);
});
26 changes: 20 additions & 6 deletions config/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const __dirname = dirname(__filename);
*/
function getBanner() {
return JSON.parse(
execSync(`node ${resolvePath("./banner.js")}`, {encoding: "utf-8"})
execSync(`node ${resolvePath("./template/banner.js")}`, {encoding: "utf-8"})
);
}

Expand All @@ -34,26 +34,40 @@ function getBanner() {
/**
* Read json from the root of the project.
* @param {string} path Path from the root
* @param {boolean} isJson If the value is JSON
*/
function readJson(path) {
return JSON.parse(readFileSync(resolvePath(`../${path}`), "utf8"));
function readFile(path, isJson = false) {
const res = readFileSync(resolvePath(`../${path}`), "utf8");

return isJson ? JSON.parse(res) : res;
}

/**
* Write json to file
* @param {string} target Path from the root
* @param {object} json JSON object
* @param {object} value Value to write
* @param {boolean} isJson If the value is JSON
*/
function writeJson(target, json) {
writeFileSync(target, JSON.stringify(json), e => {
function writeFile(target, value, isJson = false) {
writeFileSync(target, isJson ? JSON.stringify(value) : value, {flag: "w"}, e => {
console.error(e);
});
}

function readJson(path) {
return readFile(path, true);
}

function writeJson(target, value) {
return writeFile(target, value, true);
}

export {
__dirname,
getBanner,
readFile,
readJson,
resolvePath,
writeFile,
writeJson
};
2 changes: 1 addition & 1 deletion config/webpack/packaged.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const {merge} = require("webpack-merge");
const webpack = require("webpack");
const TerserPlugin = require("terser-webpack-plugin");
const terserConfig = require("../terserConfig.cjs");
const banner = require("../banner.cjs");
const banner = require("../template/banner.cjs");


const config = {
Expand Down
15 changes: 7 additions & 8 deletions config/webpack/plugin.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,28 @@ const path = require("path");
const CleanWebpackPlugin = require("clean-webpack-plugin").CleanWebpackPlugin;
const TerserPlugin = require("terser-webpack-plugin");
const terserConfig = require("../terserConfig.cjs");
const banner = require("../banner.cjs");
const banner = require("../template/banner.cjs");

const srcPath = "./src/Plugin/";
const distPath = path.resolve(__dirname, "../../dist/plugin/");
const prefix = require("../const.json").pluginPrefix;

// construct entry point
const entry = {};

fs.readdirSync(path.resolve(__dirname, `../../${srcPath}`), {
withFileTypes: true
}).forEach(dirent => {
if (dirent.isDirectory()) {
const name = dirent.name;

})
.filter(dirent => dirent.isDirectory())
.forEach(({name}) => {
entry[name] = `${srcPath}${name}/index.ts`;
}
});
});

const config = {
entry,
output: {
path: distPath,
filename: `billboardjs-plugin-[name].js`,
filename: `${prefix}-[name].js`,
library: ["bb", "plugin", "[name]"],
libraryExport: "default",
publicPath: "/dist/plugin"
Expand Down
2 changes: 1 addition & 1 deletion config/webpack/production.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const path = require("path");
const CleanWebpackPlugin = require("clean-webpack-plugin").CleanWebpackPlugin;
const TerserPlugin = require("terser-webpack-plugin");
const terserConfig = require("../terserConfig.cjs");
const banner = require("../banner.cjs");
const banner = require("../template/banner.cjs");
const CssMinimizerPlugin = require("css-minimizer-webpack-plugin");
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
const ESLintPlugin = require("eslint-webpack-plugin");
Expand Down
2 changes: 1 addition & 1 deletion config/webpack/theme.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const fs = require("fs");
const path = require("path");
const CleanWebpackPlugin = require("clean-webpack-plugin").CleanWebpackPlugin;
const WebpackCleanPlugin = require("webpack-clean");
const banner = require("../banner.cjs");
const banner = require("../template/banner.cjs");
const CssMinimizerPlugin = require("css-minimizer-webpack-plugin");
const MiniCssExtractPlugin = require("mini-css-extract-plugin");

Expand Down
9 changes: 5 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@
"require": "./dist/billboard.pkgd.js"
},
"./dist/plugin/*": {
"import": "./dist-esm/plugin/*.js",
"require": "./dist/plugin/pkgd/*.js"
"import": "./dist-esm/plugin/**/*",
"require": "./dist/plugin/pkgd/**/*"
},
"./dist/*": "./dist/*"
},
"scripts": {
"start": "webpack serve --open",
"start:plugin": "cross-env PLUGIN=true webpack-dev-server --open",
"build": "npm run build:production && npm run build:packaged && npm run build:theme && npm run build:plugin && npm run build:esm && npm run build:cjs",
"build": "npm run build:production && npm run build:packaged && npm run build:theme && npm run build:plugin && npm run build:esm && npm run build:cjs && npm run build:plugin:types",
"build:cjs": "node ./config/cjs.js",
"build:esm": "rollup -c ./config/rollup/esm.js",
"build:production": "cross-env NODE_ENV=production webpack",
Expand All @@ -30,7 +30,8 @@
"build:theme": "cross-env NODE_ENV=theme webpack",
"build:production:analyzer": "cross-env ANALYZER=true npm run build:production",
"build:packaged:analyzer": "cross-env ANALYZER=true npm run build:packaged",
"build:plugin": "cross-env NODE_ENV=plugin webpack && cross-env NODE_ENV=plugin MODE=min webpack && cross-env NODE_ENV=plugin MODE=pkgd webpack",
"build:plugin": "cross-env NODE_ENV=plugin webpack && cross-env NODE_ENV=plugin MODE=min webpack && cross-env NODE_ENV=plugin MODE=pkgd webpack",
"build:plugin:types": "node ./config/type.d-plugin.js",
"release": "semantic-release",
"lint": "npm run lint:types && eslint src --ext .ts",
"lint:types": "dtslint --onlyTestTsNext types",
Expand Down
6 changes: 3 additions & 3 deletions types/plugin/tableview/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
* billboard.js project is licensed under the MIT license
*/
import {Plugin} from "../plugin";
import {TabieViewOptions} from "./options";
import {TableViewOptions} from "./options";

export default class TabieView extends Plugin {
export default class TableView extends Plugin {
/**
* Generate stanford diagram
*/
constructor(options?: TabieViewOptions);
constructor(options?: TableViewOptions);
}
2 changes: 1 addition & 1 deletion types/plugin/tableview/options.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export interface TabieViewOptions {
export interface TableViewOptions {
/**
* Set tableview holder selector.
* - **NOTE:** If not set, will append new holder element dynamically.
Expand Down

0 comments on commit f3690f9

Please sign in to comment.