Skip to content

match text in postprocess for compiled pug #38

Description

@webdiscus

@webdiscus something is wrong with postprocess for js. I think /main.js/ is not here in postprocess moment

new PugPlugin({
  postprocess: (content) => {
    // ...content
    content = content.replace(
      /src="(\/.*?\.js)"/g, // doesn't find anything
      `src="FROM(my_zip, '$1')"`,
    );
    return content;
  }),
})

src="(\/.*?\.js)" matches src="/js/main.js"

The links of JS and CSS should be replaced in generated HTML.

Sometimes we need to paste HTML in another parser which reads assets (images, css, js) from zip file and sends to client. So "replacer" replaces to path to zip folder

Originally posted by @silveoj in #36 (comment)

Activity

  1. webdiscus commented on Jul 21, 2022

    @webdiscus
    OwnerAuthor

    @silveoj

    The postprocess will be called after Pug compilation. At this stage the generated HTML contains already hashed assets output filenames, exclude JS. JS files have at this stage resolved absolute path, because inner processing is very complex.

  2. silveoj commented on Jul 21, 2022

    @silveoj

    It works in 3.0.1 but doesn't work in 3.1.1.
    3.1.0 can't check

    3.0.1 content has relative path to main.js: "/js/main.js"
    3.1.1 content has absolute path to main.js in disk: "C:\myProj\src\js\main.js"

    If you need to use only absolute path I can change my regex of course

  3. webdiscus commented on Jul 21, 2022

    @webdiscus
    OwnerAuthor

    3.1.0 is deprecated, because contains many bugs.

    3.0.1 contains source path /js/main.js because it was the BUG, such absolute path was not resolved.
    3.1.1 contains correct resolved absolute path, but not yet output filename, because inner webpack processing is very very complex

    The the content argument of postprocess has already compiled HTML. At compilation all required files (Images, Styles, Fonts) will be resolved with output filenames, exclude JS.

    Also, the postprocessing with compiled HTML and original required files is principal impossible.

    P.S. you has wrong Regexp.
    Following Regexp match SOURCE_FILENAME in src="SOURCE_FILENAME" and replace with REPLACEMENT.

    return content.replace(/(?<=src=")(.+\.js)(?=")/g, 'REPLACEMENT');
  4. Repository owner locked and limited conversation to collaborators on Jul 21, 2022
  5. converted this issue into a discussion #39 on Jul 21, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions