Add a version number to the module introduced by Gulp for Requirejs

Source: Internet
Author: User

Because the project uses REQUIREJS, and the project is managed uniformly through gulp, in order to prevent the browser from caching the file, add the version number to the file in the project by Gulp.

 

1, respectively installed Gulp-rev, Gulp-rev-collerctor

NPM Install--save-dev gulp-revnpm install--save-dev gulp-rev-collector

The effect after packaging

"/css/style.css" = "/dist/css/style-1d87bebe.css"    

Expected effect

"/css/style.css" = "/dist/css/style.css?v=1d87bebe" "/js/script1.js" and "/dist/script1.js?v=61e0be79"

  

2. Opennode_modules\gulp-rev\index.js

Line 144th Manifest[originalfile] = revisionedfile;//instead of manifest[originalfile] = Originalfile + '? v= ' + File.revhash;

3. Opennode_modules\rev-path\index.js

Line 10th return filename + '-' + hash + ext;//change to return filename + ext;

4. Opennode_modules\gulp-rev-collector\index.js

Line 31st if (!_.isstring (Json[key]) | | path.basename (Json[key]). Replace (new RegExp (Opts.revsuffix), ')!==  Path.basename (key)) {Isrev = 0;} Change to if (Path.basename (Json[key]). Split ('? ') [0]!== Path.basename (key)) {    Isrev = 0;}

5.gulp generate version number and replace the file introduction path in the Html,css,js file (many online tutorials, not much to say on the connection)

Http://www.tuicool.com/articles/UbaqyyJ

Http://www.cnblogs.com/givebest/p/4707432.html

  

6. Introduction of PATH Considerations

The path in each file must be the same as the root path in the JSON file that modifies the version number (including the require.config.paths path) so that gulp can replace

JSON file {    "app/css/demo.css": "App/css/demo.css?v=768b6e5d87",    "app/js/demo.js": "app/js/demo.js?v= 09521ddbe3 ",    " app/js/a.js ":" App/js/a.js?v=59dd7446a0 "}

  

Require's entry file must write the full path <script src= ". /lib/requires/requires.js "data-main=" js/a "></script>//changed to <script src=". /lib/requires/requires.js "data-main=". /app/js/a.js "></script>//output <script src=". /lib/requires/requires.js "data-main=". /app/js/a.js?v=59dd7446a0 "></script>

Require.config.paths

paths:{    "Demo": ". /app/js/demo "}//changed to paths:{    " demo ":". /app/js/demo.js "}//output config file after paths:{    " demo ":". /app/js/demo.js?v=09521ddbe3 "}

7. Modify the require.js source to solve the introduction path contains search worthy path error problem (version "1.0.8")

Modify the Resume function to add after 1169 rows

  

Modify the Resume function after 1169 lines to add if (Url.indexof ('? v= ')!==-1) {    if (/\.js$/.test (URL)) {        URL = url.slice (0,-3);    }} if (/\.js\.js$/.test (URL)) {    URL = url.slice (0,-3);}

  

Modify the Nametourl function to add var Recordurl = "After ModuleName Assignment (1488 rows); if (Modulename.indexof ('. js? ') >-1) {    Recordurl = MoD Ulename.slice (Modulename.indexof ('? '));    ModuleName = Modulename.slice (0,modulename.indexof ('. js '));} Modify the path address URL = URL + recordurl;if (!config.urlargs) {return URL) after the file path is generated (before Config.urlargs judgment)    ;

  

Add a version number to the module introduced by Gulp for Requirejs

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.