Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BUG: postcss module generates wrong hashes #972

Closed
laurib opened this issue Mar 9, 2018 · 5 comments · Fixed by #2922
Closed

BUG: postcss module generates wrong hashes #972

laurib opened this issue Mar 9, 2018 · 5 comments · Fixed by #2922
Labels
🐛 Bug CSS Preprocessing All the PostCSS, Less, SASS, etc issues

Comments

@laurib
Copy link

laurib commented Mar 9, 2018

This a 🐛 bug report

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

postcss.config.js

module.exports = {
  "modules": true,
  "plugins": {
    "autoprefixer": {
      "grid": true,
      "browsers": [
        ">1%",
        "last 2 versions",
        "not ie < 10"
      ],
      "flexbox": "no-2009"
    },
    "postcss-modules": {
      "generateScopedName": "[local]__[hash:base64:4]"
    }
  }
}

package.json

{
  "name": "react-project",
  "version": "1.0.0",
  "description": "Small test",
  "main": "index.js",
  "scripts": {
    "start": "parcel public/index.html -p 4000 --no-cache",
    "build": "parcel build public/index.html --out-dir build --no-cache --public-url ./",
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "repository": {},
  "author": "",
  "license": "ISC",
  "homepage": "",
  "dependencies": {
    "apollo-boost": "^0.1.2",
    "babel-eslint": "^7.2.3",
    "classnames": "^2.2.5",
    "graphql": "^0.13.1",
    "react": "^16.2.0",
    "react-apollo": "^2.0.4",
    "react-dom": "^16.2.0",
    "react-loadable": "^5.3.1",
    "react-router-dom": "^4.2.2"
  },
  "devDependencies": {
    "autoprefixer": "^8.1.0",
    "babel-core": "^6.26.0",
    "babel-plugin-module-resolver": "^3.1.0",
    "babel-preset-react-app": "^3.1.1",
    "eslint": "^4.18.2",
    "eslint-config-react-app": "^2.1.0",
    "eslint-plugin-flowtype": "^2.46.1",
    "eslint-plugin-import": "^2.9.0",
    "eslint-plugin-jsx-a11y": "^5.1.1",
    "eslint-plugin-react": "^7.7.0",
    "node-sass": "^4.7.2",
    "postcss-modules": "^1.1.0",
    "postcss-scss": "^1.0.4"
  }
}

.babelrc

{
  "plugins": [
    ["module-resolver", {
      "root": [
        "./src/**/components"
      ],
      "alias": {}
    }]
  ],
  "presets": [
    "react-app"
  ]
}

🤔 Expected Behavior

Classnames in generated .css file should have hash which is 4 chars long and :global classname should not contain hash

😯 Current Behavior

Seems working correctly after restarting parcel.
Example:
.page-header {} is converted to .page-header__1x8y {} and :global .app-root is converted to .app-root {}

Classnames are generated incorrectly after changing and saving css file
.page-header {} is converted to ._page-header__1x8y_1dxnl_1 {} and :global .app-root is converted to ._app-root_1dxnl_12

When saving again then classnames are converted as shown below
.page-header {} -> ._page-header__1x8y_15rqa_1 {}
:global .app-root -> ._app-root_15rqa_12

💁 Possible Solution

🔦 Context

This is blocker bug.

💻 Code Sample

🌍 Your Environment

Software Version(s)
Parcel 1.62
Node 8.1.3
npm/Yarn npm 5.7.1
Operating System macOS High Sierra
@davidnagli davidnagli added 🐛 Bug CSS Preprocessing All the PostCSS, Less, SASS, etc issues labels Mar 12, 2018
@kidandcat
Copy link
Contributor

Same here, this is a stopper for us, we cannot run our project which uses css modules at all.

@iocat
Copy link

iocat commented Mar 25, 2019

Second this

{
  "modules": true,
  "plugins": {
    "autoprefixer": {
      "grid": true
    },
    "postcss-modules": {
       "generateScopedName": "[name]__[local]___[hash:base64:3]"
    }
  }
}

My generateScopedName never gets applied for some reason :(

@lpython
Copy link

lpython commented Mar 31, 2019

@iocat @laurib @kidandcat May have found something...

///!!! Just saw #2865

I could not get my package.json to work, specifficaly, the ["postcss-modules"].generateScopedName , until .....

"babel": {  
   "presets": [  
     "@babel/preset-env"  
   ],  
   "plugins": [  
     "@babel/plugin-proposal-class-properties",  
     [  
       "@babel/plugin-transform-react-jsx",  
       { 
         "pragmaFrag": "React.Fragment"  
       }  
     ],  
     [  
       "react-css-modules",  
       {  
         "generateScopedName": "[path]__[name]__[local]__",  
         "handleMissingStyleName": "warn"    
       }  
     ] 
   ]  
 },
 "postcss": {
   "modules": true,
   "plugins": {
     "postcss-modules": {
       "generateScopedName": "[path]__[name]__[local]__",
       "globalModulePaths": [
         ".global."
       ],
       "root": "."
     },
     "autoprefixer": {
       "grid": true
     }
   }
 }

.... until I modified /src/transforms/postcss.js : SWITCHED LINES 55 / 56

File: node_modules\parcel-bundler\src\transforms\postcss.js
45: 
46:   let postcssModulesConfig = {
47:     getJSON: (filename, json) => (asset.cssModules = json),
48:     Loader: createLoader(asset),
49:     generateScopedName: (name, filename) =>
50:       `_${name}_${md5(filename).substr(0, 5)}`
51:   };
52: 
53:   if (config.plugins && config.plugins['postcss-modules']) {
54:     postcssModulesConfig = Object.assign(
55:       postcssModulesConfig,
56:       config.plugins['postcss-modules']
57:     );
58:     delete config.plugins['postcss-modules'];
59:   }

The original order of line 55 / 56 prevent anyone from overriding generateScopedName on line 49. It cannot be overrided via package.json | .postcssrc | .postcssrc.js | etc....
This isn't desired behavior, is it?

@kidandcat
Copy link
Contributor

Bump @devongovett the PR is already created

kidandcat added a commit to kidandcat/parcel that referenced this issue Apr 15, 2019
devongovett pushed a commit that referenced this issue May 2, 2019
@rfgamaral
Copy link

Any chance we can get this fix into the current v1.x branch?

karkh pushed a commit to karkh/parcel that referenced this issue Aug 8, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🐛 Bug CSS Preprocessing All the PostCSS, Less, SASS, etc issues
Projects
None yet
6 participants