Skip to content

Support both csso v1 and v2 - #219

Closed
prantlf wants to merge 1 commit into
guybedford:masterfrom
prantlf:csso-v2
Closed

Support both csso v1 and v2#219
prantlf wants to merge 1 commit into
guybedford:masterfrom
prantlf:csso-v2

Conversation

@prantlf

@prantlf prantlf commented Dec 10, 2016

Copy link
Copy Markdown
Contributor

No description provided.

Comment thread css-builder.js
try {
css = csso.justDoIt(css);
if (typeof csso.minify === 'function') {
css = csso.minify(css).css;

@alundiak alundiak Feb 1, 2017

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Really, csso v2.3.1 has minify() function output result in this form:

result = debugOutput('translate', options, Date.now(), {
            css: translate(compressResult.ast),
            map: null
        });

Need to test require.css for 2 possible ways: minify(css) and minify(css).css

@alundiak alundiak Feb 18, 2017

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I checked this code changes in regards to csso versions: 1.8.0, 2.0.0 and 2.3.1. And suggested code not always works correctly.

Suggested code (csso.minify(css).css) will cause error in case of usage csso v1.8.0

Error: TypeError: Cannot read property 'length' of undefined
    at compress (eval at <anonymous> (/usr/local/lib/node_modules/requirejs/bin/r.js:25271:33), <anonymous>:50:63)

Note: tested with requirejs v 2.1.10 (as bundled in require-css) and 2.3.2 (as latest)

So we need to improve code a bit, so that to support at least 3 major versions.

I will provide a comment a bit later, or create my own PR.

@alundiak alundiak mentioned this pull request Feb 2, 2017
@alundiak

alundiak commented Feb 18, 2017

Copy link
Copy Markdown
Collaborator

csso releases review:

  • since 1.4.0 we have minify() and earlier (eg. 1.3.12) it was ONLY justDoIt()
  • in csso 1.8.0 csso.minify() and csso.justDoIt() both exists and they are functions.
  • in csso 2.0.0 and 2.3.1 has no justDoIt() AT ALL.

I did deep research of how your suggested change can affect potential end customers/consumers of require-css.

And here is my code change suggestion:

try {
  if (typeof csso.minify === 'function') {
      var minifyResult = csso.minify(css);
      if (typeof minifyResult === 'string'){ // for csso < 2.0.0
        css = minifyResult; 
      } else if (typeof minifyResult === 'object'){ // for csso >= 2.0.0
        css = minifyResult.css; 
      } 
  } else { // justDoIt() was always. minify() appeared in csso 1.4.0.
    css = csso.justDoIt(css);
  }
}

I did this, because I know enterprise projects, which still use old version of require-css and csso, so my approach is universal. Tested with:

  • require-css 0.1.8.
  • csso 1.3.12, 1.4.0, 1.8.0, 2.0.0 and latest (checked 2017/02/18) 2.3.1.
  • requirejs 2.1.10 and 2.3.2
  • node 6.9.5 + npm 3.10.10
  • node 7.5.2 + npm 4.1.2
  • command r.js -o example/build.js with optimizeCss: "standard"

Note: I realize, this change may be overthinking/overcomplex, but so far the fact is - if we merge ur current code change, someone who will upgrade to new require-css version, but remain old csso version, they will have errors like I mentioned in this PR and in #203.

So @prantlf please consider my code above as a suggestion to apply to your PR. After 1 week silence, I will close this PR, and recreate new one with my suggested code.

cc/ @guybedford

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants