Skip to content

Commit

Permalink
feat(antdsite): add globalComponent and you can set global compoennts.
Browse files Browse the repository at this point in the history
  • Loading branch information
wangyi7099 committed Aug 12, 2019
1 parent 9ed0c38 commit 304719f
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 18 deletions.
12 changes: 6 additions & 6 deletions packages/antdsite/lib/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const path = require('path');
const chalk = require('chalk');
const fs = require('fs-extra');
const os = require('os');
const { configName } = require('./constant');
const { configPath, cacheDir } = require('./constant');
const { deepMerge } = require('./util');

let userConfig;
Expand Down Expand Up @@ -59,7 +59,7 @@ const defaultConfig = {
module.exports.defaultConfig = defaultConfig;

const createFinalConfig = config => {
const filePath = path.resolve(__dirname, '../.cache/finalConfig.js');
const filePath = path.resolve(cacheDir, 'finalConfig.js');
fs.ensureFileSync(filePath);
const exportConfig = `module.exports =
${JSON.stringify(config)}
Expand All @@ -70,15 +70,15 @@ const createFinalConfig = config => {
const getUserConfig = () => {
if (userConfig) return userConfig;

let configPath;
let fullConfigPath;

try {
configPath = path.resolve(configName);
userConfig = require(configPath);
fullConfigPath = path.resolve(configPath);
userConfig = require(fullConfigPath);
} catch (error) {
console.error(
chalk.red(
`[AntdSite]: Error when parsing ${configName} at ${configPath}, fallback to default config, the detail error is bellow:`
`[AntdSite]: Error when parsing ${configPath} at ${fullConfigPath}, fallback to default config, the detail error is bellow:`
)
);
console.error(error);
Expand Down
6 changes: 5 additions & 1 deletion packages/antdsite/lib/constant.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
module.exports.configName = '.antdsite/config.js';
const path = require('path');

module.exports.configPath = '.antdsite/config.js';
module.exports.themePath = '.antdsite/theme';
module.exports.globalCompnentPath = '.antdsite/globalComponent';
module.exports.cacheDir = path.resolve(__dirname, '../.cache');
24 changes: 23 additions & 1 deletion packages/antdsite/lib/gatsby/onCreateWebpackConfig.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { themePath } = require('../constant');
const { themePath, globalCompnentPath } = require('../constant');
const { getFinalConfig } = require('../config');
const path = require('path');
const fs = require('fs');
Expand All @@ -8,6 +8,7 @@ const config = getFinalConfig();
module.exports = ({ stage, actions, loaders }) => {
resolveLayouts(actions);
setThemeColors(actions);
setGlobalComponent(actions);

if (stage === 'develop') {
actions.setWebpackConfig({
Expand Down Expand Up @@ -94,3 +95,24 @@ function setThemeColors(actions) {
});
}
}

function getGlobalComponentPath() {
const compPath = path.resolve(process.cwd(), globalCompnentPath);
if (componentExist(compPath)) {
return compPath;
} else {
return path.resolve(__dirname, `../../src/globalComponent`);
}
}

function setGlobalComponent(actions) {
const compPath = getGlobalComponentPath();

actions.setWebpackConfig({
resolve: {
alias: {
'antdsite-g-component': compPath
}
}
});
}
5 changes: 0 additions & 5 deletions packages/antdsite/src/default-theme/layout/index.d.ts

This file was deleted.

1 change: 1 addition & 0 deletions packages/antdsite/src/globalComponent.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default {};
6 changes: 6 additions & 0 deletions packages/antdsite/src/module.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,9 @@ declare module 'antdsite';
declare module '*.jsx';
declare module '@mdx-js/react';
declare module 'nprogress';
declare module 'antdsite-g-component';
declare module 'antdsite-layout';
declare module 'antdsite-header';
declare module 'antdsite-footer';
declare module 'antdsite-main-content';
declare module 'antdsite-home';
7 changes: 2 additions & 5 deletions packages/antdsite/src/templates/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import Layout from 'antdsite-layout';
import MDXRenderer from 'gatsby-mdx-fix/mdx-renderer';
import Media from 'react-media';
import { MDXProvider } from '@mdx-js/react';
import globalComponent from 'antdsite-g-component';

export interface IGraphqlFrontmatterData {
title: string;
Expand Down Expand Up @@ -91,9 +92,8 @@ export default function Template(props: {
);

const { body } = props.data.mdx.code;

return (
<MDXProvider components={{ ...{ PageCustomer: PageContext.Consumer } }}>
<MDXProvider components={{ ...globalComponent, ...{ PageCustomer: PageContext.Consumer } }}>
<PageContext.Provider
value={{
...pageContext,
Expand Down Expand Up @@ -137,9 +137,6 @@ export const pageQuery = graphql`
important
disabled
}
code {
body
}
}
}
}
Expand Down

0 comments on commit 304719f

Please sign in to comment.