Skip to content

Commit

Permalink
fix(react): apply postcss config from apps to their lib dependencies (#…
Browse files Browse the repository at this point in the history
…7265)

Fixes #7040
  • Loading branch information
jaysoo authored Oct 8, 2021
1 parent 4b9c00e commit a8bcd27
Show file tree
Hide file tree
Showing 8 changed files with 70 additions and 40 deletions.
7 changes: 6 additions & 1 deletion packages/react/plugins/storybook/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,12 @@ export const webpack = async (
// ESM build for modern browsers.
const baseWebpackConfig = mergeWebpack([
getBaseWebpackPartial(builderOptions, esm, isScriptOptimizeOn),
getStylesPartial(options.configDir, builderOptions, extractCss),
getStylesPartial(
options.workspaceRoot,
options.configDir,
builderOptions,
extractCss
),
]);

// run it through the React customizations
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ function storybookOptionMapper(
...builderOptions,
mode: 'static',
outputDir: builderOptions.outputPath,
workspaceRoot: context.root,
configDir: storybookConfig,
...frameworkOptions,
frameworkPresets: [...(frameworkOptions.frameworkPresets || [])],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ function storybookOptionMapper(
const optionsWithFramework = {
...builderOptions,
mode: 'dev',
workspaceRoot: context.root,
configDir: storybookConfig,
...frameworkOptions,
frameworkPresets: [...(frameworkOptions.frameworkPresets || [])],
Expand Down
2 changes: 2 additions & 0 deletions packages/web/src/executors/build/build.impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ function getWebpackConfigs(
// ESM build for modern browsers.
getWebConfig(
context.root,
projectRoot,
sourceRoot,
options,
true,
Expand All @@ -100,6 +101,7 @@ function getWebpackConfigs(
isScriptOptimizeOn && buildBrowserFeatures.isDifferentialLoadingNeeded()
? getWebConfig(
context.root,
projectRoot,
sourceRoot,
options,
false,
Expand Down
4 changes: 3 additions & 1 deletion packages/web/src/executors/dev-server/dev-server.impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,16 @@ export default async function* devServerExecutor(
context: ExecutorContext
) {
const { webpack } = require('../../webpack/entry');
const sourceRoot = context.workspace.projects[context.projectName].sourceRoot;
const { root: projectRoot, sourceRoot } =
context.workspace.projects[context.projectName];
const buildOptions = normalizeWebBuildOptions(
getBuildOptions(serveOptions, context),
context.root,
sourceRoot
);
let webpackConfig = getDevServerConfig(
context.root,
projectRoot,
sourceRoot,
buildOptions,
serveOptions
Expand Down
8 changes: 5 additions & 3 deletions packages/web/src/utils/devserver.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,23 @@ import { OptimizationOptions } from './types';
import { readFileSync } from 'fs-extra';

export function getDevServerConfig(
root: string,
workspaceRoot: string,
projectRoot: string,
sourceRoot: string,
buildOptions: WebBuildBuilderOptions,
serveOptions: WebDevServerOptions
) {
const webpackConfig = getWebConfig(
root,
workspaceRoot,
projectRoot,
sourceRoot,
buildOptions,
true, // Don't need to support legacy browsers for dev.
false
);

(webpackConfig as any).devServer = getDevServerPartial(
root,
workspaceRoot,
serveOptions,
buildOptions
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ export function getCommonConfig(wco: WebpackConfigOptions): Configuration {
} = require('../../../../../webpack/entry');
const { ContextReplacementPlugin, debug } = webpack;

const { root, projectRoot, buildOptions, tsConfig } = wco;
const { root, projectRoot, sourceRoot, buildOptions, tsConfig } = wco;
const { styles: stylesOptimization, scripts: scriptsOptimization } =
buildOptions.optimization;
const {
Expand Down Expand Up @@ -226,7 +226,7 @@ export function getCommonConfig(wco: WebpackConfigOptions): Configuration {
sourceMap: scriptsSourceMap,
filename: `${path.basename(bundleName)}${hash}.js`,
scripts: script.paths,
basePath: projectRoot,
basePath: sourceRoot,
})
);
});
Expand Down
83 changes: 50 additions & 33 deletions packages/web/src/utils/web.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
import { getBrowserConfig } from './third-party/cli-files/models/webpack-configs/browser';
import { getCommonConfig } from './third-party/cli-files/models/webpack-configs/common';
import { getStylesConfig } from './third-party/cli-files/models/webpack-configs/styles';
import { basename, resolve, posix } from 'path';
import * as path from 'path';
import { basename, posix, resolve } from 'path';
import { WebBuildBuilderOptions } from '../executors/build/build.impl';
import { convertBuildOptions } from './normalize';
import { readTsConfig } from '@nrwl/workspace/src/utilities/typescript';
Expand All @@ -12,15 +13,23 @@ import { IndexHtmlWebpackPlugin } from './third-party/cli-files/plugins/index-ht
import { generateEntryPoints } from './third-party/cli-files/utilities/package-chunk-sort';
import { ScriptTarget } from 'typescript';
import { getHashDigest, interpolateName } from 'loader-utils';
import postcssImports = require('postcss-import');
import * as path from 'path';
import { sassImplementation } from './sass';
import postcssImports = require('postcss-import');

// TODO(jack): Remove this in Nx 13
type Configuration = any;

// 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;
}

export function getWebConfig(
root,
workspaceRoot,
projectRoot,
sourceRoot,
options: WebBuildBuilderOptions,
esm?: boolean,
Expand All @@ -40,8 +49,9 @@ export function getWebConfig(
tsConfig.options.target = ScriptTarget.ES5;
}
const wco: any = {
root,
projectRoot: resolve(root, sourceRoot),
root: workspaceRoot,
projectRoot: resolve(workspaceRoot, projectRoot),
sourceRoot: resolve(workspaceRoot, sourceRoot),
buildOptions: convertBuildOptions(options),
esm,
console,
Expand All @@ -62,7 +72,12 @@ export function getWebConfig(
esm,
isScriptOptimizeOn
),
getStylesPartial(wco.root, wco.buildOptions, options.extractCss),
getStylesPartial(
wco.root,
wco.projectRoot,
wco.buildOptions,
options.extractCss
),
getCommonPartial(wco),
getBrowserPartial(wco, options, isScriptOptimizeOn),
]);
Expand Down Expand Up @@ -142,7 +157,8 @@ function getCommonPartial(wco: any): Configuration {
}

export function getStylesPartial(
root: string,
workspaceRoot: string,
projectRoot: string,
options: any,
extractCss: boolean
): Configuration {
Expand All @@ -154,11 +170,11 @@ export function getStylesPartial(
if (options?.stylePreprocessorOptions?.includePaths?.length > 0) {
options.stylePreprocessorOptions.includePaths.forEach(
(includePath: string) =>
includePaths.push(path.resolve(root, includePath))
includePaths.push(path.resolve(workspaceRoot, includePath))
);
}

const partial = getStylesConfig(root, options, includePaths);
const partial = getStylesConfig(workspaceRoot, options, includePaths);
const rules = partial.module.rules.map((rule) => {
if (!Array.isArray(rule.use)) {
return rule;
Expand All @@ -184,6 +200,29 @@ export function getStylesPartial(
},
importLoaders: 1,
};
const postcssOptions: PostcssOptions = (loader) => ({
plugins: [
postcssImports({
addModulesDirectories: includePaths,
resolve: (url: string) => (url.startsWith('~') ? url.substr(1) : url),
load: (filename: string) => {
return new Promise<string>((resolve, reject) => {
loader.fs.readFile(filename, (err: Error, data: Buffer) => {
if (err) {
reject(err);

return;
}

const content = data.toString();
resolve(content);
});
});
},
}),
],
});
postcssOptions.config = projectRoot;

const commonLoaders = [
{
Expand All @@ -199,29 +238,7 @@ export function getStylesPartial(
loader: require.resolve('postcss-loader'),
options: {
implementation: require('postcss'),
postcssOptions: (loader) => ({
plugins: [
postcssImports({
addModulesDirectories: includePaths,
resolve: (url: string) =>
url.startsWith('~') ? url.substr(1) : url,
load: (filename: string) => {
return new Promise<string>((resolve, reject) => {
loader.fs.readFile(filename, (err: Error, data: Buffer) => {
if (err) {
reject(err);

return;
}

const content = data.toString();
resolve(content);
});
});
},
}),
],
}),
postcssOptions: postcssOptions,
},
},
];
Expand Down

0 comments on commit a8bcd27

Please sign in to comment.