UNPKG

@umijs/plugins

Version:
151 lines (148 loc) 5.47 kB
var __create = Object.create; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __getProtoOf = Object.getPrototypeOf; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps( // If the importer is in node compatibility mode or this is not an ESM // file that has been converted to a CommonJS file using a Babel- // compatible transform (i.e. "__esModule" has not been set), then set // "default" to the CommonJS "module.exports" for node compatibility. isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod )); var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // src/styled-components.ts var styled_components_exports = {}; __export(styled_components_exports, { default: () => styled_components_default }); module.exports = __toCommonJS(styled_components_exports); var import_utils = require("@umijs/utils"); var import_path = require("path"); var import_resolveProjectDep = require("./utils/resolveProjectDep"); var import_withTmpPath = require("./utils/withTmpPath"); var styled_components_default = (api) => { api.describe({ key: "styledComponents", config: { schema({ zod }) { return zod.object({ babelPlugin: zod.record(zod.any()).optional() }); } }, enableBy: api.EnableBy.config }); api.modifyConfig((memo) => { var _a, _b; if (api.userConfig.mako || process.env.OKAM || api.userConfig.utoopack) { return memo; } const isProd = api.env === "production"; const pluginConfig = { // https://github.com/styled-components/babel-plugin-styled-components/blob/f8e9fb480d1645be8be797d73e49686bdf98975b/src/utils/options.js#L11 topLevelImportPaths: [ "@umijs/max", "@alipay/bigfish", "umi", "alita", "@kmi/kmi" ], ...isProd ? { displayName: false } : {}, ...((_a = api.config.styledComponents) == null ? void 0 : _a.babelPlugin) || {}, ...((_b = api.userConfig.styledComponents) == null ? void 0 : _b.babelPlugin) || {} }; memo.extraBabelPlugins = [ ...memo.extraBabelPlugins || [], [require.resolve("babel-plugin-styled-components"), pluginConfig] ]; return memo; }); api.addRuntimePlugin(() => { return [(0, import_withTmpPath.withTmpPath)({ api, path: "runtime.tsx" })]; }); api.addRuntimePluginKey(() => { return ["styledComponents"]; }); const SC_NAME = `styled-components`; let libPath; try { libPath = (0, import_resolveProjectDep.resolveProjectDep)({ pkg: api.pkg, cwd: api.cwd, dep: SC_NAME }) || (0, import_path.dirname)(require.resolve(`${SC_NAME}/package.json`)); } catch (e) { } api.modifyConfig((memo) => { memo.alias[SC_NAME] = libPath; return memo; }); api.onGenerateFiles(() => { var _a, _b, _c; api.writeTmpFile({ path: "index.tsx", content: ` import styled, { ThemeProvider, createGlobalStyle, css, keyframes, StyleSheetManager, useTheme } from '${(0, import_utils.winPath)( libPath )}'; export { styled, ThemeProvider, createGlobalStyle, css, keyframes, StyleSheetManager, useTheme }; ` }); const styledComponentsRuntimeCode = ((_a = api.appData.appJS) == null ? void 0 : _a.exports.includes( "styledComponents" )) ? `import { styledComponents as styledComponentsConfig } from '@/app';` : `const styledComponentsConfig = {};`; const isLegacy = !import_utils.lodash.isEmpty((_b = api.config.targets) == null ? void 0 : _b.ie) || api.config.legacy; const disableCSSOM = !!((_c = api.config.qiankun) == null ? void 0 : _c.slave); const providerOptions = { // https://styled-components.com/docs/faqs#vendor-prefixes-are-omitted-by-default ...isLegacy ? { enableVendorPrefixes: true } : {}, ...disableCSSOM ? { disableCSSOMInjection: true } : {} }; const hasProvider = !import_utils.lodash.isEmpty(providerOptions); api.writeTmpFile({ path: "runtime.tsx", content: ` import React from 'react'; ${hasProvider ? `import { StyleSheetManager } from '${(0, import_utils.winPath)(libPath)}';` : ``} ${styledComponentsRuntimeCode} export function rootContainer(container) { const scConfig = typeof styledComponentsConfig === 'function' ? styledComponentsConfig() : styledComponentsConfig; const globalStyle = scConfig.GlobalStyle ? <scConfig.GlobalStyle /> : null; const inner = ( <> {globalStyle} {container} </> ); ${hasProvider ? ` return ( <StyleSheetManager {...${JSON.stringify(providerOptions)}}> {inner} </StyleSheetManager> ); ` : "return inner;"} } ` }); }); };