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

feat(stylus): 添加stylus支持 #3483

Merged
merged 2 commits into from
Oct 31, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
11 changes: 11 additions & 0 deletions packages/af-webpack/src/getUserConfig/configs/stylus.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import assert from 'assert';
import { isPlainObject } from 'lodash';

export default function() {
return {
name: 'stylus',
validate(val) {
assert(isPlainObject(val), `The stylus config must be Plain Object, but got ${val}`);
},
};
}
62 changes: 60 additions & 2 deletions packages/af-webpack/src/getWebpackConfig/css.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ const DEFAULT_BROWSERS = [
'not ie < 9', // React doesn't support IE8 anyway
];

const DEFAULT_STYLUS_LOADER = 'stylus-loader';
const DEFAULT_STYLUS_OPTIONS = { preferPathResolver: 'webpack' };
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里没必要常量

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ycjcl868 我一般写工具都比较灵活,这里主要是配合配合解构,现在的代码合并不了,是这个原因吗?这个工程运行npm run test报错某个文件找不到,目前先build模块代码,把build后的几个文档复制到本地demo中测试正常。

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ycjcl868 请问本地如何跑单元测试,如何指定build哪几个模块,能不能在工程中加一个readme?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sorrycc 好滴,没问题就尽快合并吧!


export default function(webpackConfig, opts) {
const isDev = process.env.NODE_ENV === 'development';
const cssOpts = {
Expand Down Expand Up @@ -68,7 +71,7 @@ export default function(webpackConfig, opts) {
hasSassLoader = false;
}

function applyCSSRules(rule, { cssModules, less, sass }) {
function applyCSSRules(rule, { cssModules, less, sass, stylus }) {
if (!opts.ssr) {
if (opts.styleLoader) {
rule
Expand Down Expand Up @@ -130,6 +133,35 @@ export default function(webpackConfig, opts) {
.loader(require.resolve('sass-loader'))
.options(opts.sass);
}

// 开始添加 stylus 支持
if (stylus) {
const {
loader: stylusLoader = DEFAULT_STYLUS_LOADER,
options: stylusOptions = DEFAULT_STYLUS_OPTIONS,
poststylus,
} = opts.stylus || {};

// 避免项目启动时出现异常 Error: Cannot find module 'stylus-loader'
let stylusLoaderPath;
try {
stylusLoaderPath = require.resolve(stylusLoader);
// eslint-disable-next-line no-empty
} catch (e) {}

// 如果安装了 stylus-loader 模块
if (stylusLoaderPath) {
rule
.use('stylus-loader')
.loader(stylusLoaderPath)
.options(stylusOptions);

// 如果使用了 poststylus, 需要移除 postcss-loader
if (poststylus) {
rule.delete('postcss-loader');
}
}
}
}

if (opts.cssModulesExcludes) {
Expand All @@ -146,6 +178,7 @@ export default function(webpackConfig, opts) {
applyCSSRules(config, {
less: ext === '.less',
sass: ext === '.sass' || ext === '.scss',
stylus: ext === '.styl' || ext === '.stylus',
});
});
}
Expand All @@ -162,14 +195,18 @@ export default function(webpackConfig, opts) {
cssModules: true,
sass: true,
});
applyCSSRules(webpackConfig.module.rule('.module.stylus').test(/\.module\.styl(us)?$/), {
cssModules: true,
stylus: true,
});
}

function cssExclude(filePath) {
if (/node_modules/.test(filePath)) {
return true;
}
if (opts.cssModulesWithAffix) {
if (/\.module\.(css|less|sass|scss)$/.test(filePath)) return true;
if (/\.module\.(css|less|sass|scss|styl(us)?)$/.test(filePath)) return true;
}
if (opts.cssModulesExcludes) {
for (const exclude of opts.cssModulesExcludes) {
Expand Down Expand Up @@ -239,6 +276,27 @@ export default function(webpackConfig, opts) {
sass: true,
},
);
applyCSSRules(
webpackConfig.module
.rule('stylus')
.test(/\.styl(us)?$/)
.exclude.add(cssExclude)
.end(),
{
cssModules: !opts.disableCSSModules,
stylus: true,
},
);
applyCSSRules(
webpackConfig.module
.rule('stylus-in-node_modules')
.test(/\.styl(us)?$/)
.include.add(/node_modules/)
.end(),
{
stylus: true,
},
);

const hash = !isDev && opts.hash ? '.[contenthash:8]' : '';
if (!opts.ssr && !opts.styleLoader) {
Expand Down
4 changes: 2 additions & 2 deletions packages/af-webpack/src/getWebpackConfig/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { EOL } from 'os';
import assert from 'assert';
import { getPkgPath, shouldTransform } from './es5ImcompatibleVersions';
import resolveDefine from './resolveDefine';
import send, { STARTING } from '../send';
// import send, { STARTING } from '../send';

function makeArray(item) {
if (Array.isArray(item)) return item;
Expand Down Expand Up @@ -96,7 +96,7 @@ export default function(opts) {
.exclude.add(/\.json$/)
.add(/\.(js|jsx|ts|tsx|mjs|wasm)$/)
.add(/\.(graphql|gql)$/)
.add(/\.(css|less|scss|sass)$/);
.add(/\.(css|less|scss|sass|styl(us)?)$/);
if (opts.urlLoaderExcludes) {
opts.urlLoaderExcludes.forEach(exclude => {
rule.add(exclude);
Expand Down
2 changes: 2 additions & 0 deletions packages/umi-build-dev/src/plugins/global-css.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import { existsSync } from 'fs';
export default function(api) {
const { paths, winPath } = api;
const cssFiles = [
join(paths.absSrcPath, 'global.styl'),
join(paths.absSrcPath, 'global.stylus'),
join(paths.absSrcPath, 'global.sass'),
join(paths.absSrcPath, 'global.scss'),
join(paths.absSrcPath, 'global.less'),
Expand Down