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

Add process method (compatibility with optimize-css-assets-webpack-plugin) #944

Merged
merged 8 commits into from
May 23, 2017

Conversation

strarsis
Copy link
Contributor

@strarsis strarsis commented May 11, 2017

This PR adds the process method to clean-css so
optimize-css-assets-webpack-plugin can use it as css processor.

Closes issue #943.

Remove unused reject parameter.
@jakubpawlowicz
Copy link
Collaborator

@strarsis any updates?

@strarsis
Copy link
Contributor Author

strarsis commented May 17, 2017

@jakubpawlowicz: Yes, this can be merged now which
adds compatibility for optimize-css-assets-webpack-plugin.

@jakubpawlowicz
Copy link
Collaborator

I meant do you have any comments to my PR review above? ^

@strarsis
Copy link
Contributor Author

strarsis commented May 18, 2017

@jakubpawlowicz: I don't see any PR reviews / file change comments.

lib/clean.js Outdated

// for compatibility with optimize-css-assets-webpack-plugin
CleanCSS.process = function (input, opts) {
return new Promise(function (resolve) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can it somehow use our existing promise interface - https://github.com/jakubpawlowicz/clean-css#promise-interface ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, the code is nicer using it.

lib/clean.js Outdated
return new Promise(function (resolve) {
var cleanCss = new CleanCSS(opts);
var output = cleanCss.minify(input);
resolve({ css: output.styles });
Copy link
Collaborator

Choose a reason for hiding this comment

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

Another question here - what happens with other output fields, like errors, sourceMap, stats, or warnings?

Copy link
Contributor Author

@strarsis strarsis May 18, 2017

Choose a reason for hiding this comment

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

The webpack plugin only expects/uses the css property of returned object: https://github.com/NMFR/optimize-css-assets-webpack-plugin/blob/master/index.js#L78

@jakubpawlowicz
Copy link
Collaborator

Ah sorry, forgot to submit the review.

lib/clean.js Outdated
return cleanCss.minify(input)
.then(function(output) {
return { css: output.styles };
});
Copy link
Collaborator

Choose a reason for hiding this comment

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

Cool. Please use 2-space indent in lines 54-56 to be consistent with other code here and I'll merge it, thanks!

@strarsis
Copy link
Contributor Author

Extra note: optimize-css-assets-webpack-plugin passes the to option to cssnano which in turn passes it to postcss internally for sourcemap support (see NMFR/optimize-css-assets-webpack-plugin#9).
Sourcemaps are supported + enabled by default in clean-css?

@jakubpawlowicz
Copy link
Collaborator

In case of clean-css that to option may relate to rebaseTo. And we do support source maps via sourceMap and sourceMapInlineSources flags, see: https://github.com/jakubpawlowicz/clean-css#how-to-work-with-source-maps

@strarsis
Copy link
Contributor Author

strarsis commented May 20, 2017

Option to is now mapped to rebaseTo and cleaned up from options object.

@jakubpawlowicz
Copy link
Collaborator

👍 somehow indents are wrong again, please format it as:

return cleanCss.minify(input)
  .then(function(output) {
    return { css: output.styles };
  });

@strarsis
Copy link
Contributor Author

@jakubpawlowicz: Fixed

@jakubpawlowicz jakubpawlowicz merged commit a0b251b into clean-css:master May 23, 2017
@jakubpawlowicz
Copy link
Collaborator

Thanks!

jakubpawlowicz added a commit that referenced this pull request Jun 7, 2017
jakubpawlowicz added a commit that referenced this pull request Jun 17, 2017
@maxmilton
Copy link

I wasn't able to get this working at all, let alone with source maps.

Instead I had to put together something a bit custom to get it working and with source map support. Might be useful for someone in the future:

const path = require('path');
const OptimizeCSSPlugin = require('optimize-css-assets-webpack-plugin');
const CleanCSS = require('clean-css');

...

  plugins: [
    // Compress extracted CSS. We are using this plugin so that possible
    // duplicated CSS from different components can be deduped.
    new OptimizeCSSPlugin({
      // use clean-css instead of the default cssnano
      cssProcessor: {
        process: (css, options) => new Promise((resolve, reject) => {
          const opts = Object.assign(options.map ? {
            sourceMap: true,
            sourceMapInlineSources: true,
          } : {}, options);

          opts.returnPromise = true;
          const filename = path.basename(opts.to);

          new CleanCSS(opts)
            .minify({
              [filename]: {
                styles: css,
                sourceMap: opts.map && opts.map.prev, // probably empty but here just in case
              }
            })
            .then((result) => {
              if (result.warnings) console.warn(result.warnings);

              resolve({
                css: options.map
                  ? result.styles + `\n/*# sourceMappingURL=${filename}.map */`
                  : result.styles,
                map: result.sourceMap,
              });
            })
            .catch(reject);
        }),
      },
      cssProcessorOptions: {
        // passing an object will enable source maps OR comment out to disable
        map: {},

        // the actual clean-css options
        level: {
          1: { all: true },
          2: { all: true },
        },
      },
    }),
  ]

...

@yshterev
Copy link

yshterev commented May 5, 2018

@jakubpawlowicz Has this been published to the npm?
I just installed clean-css 4.1.11, I got process is not a function.. and the code is missing from node_modules/clean-css/lib/clean.js

@beanlee
Copy link

beanlee commented May 11, 2018

@yshterev same to you, it doesn't work. : (

@yshterev
Copy link

It can be used like this until it is fixed:

new OptimizeCssAssetsPlugin({
      cssProcessor: {
        process: (input, opts) => {
          const optsTo = opts.to;
          delete opts.to;

          const cleanCss = new CleanCSS(Object.assign({ returnPromise: true, rebaseTo: optsTo }, opts));
          return cleanCss.minify(input).then(output => ({css: output.styles }));
        }
      },

@tadinski
Copy link

tadinski commented Jul 2, 2018

this.options.cssProcessor.process is not a function

still an issue

@glen-84
Copy link

glen-84 commented Nov 23, 2018

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.

None yet

7 participants