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

refactor(live-codeblock): migrate package to TS #5851

Merged
merged 2 commits into from
Nov 2, 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
20 changes: 20 additions & 0 deletions packages/docusaurus-theme-live-codeblock/copyUntypedFiles.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

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

/**
* Copy all untyped and static assets files to lib.
*/
const srcDir = path.resolve(__dirname, 'src');
const libDir = path.resolve(__dirname, 'lib');
fs.copySync(srcDir, libDir, {
filter(filepath) {
return !/__tests__/.test(filepath) && !/\.tsx?$/.test(filepath);
},
});
11 changes: 10 additions & 1 deletion packages/docusaurus-theme-live-codeblock/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@
"name": "@docusaurus/theme-live-codeblock",
"version": "2.0.0-beta.8",
"description": "Docusaurus live code block component.",
"main": "src/index.js",
"main": "lib/index.js",
"publishConfig": {
"access": "public"
},
"scripts": {
"build": "tsc && node copyUntypedFiles.js",
"watch": "node copyUntypedFiles.js && tsc --watch"
},
"repository": {
"type": "git",
"url": "https://github.com/facebook/docusaurus.git",
Expand All @@ -17,10 +21,15 @@
"@docusaurus/utils-validation": "2.0.0-beta.8",
"@philpl/buble": "^0.19.7",
"clsx": "^1.1.1",
"fs-extra": "^10.0.0",
"parse-numeric-range": "^1.3.0",
"prism-react-renderer": "^1.2.1",
"react-live": "2.2.3"
},
"devDependencies": {
"@docusaurus/types": "2.0.0-beta.8",
"@types/buble": "^0.20.1"
},
"peerDependencies": {
"react": "^16.8.4 || ^17.0.0",
"react-dom": "^16.8.4 || ^17.0.0"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* LICENSE file in the root directory of this source tree.
*/

const {validateThemeConfig, DEFAULT_CONFIG} = require('../validateThemeConfig');
import {validateThemeConfig, DEFAULT_CONFIG} from '../validateThemeConfig';

function testValidateThemeConfig(themeConfig) {
function validate(schema, cfg) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,23 @@
// fork of Buble which removes Buble's large dependency and weighs in
// at a smaller size of ~51kB
// https://github.com/FormidableLabs/react-live#what-bundle-size-can-i-expect
const {transform, features: bubleFeatures} = require('@philpl/buble');
import {
transform as bubleTransform,
features as bubleFeatures,
TransformOptions,
TransformOutput,
} from '@philpl/buble';

// This file is designed to mimic what's written in
// https://github.com/kitten/buble/blob/mini/src/index.js, with custom transforms options,
// so that webpack can consume it correctly.
exports.features = bubleFeatures;
export {bubleFeatures as features};

exports.transform = function customTransform(source, options) {
return transform(source, {
export function transform(
source: string,
options: TransformOptions,
): TransformOutput {
return bubleTransform(source, {
...options,
transforms: {
asyncAwait: false,
Expand All @@ -25,4 +33,4 @@ exports.transform = function customTransform(source, options) {
...options.transforms,
},
});
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
* LICENSE file in the root directory of this source tree.
*/

const path = require('path');
const {validateThemeConfig} = require('./validateThemeConfig');
import path from 'path';
import {Plugin} from '@docusaurus/types';

function theme() {
export default function theme(): Plugin {
return {
name: 'docusaurus-theme-live-codeblock',

Expand All @@ -28,6 +28,4 @@ function theme() {
};
}

module.exports = theme;

theme.validateThemeConfig = validateThemeConfig;
export {validateThemeConfig} from './validateThemeConfig';
19 changes: 19 additions & 0 deletions packages/docusaurus-theme-live-codeblock/src/types.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

declare module '@philpl/buble' {
import type {TransformOptions as OriginalTransformOptions} from 'buble';
// eslint-disable-next-line import/no-extraneous-dependencies
export * from 'buble';
export const features: string[];
export interface TransformOptions extends OriginalTransformOptions {
transforms?: OriginalTransformOptions['transforms'] & {
asyncAwait?: boolean;
getterSetter?: boolean;
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
* LICENSE file in the root directory of this source tree.
*/

const {Joi} = require('@docusaurus/utils-validation');
import {Joi} from '@docusaurus/utils-validation';
import type {ThemeConfig, Validate, ValidationResult} from '@docusaurus/types';

const DEFAULT_CONFIG = {
playgroundPosition: 'bottom',
};
exports.DEFAULT_CONFIG = DEFAULT_CONFIG;

const Schema = Joi.object({
liveCodeBlock: Joi.object({
Expand All @@ -21,8 +21,15 @@ const Schema = Joi.object({
.label('themeConfig.liveCodeBlock')
.default(DEFAULT_CONFIG),
});
exports.Schema = Schema;

exports.validateThemeConfig = function ({validate, themeConfig}) {
function validateThemeConfig({
validate,
themeConfig,
}: {
validate: Validate<ThemeConfig>;
themeConfig: ThemeConfig;
}): ValidationResult<ThemeConfig> {
return validate(Schema, themeConfig);
};
}

export {DEFAULT_CONFIG, Schema, validateThemeConfig};
9 changes: 9 additions & 0 deletions packages/docusaurus-theme-live-codeblock/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"incremental": true,
"tsBuildInfoFile": "./lib/.tsbuildinfo",
"rootDir": "src",
"outDir": "lib"
}
}
7 changes: 7 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3928,6 +3928,13 @@
resolved "https://registry.yarnpkg.com/@types/braces/-/braces-3.0.1.tgz#5a284d193cfc61abb2e5a50d36ebbc50d942a32b"
integrity sha512-+euflG6ygo4bn0JHtn4pYqcXwRtLvElQ7/nnjDu7iYG56H0+OhCd7d6Ug0IE3WcFpZozBKW2+80FUbv5QGk5AQ==

"@types/buble@^0.20.1":
version "0.20.1"
resolved "https://registry.yarnpkg.com/@types/buble/-/buble-0.20.1.tgz#cba009801fd417b0d2eb8fa6824b537842e05803"
integrity sha512-itmN3lGSTvXg9IImY5j290H+n0B3PpZST6AgEfJJDXfaMx2cdJJZro3/Ay+bZZdIAa25Z5rnoo9rHiPCbANZoQ==
dependencies:
magic-string "^0.25.0"

"@types/cacheable-request@^6.0.1":
version "6.0.2"
resolved "https://registry.yarnpkg.com/@types/cacheable-request/-/cacheable-request-6.0.2.tgz#c324da0197de0a98a2312156536ae262429ff6b9"
Expand Down