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

使用posthtml实现html和css模块化 #6

Open
zhouwenbin opened this issue Nov 22, 2015 · 3 comments
Open

使用posthtml实现html和css模块化 #6

zhouwenbin opened this issue Nov 22, 2015 · 3 comments

Comments

@zhouwenbin
Copy link
Owner

上两篇文章介绍了postcss,这篇文章介绍下posthtml,这东西还比较新,但是有些东西已经可以用起来了。
在这之前模块化我都是用slim+scss来做的,现在换posthtml,来看看差别在哪里。
我们的目录结构如下

|-component
    |-button
      button.html
      button.css
    |-box
      box.html
      box.css
index.html

component下放了所有组件,把html和css放一起,
button.html代码

<css src="button/button.css"></css>
<a href="http://best-url-ever.com" class="button">button</a>

button.css代码

.button{
    display: block;
    height: 40px;
    line-height: 40px;
    text-align: center;
    width: 200px;
}

box.html代码

<css src="box/box.css"></css>
<div class="box">
    <h1>title</h1>
    <p>Some text</p>
</div>

box.css代码

.box{
    background: #ccc;
    margin: 0 auto;
    width: 50%;
}
.box h1{
    text-transform: uppercase;
}

�index.html代码

<html>
<head>
    <title>index.html</title>
</head>
<body>
    <include src="component/button/button.html"></include>
    <include src="component/box/box.html"></include>
</body>
</html>

代码里面可以看到includecss指令,我们需要装一些插件来转换。
首先安装node,npm在命令行输入

npm install gulp posthtml posthtml-include posthtml-modular-css

在根目录下新建一个posthtml.js文件,内容如下

var posthtml = require('posthtml'),
    html = require('fs').readFileSync('index.html').toString();

posthtml()
    .use(require('posthtml-include')({ encoding: 'utf-8' }))
    .process(html)
    .then(function(result) {
        require('fs').writeFile('output.html', result.html, function (err) {
            if (err) {
                throw err
            }
        })
    })

由于posthtml-include插件还没有gulp版本,所有我们先用node命令来执行,在命令行输入

node posthtml.js

接着会在根目录生成output.html文件,内容如下

<html>
<head>
    <title>index.html</title>
</head>
<body>
    <css src="button/button.css"></css>
<a href="http://best-url-ever.com" class="button">button</a>
    <css src="box/box.css"></css>
<div class="box">
    <h1>title</h1>
    <p>Some text</p>
</div>
</body>
</html>

node已经帮我们把多个html文件合并起来了。
然后在根目录新建一个gulpfile.js文件,内容如下

var gulp = require('gulp');
var posthtml = require('gulp-posthtml');

gulp.task('posthtml', function () {
    var plugins = [
        require('posthtml-modular-css')({
            srcFolder: __dirname + '/component/',
            outputCSS: './dest/style.css'
        })
    ];
    var options = {};

    return gulp.src('output.html')
        .pipe(posthtml(plugins, options))
        .pipe(gulp.dest('./dest'));
});

在命令行输入gulp posthtml,gulp会在dest目录下生成两个文件
output.html代码

<html>
<head>
    <title>index.html</title>
</head>
<body>

<a href="http://best-url-ever.com" class="button">button</a>

<div class="box">
    <h1>title</h1>
    <p>Some text</p>
</div>
</body>
</html>

style.css代码

.button{
    display: block;
    height: 40px;
    line-height: 40px;
    text-align: center;
    width: 200px;
}.box{
    background: #ccc;
    margin: 0 auto;
    width: 50%;
}
.box h1{
    text-transform: uppercase;
}

gulp帮我们把用<css>引入的语句去掉,并打包成一个样式文件。
有了这两个插件,我们开发的时候只要定义好组件的结构和样式,需要用到的时候<include>进来就好了,最终都会打成一个文件,确实方便了好多。

@kujian
Copy link

kujian commented Nov 24, 2015

看不懂这样子写有什么便利,你还不如直接使用SSI

@zhouwenbin
Copy link
Owner Author

@kujian 这个不用服务端支持,定义好组件结构和样式依赖,打包完的样式是最精简的。不用结构和样式都分别include

@KoreSamuel
Copy link

可以同时对多个html打包了。不指定html文件名,指定某个文件夹吗?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants