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

build: added new debugging command dev to support demo debugging #239

Merged
merged 6 commits into from
Feb 29, 2024
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
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@
"lint-staged": "lint-staged",
"site:home": "cross-env NODE_ENV=production webpack --progress --config scripts/sites/webpack.prod.home.js",
"site:mobile": "cross-env NODE_ENV=production webpack --progress --config scripts/sites/webpack.prod.mobile.js",
"site:pc": "cross-env NODE_ENV=production webpack --progress --config scripts/sites/webpack.prod.pc.js"
"site:pc": "cross-env NODE_ENV=production webpack --progress --config scripts/sites/webpack.prod.pc.js",
"dev:demo": "node scripts/dev/dev-demo.js $@"
},
"author": "[email protected]",
"license": "MIT",
Expand Down
9 changes: 9 additions & 0 deletions scripts/dev/dev-demo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const childProcess = require('child_process');
const { argv } = process;

// 单组件 - 单 demo 测试命令:npm run dev:demo slider mark
// 多组件 - 多 demo 测试命令:npm run dev:demo slider,tabs mark,basic
childProcess.execSync(
`cross-env NODE_ENV=development COMPONENTS=${argv[2]} DEMOS=${argv[3]} webpack-dev-server --progress --config scripts/sites/webpack.dev.mobile.js`,
{ stdio: 'inherit' },
);
36 changes: 30 additions & 6 deletions scripts/sites/plugins/DemoGeneratePlugin/generate-demo.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,23 @@ const sitePath = path.join(rootPath, siteFolder);
const compositeCompPath = path.join(rootPath, compositeCompFolder);
const compositePath = path.join(rootPath, compositeFolder);

function renderSource({ comp, demoName, depsCompSet, language, compileEnv, demoPath, sitePath }) {
function renderSource({
comp,
demoName,
depsCompSet,
language,
compileEnv,
demoPath,
sitePath,
devComponents = [],
devDemos = [],
}) {
if (!/^\w+.*\w+$/g.test(demoName)) return;
const docPath = path.join(sitePath, comp);
// 是当前组件且不是当前开发 demo 不用设置渲染内容
const notDevDemo = devComponents.includes(comp) && !devDemos.includes(demoName);
const demo = fs.readFileSync(path.join(demoPath, `${demoName}.md`), 'utf8');
const newDemo = notDevDemo ? demo.replace(/export default ([\s\S]*?)\(\) \{([\s\S]*?)\}\n```/g, 'export default $1() { return null }\n```') : demo;
const renderer = new marked.Renderer();
const compNpmReg = new RegExp(compPackageName, 'g');
const utilsNpmReg = new RegExp(utilsPackageName, 'g');
Expand All @@ -40,7 +53,6 @@ function renderSource({ comp, demoName, depsCompSet, language, compileEnv, demoP
.replace(compNpmReg, `../../../../../${compFolder}`)
.replace(utilsNpmReg, `../../../../../${utilsFolder}`)
.replace(/\/esm\//g, '/')}`);

fs.mkdirpSync(docPath);
const demoFileName = compileEnv === 'vite' ? `${filename}.jsx` : `${filename}.js`;
const demoFilePath = path.join(docPath, demoFileName);
Expand All @@ -64,6 +76,9 @@ function renderSource({ comp, demoName, depsCompSet, language, compileEnv, demoP
};

renderer.heading = (text, level) => {
if (notDevDemo) {
return '';
}
if (level === 2) {
return `
<div className="arcodesign-mobile-title">${utils.getReadMeTextByLang(text, language)}</div>`;
Expand All @@ -75,10 +90,13 @@ function renderSource({ comp, demoName, depsCompSet, language, compileEnv, demoP
};

renderer.paragraph = text => {
if (notDevDemo) {
return '';
}
return `<p>${utils.getReadMeTextByLang(text, language)}</p>`;
};

const result = marked(demo, { renderer });
const result = marked(newDemo, { renderer });

return {
order,
Expand Down Expand Up @@ -168,7 +186,9 @@ function generateSiteDemo({
compileComps = [],
language = 'ch',
depsCompSet,
compileEnv
compileEnv,
devComponents,
devDemos,
} = {}) {
const readmeRoutes = {};
let compNames;
Expand Down Expand Up @@ -224,7 +244,9 @@ function generateSiteDemo({
language,
compileEnv,
demoPath,
sitePath
sitePath,
devComponents,
devDemos,
});
importStr += `import ${importDemoName} from './_${importDemoName}';`;
demoSource.push({
Expand Down Expand Up @@ -500,6 +522,8 @@ function generateDemo({
languages = ['ch', 'en'],
compileComps = [],
compileEnv = 'webpack',
devComponents,
devDemos,
} = {}) {
const depsCompSet = new Set();
if (compileComps.length) {
Expand All @@ -516,7 +540,7 @@ function generateDemo({
console.log(`>>> Start generate demo entry files...`);
languages.forEach(language => {
generateSiteCompositeDemo({ depsCompSet, language, compileEnv });
generateSiteDemo({ depsCompSet, language, compileComps, compileEnv });
generateSiteDemo({ depsCompSet, language, compileComps, compileEnv, devComponents, devDemos });
});
if (compileComps.length) {
generateRootDemo(depsCompSet);
Expand Down
7 changes: 6 additions & 1 deletion scripts/sites/webpack.dev.mobile.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ const compileComps = (process.env.FILTER_COMP || '').split(' ')
.filter(e => e)
.map(utils.getFolderName);

const devComponents = (process.env.COMPONENTS || '').split(',');
const devDemos = (process.env.DEMOS || '').split(',');

const devConfig = merge(baseConfig, {
mode: 'development',
entry: {
Expand Down Expand Up @@ -64,7 +67,9 @@ const devConfig = merge(baseConfig, {
plugins: [
new VConsolePlugin({ enable: true }),
new DemoGeneratePlugin({
compileComps
compileComps,
devComponents,
devDemos,
}),
new TokenGeneratePlugin(),
new HtmlWebpackPlugin({
Expand Down
Loading