From 1e9512f673813d1338f25e455a2a9905ab4b6790 Mon Sep 17 00:00:00 2001 From: Joe Haddad Date: Fri, 6 Dec 2019 16:56:11 -0500 Subject: [PATCH 1/8] WIP --- packages/next/build/webpack-config.ts | 248 +----------------- .../next/build/webpack/config/blocks/base.ts | 25 ++ .../build/webpack/config/blocks/css/index.ts | 172 ++++++++++++ .../webpack/config/blocks/css/messages.ts | 11 + .../webpack/config/blocks/css/plugins.ts | 102 +++++++ packages/next/build/webpack/config/helpers.ts | 22 ++ packages/next/build/webpack/config/index.ts | 54 ++++ packages/next/build/webpack/config/utils.ts | 19 ++ packages/next/package.json | 2 + yarn.lock | 47 ++-- 10 files changed, 449 insertions(+), 253 deletions(-) create mode 100644 packages/next/build/webpack/config/blocks/base.ts create mode 100644 packages/next/build/webpack/config/blocks/css/index.ts create mode 100644 packages/next/build/webpack/config/blocks/css/messages.ts create mode 100644 packages/next/build/webpack/config/blocks/css/plugins.ts create mode 100644 packages/next/build/webpack/config/helpers.ts create mode 100644 packages/next/build/webpack/config/index.ts create mode 100644 packages/next/build/webpack/config/utils.ts diff --git a/packages/next/build/webpack-config.ts b/packages/next/build/webpack-config.ts index d416100c7fba1..a7af7353e583b 100644 --- a/packages/next/build/webpack-config.ts +++ b/packages/next/build/webpack-config.ts @@ -1,4 +1,3 @@ -import chalk from 'chalk' import crypto from 'crypto' import ForkTsCheckerWebpackPlugin from 'fork-ts-checker-webpack-plugin' import MiniCssExtractPlugin from 'mini-css-extract-plugin' @@ -6,7 +5,6 @@ import path from 'path' // @ts-ignore: Currently missing types import PnpWebpackPlugin from 'pnp-webpack-plugin' import webpack from 'webpack' - import { DOT_NEXT_ALIAS, NEXT_PROJECT_ROOT, @@ -14,15 +12,14 @@ import { PAGES_DIR_ALIAS, } from '../lib/constants' import { fileExists } from '../lib/file-exists' -import { findConfig } from '../lib/find-config' import { resolveRequest } from '../lib/resolve-request' import { CLIENT_STATIC_FILES_RUNTIME_MAIN, CLIENT_STATIC_FILES_RUNTIME_POLYFILLS, CLIENT_STATIC_FILES_RUNTIME_WEBPACK, REACT_LOADABLE_MANIFEST, - SERVER_DIRECTORY, SERVERLESS_DIRECTORY, + SERVER_DIRECTORY, } from '../next-server/lib/constants' import { findPageFile } from '../server/lib/find-page-file' import { WebpackEntrypoints } from './entries' @@ -31,6 +28,7 @@ import { PluginMetaData, VALID_MIDDLEWARE, } from './plugins/collect-plugins' +import { build as buildConfiguration } from './webpack/config' // @ts-ignore: JS file import { pluginLoaderOptions } from './webpack/loaders/next-plugin-loader' import BuildManifestPlugin from './webpack/plugins/build-manifest-plugin' @@ -93,105 +91,6 @@ function getOptimizedAliases(isServer: boolean): { [pkg: string]: string } { } } -async function getPostCssPlugins(dir: string): Promise { - function load(plugins: { [key: string]: object | false }): unknown[] { - return Object.keys(plugins) - .map(pkg => { - const options = plugins[pkg] - if (options === false) { - return false - } - - const pluginPath = resolveRequest(pkg, `${dir}/`) - - if (options == null || Object.keys(options).length === 0) { - return require(pluginPath) - } - return require(pluginPath)(options) - }) - .filter(Boolean) - } - - const config = await findConfig<{ plugins: { [key: string]: object } }>( - dir, - 'postcss' - ) - - let target: unknown[] - - if (!config) { - target = load({ - [require.resolve('postcss-flexbugs-fixes')]: {}, - [require.resolve('postcss-preset-env')]: { - autoprefixer: { - // Disable legacy flexbox support - flexbox: 'no-2009', - }, - // Enable CSS features that have shipped to the - // web platform, i.e. in 2+ browsers unflagged. - stage: 3, - }, - }) - } else { - const plugins = config.plugins - if (plugins == null || typeof plugins !== 'object') { - throw new Error( - `Your custom PostCSS configuration must export a \`plugins\` key.` - ) - } - - const invalidKey = Object.keys(config).find(key => key !== 'plugins') - if (invalidKey) { - console.warn( - `${chalk.yellow.bold( - 'Warning' - )}: Your PostCSS configuration defines a field which is not supported (\`${invalidKey}\`). ` + - `Please remove this configuration value.` - ) - } - - // These plugins cannot be enabled by the user because they'll conflict with - // `css-loader`'s behavior to make us compatible with webpack. - ;[ - 'postcss-modules-values', - 'postcss-modules-scope', - 'postcss-modules-extract-imports', - 'postcss-modules-local-by-default', - ].forEach(plugin => { - if (!plugins.hasOwnProperty(plugin)) { - return - } - - console.warn( - `${chalk.yellow.bold('Warning')}: Please remove the ${chalk.underline( - plugin - )} plugin from your PostCSS configuration. ` + - `This plugin is automatically configured by Next.js.` - ) - delete plugins[plugin] - }) - - // Next.js doesn't support CSS Modules yet. When we do, we should respect the - // options passed to this plugin (even though we need to remove the plugin - // itself). - if (plugins['postcss-modules']) { - delete plugins['postcss-modules'] - - console.warn( - `${chalk.yellow.bold( - 'Warning' - )}: Next.js does not support CSS Modules (yet). The ${chalk.underline( - 'postcss-modules' - )} plugin will have no effect.` - ) - } - - target = load(plugins as { [key: string]: object }) - } - - return target -} - export default async function getBaseWebpackConfig( dir: string, { @@ -504,15 +403,7 @@ export default async function getBaseWebpackConfig( customAppFile = path.resolve(path.join(pagesDir, customAppFile)) } - const postCssPlugins: unknown[] = config.experimental.css - ? await getPostCssPlugins(dir) - : [] - let webpackConfig: webpack.Configuration = { - devtool, - mode: webpackMode, - name: isServer ? 'server' : 'client', - target: isServer ? 'node' : 'web', externals: !isServer ? undefined : !isServerless @@ -741,7 +632,6 @@ export default async function getBaseWebpackConfig( }, // @ts-ignore this is filtered module: { - strictExportPresence: true, rules: [ config.experimental.ampBindInitData && !isServer && { @@ -760,132 +650,6 @@ export default async function getBaseWebpackConfig( }, use: defaultLoaders.babel, }, - config.experimental.css && - // Support CSS imports - ({ - oneOf: [ - { - test: /\.css$/, - issuer: { include: [customAppFile].filter(Boolean) }, - use: isServer - ? // Global CSS is ignored on the server because it's only needed - // on the client-side. - require.resolve('ignore-loader') - : [ - // During development we load CSS via JavaScript so we can - // hot reload it without refreshing the page. - dev && { - loader: require.resolve('style-loader'), - options: { - // By default, style-loader injects CSS into the bottom - // of . This causes ordering problems between dev - // and prod. To fix this, we render a