Skip to content

Commit

Permalink
fix: emit url-loader assets to public folder
Browse files Browse the repository at this point in the history
  • Loading branch information
atanasster committed Jul 7, 2020
1 parent 5712ba3 commit d77775c
Show file tree
Hide file tree
Showing 8 changed files with 59 additions and 22 deletions.
4 changes: 4 additions & 0 deletions core/webpack-compile/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@ export const compile = ({
webPack,
presets,
configPath,
outputFolder,
}: CompileProps): Promise<CompileResults> => {
return runCompiler((compiler, callback) => compiler.run(callback), {
webPack,
mode: 'production',
presets,
configPath,
outputFolder,
});
};

Expand All @@ -27,6 +29,7 @@ export const watch = ({
presets,
configPath,
watchOptions,
outputFolder,
}: WatchProps): Promise<CompileResults> => {
return runCompiler(
(compiler, callback) => compiler.watch({ ...watchOptions }, callback),
Expand All @@ -35,6 +38,7 @@ export const watch = ({
mode: 'development',
presets,
configPath,
outputFolder,
},
);
};
3 changes: 3 additions & 0 deletions core/webpack-compile/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ export interface CompileProps {
* path to the configuration file e.g : '.storybook'
*/
configPath?: string;

/** public output folder for the bundle */
outputFolder?: string;
}

/**
Expand Down
3 changes: 2 additions & 1 deletion core/webpack-compile/src/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export type CompileRunProps = CompileProps & {
};

const createConfig = (options: CompileRunProps): webpack.Configuration => {
const { webPack, presets, configPath, mode } = options;
const { webPack, presets, configPath, mode, outputFolder } = options;
const plugins = [
new LoaderPlugin({
config: configPath,
Expand Down Expand Up @@ -48,6 +48,7 @@ const createConfig = (options: CompileRunProps): webpack.Configuration => {
...(webPack || {}),
},
presets,
{ outputFolder },
);

//add all the aliases to avoid double loading of packages
Expand Down
35 changes: 27 additions & 8 deletions core/webpack-configs/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,33 @@ import { react } from './react';
import { instrument } from './instrument';
import { reactDocgen } from './react-docgen';
import { reactDocgenTypescript } from './react-docgen-typescript';
import { RuleOptions, RuleTypes, RuleType } from './types';
import {
RuleOptions,
RuleTypes,
RuleType,
PresetOptions,
PresetType,
} from './types';
export * from './types';

export { WebpackConfiguration };
export const presetsFactory: {
[key: string]: WebpackConfiguration;
[key: string]: PresetType;
} = {
'react-docgen-typescript': reactDocgenTypescript,
'react-docgen': reactDocgen,
instrument,
react,
};

const getConfigredPreset = (name: string, options?: PresetOptions) => {
const preset = presetsFactory[name];
if (typeof preset === 'function') {
return preset(options);
}
return preset;
};

const arrayMerge = (dest: any[], src: any[]) => {
const srcCache = src ? [...src] : [];
const destItems = dest
Expand Down Expand Up @@ -55,17 +69,21 @@ const deepMerge = (dest: any, source: any) => {
* expands the presets into webpack config
* @param presets custom config
*/
export const getWebpackConfig = (presets: RuleTypes): WebpackConfiguration => {
export const getWebpackConfig = (
presets: RuleTypes,
options?: PresetOptions,
): WebpackConfiguration => {
const result: WebpackConfiguration = presets.reduce(
(acc: WebpackConfiguration, config: RuleType) => {
if (typeof config === 'string') {
const f = presetsFactory[config];
return deepMergeWithPresets(acc, f);
const p = getConfigredPreset(config, options);
return deepMergeWithPresets(acc, p);
}
if (config && (config as RuleOptions).name) {
const name = (config as RuleOptions).name;
if (presetsFactory[name]) {
const r: WebpackConfiguration = presetsFactory[name];
const p = getConfigredPreset(name, options);
if (p) {
const r: WebpackConfiguration = p;
const o: WebpackConfiguration = (config as RuleOptions).config;
const merged: WebpackConfiguration[] = deepMergeWithPresets(r, o);
return deepMergeWithPresets(acc, merged);
Expand Down Expand Up @@ -101,10 +119,11 @@ export const deepMergeWebpackConfig = (
export const mergeWebpackConfig = (
webPack?: WebpackConfiguration,
presets?: RuleTypes,
options?: PresetOptions,
): WebpackConfiguration => {
if (!presets) {
return {};
}
const newConfig = getWebpackConfig(presets);
const newConfig = getWebpackConfig(presets, options);
return deepMerge(webPack || {}, newConfig);
};
21 changes: 8 additions & 13 deletions core/webpack-configs/src/react/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Configuration } from 'webpack';
import { PresetType, PresetOptions } from '../types';

export const react: Configuration = {
export const react: PresetType = (options?: PresetOptions) => ({
performance: { hints: false },
module: {
rules: [
Expand All @@ -20,19 +20,14 @@ export const react: Configuration = {
],
},
{
test: /\.(eot|md|svg|ico|jpg|jpeg|png|gif|ttf|woff|woff2|pdf|mp4|web|wav|mp3|m4a|aac|oga)$/,
test: /\.(eot|md|svg|ico|jpg|jpeg|png|gif|ttf|woff|woff2|pdf|mp4|web|wav|mp3|m4a|aac|oga)$/i,
exclude: [/node_modules/],
loader: 'url-loader',
options: {
limit: 25000,
},
},
{
test: /\.(eot|md|svg|ico|jpg|jpeg|png|gif|ttf|woff|woff2|pdf|mp4|web|wav|mp3|m4a|aac|oga)$/,
use: {
loader: 'file-loader',
options: {
name: '[path][name].[hash].[ext]',
},
name: '[name].[hash].[ext]',
publicPath: '/static',
outputPath: options?.outputFolder,
},
},
{
Expand Down Expand Up @@ -82,4 +77,4 @@ export const react: Configuration = {
resolve: {
extensions: ['.ts', '.tsx', '.js', '.jsx'],
},
};
});
7 changes: 7 additions & 0 deletions core/webpack-configs/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,10 @@ export interface RuleOptions {
export type RuleType = string | RuleOptions;

export type RuleTypes = RuleType[];

export interface PresetOptions {
outputFolder?: string;
}

export type PresetCallback = (options?: PresetOptions) => Configuration;
export type PresetType = Configuration | PresetCallback;
3 changes: 3 additions & 0 deletions integrations/gatsby-theme-stories/src/gatsby-node.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import * as path from 'path';
import {
getDocPath,
getStoryPath,
Expand All @@ -24,6 +25,8 @@ exports.createPages = async (
webPack: options.webpack,
presets: defaultPresets,
configPath: options.configPath,
outputFolder:
options.outputFolder || `${path.join(process.cwd(), 'public')}`,
};
const { store } =
process.env.NODE_ENV === 'development'
Expand Down
5 changes: 5 additions & 0 deletions integrations/gatsby-theme-stories/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,9 @@ export interface LoaderOptions {
* presets options that will be passed to the instrumenter.
*/
presets?: RuleTypes;

/**
* output folder for generated bundle
*/
outputFolder?: string;
}

0 comments on commit d77775c

Please sign in to comment.