Skip to content

PostHTML plugins that require css-modules don't work. #395

Open

Description

@XAMPPRocky

Choose one: is this a 🐛 bug report or 🙋 feature request?
🐛

Trying to use posthtml plugins that require the json containing the css classes causes the index.html to become <div></div>. regardless of what it was.

🎛 Configuration (.babelrc, package.json, cli command)

index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title></title>
</head>
<body>
    <h1 classname="foo">Hello World</h1>
</body>
</html>

index.css

.foo {
    background-color: red;
}

.postcssrc

{
    "modules": true,
}

.posthtmlrc

{
    "plugins": {
        "posthtml-css-modules": true
    }
}

package.json

{
  "name": "parcel",
  "version": "1.0.0",
  "main": "index.js",
  "license": "MIT",
  "devDependencies": {
    "postcss-modules": "^1.1.0",
    "posthtml-postcss-modules": "^0.1.5"
  }
}

🤔 Expected Behavior

Generate the expected html with modifications performed by the posthtml plugins.

😯 Current Behavior

Generates a index.html containing only <div></div>

🔦 Context

This prevents me from working on any project using parcel.

🌍 Your Environment

Software Version(s)
Parcel 1.3.1
Node v9.3.0
npm/Yarn 5.6.0/1.3.2
Operating System macOS 10.13.2

Activity

  1. devongovett commented on Dec 24, 2017

    @devongovett
    Member

    There is an example in the tests that works. Can you give any more details about what doesn't work for you? Are there error messages?

    https://github.com/parcel-bundler/parcel/tree/master/test/integration/posthtml

  2. XAMPPRocky commented on Dec 24, 2017

    @XAMPPRocky
    Author

    @devongovett Hello, there are no error messages it just outputs <div></div>. That example is not the same as what I've shown above all that integration test is if a single posthtml plugin works not posthtml with postcss.

  3. changed the title [-]PostHTML plugins don't work.[/-] [+]PostHTML plugins with non object arguments don't work.[/+] on Dec 26, 2017
  4. changed the title [-]PostHTML plugins with non object arguments don't work.[/-] [+]PostHTML plugins that require css-modules don't work.[/+] on Dec 26, 2017
  5. bndnsmth commented on Dec 28, 2017

    @bndnsmth
    Contributor

    @Aaronepower a reproducible test repo or gist would be hugely helpful in debugging!

  6. XAMPPRocky commented on Dec 29, 2017

    @XAMPPRocky
    Author

    @brandon93s Hello, I have provided the needed files to reproduce the bug in the original post. I've also done some debugging into the issue and have traced it to the posthtml line where the res.tree returned is a function named parse rather than an AST.

  7. d4rkr00t commented on Jan 1, 2018

    @d4rkr00t

    I've investigated a little bit, and some of the things i've found:

    1. It's a function because of the logic here:
      if (Object.keys(options).length > 0) {
      plugin = plugin(options);
      }
      Since options there, for this particular use case, is a boolean, parcel never calls plugin().
    2. Also you need to specify a file, dir with files or object that will be used for replacing class names with generated class names e.g. posthtml([require('posthtml-css-modules')('./cssClasses.json')]).
    3. Here
      if (typeof options !== 'object') {
      options = {};
      }
      parcel is ignoring all none object options meaning even if you specify a file with classes something like: "posthtml-css-modules": "./cssClasses.json" it won't work.
    4. But you still can specify options as an object e.g. "posthtml-css-modules": { "title": "_title_116zl_1 _heading_9dkf" } – which works fine...

    @devongovett is there any particular reason for:

    if (typeof options !== 'object') {
    options = {};
    }

    Is it fine if i change it to something like if (typeof options !== 'object' && typeof options !== 'string') { ?

  8. XAMPPRocky commented on Jan 1, 2018

    @XAMPPRocky
    Author

    It might be out of scope for this single issue, but I was wondering about how closely using css-modules in parcel should be with posthtml since they are already generated and special cased in parcel and they are not stored in a way that is visible for the user of parcel to use. I was thinking that there are two solutions to the problem.

    • Provide an option in .postcssrc to allow outputting css-modules to a file.
    • Have HTMLAsset store the generated class names of it's child CSSAssets and use posthtml-css-modules if it is enabled in a similar way to "modules": true in .postcssrc.
  9. wryk commented on Jan 12, 2018

    @wryk

    Because of issue renaming I don't know if it's still belongs here :
    Running any posthtml plugin with empty options ({}) generate <div></div>.

    With this .posthtmlrc configuration parcel works fine but meeeh :

    {
    	"plugins": {
    		"posthtml-include": {
    			"useless-option": true
    		},
    		"posthtml-md": {
    			"useless-option": true
    		}
    	}
    }
    
  10. sandrina-p commented on Jan 13, 2018

    @sandrina-p

    I have the same problem and @wryk solution didn't work for me... I just can't use posthtml-include...

    @brandon93s I added a repo so you guys can debug it

  11. PatrickNausha commented on Jan 14, 2018

    @PatrickNausha

    A finding that may be helpful to @sandrina-p:

    I got posthtml-include to work by ensuring that I had set the "root" property in my .posthtmlrc. If the include tag src attribute isn't correct with respect to root, <include src ... simply gets emitted as <include src ... in the parcel build output. IMHO, this should emit an error instead. Thanks, @devongovett for the link to the integration test!

    See full code at https://github.com/PatrickNausha/PersonalWebsite/tree/parcel

    A finding related to parcel vs parcel build that perhaps warrants a new issue:

    1. Change .posthtmlrc in my code to the below (or pull down https://github.com/PatrickNausha/PersonalWebsite/tree/parcel-broken-posthtmlrc and yarn install):
    {
    	"plugins": {
    		"posthtml-include": true
    	}
    }
    
    1. Run parcel build ./src/index.html to see
    yarn run v1.2.1
    $ parcel build ./src/index.html
    🚨  /Users/patrick.nausha/code/PersonalWebsite/src/index.html: tree.forEach is not a function
        at Object.collapseWhitespace [as default] (/Users/patrick.nausha/code/PersonalWebsite/node_modules/htmlnano/lib/modules/collapseWhitespace.js:18:10)
        at /Users/patrick.nausha/code/PersonalWebsite/node_modules/htmlnano/lib/htmlnano.js:58:42
        at <anonymous>
    error Command failed with exit code 1.
    info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
    

    The reference to tree seems related to

    I've also done some debugging into the issue and have traced it to the posthtml line where the res.tree returned is a function named parse rather than an AST.

    1. Run parcel ./src/index.html and watch it succeed happily.
    2. Open the browser to http://localhost:1234/
    3. The page source will be <div></div>

    Here's what I expect to be different:

    1. Both parcel and parcel build should probably fail.
    2. The error message should be related to .posthtmlrc or other PostHTML validation.

    I hope this helps!

  12. XAMPPRocky commented on May 20, 2020

    @XAMPPRocky
    Author

    Hey, I wish you wouldn't close bug reports just because they're stale, it kind of defeats the point of making the bug report in the first place.

  13. added
    Stale IgnoreThis issue is exempt from getting flagged as stale and autoremoved
    on May 20, 2020
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

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions