diff --git a/packages/react/plugins/component-testing/webpack-fallback.ts b/packages/react/plugins/component-testing/webpack-fallback.ts index 397314e16f08bf..a35a6933c86c5c 100644 --- a/packages/react/plugins/component-testing/webpack-fallback.ts +++ b/packages/react/plugins/component-testing/webpack-fallback.ts @@ -1,6 +1,6 @@ import { TsconfigPathsPlugin } from 'tsconfig-paths-webpack-plugin'; import { Configuration } from 'webpack'; -import { getCSSModuleLocalIdent } from '@nrwl/webpack/src/executors/webpack/lib/get-webpack-config'; +import { getCSSModuleLocalIdent } from '@nrwl/webpack'; export function buildBaseWebpackConfig({ tsConfigPath = 'tsconfig.cy.json', diff --git a/packages/react/plugins/storybook/index.ts b/packages/react/plugins/storybook/index.ts index 38f98ba9b69148..038c2c8b3a638e 100644 --- a/packages/react/plugins/storybook/index.ts +++ b/packages/react/plugins/storybook/index.ts @@ -6,7 +6,6 @@ import { } from '@nrwl/devkit'; import { getBaseWebpackPartial } from '@nrwl/webpack/src/utils/config'; import { NormalizedWebpackExecutorOptions } from '@nrwl/webpack/src/executors/webpack/schema'; -import { getStylesPartial } from '@nrwl/webpack/src/executors/webpack/lib/get-webpack-config'; import { checkAndCleanWithSemver } from '@nrwl/workspace/src/utilities/version-utils'; import { join } from 'path'; import { gte } from 'semver'; @@ -118,18 +117,8 @@ export const webpack = async ( const extractCss = storybookWebpackConfig.mode === 'production'; // ESM build for modern browsers. - const baseWebpackConfig = mergeWebpack.merge([ - getBaseWebpackPartial(builderOptions, { - isScriptOptimizeOn, - skipTypeCheck: true, - }), - getStylesPartial( - options.workspaceRoot, - options.configDir, - builderOptions, - extractCss - ), - ]); + let baseWebpackConfig: Configuration = {}; + baseWebpackConfig = getBaseWebpackPartial(builderOptions); // run it through the React customizations const finalConfig = reactWebpackConfig(baseWebpackConfig); diff --git a/packages/webpack/index.ts b/packages/webpack/index.ts index 57e56c9b3afbc1..1cd12a35512ac2 100644 --- a/packages/webpack/index.ts +++ b/packages/webpack/index.ts @@ -1,3 +1,4 @@ +export * from './src/utils/create-copy-plugin'; export * from './src/utils/config'; export * from './src/generators/init/init'; export * from './src/generators/webpack-project/webpack-project'; @@ -11,3 +12,6 @@ export type { FileReplacement, } from './src/executors/webpack/schema'; export * from './src/executors/webpack/webpack.impl'; +export * from './src/utils/get-css-module-local-ident'; +export * from './src/utils/with-nx'; +export * from './src/utils/with-web'; diff --git a/packages/webpack/src/executors/webpack/lib/get-webpack-config.ts b/packages/webpack/src/executors/webpack/lib/get-webpack-config.ts index 85c5d0c82e1e0f..0646293e7364f8 100644 --- a/packages/webpack/src/executors/webpack/lib/get-webpack-config.ts +++ b/packages/webpack/src/executors/webpack/lib/get-webpack-config.ts @@ -1,28 +1,10 @@ -import * as path from 'path'; -import { posix, resolve } from 'path'; -import { readTsConfig } from '@nrwl/workspace/src/utilities/typescript'; -import { getHashDigest, interpolateName } from 'loader-utils'; import type { Configuration } from 'webpack'; - -import { NormalizedWebpackExecutorOptions } from '../schema'; - -// TODO(jack): These should be inlined in a single function so it is easier to understand -import { getBaseWebpackPartial } from '../../../utils/config'; -import { getBrowserConfig } from '../../../utils/webpack/partials/browser'; -import { getCommonConfig } from '../../../utils/webpack/partials/common'; -import { getStylesConfig } from '../../../utils/webpack/partials/styles'; import { ExecutorContext } from '@nrwl/devkit'; -import MiniCssExtractPlugin = require('mini-css-extract-plugin'); -import webpackMerge = require('webpack-merge'); -import postcssImports = require('postcss-import'); - -// PostCSS options depend on the webpack loader, but we need to set the `config` path as a string due to this check: -// https://github.com/webpack-contrib/postcss-loader/blob/0d342b1/src/utils.js#L36 -interface PostcssOptions { - (loader: any): any; - config?: string; -} +import { NormalizedWebpackExecutorOptions } from '../schema'; +import { withNx } from '../../../utils/with-nx'; +import { withWeb } from '../../../utils/with-web'; +import { composePlugins } from '@nrwl/webpack'; interface GetWebpackConfigOverrides { root: string; @@ -33,285 +15,12 @@ interface GetWebpackConfigOverrides { export function getWebpackConfig( context: ExecutorContext, options: NormalizedWebpackExecutorOptions, - isScriptOptimizeOn?: boolean, - overrides?: GetWebpackConfigOverrides -): Configuration { - const tsConfig = readTsConfig(options.tsConfig); - const workspaceRoot = context.root; - - let sourceRoot: string; - let projectRoot: string; - if (overrides) { - projectRoot = overrides.root; - sourceRoot = overrides.sourceRoot; - } else { - const project = - context.projectsConfigurations.projects[context.projectName]; - projectRoot = project.root; - sourceRoot = project.sourceRoot; - } - - const wco: any = { - root: workspaceRoot, - projectRoot: resolve(workspaceRoot, projectRoot), - sourceRoot: resolve(workspaceRoot, sourceRoot), - buildOptions: convertBuildOptions(options), - console, - tsConfig, - tsConfigPath: options.tsConfig, - }; - // TODO(jack): Replace merge behavior with an inlined config so it is easier to understand. - return webpackMerge.merge([ - _getBaseWebpackPartial( - context, - options, - isScriptOptimizeOn, - tsConfig.options.emitDecoratorMetadata, - overrides - ), - options.target === 'web' - ? getPolyfillsPartial(options.polyfills, isScriptOptimizeOn) - : {}, - options.target === 'web' - ? getStylesPartial( - wco.root, - wco.projectRoot, - wco.buildOptions, - options.extractCss, - options.postcssConfig - ) - : {}, - getCommonPartial(wco), - options.target === 'web' ? getBrowserConfig(wco) : {}, - ]); -} - -function _getBaseWebpackPartial( - context: ExecutorContext, - options: NormalizedWebpackExecutorOptions, - isScriptOptimizeOn: boolean, - emitDecoratorMetadata: boolean, + /** @deprecated Check options object instead */ + _isScriptOptimizeOn?: boolean, + // TODO(jack): The overrides object is needed for component testing (can we set it on context object?) overrides?: GetWebpackConfigOverrides -) { - let partial = getBaseWebpackPartial( - options, - { - isScriptOptimizeOn, - emitDecoratorMetadata, - configuration: overrides?.configuration ?? context.configurationName, - }, - context - ); - delete partial.resolve.mainFields; - return partial; -} - -function getCommonPartial(wco: any): Configuration { - const commonConfig: Configuration = getCommonConfig(wco); - delete commonConfig.entry; - delete commonConfig.resolve.modules; - delete commonConfig.resolve.extensions; - delete commonConfig.output.path; - delete commonConfig.module; - return commonConfig; -} - -export function getStylesPartial( - workspaceRoot: string, - projectRoot: string, - options: any, - extractCss: boolean, - postcssConfig?: string -): Configuration { - const includePaths: string[] = []; - - if (options?.stylePreprocessorOptions?.includePaths?.length > 0) { - options.stylePreprocessorOptions.includePaths.forEach( - (includePath: string) => - includePaths.push(path.resolve(workspaceRoot, includePath)) - ); - } - - const partial = getStylesConfig(workspaceRoot, options, includePaths); - const rules = partial.module.rules.map((rule) => { - if (!Array.isArray(rule.use)) { - return rule; - } - rule.use = rule.use.map((loaderConfig) => { - if ( - typeof loaderConfig === 'object' && - loaderConfig.loader === require.resolve('raw-loader') - ) { - return { - loader: require.resolve('style-loader'), - }; - } - return loaderConfig; - }); - return rule; - }); - - const loaderModulesOptions = { - modules: { - mode: 'local', - getLocalIdent: getCSSModuleLocalIdent, - }, - importLoaders: 1, - }; - const postcssOptions: PostcssOptions = () => ({ - plugins: [ - postcssImports({ - addModulesDirectories: includePaths, - resolve: (url: string) => (url.startsWith('~') ? url.slice(1) : url), - }), - ], - }); - // If a path to postcssConfig is passed in, set it for app and all libs, otherwise - // use automatic detection. - if (typeof postcssConfig === 'string') { - postcssOptions.config = path.join(workspaceRoot, postcssConfig); - } - - const commonLoaders = [ - { - loader: extractCss - ? MiniCssExtractPlugin.loader - : require.resolve('style-loader'), - }, - { - loader: require.resolve('css-loader'), - options: loaderModulesOptions, - }, - { - loader: require.resolve('postcss-loader'), - options: { - implementation: require('postcss'), - postcssOptions: postcssOptions, - }, - }, - ]; - - partial.module.rules = [ - { - test: /\.css$|\.scss$|\.sass$|\.less$|\.styl$/, - oneOf: [ - { - test: /\.module\.css$/, - use: commonLoaders, - }, - { - test: /\.module\.(scss|sass)$/, - use: [ - ...commonLoaders, - { - loader: require.resolve('sass-loader'), - options: { - implementation: require('sass'), - sassOptions: { - fiber: false, - precision: 8, - includePaths, - }, - }, - }, - ], - }, - { - test: /\.module\.less$/, - use: [ - ...commonLoaders, - { - loader: require.resolve('less-loader'), - options: { - lessOptions: { - paths: includePaths, - }, - }, - }, - ], - }, - { - test: /\.module\.styl$/, - use: [ - ...commonLoaders, - { - loader: require.resolve('stylus-loader'), - options: { - stylusOptions: { - include: includePaths, - }, - }, - }, - ], - }, - ...rules, - ], - }, - ]; - return partial; -} - -export function getPolyfillsPartial( - polyfills: string, - isScriptOptimizeOn: boolean ): Configuration { - const config = { - entry: {} as { [key: string]: string[] }, - }; - - if (polyfills && isScriptOptimizeOn) { - // Safari 10.1 supports