Home>

There is a file:

<
! DOCTYPE HTML >
≪
HTML LANG= "EN" >
Sometext.
≪
HEAD >
    ≪
Meta Charset= "UTF-8" >
    ≪
Meta Name= "ViewPort" CONTENT= "WIDTH= device-width, initial-scale= 1" >
    ≪
Link REL= "StyleSheet" Type= "text /css" href= "css /style.min.css" >
    ≪
Title >
Document <
/title >
≪
/Head >
≪
Body class= "wrapper" >
    ≪
Main class= "Page" >
    ≪
/Main >
    ≪
/div >
≪
/Body >
≪
Script Type= "text /javascript" src= "js /script.min.js" >
≪
/script >
≪
/HTML >

in it with nodejs. and Gulp. I want to replace strings . *? ≪ script. * and . *? ≪ Link. * On the connection of styles and WordPress scripts. I do with FS.WriteStream since it allows you to record in the file line. Here is the code:

gulp.task (`WP`, ()= >
 {
    Let FilePathes= [];
    Return SRC (project.src.html) //opened the file
        .pipe (Rename ({ExtName: `.php`}))
        .pipe (Dest (Project.Build.php))
        .pipe (TRANSFOB ((File, Enc, Next)= >
 {
            FS.ReadFile (file.path, 'UTF8', (ERR, DOC)= >
 {
                if (ERR) Return Console.log (ERR);
                filepathes= file.contents.tostring (). Match (/([`" ']) \ s +? \. (CSS | JS) \ 1 /gi); //Found paths to scripts and styles
                Console.log (FilePathes);
                Let ReplecesTrings= file.contents.tostring (). Match (/.* *<
(link | script). * /gi);
                Let Filestream= FS.CreateWriteStream (file.path, {Flags: `A`, Encoding:" UTF-8 "});
                For (Let i in replecesTrings) {
                    Let Result= '';
                    if (filepathes [i] .match (/\. js /)) result= doc.replace (replecesTrings [i],
 `/twp_enque_script ($ {filepathes [i]}); \ r \ n`); //Forming strings with scripts
                    ELSE If (FilePathes [i] .match (/\. CSS /)) Result= Doc.replace (ReplecesTrings [i],
`\ twp_enque_style ($ {filepathes [i]}); \ r \ n`); //with styles
                    FILESTREAM.ONCE ("Open", (FD)= >
 {FILESTREAM.WRITE (RESULT);}) //Write a string to a file
                }
            });
            NEXT (NULL, FILE);
        }))
}
);

The problem is that at the output I get such a file:

<
! DOCTYPE HTML >
≪
HTML LANG= "EN" >
Sometext.
≪
HEAD >
    ≪
Meta Charset= "UTF-8" >
    ≪
Meta Name= "ViewPort" CONTENT= "WIDTH= device-width, initial-scale= 1" >
    ≪
Link REL= "StyleSheet" Type= "text /css" href= "css /style.min.css" >
    ≪
Title >
Document <
/title >
≪
/Head >
≪
Body class= "wrapper" >
    ≪
Main class= "Page" >
    ≪
/Main >
    ≪
/div >
≪
/Body >
≪
Script Type= "text /javascript" src= "js /script.min.js" >
≪
/script >
≪
/HTML >
≪
! DOCTYPE HTML >
≪
HTML LANG= "EN" >
Sometext.
≪
HEAD >
    ≪
Meta Charset= "UTF-8" >
    ≪
Meta Name= "ViewPort" CONTENT= "WIDTH= device-width, initial-scale= 1" >
    WP_ENQUE_STYLE ("CSS /STYLE.MIN.CSS");
    ≪
Title >
Document <
/title >
≪
/Head >
≪
Body class= "wrapper" >
    ≪
Main class= "Page" >
    ≪
/Main >
    ≪
/div >
≪
/Body >
≪
Script Type= "text /javascript" src= "js /script.min.js" >
≪
/script >
≪
/HTML >
≪
! DOCTYPE HTML >
≪
HTML LANG= "EN" >
Sometext.
≪
HEAD >
    ≪
Meta Charset= "UTF-8" >
    ≪
Meta Name= "ViewPort" CONTENT= "WIDTH= device-width, initial-scale= 1" >
    ≪
Link REL= "StyleSheet" Type= "text /css" href= "css /style.min.css" >
    ≪
Title >
Document <
/title >
≪
/Head >
≪
Body class= "wrapper" >
    ≪
Main class= "Page" >
    ≪
/Main >≪ /div >
≪
/Body >
    WP_ENQUE_Script ("js /script.min.js");
≪
/HTML >

Explain why all its contents are duplicated three times? How nevertheless write to the file line?

  • Answer # 1

    Inline Plugins Are One-Off TRANSFORM STREAMS You DEFINE INSIDE YUR Gulpfile by Writing The Desired Behavior.

    const pat= Require ('Path');
    const {SRC, DEST}= REQUIRE ('GULP');
    Const Rename= Require ('Gulp-Rename');
    Const Through2= Require ('Through2');
    Const Project= {
      SRC: {
        HTML: `$ {__ dirname} /*. HTML`
      }
      Build: {
        php: path.join (__ dirname, 'php'),
      }
    };
    src (project.src.html)
      .pipe (Rename ({ExtName: `.php`}))
      .pipe (File, Enc, CB) {
        if (file.isbuffer ()) {
          Let Raw= file.contents.tostring (ENC);
          Raw= raw.replace (/(? &lt;
    = \ &Lt;
    Link. +? href= ["']) (. +) (?= ['"]. *? &gt;
    ) /g, "WP_ENQUE_STYLE ('$ 1") ");
          Raw= raw.replace (/(? &lt;
    = \ &Lt;
    Script. +? src= ["]) (. +) (?= ['"]. *? &gt;
    ) /g, "WP_ENQUE_Script ('$ 1") ");
          file.contents= buffer.from (raw);
        }
        CB (NULL, FILE);
      }))
      .pipe (Dest (Project.Build.php));
    

    Result:

    &lt;
    ! DOCTYPE HTML &GT;
    &Lt;
    HTML LANG= "EN" &GT;
    &Lt;
    HEAD &GT;
        &Lt;
    Meta Charset= "UTF-8" &gt;
        &Lt;
    Meta Name= "ViewPort" CONTENT= "WIDTH= device-width, initial-scale= 1" &gt;
        &Lt;
    Link REL= "StyleSheet" Type= "text /css" href= "wp_enque_style ('css /style.min.css')" &gt;
        &Lt;
    Title &gt;
    Document &LT;
    /title &gt;
    &Lt;
    /Head &gt;
    &Lt;
    Body class= "wrapper" &gt;
        &Lt;
    Main class= "Page" &gt;
        &Lt;
    /Main &gt;
        &Lt;
    /div &gt;
    &Lt;
    /Body &GT;
    &Lt;
    Script Type= "text /javascript" src= "WP_ENQUE_Script ('js /script.min.js')" &gt;
    &Lt;
    /script &gt;
    &Lt;
    /HTML &GT;
    

    Perhaps I did not fully understand the Through2 module, sorry. I can explain this work with regular regularities: I needed to replace Konstantin_SH2021-09-13 17:23:45

    I am not about Through2, not about him at all. To justify your reproach, I made a small code-root code for a small piece of code from the question.

    nörbörnën2021-09-13 20:28:04
  • Answer # 2

    Inline Plugins Are One-Off TRANSFORM STREAMS You DEFINE INSIDE YUR Gulpfile by Writing The Desired Behavior.

    const pat= Require ('Path');
    const {SRC, DEST}= REQUIRE ('GULP');
    Const Rename= Require ('Gulp-Rename');
    Const Through2= Require ('Through2');
    Const Project= {
      SRC: {
        HTML: `$ {__ dirname} /*. HTML`
      }
      Build: {
        php: path.join (__ dirname, 'php'),
      }
    };
    src (project.src.html)
      .pipe (Rename ({ExtName: `.php`}))
      .pipe (File, Enc, CB) {
        if (file.isbuffer ()) {
          Let Raw= file.contents.tostring (ENC);
          Raw= raw.replace (/(? &lt;
    = \ &Lt;
    Link. +? href= ["']) (. +) (?= ['"]. *? &gt;
    ) /g, "WP_ENQUE_STYLE ('$ 1") ");
          Raw= raw.replace (/(? &lt;
    = \ &Lt;
    Script. +? src= ["]) (. +) (?= ['"]. *? &gt;
    ) /g, "WP_ENQUE_Script ('$ 1") ");
          file.contents= buffer.from (raw);
        }
        CB (NULL, FILE);
      }))
      .pipe (Dest (Project.Build.php));
    

    Result:

    &lt;
    ! DOCTYPE HTML &GT;
    &Lt;
    HTML LANG= "EN" &GT;
    &Lt;
    HEAD &GT;
        &Lt;
    Meta Charset= "UTF-8" &gt;
        &Lt;
    Meta Name= "ViewPort" CONTENT= "WIDTH= device-width, initial-scale= 1" &gt;
        &Lt;
    Link REL= "StyleSheet" Type= "text /css" href= "wp_enque_style ('css /style.min.css')" &gt;
        &Lt;
    Title &gt;
    Document &LT;
    /title &gt;
    &Lt;
    /Head &gt;
    &Lt;
    Body class= "wrapper" &gt;
        &Lt;
    Main class= "Page" &gt;
        &Lt;
    /Main &gt;
        &Lt;
    /div &gt;
    &Lt;
    /Body &GT;
    &Lt;
    Script Type= "text /javascript" src= "WP_ENQUE_Script ('js /script.min.js')" &gt;
    &Lt;
    /script &gt;
    &Lt;
    /HTML &GT;
    

    Perhaps I did not fully understand the Through2 module, sorry. I can explain this work with regular regularities: I needed to replace Konstantin_SH2021-09-13 17:23:45

    I am not about Through2, not about him at all. To justify your reproach, I made a small code-root code for a small piece of code from the question.

    nörbörnën2021-09-13 20:28:04
  • Answer # 3

    In your cases FS use no need to use at all as Gulp has a functionality called Vinyl-Fs.. He has already read all the files and works with their content. The following code processes the file from your question. But use it only as an example because I could set up with regular regularities. The Contents variable contains all the contents of the file, you can work with it and line up and as with a full file.

    const gulp= require ('gulp');
    Const Rename= Require ('Gulp-Rename');
    Const Transform= Require ('Gulp-Transform');
    gulp.task ('Default', ()= &gt;
     {
      Return Gulp.
        .src ('./2. HTML')
        .pipe (Rename ({
          ExtName: '.php'
        }))
        .pipe (TRANSFORM ('UTF8', (Contents)= &GT;
     Contents
          .replace (/(&lt;
    Script Type= "text \ /javascript" src= ") (. +" &gt;
    &Lt;
    \ /script &gt;
    ) /Gi, '$ 1WP_ENQUE_Script /$ 2')
          .replace (/(&lt;
    Link REL= "STYLESHEET" TYPE= "TEXT \ /CSS" href= ") (. +" &gt;
    ) /Gi, '$ 1WP_ENQUE_Style /$ 2')
        )))
        .pipe (gulp.dest ('/Output '));
    }); 
  • Answer # 4

    In your cases FS use no need to use at all as Gulp has a functionality called Vinyl-Fs.. He has already read all the files and works with their content. The following code processes the file from your question. But use it only as an example because I could set up with regular regularities. The Contents variable contains all the contents of the file, you can work with it and line up and as with a full file.

    const gulp= require ('gulp');
    Const Rename= Require ('Gulp-Rename');
    Const Transform= Require ('Gulp-Transform');
    gulp.task ('Default', ()= &gt;
     {
      Return Gulp.
        .src ('./2. HTML')
        .pipe (Rename ({
          ExtName: '.php'
        }))
        .pipe (TRANSFORM ('UTF8', (Contents)= &GT;
     Contents
          .replace (/(&lt;
    Script Type= "text \ /javascript" src= ") (. +" &gt;
    &Lt;
    \ /script &gt;
    ) /Gi, '$ 1WP_ENQUE_Script /$ 2')
          .replace (/(&lt;
    Link REL= "STYLESHEET" TYPE= "TEXT \ /CSS" href= ") (. +" &gt;
    ) /Gi, '$ 1WP_ENQUE_Style /$ 2')
        )))
        .pipe (gulp.dest ('/Output '));
    }); 
  • Answer # 5

    Unfortunately, with Gulp. Not familiar, so I can only offer a pure Nodovsky code for adaptation. One of the possible options, if you need to use threads:

    const FS= Require ('FS');
    const readline= Require ('Readline');
    const {Pipeline}= Require ('Stream');
    ASYNC FUNCTION * PROSESSLINEBYLINE (PATH) {
      const filestream= fs.createReadStream (PATH);
      Const RL= readline.createInterface ({
        INPUT: FILESTREAM,
        CRLFDELAY: Infinity.
      });
      For AWAIT (Const Line of RL) {
        //Do Something With The Line.
        Yield `$ {Line} \ N`;
      }
    }
    Pipeline (
      ProcessLinebyLine ('Test.txt'),
      FS.CREATEWRITESTREAM ('TEST2.TXT'),
      ()= &Gt;
     {Console.log ('Done'); }
    );
    
  • Answer # 6

    Unfortunately, with Gulp. Not familiar, so I can only offer a pure Nodovsky code for adaptation. One of the possible options, if you need to use threads:

    const FS= Require ('FS');
    const readline= Require ('Readline');
    const {Pipeline}= Require ('Stream');
    ASYNC FUNCTION * PROSESSLINEBYLINE (PATH) {
      const filestream= fs.createReadStream (PATH);
      Const RL= readline.createInterface ({
        INPUT: FILESTREAM,
        CRLFDELAY: Infinity.
      });
      For AWAIT (Const Line of RL) {
        //Do Something With The Line.
        Yield `$ {Line} \ N`;
      }
    }
    Pipeline (
      ProcessLinebyLine ('Test.txt'),
      FS.CREATEWRITESTREAM ('TEST2.TXT'),
      ()= &Gt;
     {Console.log ('Done'); }
    );