如何为一些react组件调整Docusaurus webpack配置?

7eumitmz  于 2023-08-06  发布在  Webpack
关注(0)|答案(1)|浏览(163)

看起来我的一个组件(https://www.npmjs.com/package/@stoplight/json-schema-viewer)需要在Docusaurus的webpack中进行一些调整
x1c 0d1x的数据
有什么想法吗?我试着跟随https://gist.github.com/sibelius/24f63eef7f43b15dc73c4a0be11bbef8,但它没有工作...
重现步骤:

npx create-docusaurus@latest my-website classic --typescript
cd my-website
npm install @stoplight/json-schema-viewer

字符串
在静态文件夹中创建schema.json,例如:

{
  "$id": "https://example.com/person.schema.json",
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "title": "Person",
  "type": "object",
  "properties": {
    "firstName": {
      "type": "string",
      "description": "The person's first name."
    },
    "lastName": {
      "type": "string",
      "description": "The person's last name."
    },
    "age": {
      "description": "Age in years which must be equal to or greater than zero.",
      "type": "integer",
      "minimum": 0
    }
  }
}


在src/pages中创建文件json_schema.tsx,并使用:

import React, { useState, useEffect } from 'react';
import Layout from '@theme/Layout';
import useBaseUrl from '@docusaurus/useBaseUrl';
import {useColorMode} from '@docusaurus/theme-common';
import { JsonSchemaViewer } from "@stoplight/json-schema-viewer";

// docusaurus.config.js
// https://gist.github.com/sibelius/24f63eef7f43b15dc73c4a0be11bbef8

function fetchSchema(URL : string, callback : (any) => void) {
    fetch(URL, {
        method: "GET",
        headers: {
            'Accept': 'application/json'
        }
    })
    .then(response => response.json())
    .then(json => callback(json))
    .catch(err => callback(undefined));
}

export default function JSONSchema(): JSX.Element {
    const [schema, setSchema] = useState(undefined);
    const {colorMode} = useColorMode();
    const JSON_SCHEMA_URL = useBaseUrl("/schema.json");

    useEffect(() => fetchSchema(JSON_SCHEMA_URL, setSchema), []);

    return (
        <Layout
            title={`JSON Schema Viewer`}
            description={`JSON Schema for xxx`}
        >
            {
                (schema !== undefined) &&
                    <JsonSchemaViewer
                        schema={schema}
                        emptyText="No schema found"
                        defaultExpandedDepth={1}
                    />
            }
            {
                (schema === undefined ) && <div>Loading ...</div>
            }
        </Layout>
    )
}


先谢谢你,

vs3odd8k

vs3odd8k1#

你好,我不知道这是否会帮助你,但从我的形式,我不能添加任何配置到docusaurus配置修改webpack配置,这就是为什么我试图通过创建一个自定义加载器为我的webpack配置添加一些行为以下这些步骤遇到这个问题:
1.在项目根目录下创建一个插件文件夹。
1.在plugins文件夹中创建一个插件(例如custom-loaders),其中包含index.jspackage.json文件:
正如Docusaurus文档中所指出的,下面突出显示的返回对象被合并到最终的webpack配置中。
下面是一个例子:/plugins/custom-loaders/index.js

module.exports = function (context, options) {
  return {
    name: 'custom-loaders',
    configureWebpack(config, isServer) {
      return {
        module: {
          rules: [
            {
              test: /\.m?js/,
              resolve: {
                fullySpecified: false
              }
            },
          ],
        },
      };
    },
  };
};

字符串
/plugins/custom-loaders/package.json

{
  "name": "custom-loaders",
  "version": "0.0.0",
  "private": true
}


1.更新Docusaurus配置文件以使用插件:/docusaurus.config.js

plugins: [
  // ...
  'custom-loaders'
  // ...
]


4.在项目根目录的package.json中指定插件作为依赖项:/package.json

{
  // ...
  "dependencies": {
    // ...
    "custom-loaders": "file:plugins/custom-loaders",
    // ...
  },
    // ...
}


5.为项目安装新的插件依赖项:

npm i


欲了解更多详情,请阅读以下内容:https://dwf.dev/blog/2022/11/12/2022/updating-docusaurus-webpack-config

相关问题