javascript - gulp如何把独立less文件编译成css后嵌入到html页面,变成嵌入式样式表?
黄舟
黄舟 2017-04-10 18:03:16
[JavaScript讨论组]

我用的gulp,下载编译了less的gulp插件,但是less编译最终都是对应的css文件。
我想写个less文件,不想编译到独立css文件里面去,而是想把编译好的css弄到html页面的

<style type="text/css">
</style>

中间,也就是嵌入式样式表,怎么处理呢?
因为这段样式没有引用独立css文件的必要,所以写成

<style type="text/css">
/* 嵌入式css内容 */
</style>

的方式,怎么办?

黄舟
黄舟

人生最曼妙的风景,竟是内心的淡定与从容!

全部回复(1)
PHPz

方案

方案1:通过 gulp-replace 替换 link

(1)首先 html 页面中应该有对应 css 文件的 link 标签
(2)通过 gulp 把 link 标签替换成 style 标签的,并添加对应的 css 内容

方案2:直接使用 gulp-inline-source

原文地址:直接看原文吧

方案示例:gulp-inline-source

github 地址:gulp-inline-source

资源准备

(1)./src/html/index.html。注意链接时后面的属性 inline

<html>
  <head>
    <script src="../js/site.js" inline></script>
    <link rel="stylesheet" href="../css/style.css" inline>
  </head>
  <body>
  </body>
</html>

(2)./src/css/style.css

h1{
    font-size: 24px;
    color: red;
}

(3)./src/js/site.js

function hello(){
    console.log('hello');
}

(4)./gulpfile.js。执行后结果输出到 ./dist 文件夹

var gulp = require('gulp');
var inlinesource = require('gulp-inline-source');

gulp.task('default', function () {
    return gulp.src('./src/html/*.html')
        .pipe(inlinesource())
        .pipe(gulp.dest('./dist'));
});

输出效果

<html>
  <head>
    <script>function hello(){console.log("hello")}</script>
    <style>h1{font-size:24px;color:red}</style>
  </head>
  <body>
  </body>
</html>
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号