Unverified Commit 61c3db73 authored by Joe Haddad's avatar Joe Haddad Committed by GitHub

Fix minifying inline CSS comments (#19167)

We accidentally regressed back in 9.5 and dropped support for inline CSS comments. PostCSS always parses these as pass-through (and not a syntax error), which can cause problems when minifying.

Browsers do a similar thing and ignore the comments.

To ensure we generate valid CSS, this adds support for stripping the CSS comments from the build.

--- 

Fixes #15589
Closes #17130
parent a32b1f48
import cssnanoSimple from 'cssnano-simple' import cssnanoSimple from 'cssnano-simple'
import postcss from 'postcss' import postcssScss from 'next/dist/compiled/postcss-scss'
import postcss, { Parser } from 'postcss'
import webpack from 'webpack' import webpack from 'webpack'
import sources from 'webpack-sources' import sources from 'webpack-sources'
...@@ -31,6 +32,11 @@ export class CssMinimizerPlugin { ...@@ -31,6 +32,11 @@ export class CssMinimizerPlugin {
...this.options.postcssOptions, ...this.options.postcssOptions,
to: file, to: file,
from: file, from: file,
// We don't actually add this parser to support Sass. It can also be used
// for inline comment support. See the README:
// https://github.com/postcss/postcss-scss/blob/master/README.md#2-inline-comments-for-postcss
parser: (postcssScss as any) as Parser,
} }
let input: string let input: string
......
This source diff could not be displayed because it is too large. You can view the blob instead.
The MIT License (MIT)
Copyright 2013 Andrey Sitnik <andrey@sitnik.ru>
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
{"name":"postcss-scss","main":"scss-syntax.js","author":"Andrey Sitnik <andrey@sitnik.ru>","license":"MIT"}
This diff is collapsed.
...@@ -87,7 +87,7 @@ ...@@ -87,7 +87,7 @@
"node-html-parser": "1.4.9", "node-html-parser": "1.4.9",
"path-browserify": "1.0.1", "path-browserify": "1.0.1",
"pnp-webpack-plugin": "1.6.4", "pnp-webpack-plugin": "1.6.4",
"postcss": "8.1.1", "postcss": "8.1.7",
"process": "0.11.10", "process": "0.11.10",
"prop-types": "15.7.2", "prop-types": "15.7.2",
"raw-body": "2.4.1", "raw-body": "2.4.1",
...@@ -204,6 +204,7 @@ ...@@ -204,6 +204,7 @@
"postcss-flexbugs-fixes": "4.2.1", "postcss-flexbugs-fixes": "4.2.1",
"postcss-loader": "4.0.3", "postcss-loader": "4.0.3",
"postcss-preset-env": "6.7.0", "postcss-preset-env": "6.7.0",
"postcss-scss": "3.0.4",
"recast": "0.18.5", "recast": "0.18.5",
"resolve": "1.11.0", "resolve": "1.11.0",
"semver": "7.3.2", "semver": "7.3.2",
......
...@@ -395,6 +395,21 @@ export async function ncc_postcss_preset_env(task, opts) { ...@@ -395,6 +395,21 @@ export async function ncc_postcss_preset_env(task, opts) {
.target('compiled/postcss-preset-env') .target('compiled/postcss-preset-env')
} }
// eslint-disable-next-line camelcase // eslint-disable-next-line camelcase
externals['postcss-scss'] = 'next/dist/compiled/postcss-scss'
export async function ncc_postcss_scss(task, opts) {
await task
.source(opts.src || relative(__dirname, require.resolve('postcss-scss')))
.ncc({
packageName: 'postcss-scss',
externals: {
postcss: 'postcss',
'postcss/lib/parser': 'postcss/lib/parser',
...externals,
},
})
.target('compiled/postcss-scss')
}
// eslint-disable-next-line camelcase
externals['recast'] = 'next/dist/compiled/recast' externals['recast'] = 'next/dist/compiled/recast'
export async function ncc_recast(task, opts) { export async function ncc_recast(task, opts) {
await task await task
...@@ -584,6 +599,7 @@ export async function ncc(task) { ...@@ -584,6 +599,7 @@ export async function ncc(task) {
'ncc_postcss_flexbugs_fixes', 'ncc_postcss_flexbugs_fixes',
'ncc_postcss_loader', 'ncc_postcss_loader',
'ncc_postcss_preset_env', 'ncc_postcss_preset_env',
'ncc_postcss_scss',
'ncc_recast', 'ncc_recast',
'ncc_resolve', 'ncc_resolve',
'ncc_schema_utils', 'ncc_schema_utils',
......
...@@ -174,6 +174,10 @@ declare module 'next/dist/compiled/semver' { ...@@ -174,6 +174,10 @@ declare module 'next/dist/compiled/semver' {
import m from 'semver' import m from 'semver'
export = m export = m
} }
declare module 'next/dist/compiled/postcss-scss' {
import m from 'postcss-scss'
export = m
}
declare module 'next/dist/compiled/text-table' { declare module 'next/dist/compiled/text-table' {
function textTable( function textTable(
rows: Array<Array<{}>>, rows: Array<Array<{}>>,
......
{
"browserslist": [
"last 1 chrome version"
]
}
import './global.css'
export default function MyApp({ Component, pageProps }) {
return <Component {...pageProps} />
}
// Aaaand we are dead
// These comments are invalid CSS and it crashes the build since Next.js 9.5.0
* {
box-sizing: border-box;
}
export default function () {
return <div></div>
}
...@@ -253,3 +253,35 @@ describe('CSS Modules: Import Exports', () => { ...@@ -253,3 +253,35 @@ describe('CSS Modules: Import Exports', () => {
) )
}) })
}) })
describe('Inline Comments: Minify', () => {
const appDir = join(fixturesDir, 'inline-comments')
let stdout
let code
beforeAll(async () => {
await remove(join(appDir, '.next'))
;({ code, stdout } = await nextBuild(appDir, [], {
stdout: true,
}))
})
it('should have compiled successfully', () => {
expect(code).toBe(0)
expect(stdout).toMatch(/Compiled successfully/)
})
it(`should've emitted a single CSS file`, async () => {
const cssFolder = join(appDir, '.next/static/css')
const files = await readdir(cssFolder)
const cssFiles = files.filter((f) => /\.css$/.test(f))
expect(cssFiles.length).toBe(1)
const cssContent = await readFile(join(cssFolder, cssFiles[0]), 'utf8')
expect(cssContent.replace(/\/\*.*?\*\//g, '').trim()).toMatchInlineSnapshot(
`"*{box-sizing:border-box}"`
)
})
})
...@@ -8111,6 +8111,7 @@ icss-utils@^2.1.0: ...@@ -8111,6 +8111,7 @@ icss-utils@^2.1.0:
icss-utils@^4.0.0, icss-utils@^4.1.1: icss-utils@^4.0.0, icss-utils@^4.1.1:
version "4.1.1" version "4.1.1"
resolved "https://registry.yarnpkg.com/icss-utils/-/icss-utils-4.1.1.tgz#21170b53789ee27447c2f47dd683081403f9a467" resolved "https://registry.yarnpkg.com/icss-utils/-/icss-utils-4.1.1.tgz#21170b53789ee27447c2f47dd683081403f9a467"
integrity sha512-4aFq7wvWyMHKgxsH8QQtGpvbASCf+eM3wPRLI6R+MgAnTCZ6STYsRvttLvRWK0Nfif5piF394St3HeJDaljGPA==
dependencies: dependencies:
postcss "^7.0.14" postcss "^7.0.14"
...@@ -10943,10 +10944,10 @@ nanoid@2.0.3: ...@@ -10943,10 +10944,10 @@ nanoid@2.0.3:
version "2.0.3" version "2.0.3"
resolved "https://registry.yarnpkg.com/nanoid/-/nanoid-2.0.3.tgz#dde999e173bc9d7bd2ee2746b89909ade98e075e" resolved "https://registry.yarnpkg.com/nanoid/-/nanoid-2.0.3.tgz#dde999e173bc9d7bd2ee2746b89909ade98e075e"
nanoid@^3.1.12: nanoid@^3.1.16:
version "3.1.12" version "3.1.16"
resolved "https://registry.yarnpkg.com/nanoid/-/nanoid-3.1.12.tgz#6f7736c62e8d39421601e4a0c77623a97ea69654" resolved "https://registry.yarnpkg.com/nanoid/-/nanoid-3.1.16.tgz#b21f0a7d031196faf75314d7c65d36352beeef64"
integrity sha512-1qstj9z5+x491jfiC4Nelk+f8XBad7LN20PmyWINJEMRSf3wcAjAWysw1qaA8z6NSKe2sjq1hRSDpBH5paCb6A== integrity sha512-+AK8MN0WHji40lj8AEuwLOvLSbWYApQpre/aFJZD71r43wVRLrOYS4FmJOPQYon1TqB462RzrrxlfA74XRES8w==
nanomatch@^1.2.9: nanomatch@^1.2.9:
version "1.2.13" version "1.2.13"
...@@ -12614,6 +12615,7 @@ postcss-modules-extract-imports@^1.2.0: ...@@ -12614,6 +12615,7 @@ postcss-modules-extract-imports@^1.2.0:
postcss-modules-extract-imports@^2.0.0: postcss-modules-extract-imports@^2.0.0:
version "2.0.0" version "2.0.0"
resolved "https://registry.yarnpkg.com/postcss-modules-extract-imports/-/postcss-modules-extract-imports-2.0.0.tgz#818719a1ae1da325f9832446b01136eeb493cd7e" resolved "https://registry.yarnpkg.com/postcss-modules-extract-imports/-/postcss-modules-extract-imports-2.0.0.tgz#818719a1ae1da325f9832446b01136eeb493cd7e"
integrity sha512-LaYLDNS4SG8Q5WAWqIJgdHPJrDDr/Lv775rMBFUbgjTz6j34lUznACHcdRWroPvXANP2Vj7yNK57vp9eFqzLWQ==
dependencies: dependencies:
postcss "^7.0.5" postcss "^7.0.5"
...@@ -12644,6 +12646,7 @@ postcss-modules-scope@1.1.0, postcss-modules-scope@^1.1.0: ...@@ -12644,6 +12646,7 @@ postcss-modules-scope@1.1.0, postcss-modules-scope@^1.1.0:
postcss-modules-scope@^2.2.0: postcss-modules-scope@^2.2.0:
version "2.2.0" version "2.2.0"
resolved "https://registry.yarnpkg.com/postcss-modules-scope/-/postcss-modules-scope-2.2.0.tgz#385cae013cc7743f5a7d7602d1073a89eaae62ee" resolved "https://registry.yarnpkg.com/postcss-modules-scope/-/postcss-modules-scope-2.2.0.tgz#385cae013cc7743f5a7d7602d1073a89eaae62ee"
integrity sha512-YyEgsTMRpNd+HmyC7H/mh3y+MeFWevy7V1evVhJWewmMbjDHIbZbOXICC2y+m1xI1UVfIT1HMW/O04Hxyu9oXQ==
dependencies: dependencies:
postcss "^7.0.6" postcss "^7.0.6"
postcss-selector-parser "^6.0.0" postcss-selector-parser "^6.0.0"
...@@ -12658,6 +12661,7 @@ postcss-modules-values@1.3.0, postcss-modules-values@^1.3.0: ...@@ -12658,6 +12661,7 @@ postcss-modules-values@1.3.0, postcss-modules-values@^1.3.0:
postcss-modules-values@^3.0.0: postcss-modules-values@^3.0.0:
version "3.0.0" version "3.0.0"
resolved "https://registry.yarnpkg.com/postcss-modules-values/-/postcss-modules-values-3.0.0.tgz#5b5000d6ebae29b4255301b4a3a54574423e7f10" resolved "https://registry.yarnpkg.com/postcss-modules-values/-/postcss-modules-values-3.0.0.tgz#5b5000d6ebae29b4255301b4a3a54574423e7f10"
integrity sha512-1//E5jCBrZ9DmRX+zCtmQtRSV6PV42Ix7Bzj9GbwJceduuf7IqP8MgeTXuRDHOWj2m0VzZD5+roFWDuU8RQjcg==
dependencies: dependencies:
icss-utils "^4.0.0" icss-utils "^4.0.0"
postcss "^7.0.6" postcss "^7.0.6"
...@@ -12912,6 +12916,13 @@ postcss-safe-parser@4.0.2: ...@@ -12912,6 +12916,13 @@ postcss-safe-parser@4.0.2:
dependencies: dependencies:
postcss "^7.0.26" postcss "^7.0.26"
postcss-scss@3.0.4:
version "3.0.4"
resolved "https://registry.yarnpkg.com/postcss-scss/-/postcss-scss-3.0.4.tgz#74ce30ec9de615b41aeab1249e7557698e372e73"
integrity sha512-BAkBZ35aXhCeBRmliHylYqTN1PvNJyh9aBPQHUmk9SdDdbk7n3GExm7cQivDckOgJpB+agyig9TwRAmf6WnvfA==
dependencies:
postcss "^8.1.6"
postcss-selector-matches@^4.0.0: postcss-selector-matches@^4.0.0:
version "4.0.0" version "4.0.0"
resolved "https://registry.yarnpkg.com/postcss-selector-matches/-/postcss-selector-matches-4.0.0.tgz#71c8248f917ba2cc93037c9637ee09c64436fcff" resolved "https://registry.yarnpkg.com/postcss-selector-matches/-/postcss-selector-matches-4.0.0.tgz#71c8248f917ba2cc93037c9637ee09c64436fcff"
...@@ -13063,14 +13074,14 @@ postcss@7.0.32, postcss@^7.0.32: ...@@ -13063,14 +13074,14 @@ postcss@7.0.32, postcss@^7.0.32:
source-map "^0.6.1" source-map "^0.6.1"
supports-color "^6.1.0" supports-color "^6.1.0"
postcss@8.1.1: postcss@8.1.7, postcss@^8.1.6:
version "8.1.1" version "8.1.7"
resolved "https://registry.yarnpkg.com/postcss/-/postcss-8.1.1.tgz#c3a287dd10e4f6c84cb3791052b96a5d859c9389" resolved "https://registry.yarnpkg.com/postcss/-/postcss-8.1.7.tgz#ff6a82691bd861f3354fd9b17b2332f88171233f"
integrity sha512-9DGLSsjooH3kSNjTZUOt2eIj2ZTW0VI2PZ/3My+8TC7KIbH2OKwUlISfDsf63EP4aiRUt3XkEWMWvyJHvJelEg== integrity sha512-llCQW1Pz4MOPwbZLmOddGM9eIJ8Bh7SZ2Oj5sxZva77uVaotYDsYTch1WBTNu7fUY0fpWp0fdt7uW40D4sRiiQ==
dependencies: dependencies:
colorette "^1.2.1" colorette "^1.2.1"
line-column "^1.0.2" line-column "^1.0.2"
nanoid "^3.1.12" nanoid "^3.1.16"
source-map "^0.6.1" source-map "^0.6.1"
postcss@^5.0.10, postcss@^5.0.11, postcss@^5.0.12, postcss@^5.0.13, postcss@^5.0.14, postcss@^5.0.16, postcss@^5.0.2, postcss@^5.0.4, postcss@^5.0.5, postcss@^5.0.8, postcss@^5.2.16: postcss@^5.0.10, postcss@^5.0.11, postcss@^5.0.12, postcss@^5.0.13, postcss@^5.0.14, postcss@^5.0.16, postcss@^5.0.2, postcss@^5.0.4, postcss@^5.0.5, postcss@^5.0.8, postcss@^5.2.16:
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment