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

Disable CSS Support When Manually Configured #9735

Merged
merged 9 commits into from
Dec 13, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@
"tailwindcss": "1.1.3",
"taskr": "1.1.0",
"tree-kill": "1.2.1",
"typescript": "3.7.2",
"typescript": "3.7.3",
"wait-port": "0.2.2",
"wd": "1.11.3",
"webpack-bundle-analyzer": "3.3.2"
Expand Down
2 changes: 1 addition & 1 deletion packages/create-next-app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
"prompts": "2.1.0",
"rimraf": "3.0.0",
"tar": "4.4.10",
"typescript": "^3.5.3",
"typescript": "3.7.3",
"update-check": "1.5.3",
"validate-npm-package-name": "3.0.0"
}
Expand Down
61 changes: 60 additions & 1 deletion packages/next/build/webpack-config.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import crypto from 'crypto'
import ForkTsCheckerWebpackPlugin from 'fork-ts-checker-webpack-plugin'
import MiniCssExtractPlugin from 'mini-css-extract-plugin'
import path from 'path'
// @ts-ignore: Currently missing types
import PnpWebpackPlugin from 'pnp-webpack-plugin'
Expand Down Expand Up @@ -35,6 +34,7 @@ import BuildManifestPlugin from './webpack/plugins/build-manifest-plugin'
import ChunkNamesPlugin from './webpack/plugins/chunk-names-plugin'
import { CssMinimizerPlugin } from './webpack/plugins/css-minimizer-plugin'
import { importAutoDllPlugin } from './webpack/plugins/dll-import'
import MiniCssExtractPlugin from './webpack/plugins/mini-css-extract-plugin'
import { DropClientPage } from './webpack/plugins/next-drop-client-page-plugin'
import NextEsmPlugin from './webpack/plugins/next-esm-plugin'
import NextJsSsrImportPlugin from './webpack/plugins/nextjs-ssr-import'
Expand Down Expand Up @@ -864,6 +864,65 @@ export default async function getBaseWebpackConfig(
}
}

function canMatchCss(rule: webpack.RuleSetCondition | undefined): boolean {
if (!rule) {
return false
}

const fileName = '/tmp/test.css'

if (rule instanceof RegExp && rule.test(fileName)) {
return true
}

if (typeof rule === 'function') {
try {
if (rule(fileName)) {
return true
}
} catch (_) {}
}

if (Array.isArray(rule) && rule.some(canMatchCss)) {
return true
}

return false
}

if (config.experimental.css) {
const hasUserCssConfig =
webpackConfig.module &&
webpackConfig.module.rules.some(
rule => canMatchCss(rule.test) || canMatchCss(rule.include)
)

if (hasUserCssConfig) {
if (webpackConfig.module?.rules.length) {
// Remove default CSS Loader
webpackConfig.module.rules = webpackConfig.module.rules.filter(
r =>
!(
typeof r.oneOf?.[0]?.options === 'object' &&
r.oneOf[0].options.__next_css_remove === true
)
)
}
if (webpackConfig.plugins?.length) {
// Disable CSS Extraction Plugin
webpackConfig.plugins = webpackConfig.plugins.filter(
p => (p as any).__next_css_remove !== true
)
}
if (webpackConfig.optimization?.minimizer?.length) {
// Disable CSS Minifier
webpackConfig.optimization.minimizer = webpackConfig.optimization.minimizer.filter(
e => (e as any).__next_css_remove !== true
)
}
}
}

// check if using @zeit/next-typescript and show warning
if (
isServer &&
Expand Down
15 changes: 13 additions & 2 deletions packages/next/build/webpack/config/blocks/css/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import curry from 'lodash.curry'
import MiniCssExtractPlugin from 'mini-css-extract-plugin'
import path from 'path'
import webpack, { Configuration } from 'webpack'
import MiniCssExtractPlugin from '../../../plugins/mini-css-extract-plugin'
import { loader } from '../../helpers'
import { ConfigurationContext, ConfigurationFn, pipe } from '../../utils'
import { getCssModuleLocalIdent } from './getCssModuleLocalIdent'
Expand Down Expand Up @@ -71,7 +71,18 @@ export const css = curry(async function css(
return config
}

const fns: ConfigurationFn[] = []
const fns: ConfigurationFn[] = [
loader({
oneOf: [
{
// Impossible regex expression
test: /a^/,
loader: 'noop-loader',
options: { __next_css_remove: true },
},
],
}),
]

const postCssPlugins = await getPostCssPlugins(ctx.rootDirectory)
// CSS Modules support must be enabled on the server and client so the class
Expand Down
2 changes: 2 additions & 0 deletions packages/next/build/webpack/plugins/css-minimizer-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ type CssMinimizerPluginOptions = {
}

export class CssMinimizerPlugin {
__next_css_remove = true

private options: CssMinimizerPluginOptions

constructor(options: CssMinimizerPluginOptions) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import MiniCssExtractPlugin from 'mini-css-extract-plugin'

export default class NextMiniCssExtractPlugin extends MiniCssExtractPlugin {
__next_css_remove = true
}
2 changes: 1 addition & 1 deletion packages/next/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@
"resolve": "1.11.0",
"taskr": "1.1.0",
"text-table": "0.2.0",
"typescript": "3.5.1",
"typescript": "3.7.3",
"unistore": "3.4.1"
},
"engines": {
Expand Down
1 change: 1 addition & 0 deletions packages/next/taskfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ const babelServerOpts = {
],
],
plugins: [
'@babel/plugin-proposal-optional-chaining',
'babel-plugin-dynamic-import-node',
['@babel/plugin-proposal-class-properties', { loose: true }],
],
Expand Down
1 change: 1 addition & 0 deletions test/integration/production-config/next.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ module.exports = withCSS(
// Make sure entries are not getting disposed.
maxInactiveAge: 1000 * 60 * 60,
},
experimental: { css: true },
webpack(config) {
// When next-css is `npm link`ed we have to solve loaders from the project root
const nextLocation = path.join(
Expand Down
2 changes: 0 additions & 2 deletions test/integration/production-config/pages/_app.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import App from 'next/app'
import React from 'react'

import '../styles.css'

export default class MyApp extends App {
render() {
const { Component, pageProps } = this.props
Expand Down
2 changes: 2 additions & 0 deletions test/integration/production-config/pages/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import React, { Component } from 'react'

import '../styles.css'

export default class extends Component {
constructor(props) {
super(props)
Expand Down
18 changes: 4 additions & 14 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -15202,20 +15202,10 @@ typedarray@^0.0.6:
resolved "https://registry.yarnpkg.com/typedarray/-/typedarray-0.0.6.tgz#867ac74e3864187b1d3d47d996a78ec5c8830777"
integrity sha1-hnrHTjhkGHsdPUfZlqeOxciDB3c=

[email protected]:
version "3.5.1"
resolved "https://registry.yarnpkg.com/typescript/-/typescript-3.5.1.tgz#ba72a6a600b2158139c5dd8850f700e231464202"
integrity sha512-64HkdiRv1yYZsSe4xC1WVgamNigVYjlssIoaH2HcZF0+ijsk5YK2g0G34w9wJkze8+5ow4STd22AynfO6ZYYLw==

[email protected]:
version "3.7.2"
resolved "https://registry.yarnpkg.com/typescript/-/typescript-3.7.2.tgz#27e489b95fa5909445e9fef5ee48d81697ad18fb"
integrity sha512-ml7V7JfiN2Xwvcer+XAf2csGO1bPBdRbFCkYBczNZggrBZ9c7G3riSUeJmqEU5uOtXNPMhE3n+R4FA/3YOAWOQ==

typescript@^3.5.3:
version "3.5.3"
resolved "https://registry.yarnpkg.com/typescript/-/typescript-3.5.3.tgz#c830f657f93f1ea846819e929092f5fe5983e977"
integrity sha512-ACzBtm/PhXBDId6a6sDJfroT2pOWt/oOnk4/dElG5G33ZL776N3Y6/6bKZJBFpd+b05F3Ct9qDjMeJmRWtE2/g==
[email protected]:
version "3.7.3"
resolved "https://registry.yarnpkg.com/typescript/-/typescript-3.7.3.tgz#b36840668a16458a7025b9eabfad11b66ab85c69"
integrity sha512-Mcr/Qk7hXqFBXMN7p7Lusj1ktCBydylfQM/FZCk5glCNQJrCUKPkMHdo9R0MTFWsC/4kPFvDS0fDPvukfCkFsw==

uglify-js@^3.1.4:
version "3.5.15"
Expand Down