cssnano/cssnano-nightly

[Bug] : TypeError: (0 , _cosmiconfig.cosmiconfig) is not a function

rschristian opened this issue · 28 comments

Describe the bug

I'm trying to use cssnano@nightly but I keep running up against the following:

TypeError: (0 , _cosmiconfig.cosmiconfig) is not a function

at resolveConfig (node_modules/cssnano/dist/index.js:104:26)

To Reproduce

Currently trying to update optimize-css-assets-webpack-plugin. My fork that reproduces this bug can be found here.

  1. Install dependencies with npm install
  2. Run the test suite with npm test

The error should be displayed.

Expected behavior

The test suite should complete, and theoretically do so without errors.

I mention below a work-around that gets the test suite to pass, and while I fixed a bug in the upgrade to cssnano@nightly, more might exist. It's entirely possible for the test suite to fail even after this is fixed, so it's a loose expectation for the test suite to pass.

Screenshots

N/A

Desktop (please complete the following information):

npx envinfo:

System:
    OS: Linux 5.7 Manjaro Linux
    CPU: (4) x64 Intel(R) Core(TM) i7-7700HQ CPU @ 2.80GHz
    Memory: 3.32 GB / 11.67 GB
    Container: Yes
    Shell: 5.8 - /usr/bin/zsh
  Binaries:
    Node: 14.8.0 - /usr/bin/node
    Yarn: 1.22.5 - ~/.yarn/bin/yarn
    npm: 6.14.8 - ~/.npm-global/bin/npm

npm ls cssnano:

optimize-css-assets-webpack-plugin@5.0.4 /home/ryun/Projects/optimize-css-assets-webpack-plugin
└── cssnano@4.0.0-nightly.2020.9.1 

Additional context

While I know very little in this area, I noticed in an older version there was the following code:

var _cosmiconfig = require("cosmiconfig");

var _cosmiconfig2 = _interopRequireDefault(_cosmiconfig);

...

const configExplorer = (0, _cosmiconfig2.default)(cssnano);

Nightly looks like the following:

var _cosmiconfig = require("cosmiconfig");

...

const configExplorer = (0, _cosmiconfig.cosmiconfig)(cssnano);

Going back to the old version seems to work just fine, the test suite will run without throwing this error. Though, again, have very little understanding in this area, it's likely going back to whatever generated this built output is not a valid fix.

Why you transpile node_modules? Anyway optimize-css-assets-webpack-plugin is bad solution with many bugs, also no caches and parallels, we have official plugin for wepback https://github.com/webpack-contrib/css-minimizer-webpack-plugin

Sorry, do you mean to ask why the plugin transpiles? I don't think I have, as for the plugin, no clue. I know next to nothing about what it does, just trying to fix an error.

Thanks for the information, though it's not that I use it, but that the Preact CLI does.

We have an old 1% opacity bug there, which traces back to optimize-css-assets-webpack-plugin, which traces back further to here, but has been fixed. I figured I'd try to help optimize-css-assets-webpack-plugin's author use cssnano@nightly so we could get the fix downstream in the CLI.

I will look at issue in near future

Cheers. I'll provide a link to this in the Preact CLI so that people much more informed than I can see your thoughts on the matter.

I am not able to reproduce this issue.

Can you send a repo without the plugin you mentioned? Just an independent repro using cssnano nightly.
it would be great to have it as codesandbox as well.

Can't reproduce too

Sorry, but the whole issue is that the forked plugin, when upgraded to use cssnano@nightly, fails its test suite. There is no other repo I can really give? The issue is entirely in that plugin's test suit.

I'll try it again shortly to see if somehow the issue entirely went away.

@ryanchristian4427 maybe you have invalid environment for tests, sorry I cant fix something not reproducible

Don't know then. All I have is that repo and the tests are absolutely still failing due to cssnano for me. Not anything related to the actual plugin, just cssnano.

TypeError: (0 , _cosmiconfig.cosmiconfig) is not a function

      72 |     return this.options
      73 |       .cssProcessor.process(css.source, processOptions, this.options.cssProcessorPluginOptions)
    > 74 |       .then(r => {
         |        ^
      75 |         if (processOptions.map && r.map && r.map.toString) {
      76 |           assets.setAsset(`${assetInfo.path}.map${assetInfo.query}`, r.map.toString());
      77 |         }

      at resolveConfig (node_modules/cssnano/dist/index.js:104:55)
      at plugin (node_modules/cssnano/dist/index.js:141:12)

Please open an issue in repo plugin, sorry I can't help you

Please migrate on https://github.com/webpack-contrib/css-minimizer-webpack-plugin, I will say again that the plugin is not designed correctly and does not support all webpack features

For what it's worth, I was able to get a codesandbox up here that reproduces. Simply run yarn test in the terminal.

I have little experience with CodeSandbox and as such had no idea how to get es6 imports working (or why they were failing only there), so the code is a bit different from the actual repo. The same error does pop up though, so hopefully this is a usable reproduction, even if it isn't 1:1.

As for swapping it out entirely, I get your point but I'm not really the one to do it. Like I said, I have little to no experience in this area, and as such, advocating for a swapping out of one of the most used libraries around (millions of downloads a week, likely due to CRA) for one that might be better (I couldn't actually say myself) isn't something I can really do. I'm just following a thread from the Preact CLI is all. I'll bring it up however.

Thanks for your time. I suppose it is an issue with the plugin's setup/environment.
the nightly version is working fine when used independently and with postcss config.

Alright, well figure I may as well close this. If it's just an issue with how another library uses this one, then there's nothing for you to fix, and an alternative has been presented. Thank you two for your help. Really appreciate your time.

@ryanchristian4427 Still in my TODO, let's keep open, I will give you advises why it is broken

@ryanchristian4427 Very strange, in our source code we have

const configExplorer = (0, _cosmiconfig.default)(cssnano);

But in cssnano@4.0.0-nightly.2020.9.1 we have:

 const configExplorer = (0, _cosmiconfig.cosmiconfig)(cssnano);

@anikethsaha Looks we have bugs, do you run something (maybe babel) on cssnano-nightly?

hm, after run yarn in cssnano repo I got the same problem, very weird 😕

We didn't have any changes related to cosmic after 20 Jul
cssnano/cssnano#924

hm, after run yarn in cssnano repo I got the same problem, very weird 😕

This is really weird, I am not seeing this.
I will try cleaning the node_modules and check

@anikethsaha hm, I think here other problem, give a sec

@anikethsaha

yarn add cssnano@4.0.0-nightly.2020.9.3
// Or npm i cssnano@4.0.0-nightly.2020.9.3

Go to node_modules/cssnano/package.json

{
  "dependencies": {
    "cosmiconfig": "^5.2.1",
    "cssnano-preset-default": "nightly",
    "is-resolvable": "^1.1.0",
    "opencollective-postinstall": "^2.0.2",
    "postcss": "^7.0.16"
  }
}

But in master we have https://github.com/cssnano/cssnano/blob/master/packages/cssnano/package.json#L28

cosmiconfig is 6 in master
cosmiconfig is 5 in nightly, but our code for 6 verison

Looks we have invalid logic for dependencies in our nightly script

@anikethsaha we should not have hardcored deps, this object should be:

cssnano: {
  "cssnano-preset-default": version
}

so change only necessary deps and keep as it others

We are using edit-json-file for changing the package.json and so I need to look whether we can do assign operation for the object instead of Overriding it.
I will look into this as soon as possible.

New nightly version has been release. 2020-9-9. @ryanchristian4427 can you try the latest version and check for the same bug.

@anikethsaha @evilebottnawi

Can confirm that everything works as expected with the new version. Bug is gone.

Thank you for the help. Really appreciate it.

Closing as the bug is fixed