Skip to content

Commit

Permalink
fix: fixes to webpack upgrade
Browse files Browse the repository at this point in the history
  • Loading branch information
atanasster committed Mar 6, 2021
1 parent b1fb2f1 commit 6417166
Show file tree
Hide file tree
Showing 13 changed files with 205 additions and 129 deletions.
1 change: 1 addition & 0 deletions core/core/src/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export type PresetCallback = (options: BuildProps) => Configuration;
export type PresetType = Configuration | PresetCallback;

export type WebpackLoader =
| 'mini-css-extract-plugin'
| 'css-loader'
| 'postcss-loader'
| 'sass-loader'
Expand Down
3 changes: 2 additions & 1 deletion core/webpack-compile/src/args.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import path from 'path';
import yargs, { Options, Argv } from 'yargs';

export interface CliArgTypes {
Expand Down Expand Up @@ -65,7 +66,7 @@ export const defaultCliArgs: ArgOptions = [
alias: 's',
description: 'static assets folder',
type: 'string',
default: 'dist/static',
default: path.join('dist', 'static'),
},
},
{
Expand Down
13 changes: 9 additions & 4 deletions core/webpack-compile/tests/draft.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,16 @@ expect.addSnapshotSerializer({

describe('test draft', () => {
it('compile', async () => {
const { bundleName } = await compile({
const { store } = await compile({
presets: ['react', 'react-docgen-typescript'],
configPath: path.resolve(__dirname, 'fixtures', 'draft'),
//configPath: path.resolve(__dirname, 'fixtures', 'draft'),
configPath: path.resolve(
__dirname,
'../../../examples',
'starter',
'.config',
),
});
const bundle = require(bundleName);
expect(bundle).toMatchSnapshot();
expect(store).toMatchSnapshot();
}, 50000);
});
6 changes: 4 additions & 2 deletions core/webpack-configs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,13 @@
"@component-controls/store": "^2.12.0",
"babel-loader": "^8.2.2",
"babel-preset-react-app": "^10.0.0",
"core-js": "^3.9.1",
"css-loader": "^5.1.1",
"css-minimizer-webpack-plugin": "^1.2.0",
"core-js": "^3.9.1",
"mini-css-extract-plugin": "^1.3.9",
"file-loader": "^6.2.0",
"less": "^4.1.1",
"less-loader": "^8.0.0",
"optimize-css-assets-webpack-plugin": "^5.0.4",
"path": "^0.12.7",
"postcss-loader": "^5.0.0",
"raw-loader": "^4.0.2",
Expand All @@ -57,7 +57,9 @@
"devDependencies": {
"@babel/preset-env": "^7.13.9",
"@component-controls/ts-markdown-docs": "^2.6.0",
"@types/css-minimizer-webpack-plugin": "^1.1.1",
"@types/jest": "^26.0.10",
"@types/mini-css-extract-plugin": "^1.2.2",
"@types/node": "^14.14.31",
"@types/optimize-css-assets-webpack-plugin": "^5.0.2",
"cross-env": "^5.2.1",
Expand Down
27 changes: 7 additions & 20 deletions core/webpack-configs/src/react/index.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
import path from 'path';
import { Configuration, RuleSetUseItem, WebpackPluginInstance } from 'webpack';
import OptimizeCssAssetsWebpackPlugin from 'optimize-css-assets-webpack-plugin';
import { Configuration, RuleSetUseItem } from 'webpack';
import CssMinimizerPlugin from 'css-minimizer-webpack-plugin';
import MiniCssExtractPlugin from 'mini-css-extract-plugin';
import {
PresetCallback,
BuildProps,
//TODO webpack compatible version:
/*
defCssFileName,
*/
customLoaderOptions,
} from '@component-controls/core';
import { findUpFile } from '@component-controls/core/node-utils';
Expand All @@ -33,12 +30,9 @@ export const react: PresetCallback = (options: BuildProps) => {
}
const result: Configuration = {
plugins: [
//TODO webpack compatible version:
/*
new ExtractCssPlugin({
new MiniCssExtractPlugin({
filename: options.cssFileName || defCssFileName,
}),
*/
],
optimization: { minimizer: [] },
performance: { hints: false },
Expand Down Expand Up @@ -110,7 +104,7 @@ export const react: PresetCallback = (options: BuildProps) => {
exclude: [/node_modules/],
loader: 'url-loader',
options: customLoaderOptions(options, 'url-loader', {
limit: 25000,
limit: 1024 * 24,
name: '[name].[ext]',
publicPath: '/static',
outputPath: path.relative(
Expand Down Expand Up @@ -140,9 +134,9 @@ export const react: PresetCallback = (options: BuildProps) => {
// Creates `style` nodes from JS strings
{
loader: MiniCssExtractPlugin.loader,
options: {
options: customLoaderOptions(options, 'mini-css-extract-plugin', {
publicPath: '',
},
}),
},
{
// Translates CSS into CommonJS
Expand Down Expand Up @@ -210,15 +204,8 @@ export const react: PresetCallback = (options: BuildProps) => {
},
};
if (isProd) {
const optimizeCss: WebpackPluginInstance = new (OptimizeCssAssetsWebpackPlugin as any)(
{
cssProcessorOptions: {
discardComments: { removeAll: true },
},
},
);
result.optimization = {
minimizer: [optimizeCss],
minimizer: [new CssMinimizerPlugin()],
};
}
return result;
Expand Down
6 changes: 5 additions & 1 deletion integrations/gatsby-theme-stories/src/gatsby-node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,11 @@ export const createPagesStatefully = async (
}
await searchIndexing(store);
}
process.env.GATSBY_CC_CSS_FILENAME = getCSSBundleName(store.config);
const cssBundle = getCSSBundleName(store.config);
if (fs.existsSync(cssBundle)) {
const cssStyles = fs.readFileSync(cssBundle, 'utf8');
process.env.GATSBY_CC_CSS = JSON.stringify(cssStyles);
}
}

doneCb(null, null);
Expand Down
6 changes: 2 additions & 4 deletions integrations/gatsby-theme-stories/src/gatsby-ssr.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import fs from 'fs';
import React from 'react';
import { PreRenderHTMLArgs, RenderBodyArgs } from 'gatsby';
import { Helmet } from 'react-helmet';
Expand Down Expand Up @@ -27,9 +26,8 @@ export const onPreRenderHTML = ({
replaceHeadComponents,
}: PreRenderHTMLArgs): void => {
const headComponents = getHeadComponents();
const cssBundle = process.env.GATSBY_CC_CSS_FILENAME;
if (cssBundle && fs.existsSync(cssBundle)) {
const cssStyles = fs.readFileSync(cssBundle, 'utf8');
if (typeof process.env.GATSBY_CC_CSS === 'string') {
const cssStyles = JSON.parse(process.env.GATSBY_CC_CSS);
headComponents.push(<style key="controls-styles">{cssStyles}</style>);
}
//fix issue with facebook ignoring the helmet tags
Expand Down
67 changes: 45 additions & 22 deletions integrations/nextjs-plugin/src/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,12 @@ import {
RuleOptions,
defaultCompileProps,
} from '@component-controls/core';
import { getCSSBundleName } from '@component-controls/core/node-utils';
import {
getBundleName,
getCSSBundleName,
} from '@component-controls/core/node-utils';
import { Store } from '@component-controls/core';
import { mergeBuildConfiguration } from '@component-controls/config';
import { loadStore } from '@component-controls/store';
import { getSiteMap } from '@component-controls/routes';

Expand All @@ -22,39 +26,39 @@ module.exports = ({
configPath,
presets,
staticFolder,
distFolder,
webpack,
...rest
}: BuildProps) => () => {
const userProps: BuildProps = {
bundleName,
configPath,
webpack,
};
const buildConfig: BuildProps = {
...defaultCompileProps,
...{
distFolder: distFolder || path.join(process.cwd(), 'public'),
staticFolder: staticFolder || path.join(process.cwd(), 'dist', 'static'),
},
...userProps,
};
if (presets) {
buildConfig.presets = presets;
}
return {
/**
* we need some async function, to make sure the compilation process is completed
*/
async headers() {
const userProps: BuildProps = {
bundleName,
configPath,
webpack,
};
const config: BuildProps = {
...defaultCompileProps,
...{
distFolder: path.resolve(__dirname),
staticFolder:
staticFolder || path.join(process.cwd(), 'public', 'static'),
...userProps,
},
};
if (presets) {
config.presets = presets;
}
const onBundle: CompilerCallbackFn = async ({ store: loadingStore }) => {
if (loadingStore) {
const store: Store = loadStore(loadingStore, true);
if (process.env.NODE_ENV === 'production') {
if (store.config.siteMap) {
const sitemap = getSiteMap(store);
const sitemapfolder = path.resolve(
config.staticFolder as string,
buildConfig.staticFolder as string,
'..',
);
if (!fs.existsSync(sitemapfolder)) {
Expand All @@ -71,8 +75,8 @@ module.exports = ({
};
const compiler =
process.env.NODE_ENV === 'development'
? watch(config, onBundle)
: compile(config, onBundle);
? watch(buildConfig, onBundle)
: compile(buildConfig, onBundle);
await compiler;

return [];
Expand All @@ -86,7 +90,26 @@ module.exports = ({
'@emotion/babel-plugin',
);
}
return config;
return {
...config,
module: {
...config.module,
rules: [
...(config.module?.rules || []),
{
test: require.resolve('@component-controls/store/controls-store'),
use: {
loader: require.resolve('@component-controls/store/loader.js'),
options: {
bundleFileName: getBundleName(
mergeBuildConfiguration(buildConfig),
),
},
},
},
],
},
};
},

...rest,
Expand Down
6 changes: 1 addition & 5 deletions integrations/nextjs-plugin/src/store.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1 @@
import { Store } from '@component-controls/core';
import { loadStore } from '@component-controls/store';

const bundle = require('./component-controls');
export const store: Store = loadStore(bundle, true);
export { store } from '@component-controls/store/controls-store';
3 changes: 3 additions & 0 deletions misc/storybook-custom-docs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@
"directory": "misc/storybook-custom-docs"
},
"license": "MIT",
"dependencies": {
"@component-controls/core": "^2.10.4"
},
"devDependencies": {
"@component-controls/ts-markdown-docs": "^2.6.0",
"@storybook/addons": "^6.2.0-alpha.29",
Expand Down
57 changes: 43 additions & 14 deletions misc/storybook-custom-docs/src/manager-loader.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,30 @@
import {
WebpackLoaderContext,
LoaderOptions,
} from '@component-controls/core/node-utils';
import { getOptions } from 'loader-utils';

interface DocsOptions {
pages: string[];
pages?: string[];
}
module.exports.default = async function() {
const options: DocsOptions = (getOptions(this) as DocsOptions) || {};
function loader(this: WebpackLoaderContext): any {
const options: DocsOptions = getOptions(this) || {};

const { pages } = options;
const callback = this.async();
const code = `
/* eslint-disable react/display-name */
const React = require('react');
const { addons, types } = require('@storybook/addons');
${pages
.map((file, index) => `const pageConfig_${index} = require("${file}");`)
.join('\n')}
${
pages
? pages
.map(
(file, index) => `const pageConfig_${index} = require("${file}");`,
)
.join('\n')
: ''
}
const { ManagerContainer } = require('./ManagerContainer');
const ADDON_ID = 'CUSTOM_PAGE_ADDON';
Expand Down Expand Up @@ -41,15 +52,33 @@ module.exports.default = async function() {
});
};
const pageConfigs = [];
${pages
.map(
(file, index) =>
`pageConfigs.push(pageConfig_${index}.default || pageConfig_${index});`,
)
.join('\n')}
${
pages
? pages
.map(
(file, index) =>
`pageConfigs.push(pageConfig_${index}.default || pageConfig_${index});`,
)
.join('\n')
: ''
}
registerPages(pageConfigs);
`;
return callback(null, code);
};
if (callback) {
return callback(null, code);
}
return '';
}

export default loader;

/**
* expose public types via declaration merging
*/
// eslint-disable-next-line @typescript-eslint/no-namespace
namespace loader {
// eslint-disable-next-line @typescript-eslint/no-empty-interface
export interface Options extends LoaderOptions {}
}
Loading

0 comments on commit 6417166

Please sign in to comment.