Blame view

天文台pc/tianwentai-ui/node_modules/@mui/material/styles/stringifyTheme.js 1.87 KB
bc518174   王天杨   提交两个项目文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
  "use strict";
  
  Object.defineProperty(exports, "__esModule", {
    value: true
  });
  exports.stringifyTheme = stringifyTheme;
  var _deepmerge = require("@mui/utils/deepmerge");
  /* eslint-disable import/prefer-default-export */
  
  function isSerializable(val) {
    return (0, _deepmerge.isPlainObject)(val) || typeof val === 'undefined' || typeof val === 'string' || typeof val === 'boolean' || typeof val === 'number' || Array.isArray(val);
  }
  
  /**
   * `baseTheme` usually comes from `createTheme()` or `extendTheme()`.
   *
   * This function is intended to be used with zero-runtime CSS-in-JS like Pigment CSS
   * For example, in a Next.js project:
   *
   * ```js
   * // next.config.js
   * const { extendTheme } = require('@mui/material/styles');
   *
   * const theme = extendTheme();
   * // `.toRuntimeSource` is Pigment CSS specific to create a theme that is available at runtime.
   * theme.toRuntimeSource = stringifyTheme;
   *
   * module.exports = withPigment({
   *  theme,
   * });
   * ```
   */
  function stringifyTheme(baseTheme = {}) {
    const serializableTheme = {
      ...baseTheme
    };
    function serializeTheme(object) {
      const array = Object.entries(object);
      // eslint-disable-next-line no-plusplus
      for (let index = 0; index < array.length; index++) {
        const [key, value] = array[index];
        if (!isSerializable(value) || key.startsWith('unstable_')) {
          delete object[key];
        } else if ((0, _deepmerge.isPlainObject)(value)) {
          object[key] = {
            ...value
          };
          serializeTheme(object[key]);
        }
      }
    }
    serializeTheme(serializableTheme);
    return `import { unstable_createBreakpoints as createBreakpoints, createTransitions } from '@mui/material/styles';
  
  const theme = ${JSON.stringify(serializableTheme, null, 2)};
  
  theme.breakpoints = createBreakpoints(theme.breakpoints || {});
  theme.transitions = createTransitions(theme.transitions || {});
  
  export default theme;`;
  }