11.5.2 • Published 3 months ago

@anansi/webpack-config v11.5.2

Weekly downloads
10,990
License
BSD-3-Clause
Repository
github
Last release
3 months ago

Anansi's Webpack Config

CircleCI npm downloads npm version PRs Welcome

A webpack configuration for fast development and production ready optimizations.

Currently supports webpack 4 and 5.

Usage

/webpack.config.js

const { makeConfig } = require('@anansi/webpack-config');

const options = {
  basePath: 'src',
  buildDir: 'generated_assets/',
};

module.exports = { options };

module.exports = makeConfig(options);

/package.json

{
  "scripts": {
    "start:dev": "webpack serve --mode=development",
    "build": "webpack --mode=production",
    "build:server": "webpack --mode=production --target=node",
    "analyze": "webpack --mode=production --env analyze",
    "profile": "webpack --mode=production --env profile",
    "pkgcheck": "webpack --env check=nobuild",
  }
}

Storybook 6

/.storybook/webpack.config.js

const { makeStorybookConfigGenerator } = require('@anansi/webpack-config');
const { options } = require('../webpack.config');

module.exports = makeStorybookConfigGenerator(options);

Enabling react-refresh

Install react-refresh as a dev-dependency in your project and it will automatically be detected and enabled. Be sure to use the anansi babel or include react-refresh/babel in your own babel configuration.

yarn add --dev react-refresh

CMD line arguments

analyze

If set, will build a static treemap visualization of your packages. Highly recommended to run in production mode to get accurate results.

webpack --mode=production --env analyze

check

If set will run package checks to check for duplicates or ciruclar dependencies. Set equal to 'nobuild' for a standalone run where build output is not needed.

Examples: webpack --mode=production --env check or webpack --env check=nobuild

profile

If set, will enable React DevTools Profiler. This feature is only available in production mode since it is enabled in development by default.

webpack --mode=production --env profile

readable

Skips minification. This is useful when trying to debug production code.

webpack --mode=production --env readable

target

To target node instead of default of web. This is useful when building a node server for SSR alongside the client bundle.

webpack --mode=production --target=node

ENV customization

WEBPACK_PUBLIC_HOST = ''

Sets domain of served files. This is useful to distinguish between different build environments that will serve assets from different locations like a CDN.

Serves as first half of publicPath

Note: dev mode sets its own path based assets serving is completely controlled by webpack-dev-server.

WEBPACK_PUBLIC_PATH = '/'

Forms the second half of the publicPath. Can be useful when assets are served in subdirectories as opposed to custom domains like in the case of CDNs.

Options

Pass these to makeConfig.

libraryInclude/libraryExclude

Regex to match libraries to include in the normal build process. This is useful for locally developed modules or yarn workspaces. Not this should point to the installed location, rather than the actual target. Note you'll need both the positive and negative regexes.

libraryExclude defaults to /node_modules/, which will exclude libraries from expensive and potentially incorrect processing from babel loaders.

To match all libraries in namespace @myspacespace:

const myConfig = makeConfig({
  libraryInclude: /node_modules\/(@mynamespace\/)/,
  libraryExclude: /node_modules(?!\/(@mynamespace\/))/,
});

basePath = 'src'

Marks the base directory inside the package for javascript source files. This is used to make it easy to import from the root.

Example:

-package.json
-/src
  -/components
  -/pages
  -/utils
    -network.js

Then you can do

import fetch from 'network';

from any file.

babelRoot = \$CWD

babelRoot should be where the root babel configuration file is in your repo. Usually this is CWD, but while setting up a monorepo with multiple babel configurations, you may need to change this value.

rootPath = \$CWD

Root path should be the root path of your project. Usually where your package.json and webpack.config.js are. This defaults to the current working directory you are running commands from. However, if you need to run things from another directory, you can send __dirname into this option from your webpack.config.js.

buildDir = 'generated_assets/'

Output directory for production build files

serverDir: 'server_assets/'

Output directory for production server builds. Used when using --target=node cli option.

mode: argv?.mode || process.env.NODE_ENV

Override the mode

bundleAnalyzerOptions

Customize how to analyze your bundles

hardCacheOptions = false

Customize how hard-source-webpack-plugin works. Set to {} or other options to enable plugin. This plugin often speeds up development, but can sometimes cause compatibility issues. Because of this, it is disabled by default.

manifestFilename = 'manifest.json'

Determines the filename for the stats file that includes mappings to all filenames.

babelLoader

Override any babel loader specific options.

extraJsLoaders = []

Any extra loaders to use on JavaScript/TypeScript files.

cssModuleOptions

Customize css module options.

sassResources

resources option from https://github.com/shakacode/sass-resources-loader#readme

This is useful to specify global variables and mixins to be included in every sass file.

File Support

  • SCSS with CSS modules
    • Use ${basePath}/style/export.scss to add variables or mixins avaiable in all scss files
    • Put global styles within ${basePath}/style
    • Other styles will be treated as css modules
  • Web workers
  • All font formats
  • Any media files
    • svg|png|jpg|avif|gif|ico|webp|otf|eot|woff2|woff|ttf|pdf|mp4|webm|wav|mp3|m4a|aac|oga as file urls anywhere
    • svgs imported in javascript/typescript can be used as either components or file urls
  • Raw string data: (md|txt) as a string (using raw-loader)
import starUrl, { ReactComponent as Star } from './star.svg'

const App = () => (
  <div>
    <img src={starUrl} alt="star" />
    <Star />
  </div>
)
  • Javascript & TypeScript

Working with TypeScript

Add @anansi/webpack-config/types to the types in tsconfig.json

{
  "compilerOptions": {
    "types": ["@anansi/webpack-config/types"],
  }
}

This makes all imports of supported formats typed correctly, including svgs, media files and workers.

e.g.,

import plain from './plain.css';
import Worker from './my.worker.ts';
import angleDownUrl, {
  ReactComponent as AngleDown,
} from './angle-down-solid.svg';

worker.postMessage({ message: 'rendered' });

export default function MyComponent() {
  return (
    <>
      <AngleDown className={plain.svg} />
      <img src={angleDownUrl} />
    </>
  );
}

Working with Linaria

  1. Install linaria: yarn add --dev linaria
  2. Use extraJsLoaders option, to add the loader.
const myConfig = makeConfig({
  extraJsLoaders: [
    {
      loader: 'linaria/loader',
      options: {
        sourceMap: argv?.mode !== 'production',
      },
    }
  ]
});
  1. Add linaria/babel to babel presets.
module.exports = {
  presets: [
    ['@anansi/babel-preset', { typing: 'typescript' }],
    'linaria/babel',
  ],
};
11.5.1

3 months ago

11.5.2

3 months ago

11.5.0

3 months ago

11.3.11

3 months ago

11.3.12

3 months ago

11.4.2

3 months ago

11.4.3

3 months ago

11.4.0

3 months ago

11.4.1

3 months ago

11.3.10

4 months ago

11.3.9

4 months ago

11.3.3

5 months ago

11.3.4

5 months ago

11.3.2

5 months ago

11.3.7

4 months ago

11.3.8

4 months ago

11.3.5

4 months ago

11.3.1

5 months ago

10.0.5

6 months ago

10.0.6

6 months ago

10.0.7

6 months ago

10.0.8

6 months ago

10.0.9

6 months ago

10.0.0

7 months ago

10.0.1

7 months ago

10.0.2

7 months ago

10.0.3

6 months ago

10.0.4

6 months ago

10.0.12

6 months ago

10.0.11

6 months ago

10.0.10

6 months ago

11.0.0

6 months ago

9.3.0

8 months ago

11.2.4

6 months ago

11.2.2

6 months ago

11.2.3

6 months ago

11.2.0

6 months ago

11.2.1

6 months ago

11.1.5

6 months ago

11.1.6

6 months ago

11.1.3

6 months ago

11.1.4

6 months ago

11.1.1

6 months ago

11.1.2

6 months ago

11.1.0

6 months ago

11.3.0

6 months ago

9.2.0

9 months ago

9.1.3

10 months ago

9.1.2

10 months ago

9.1.1

10 months ago

9.1.0

11 months ago

9.0.7

11 months ago

9.0.6

11 months ago

9.0.5

11 months ago

9.0.4

11 months ago

9.0.3

11 months ago

9.0.2

11 months ago

9.0.1

12 months ago

9.0.0

12 months ago

8.0.4

12 months ago

8.0.3

1 year ago

8.0.1

1 year ago

8.0.2

1 year ago

8.0.0

1 year ago

7.4.6

1 year ago

7.4.4

1 year ago

7.4.5

1 year ago

7.4.3

1 year ago

7.4.2

1 year ago

7.4.1

1 year ago

7.4.0

1 year ago

7.3.0

1 year ago

7.2.2

1 year ago

7.2.1

1 year ago

7.2.0

1 year ago

7.1.8

1 year ago

7.1.7

1 year ago

7.1.6

1 year ago

7.1.5

1 year ago

7.1.4

1 year ago

7.1.3

1 year ago

7.1.2

1 year ago

7.1.1

1 year ago

7.1.0

1 year ago

7.0.5

1 year ago

7.0.4

1 year ago

7.0.3

1 year ago

7.0.2

1 year ago

7.0.1

1 year ago

6.7.6

1 year ago

6.7.7

1 year ago

6.8.1

1 year ago

6.8.0

1 year ago

6.8.3

1 year ago

6.8.2

1 year ago

6.8.4

1 year ago

7.0.0

1 year ago

6.5.0

1 year ago

6.6.0

1 year ago

6.7.0

1 year ago

6.7.2

1 year ago

6.7.1

1 year ago

6.7.4

1 year ago

6.7.3

1 year ago

6.7.5

1 year ago

6.3.5

1 year ago

6.4.1

1 year ago

6.4.0

1 year ago

6.5.2

1 year ago

6.5.1

1 year ago

6.5.4

1 year ago

6.5.3

1 year ago

6.5.6

1 year ago

6.5.5

1 year ago

6.3.4

1 year ago

6.3.3

1 year ago

6.3.2

1 year ago

6.3.1

1 year ago

6.3.0

1 year ago

6.2.12

1 year ago

6.2.11

1 year ago

6.2.10

1 year ago

6.2.9

1 year ago

6.2.8

1 year ago

6.2.7

1 year ago

6.2.6

1 year ago

6.2.5

1 year ago

6.2.4

1 year ago

6.2.1

1 year ago

6.2.0

1 year ago

6.2.3

1 year ago

6.2.2

1 year ago

6.1.6

1 year ago

6.1.4

1 year ago

6.1.5

1 year ago

6.1.3

1 year ago

6.1.2

1 year ago

6.1.0

1 year ago

6.1.1

1 year ago

6.0.0-beta.5

1 year ago

6.0.0-beta.6

1 year ago

5.4.1

1 year ago

5.4.0

1 year ago

6.0.0-rc.0

1 year ago

6.0.0

1 year ago

5.3.0

1 year ago

5.2.4

1 year ago

5.2.3

1 year ago

5.2.2

1 year ago

5.2.1

1 year ago

6.0.0-beta.4

1 year ago

5.2.0

1 year ago

5.1.14

1 year ago

5.1.13

1 year ago

6.0.0-beta.3

1 year ago

5.1.12

1 year ago

5.1.11

1 year ago

6.0.0-beta.2

1 year ago

5.1.10

2 years ago

5.1.9

2 years ago

5.1.8

2 years ago

5.1.7

2 years ago

5.1.6

2 years ago

5.1.5

2 years ago

6.0.0-beta.1

2 years ago

5.1.4

2 years ago

5.1.3

2 years ago

5.1.2

2 years ago

6.0.0-beta.0

2 years ago

5.1.1

2 years ago

5.1.0

2 years ago

5.0.0

2 years ago

4.2.5

2 years ago

4.2.4

2 years ago

4.2.3

2 years ago

4.2.2

2 years ago

4.2.1

2 years ago

4.2.0

2 years ago

4.1.2

2 years ago

4.1.1

2 years ago

4.1.0

2 years ago

4.0.1

2 years ago

4.0.0

2 years ago

3.0.1

2 years ago

3.0.0

2 years ago

2.1.1

2 years ago

2.1.0

2 years ago

2.0.4

2 years ago

2.0.3

2 years ago

2.0.2

2 years ago

2.0.1

2 years ago

2.0.0

2 years ago

1.0.6

2 years ago

1.0.5

2 years ago

1.0.4

2 years ago

1.0.3

2 years ago

1.0.2

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago

0.20.2

2 years ago

0.20.1

2 years ago

0.20.0

2 years ago

0.19.8

2 years ago

0.19.7

2 years ago

0.19.6

2 years ago

0.19.5

2 years ago

0.19.4

3 years ago

0.19.3

3 years ago

0.19.2

3 years ago

0.19.1

3 years ago

0.19.0

3 years ago

0.18.1

3 years ago

0.18.0

3 years ago

0.17.6

3 years ago

0.17.5

3 years ago

0.17.4

3 years ago

0.17.3

3 years ago

0.17.0

3 years ago

0.16.5

3 years ago

0.16.3

3 years ago

0.16.2

3 years ago

0.16.1

3 years ago

0.16.0

3 years ago

0.15.0

3 years ago

0.14.1

3 years ago

0.14.0

3 years ago

0.13.2

3 years ago

0.13.1

3 years ago

0.13.0

3 years ago

0.12.1

3 years ago

0.12.0

3 years ago

0.11.3

3 years ago

0.11.2

3 years ago

0.11.1

3 years ago

0.11.0

3 years ago

0.10.1

3 years ago

0.10.0

3 years ago

0.9.0

3 years ago

0.8.4

3 years ago

0.8.3

3 years ago

0.8.2

3 years ago

0.8.1

3 years ago

0.8.0

3 years ago

0.7.5

3 years ago

0.7.4

3 years ago

0.7.3

3 years ago

0.7.2

3 years ago

0.7.1

3 years ago

0.7.0

3 years ago

0.6.3

3 years ago

0.6.2

3 years ago

0.6.2-alpha.0

3 years ago

0.6.1

3 years ago

0.6.0

3 years ago

0.5.4

3 years ago

0.5.3

3 years ago

0.5.2

3 years ago

0.5.0

3 years ago

0.4.5

3 years ago

0.4.4

3 years ago

0.4.3

4 years ago

0.4.2

4 years ago

0.4.1

4 years ago

0.4.0

4 years ago

0.3.4

4 years ago

0.3.4-alpha.2

4 years ago

0.3.4-alpha.1

4 years ago

0.3.4-alpha.0

4 years ago

0.3.3

4 years ago

0.3.2

4 years ago

0.3.1

4 years ago

0.3.1-alpha.3

4 years ago

0.3.1-alpha.2

4 years ago

0.3.1-alpha.1

4 years ago

0.3.1-alpha.0

4 years ago

0.3.0

4 years ago

0.2.16-alpha.2

4 years ago

0.2.16-alpha.1

4 years ago

0.2.16-alpha.0

4 years ago

0.2.15

4 years ago

0.2.14

4 years ago

0.2.13

4 years ago

0.2.12

4 years ago

0.2.11

4 years ago

0.2.10

4 years ago

0.2.9

4 years ago

0.2.8

4 years ago

0.2.7

4 years ago

0.2.6

4 years ago

0.2.5

4 years ago

0.2.4

4 years ago

0.2.3

4 years ago

0.2.2

4 years ago

0.2.1

4 years ago

0.2.0

4 years ago

0.1.4

4 years ago

0.1.3

4 years ago

0.1.2

4 years ago

0.1.1

4 years ago

0.1.0

4 years ago

0.0.5-beta.0

4 years ago

0.0.5

4 years ago

0.0.4

4 years ago

0.0.3

4 years ago

0.0.2

4 years ago

0.0.1

4 years ago