Gulp -从HTML文件中删除源Map注解字符串

5m1hhzi4  于 5个月前  发布在  Gulp
关注(0)|答案(1)|浏览(84)

我已经生成了一个css文件的副本,将副本的扩展名从.css更改为.html,并添加了<style>元素来 Package css规则。以下是结果.
下面是复制的文件,它现在是一个名为page-css.html的HTML文件

<style>
html body {
  border: solid 1rem blue;
}
html body .form-row {
  margin-bottom: 15px;
}
/*# sourceMappingURL=data:application/json;charset=utf8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbInBhZ2UtY3NzLnNjc3MiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IkFBQ0k7RUFDSTs7QUFFQTtFQUNJIiwiZmlsZSI6InBhZ2UtY3NzLmNzcyIsInNvdXJjZXNDb250ZW50IjpbImh0bWwge1xyXG4gICAgYm9keSB7XHJcbiAgICAgICAgYm9yZGVyOiBzb2xpZCAxcmVtIGJsdWU7XHJcblxyXG4gICAgICAgIC5mb3JtLXJvdyB7XHJcbiAgICAgICAgICAgIG1hcmdpbi1ib3R0b206IDE1cHg7XHJcbiAgICAgICAgfVxyXG4gICAgfVxyXG59Il19 */
</style>

字符串
如何使用Gulp从该文件中删除整个sourcemap字符串,使其消失?
我试过...

  • Gulp 分解
  • 换串
  • gulp-strip-comments

都是以相似的方式

const htmlmin = require('gulp-htmlmin');
const decomment = require('gulp-decomment');
const replace = require('gulp-string-replace');
const strip = require('gulp-strip-comments');

...

  //example 1
  .pipe(decomment({trim: true}))
  //example 2
  .pipe(htmlmin({ removeComments: true }))
  //example 3
  .pipe(strip())


.但是这些包都不能替换/删除整个sourcemap行.
我想使用gulp-string-replace,但我找不到任何学习点,如何删除特殊字符和它之间的任何东西(例如/*#, *, */, ' ')。
下面是我尝试通过gulp来完成这个过程。但是即使使用gulp-replace(如下面的greplace),它也不会删除sourcemap行。

全Gulp代码示例

function genCSS(done) {
  return (
    src(paths.styles.src)
      .pipe(gulpif(!GPREP, sourcemaps.init({})))
      .pipe(sass().on("error", sass.logError))
      .pipe(
        gulpif(
          !GPREP, sourcemaps.write({
            addComment: true,
          })
        )
      )
      //CSS FILE WITH ITS SOURCEMAP IS READY FOR LOCAL DEV PURPOSES
      .pipe(dest(paths.styles.dest))

      //BEGIN THE COPIED/CONVERTED HTML FILE FOR GOOGLE APP PURPOSES
      .pipe(
        rename(function (path) {
          if (path.basename === "page-css") {
            path.extname = ".html";
          }
        })
      )
      .pipe(header("<style>\n"))
      .pipe(footer("</style>\n"))
      //ATTEMPTING TO REMOVE THE SOURCEMAP - BUT IT DOES NOT WORK...
      .pipe(greplace(/\/\*# sourceMappingURL[^\n]+\n/gm,''))
      //STORE THE FILE IN A CLASP LOCATION TO PUSH UP TO GOOGLE
      .pipe(dest(paths.styles.apps))
      .pipe(msync.stream())
  );
  done();
}


非常感谢这个学习过程!

wyyhbhjk

wyyhbhjk1#

如果您确信格式是一致的,则可以使用gulp-replace和RegEx。

const replace = require('gulp-replace')

...

.pipe(replace(/\/\*# sourceMappingURL[^\n]+\n/gm,''))

...

字符串

相关问题