Skip to content

Commit

Permalink
bugfix: new filter fucntion
Browse files Browse the repository at this point in the history
  • Loading branch information
chenshuai2144 committed Feb 20, 2020
1 parent 341a4ae commit abfd4c7
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 33 deletions.
5 changes: 2 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,11 @@ genCss(
min: false,
// css module
isModule: false,
// 忽略 antd 的依赖
// 忽略 antd 的依赖,用于打包 antd 自己的依赖
ignoreAntd: true,
// 忽略 pro-layout
ignoreProLayout: true,
// 不使用缓存
cache: false,
filterFileLess: filename => boolean,
},
);
```
25 changes: 16 additions & 9 deletions genModuleLess.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const AddLocalIdentName = require('./AddLocalIdentName');
const replaceDefaultLess = require('./replaceDefaultLess');

// read less file list
const genModuleLess = (parents, isModule) => {
const genModuleLess = (parents, { isModule, filterFileLess }) => {
let lessArray = [];
const promiseList = [];
lessArray = [];
Expand All @@ -29,14 +29,21 @@ const genModuleLess = (parents, isModule) => {
}
return bSortNumber - aSortNumber;
})
.filter(
filePath =>
!filePath.includes('ant.design.pro.less') &&
!filePath.includes('global.less') &&
!filePath.includes('bezierEasing.less') &&
!filePath.includes('colorPalette.less') &&
!filePath.includes('tinyColor.less'),
)
.filter(filePath => {
if (
filePath.includes('ant.design.pro.less') ||
filePath.includes('global.less') ||
filePath.includes('bezierEasing.less') ||
filePath.includes('colorPalette.less') ||
filePath.includes('tinyColor.less')
) {
return false;
}
if (filterFileLess) {
return filterFileLess(filePath);
}
return true;
})
.forEach(realPath => {
// post css add localIdentNamePlugin
const fileContent = replaceDefaultLess(realPath);
Expand Down
52 changes: 31 additions & 21 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,24 +52,31 @@ const loadAntd = async ignoreAntd => {
return false;
};

const loadAntdProLayout = async ignoreProLayout => {
const loadAntdComponents = async ({ filterFileLess }) => {
const components = ['@ant-design/pro-layout', '@ant-design/pro-table'];
try {
if (!ignoreProLayout) {
const LayoutPath = require.resolve('@ant-design/pro-layout');
if (fs.existsSync(LayoutPath)) {
await loopAllLess(path.resolve(path.join(LayoutPath, '../../es/')), []).then(content => {
fs.writeFileSync(
path.join(tempPath, '/layout.less'),
`@import './antd';
${content}
`,
);
});
return true;
}
if (components) {
const jobs = [];
components.forEach(item => {
if (filterFileLess && !filterFileLess(item)) {
return;
}
const componentPath = require.resolve(item);
if (fs.existsSync(componentPath)) {
jobs.push(loopAllLess(path.resolve(path.join(componentPath, '../../es/')), []));
}
});
const contentList = await Promise.all(jobs);
fs.writeFileSync(
path.join(tempPath, '/components.less'),
`@import './antd';
${contentList.join('\n')}
`,
);
}
// eslint-disable-next-line no-empty
} catch (error) {}
} catch (error) {
// console.log(error);
}

fs.writeFileSync(path.join(tempPath, '/layout.less'), "@import './antd';");
return false;
Expand Down Expand Up @@ -98,8 +105,11 @@ const getOldFile = filePath => {

let isEqual = false;

const genProjectLess = (filePath, { isModule, loadAny, cache, ignoreAntd, ignoreProLayout }) =>
genModuleLess(filePath, isModule).then(async content => {
const genProjectLess = (
filePath,
{ isModule, loadAny, cache, ignoreAntd, ignoreProLayout, ...rest },
) =>
genModuleLess(filePath, { isModule, ...rest }).then(async content => {
if (cache === false) {
rimraf.sync(tempPath);
}
Expand All @@ -125,7 +135,7 @@ const genProjectLess = (filePath, { isModule, loadAny, cache, ignoreAntd, ignore
if (loadAny) {
fs.writeFileSync(
winPath(path.join(tempPath, 'pro.less')),
`@import './layout';
`@import './components';
${content}`,
);
} else {
Expand All @@ -137,7 +147,7 @@ const genProjectLess = (filePath, { isModule, loadAny, cache, ignoreAntd, ignore

fs.writeFileSync(
winPath(path.join(tempPath, 'pro.less')),
`@import './layout';
`@import './components';
${lessContent}`,
);
}
Expand All @@ -146,7 +156,7 @@ const genProjectLess = (filePath, { isModule, loadAny, cache, ignoreAntd, ignore
}

await loadAntd(ignoreAntd);
await loadAntdProLayout(ignoreProLayout);
await loadAntdComponents(rest);
return true;
});

Expand Down

0 comments on commit abfd4c7

Please sign in to comment.