简体   繁体   English

使用模块捆绑器引用html中的构建文件

[英]Referring to built files in html using module bundlers

I'm using the Gulp to build my SCSS, Pug and ES6 assets for my static website. 我正在使用Gulp为我的静态网站构建我的SCSS,Pug和ES6资产。 I know it's possible to hash file names and output the files in a different directory. 我知道可以散列文件名并将文件输出到不同的目录中。

For my specific example: 对于我的具体例子:

  • my Pug markdown are found in the ~/src/pages directory and getting built to the ~/public/ directory. 我的Pug markdown位于~/src/pages目录中,并构建到~/public/目录。
  • My SCSS stylesheets are found in the ~/src/stylesheets directory. 我的SCSS样式表位于~/src/stylesheets目录中。 These are getting built to the and getting ~/public/style directory 这些都是为了获得~/public/style目录而构建的

My problem is, when I'm referring to my stylesheets files from Pug, I have to refer to the already-built folder like this: 我的问题是,当我从Pug引用我的样式表文件时,我必须像这样引用已经构建的文件夹:

link(rel='stylesheet', href='./style/example.css')

For my IDE, this doesn't make sense, because the style directory doesn't exist in the ~/src/pages directory. 对于我的IDE,这没有意义,因为~/src/pages目录中不存在style目录。

What I would find the most useful is that I can refer to my stylesheets like the example below: 我觉得最有用的是我可以参考我的样式表,如下例所示:

link(rel='stylesheet', href='../stylesheets/example.scss')

Is there any way this is possible or am I completely going in the wrong direction? 有什么方法可以做到这一点,还是我完全朝错误的方向走? If not, where am I looking for? 如果没有,我在哪里寻找?

You can use something like gulp-watch for real-time compiling of your .scss files, then your /style/example.css file will exist and it will be recompiled automatically when you modify example.scss 您可以使用gulp-watch之类的东西来实时编译/style/example.css文件,然后您的/style/example.css文件将存在,并且当您修改example.scss时它将自动重新编译

You may need to move some directories around to get everything to link, but you can use watch to build your Pug files too, so your site will always be up to date. 您可能需要移动一些目录以获取要链接的所有内容,但您也可以使用watch来构建您的Pug文件,因此您的站点将始终是最新的。

Basically, you make a change on any file in your project and view the update live. 基本上,您对项目中的任何文件进行更改并实时查看更新。

Solution to make the file name like hash 使文件名像hash一样的解决方案

  • gulp, for automating our task gulp,用于自动化我们的任务
  • gulp-rev , for renaming our files with random hashes. gulp-rev ,用于随机哈希重命名我们的文件。
  • gulp-rev-collector , for switching non-hashed references by hashed-references inside our files. gulp-rev-collector ,用于在我们的文件中通过散列引用来切换非散列引用。
  • rev-del , for deleting non-hashed files in our /dist folder. rev-del ,用于删除/ dist文件夹中的非散列文件。

Sample code : 示例代码:

gulpfile.js gulpfile.js

gulp.task("revision:rename", ["serve"], () =>
  gulp.src(["dist/**/*.html",
            "dist/**/*.css",
            "dist/**/*.js",
            "dist/**/*.{jpg,png,jpeg,gif,svg}"])
  .pipe(rev())
  .pipe(revdel())
  .pipe(gulp.dest("dist"))
  .pipe(rev.manifest({ path: "manifest.json" }))
  .pipe(gulp.dest("dist"))
);

manifest.json 的manifest.json

{
  style.css: style-ds9udjvci.css,
  main.js: main-dijds9xc9.min.js
}

For creating our revision update in the file like Rewrite every reference for every key of manifest.json to it's respective value inside every html/json/css/js file (ie: <link href="style.css"> would become <link href="style-ds9udjvci.css">) 为了在文件中创建我们的修订更新,比如将每个明确的manifest.json的每个引用重写为每个html / json / css / js文件中的相应值(ie: <link href="style.css"> would become <link href="style-ds9udjvci.css">)

gulp.task("revision:updateReferences", ["serve", "revision:rename"], () =>
  gulp.src(["dist/manifest.json","dist/**/*.{html,json,css,js}"])
 .pipe(collect())
 .pipe(gulp.dest("dist"))
);

Gulp cannot automatically change the file paths used inside the htmls. Gulp无法自动更改htmls内使用的文件路径。 Therefore you will have to use the generated file path for accessing the style files. 因此,您必须使用生成的文件路径来访问样式文件。

Although if you want to have the file path as the folder structure of your scss, then you will have to replace the contents of the pug file after gulp has finished converting it to HTML. 虽然如果您希望将文件路径作为scss的文件夹结构,那么在gulp将其转换为HTML后,您将不得不替换pug文件的内容。

You can convert the html to String and use the .replace method to replace whatever content you want to change and finally parse the string to a HTML document. 您可以将html转换为String并使用.replace方法替换您想要更改的任何内容,最后将字符串解析为HTML文档。

Hope this helps!! 希望这可以帮助!!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM