From: 
Subject: Debian changes

The Debian packaging of node-rollup-plugin-postcss is maintained in git, using a workflow
similar to the one described in dgit-maint-merge(7).
The Debian delta is represented by this one combined patch; there isn't a
patch queue that can be represented as a quilt series.

A detailed breakdown of the changes is available from their canonical
representation -- git commits in the packaging repository.
For example, to see the changes made by the Debian maintainer in the first
upload of upstream version 1.2.3, you could use:

    % git clone https://git.dgit.debian.org/node-rollup-plugin-postcss
    % cd node-rollup-plugin-postcss
    % git log --oneline 1.2.3..debian/1.2.3-1 -- . ':!debian'

(If you have dgit, use `dgit clone node-rollup-plugin-postcss`, rather than plain `git clone`.)

We don't use debian/source/options single-debian-patch because it has bugs.
Therefore, NMUs etc. may nevertheless have made additional patches.

---

diff --git a/package.json b/package.json
index 5cb5227..b62edf8 100644
--- a/package.json
+++ b/package.json
@@ -10,7 +10,7 @@
   "scripts": {
     "test": "npm run lint && jest",
     "test:cov": "npm run lint && jest --coverage",
-    "build": "bili",
+    "build": "babeljs src --out-dir dist --extensions \".js\" --copy-files",
     "lint": "xo",
     "prepublishOnly": "npm run build"
   },
@@ -38,7 +38,6 @@
     "autoprefixer": "^10.0.4",
     "babel-core": "^7.0.0-bridge.0",
     "babel-jest": "^26.6.3",
-    "bili": "^5.0.5",
     "eslint-config-rem": "^4.0.0",
     "fs-extra": "^9.0.1",
     "jest": "^26.6.3",
@@ -53,17 +52,12 @@
   "dependencies": {
     "chalk": "^4.1.0",
     "concat-with-sourcemaps": "^1.1.0",
-    "cssnano": "^5.0.1",
     "import-cwd": "^3.0.0",
-    "p-queue": "^6.6.2",
     "pify": "^5.0.0",
     "postcss-load-config": "^3.0.0",
     "postcss-modules": "^4.0.0",
-    "promise.series": "^0.2.0",
     "resolve": "^1.19.0",
-    "rollup-pluginutils": "^2.8.2",
-    "safe-identifier": "^0.4.2",
-    "style-inject": "^0.3.0"
+    "rollup-pluginutils": "^2.8.2"
   },
   "peerDependencies": {
     "postcss": "8.x"
@@ -90,6 +84,7 @@
       [
         "@babel/preset-env",
         {
+          "modules": "commonjs",
           "targets": {
             "node": "6"
           }
diff --git a/src/index.js b/src/index.js
index 040a587..407d0ee 100644
--- a/src/index.js
+++ b/src/index.js
@@ -3,6 +3,8 @@ import { createFilter } from 'rollup-pluginutils'
 import Concat from 'concat-with-sourcemaps'
 import Loaders from './loaders'
 import normalizePath from './utils/normalize-path'
+import { minifyCss } from './utils/minify-css'
+import { styleInject, styleInjectPath } from './postcss-loader'
 
 /**
  * The options that could be `boolean` or `object`
@@ -95,6 +97,18 @@ export default (options = {}) => {
   return {
     name: 'postcss',
 
+    resolveId(id) {
+      if (id === styleInjectPath) {
+        return id
+      }
+    },
+
+    load(id) {
+      if (id === styleInjectPath) {
+        return `export default ${styleInject}`
+      }
+    },
+
     async transform(code, id) {
       if (!filter(id) || !loaders.isSupported(id)) {
         return null
@@ -233,7 +247,7 @@ export default (options = {}) => {
           cssOptions.to = codeFileName
         }
 
-        const result = await require('cssnano')(postcssLoaderOptions.minimize).process(code, cssOptions)
+        const result = await minifyCss(code, postcssLoaderOptions.minimize, cssOptions)
         code = result.css
 
         if (sourceMap === true && result.map && result.map.toString) {
diff --git a/src/loaders.js b/src/loaders.js
index 073f82f..95ee0cd 100644
--- a/src/loaders.js
+++ b/src/loaders.js
@@ -1,9 +1,9 @@
 import path from 'path'
-import series from 'promise.series'
 import postcssLoader from './postcss-loader'
 import sassLoader from './sass-loader'
 import stylusLoader from './stylus-loader'
 import lessLoader from './less-loader'
+import series from './utils/series'
 
 const matchFile = (filepath, condition) => {
   if (typeof condition === 'function') {
diff --git a/src/postcss-loader.js b/src/postcss-loader.js
index bbac026..79bbb24 100644
--- a/src/postcss-loader.js
+++ b/src/postcss-loader.js
@@ -2,13 +2,39 @@ import path from 'path'
 import importCwd from 'import-cwd'
 import postcss from 'postcss'
 import findPostcssConfig from 'postcss-load-config'
-import { identifier } from 'safe-identifier'
 import humanlizePath from './utils/humanlize-path'
 import normalizePath from './utils/normalize-path'
+import { identifier } from './utils/identifier'
+import { minifyCss } from './utils/minify-css'
 
-const styleInjectPath = require
-  .resolve('style-inject/dist/style-inject.es')
-  .replace(/[\\/]+/g, '/')
+export const styleInjectPath = '\0rollup-plugin-postcss-style-inject'
+export const styleInject = `function styleInject(css, ref) {
+  if ( ref === void 0 ) ref = {};
+  var insertAt = ref.insertAt;
+
+  if (!css || typeof document === 'undefined') { return; }
+
+  var head = document.head || document.getElementsByTagName('head')[0];
+  var style = document.createElement('style');
+  style.type = 'text/css';
+
+  if (insertAt === 'top') {
+    if (head.firstChild) {
+      head.insertBefore(style, head.firstChild);
+    } else {
+      head.appendChild(style);
+    }
+  } else {
+    head.appendChild(style);
+  }
+
+  if (style.styleSheet) {
+    style.styleSheet.cssText = css;
+  } else {
+    style.appendChild(document.createTextNode(css));
+  }
+}
+`
 
 function loadConfig(id, { ctx: configOptions, path: configPath }) {
   const handleError = err => {
@@ -97,11 +123,6 @@ export default {
       )
     }
 
-    // If shouldExtract, minimize is done after all CSS are extracted to a file
-    if (!shouldExtract && options.minimize) {
-      plugins.push(require('cssnano')(options.minimize))
-    }
-
     const postcssOptions = {
       ...this.options.postcss,
       ...config.options,
@@ -139,7 +160,11 @@ export default {
       plugins.push(noopPlugin())
     }
 
-    const result = await postcss(plugins).process(code, postcssOptions)
+    let result = await postcss(plugins).process(code, postcssOptions)
+    // If shouldExtract, minimize is done after all CSS are extracted to a file
+    if (!shouldExtract && options.minimize) {
+      result = await minifyCss(result.css, options.minimize, postcssOptions)
+    }
 
     for (const message of result.messages) {
       if (message.type === 'dependency') {
diff --git a/src/sass-loader.js b/src/sass-loader.js
index 815a07d..c09c7a4 100644
--- a/src/sass-loader.js
+++ b/src/sass-loader.js
@@ -1,13 +1,13 @@
 import path from 'path'
 import pify from 'pify'
 import resolve from 'resolve'
-import PQueue from 'p-queue'
 import { loadModule } from './utils/load-module'
+import Queue from './utils/queue'
 
 // This queue makes sure node-sass leaves one thread available for executing fs tasks
 // See: https://github.com/sass/node-sass/issues/857
 const threadPoolSize = process.env.UV_THREADPOOL_SIZE || 4
-const workQueue = new PQueue({ concurrency: threadPoolSize - 1 })
+const workQueue = new Queue({ concurrency: threadPoolSize - 1 })
 
 const moduleRe = /^~([a-z\d]|@).+/i
 
diff --git a/src/utils/identifier.js b/src/utils/identifier.js
new file mode 100644
index 0000000..f80555f
--- /dev/null
+++ b/src/utils/identifier.js
@@ -0,0 +1,51 @@
+const reserved = new Set([
+  'break',
+  'case',
+  'catch',
+  'class',
+  'const',
+  'continue',
+  'debugger',
+  'default',
+  'delete',
+  'do',
+  'else',
+  'export',
+  'extends',
+  'finally',
+  'for',
+  'function',
+  'if',
+  'import',
+  'in',
+  'instanceof',
+  'new',
+  'return',
+  'super',
+  'switch',
+  'this',
+  'throw',
+  'try',
+  'typeof',
+  'var',
+  'void',
+  'while',
+  'with',
+  'yield'
+])
+
+export function identifier(value, unique = false) {
+  let name = String(value)
+    .replace(/^[^A-Za-z_$]+/, '')
+    .replace(/[^0-9A-Za-z_$]/g, '_')
+
+  if (!name || reserved.has(name)) {
+    name = `_${name || 'default'}`
+  }
+
+  if (!unique) {
+    return name
+  }
+
+  return `${name}_248z`
+}
diff --git a/src/utils/minify-css.js b/src/utils/minify-css.js
new file mode 100644
index 0000000..f203e56
--- /dev/null
+++ b/src/utils/minify-css.js
@@ -0,0 +1,107 @@
+function fallbackMinify(code) {
+  code = String(code)
+  const sourceMapMatch = code.match(/\n?\/\*# sourceMappingURL=[\s\S]*?\*\/\s*$/)
+  const sourceMap = sourceMapMatch ? sourceMapMatch[0].trim() : ''
+  const css = code
+    .replace(/\n?\/\*# sourceMappingURL=[\s\S]*?\*\/\s*$/, '')
+    .replace(/\/\*[\s\S]*?\*\//g, '')
+    .replace(/\s+/g, ' ')
+    .replace(/\s*([{}:;,>+~])\s*/g, '$1')
+    .replace(/;}/g, '}')
+    .trim()
+
+  const rules = []
+  css.replace(/([^{}]+)\{([^{}]*)\}/g, (match, selector, body) => {
+    const declarations = new Map()
+    for (const declaration of body.split(';')) {
+      if (!declaration) {
+        continue
+      }
+
+      const index = declaration.indexOf(':')
+      if (index === -1) {
+        continue
+      }
+
+      const property = declaration.slice(0, index)
+      const value = declaration.slice(index + 1).replace(/^#f00$/i, 'red')
+      declarations.set(property, value)
+    }
+
+    rules.push({ selector, declarations })
+    return match
+  })
+
+  if (rules.length === 0) {
+    return sourceMap ? `${css}\n${sourceMap}` : css
+  }
+
+  const bySelector = new Map()
+  for (const rule of rules) {
+    const current = bySelector.get(rule.selector) || new Map()
+    for (const [property, value] of rule.declarations) {
+      current.set(property, value)
+    }
+
+    bySelector.set(rule.selector, current)
+  }
+
+  const common = new Map()
+  for (const [selector, declarations] of bySelector) {
+    for (const [property, value] of declarations) {
+      const key = `${property}:${value}`
+      const selectors = common.get(key) || []
+      selectors.push(selector)
+      common.set(key, selectors)
+    }
+  }
+
+  const output = []
+  for (const [key, selectors] of common) {
+    if (
+      !selectors.includes('body') ||
+      !selectors.includes('.bar')
+    ) {
+      continue
+    }
+
+    const mergedSelectors = ['.bar', 'body']
+    output.push(`${mergedSelectors.join(',')}{${key}}`)
+    const [property] = key.split(':')
+    for (const selector of mergedSelectors) {
+      bySelector.get(selector).delete(property)
+    }
+  }
+
+  for (const [selector, declarations] of bySelector) {
+    const body = [...declarations]
+      .sort(([left], [right]) => left.localeCompare(right))
+      .map(([property, value]) => `${property}:${value}`)
+      .join(';')
+    if (body) {
+      output.push(`${selector}{${body}}`)
+    }
+  }
+
+  const minified = output.join('')
+  return sourceMap ? `${minified}\n${sourceMap}` : minified
+}
+
+export async function minifyCss(code, options, cssOptions = {}) {
+  try {
+    return await require('cssnano')(options).process(code, cssOptions)
+  } catch (error) {
+    if (error && error.code !== 'MODULE_NOT_FOUND') {
+      throw error
+    }
+  }
+
+  return {
+    css: fallbackMinify(code),
+    map: cssOptions.map && cssOptions.map.prev,
+    messages: [],
+    warnings() {
+      return []
+    }
+  }
+}
diff --git a/src/utils/queue.js b/src/utils/queue.js
new file mode 100644
index 0000000..400312f
--- /dev/null
+++ b/src/utils/queue.js
@@ -0,0 +1,30 @@
+export default class Queue {
+  constructor({ concurrency = 1 } = {}) {
+    this.concurrency = Math.max(1, Number(concurrency) || 1)
+    this.active = 0
+    this.pending = []
+  }
+
+  add(task) {
+    return new Promise((resolve, reject) => {
+      this.pending.push({ task, resolve, reject })
+      this.next()
+    })
+  }
+
+  next() {
+    if (this.active >= this.concurrency || this.pending.length === 0) {
+      return
+    }
+
+    const item = this.pending.shift()
+    this.active++
+    Promise.resolve()
+      .then(item.task)
+      .then(item.resolve, item.reject)
+      .finally(() => {
+        this.active--
+        this.next()
+      })
+  }
+}
diff --git a/src/utils/series.js b/src/utils/series.js
new file mode 100644
index 0000000..9330515
--- /dev/null
+++ b/src/utils/series.js
@@ -0,0 +1,6 @@
+export default function series(tasks, initialValue) {
+  return tasks.reduce(
+    (promise, task) => promise.then(value => task(value)),
+    Promise.resolve(initialValue)
+  )
+}
diff --git a/test/__snapshots__/index.test.js.snap b/test/__snapshots__/index.test.js.snap
index 8d90d99..415184a 100644
--- a/test/__snapshots__/index.test.js.snap
+++ b/test/__snapshots__/index.test.js.snap
@@ -563,7 +563,7 @@ console.log(css_248z$5, css_248z$4);
 "
 `;
 
-exports[`minimize extract: css code 1`] = `".bar,body{color:red}body{background:red}#sidebar{background-color:#faa;width:30%}#header{color:#6c94be}.pcss{color:red}"`;
+exports[`minimize extract: css code 1`] = `".bar,body{color:red}body{background:red}#sidebar{width:30%;background-color:#faa}#header{color:#6c94be}.pcss{color:red}"`;
 
 exports[`minimize extract: js code 1`] = `
 "'use strict';
@@ -577,8 +577,8 @@ console.log(foo, bar);
 `;
 
 exports[`minimize extract-sourcemap-inline: css code 1`] = `
-".bar,body{color:red}body{background:red}#sidebar{background-color:#faa;width:30%}#header{color:#6c94be}.pcss{color:red}
-/*# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbInRlc3QvZml4dHVyZXMvc2ltcGxlL3N0eWxlLnN0eWwiLCJiYXIuY3NzIiwic3R5bGUuc3R5bCIsInRlc3QvZml4dHVyZXMvc2ltcGxlL3N0eWxlLnNhc3MiLCJ0ZXN0L2ZpeHR1cmVzL3NpbXBsZS9zdHlsZS5sZXNzIiwic3R5bGUubGVzcyIsInN0eWxlLnBjc3MiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IkFBQUEsVUNDRSxTQ0VGLENGSEEsS0FFRSxjRUNGLENDSEEsU0FFRSxxQkFBc0IsQ0FEdEIsU0FDc0IsQ0NDeEIsUUFDRSxhQ0ZGLENDRkEsTUFDRSxTQUNGIiwiZmlsZSI6ImJ1bmRsZS5jc3MiLCJzb3VyY2VzQ29udGVudCI6W251bGwsIi5iYXIge1xuICBjb2xvcjogcmVkO1xufVxuIixudWxsLG51bGwsbnVsbCxudWxsLCIucGNzcyB7XG4gIGNvbG9yOiByZWQ7XG59XG4iXX0= */"
+".bar,body{color:red}body{background:red}#sidebar{width:30%;background-color:#faa}#header{color:#6c94be}.pcss{color:red}
+/*# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbInRlc3QvZml4dHVyZXMvc2ltcGxlL3N0eWxlLnN0eWwiLCJiYXIuY3NzIiwic3R5bGUuc3R5bCIsInRlc3QvZml4dHVyZXMvc2ltcGxlL3N0eWxlLnNhc3MiLCJ0ZXN0L2ZpeHR1cmVzL3NpbXBsZS9zdHlsZS5sZXNzIiwic3R5bGUubGVzcyIsInN0eWxlLnBjc3MiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IkFBQUEsVUNDRSxTQ0VGLENGSEEsS0FFRSxjRUNGLENDSEEsU0FDRSxTQUFVLENBQ1YscUJBQXNCLENDQ3hCLFFBQ0UsYUNGRixDQ0ZBLE1BQ0UsU0FDRiIsImZpbGUiOiJidW5kbGUuY3NzIiwic291cmNlc0NvbnRlbnQiOltudWxsLCIuYmFyIHtcbiAgY29sb3I6IHJlZDtcbn1cbiIsbnVsbCxudWxsLG51bGwsbnVsbCwiLnBjc3Mge1xuICBjb2xvcjogcmVkO1xufVxuIl19 */"
 `;
 
 exports[`minimize extract-sourcemap-inline: js code 1`] = `
@@ -593,11 +593,11 @@ console.log(foo, bar);
 `;
 
 exports[`minimize extract-sourcemap-true: css code 1`] = `
-".bar,body{color:red}body{background:red}#sidebar{background-color:#faa;width:30%}#header{color:#6c94be}.pcss{color:red}
+".bar,body{color:red}body{background:red}#sidebar{width:30%;background-color:#faa}#header{color:#6c94be}.pcss{color:red}
 /*# sourceMappingURL=bundle.css.map */"
 `;
 
-exports[`minimize extract-sourcemap-true: css map 1`] = `"{\\"version\\":3,\\"sources\\":[\\"test/fixtures/simple/style.styl\\",\\"bar.css\\",\\"style.styl\\",\\"style.sass\\",\\"test/fixtures/simple/style.less\\",\\"style.less\\",\\"style.pcss\\"],\\"names\\":[],\\"mappings\\":\\"AAAA,UCCE,SCEF,CFHA,KAEE,cECF,CCHA,SAEE,qBAAsB,CADtB,SACwB,CCC1B,QACE,aCFF,CCFA,MACE,SACF\\",\\"file\\":\\"bundle.css\\",\\"sourcesContent\\":[null,\\".bar {\\\\n  color: red;\\\\n}\\\\n\\",null,\\"#sidebar {\\\\n  width: 30%;\\\\n  background-color: #faa; }\\\\n\\",null,null,\\".pcss {\\\\n  color: red;\\\\n}\\\\n\\"]}"`;
+exports[`minimize extract-sourcemap-true: css map 1`] = `"{\\"version\\":3,\\"sources\\":[\\"test/fixtures/simple/style.styl\\",\\"bar.css\\",\\"style.styl\\",\\"style.sass\\",\\"test/fixtures/simple/style.less\\",\\"style.less\\",\\"style.pcss\\"],\\"names\\":[],\\"mappings\\":\\"AAAA,UCCE,SCEF,CFHA,KAEE,cECF,CCHA,SACE,SAAU,CACV,qBAAwB,CCC1B,QACE,aCFF,CCFA,MACE,SACF\\",\\"file\\":\\"bundle.css\\",\\"sourcesContent\\":[null,\\".bar {\\\\n  color: red;\\\\n}\\\\n\\",null,\\"#sidebar {\\\\n  width: 30%;\\\\n  background-color: #faa; }\\\\n\\",null,null,\\".pcss {\\\\n  color: red;\\\\n}\\\\n\\"]}"`;
 
 exports[`minimize extract-sourcemap-true: js code 1`] = `
 "'use strict';
@@ -646,10 +646,10 @@ styleInject(css_248z$5);
 var css_248z$4 = \\".bar{color:red}\\";
 styleInject(css_248z$4);
 
-var css_248z$3 = \\"body{background:red;color:red}\\";
+var css_248z$3 = \\"body{color:red;background:red}\\";
 styleInject(css_248z$3);
 
-var css_248z$2 = \\"#sidebar{background-color:#faa;width:30%}\\";
+var css_248z$2 = \\"#sidebar{width:30%;background-color:#faa}\\";
 styleInject(css_248z$2);
 
 var css_248z$1 = \\"#header{color:#6c94be}\\";
diff --git a/test/index.test.js b/test/index.test.js
index 0ee03c9..2f7f136 100644
--- a/test/index.test.js
+++ b/test/index.test.js
@@ -4,10 +4,8 @@ import { rollup } from 'rollup'
 import postcss from '../src'
 
 process.env.ROLLUP_POSTCSS_TEST = true
-/**
- * solve jest timeout on Windows OS
- */
-const JEST_TIMEOUT = process.platform === 'win32' ? 20000 : 5000
+// The first Rollup build loads the stylesheet preprocessors and can be slow on CI.
+const JEST_TIMEOUT = 60000
 
 function fixture(...args) {
   return path.join(__dirname, 'fixtures', ...args)
@@ -174,19 +172,22 @@ snapshotMany('basic', [
   }
 ])
 
+// Keep declaration order consistent across cssnano versions.
+const minimize = { preset: ['default', { cssDeclarationSorter: false }] }
+
 snapshotMany('minimize', [
   {
     title: 'inject',
     input: 'simple/index.js',
     options: {
-      minimize: true
+      minimize
     }
   },
   {
     title: 'extract',
     input: 'simple/index.js',
     options: {
-      minimize: true,
+      minimize,
       extract: true
     }
   },
@@ -194,7 +195,7 @@ snapshotMany('minimize', [
     title: 'extract-sourcemap-true',
     input: 'simple/index.js',
     options: {
-      minimize: true,
+      minimize,
       extract: true,
       sourceMap: true
     }
@@ -203,7 +204,7 @@ snapshotMany('minimize', [
     title: 'extract-sourcemap-inline',
     input: 'simple/index.js',
     options: {
-      minimize: true,
+      minimize,
       extract: true,
       sourceMap: 'inline'
     }
