6.0.6-alpha.0 • Published 8 months ago

@trinitymirrordigital/webpack-config v6.0.6-alpha.0

Weekly downloads
-
License
MIT
Repository
bitbucket
Last release
8 months ago

@trinitymirrordigital/webpack-config

Standard Webpack setup for all dragonfly projects.

Includes set up for:

Install

yarn add -D @trinitymirrordigital/webpack-config @trinitymirrordigital/dragonfly-find-files webpack-merge webpack-config-utils

Usage

First create a config.yml with the following data (adjusting as necessary)

You can add anything you want into the config file and it will be returned to you in config object with the webpack default setup. Just be aware that any snake_case will be converted to camelCase.

Below are the list of expected options:

OptiondefaultExample
context'src'base directory see here for details
devServer{}setup for webpack dev sever
public_output_pathN/A(required)output directory see here
public_dev_outputN/A(required)Required to path assets correctly for dev see here
public_root_outputN/A(required)Required to path assets correctly for production see here
extensionsN/AList of file types to process see here
static_assets_extensionsN/AList of static file types to process like images or fonts
caching_hashfalsewhether assets are outputed with caching hash e.g 'name.hash:8.js' or 'name.js'
additional_aliasN/Alist of aliased modules see here
exclude_HTML_assetsN/Aexcludes assets in a html file from processing as regexps see here

Dragonfly set-up example

For config.yml:

default: &default
  public_output_path: 'target/build' # output path
  additional_alias: # Alias for other modules in chameleon-fe otherwise webpack will throw error
    '@trinitymirrordigital/marwood': false
    '@trinitymirrordigital/chameleon-core': false
    '/@trinitymirrordigital/chameleon-core': false
    '@trinitymirrordigital/chameleon-utilities': false
    '/@trinitymirrordigital/chameleon-utilities': false
    '/@trinitymirrordigital/article-service': false
  context: src # root folder for files
  # For finding entry points
  search_pug: 'src/**/*.pug'
  search_scss: 'src/**/!(_)*.scss'
  search_js: 'src/**/*!(.spec).js'
  # Required for production
  asset_properties: 'target/build/assets.properties'
  # Name of dev output (pugs)
  dev_folder: 'dev'
  # Sets default path for dev server
  path_alias: /@DRAGONFLY_STATIC@
  repo_name: 'dragonfly'
  # For output of pug templates for production
  template_folder: 'template'

  static_assets_extensions:
    - .jpg
    - .jpeg
    - .png
    - .gif
    - .tiff
    - .ico
    - .svg
    - .eot
    - .otf
    - .ttf
    - .woff
    - .woff2

  extensions:
    - .mjs
    - .js
    - .js.map
    - .sass
    - .scss
    - .css
    - .css.map
    - .module.sass
    - .module.scss
    - .module.css
    - .pug

development:
  <<: *default
  # Add development specific config here

production:
  <<: *default
  # Add production specific config here

Create in your project a webpack.config.js file then add the following:

const webpackConfig = require('@trinitymirrordigital/webpack-config');
const fileList = require('@trinitymirrordigital/dragonfly-find-files');
const { merge } = require('webpack-merge');
const { getIfUtils } = require('webpack-config-utils');

module.exports = async (env, args) => {
  // Gets standard set up - see https://bitbucket.org/trinitymirror-ondemand/dragonfly-build-tools/src/master/packages/webpack-config/
  const { webpackConfig, config } = await getWebpackConfig(env, args);
  const { assetProperties, context, devFolder, repoName } = config;

  // for production
  const { VERSION } = process.env;
  const version = VERSION || devFolder;
  // Gets entry points see https://bitbucket.org/trinitymirror-ondemand/dragonfly-build-tools/src/master/packages/dragonfly-find-files/
  const { entry } = await fileList(config, version, context, args.mode);
  // For production build
  const { ifProduction } = getIfUtils(env);
  const wc = ifProduction(
    {
      context: resolve(context),
      entry,
      plugins: [
        // add some production specific plugins here ...
      ],
    },
    // Development specific config here
    {
      context: resolve(context),
      entry: { ...entry, 'webpack-hot-middleware/client': 'webpack-hot-middleware/client?reload=true' },
    },
  );
  // merges with default setup
  return merge(webpackConfig, wc);
};

then in package.json add the following:

"scripts": {
  "build": "yarn webpack",
  "webpack": "YAML_CONFIG=webpack/config.yml webpack --config webpack"
},

Cue-web example set-up:

For config.yml

default: &default
  additional_alias: # Alias for other modules
    './Configuration/cue.config.js': false
  exclude_HTML_assets: # Removes assets from cue index.html from webpack processing
    - 'Images/.*'
    - 'Configuration'
    - 'scripts.(.*).js'
    - 'polyfills.(.*).js'
    - 'runtime.(.*).js'
    - 'styles.(.*).css'
    - 'vendor.(.*).js'
    - 'main.(.*).js'
  public_dev_output: https://localhost/webpack/ #path set through proxy
  public_output_path: ./dist # output path
  public_root_path: /cue-web/dist/ # production publicPath
  # Sets up static assets
  static_assets_extensions:
    - .jpg
    - .jpeg
    - .png
    - .gif
    - .tiff
    - .ico
    - .svg
    - .eot
    - .otf
    - .ttf
    - .woff
    - .woff2
  # for resolve assets
  extensions:
    - .mjs
    - .js
    - .jsx
    - .ts
    - .tsx
    - .sass
    - .scss
    - .css
    - .module.sass
    - .module.scss
    - .module.css
    - .png
    - .svg
    - .gif
    - .ico
    - .jpeg
    - .jpg

  source_path: assets # where to find entry points

  ###### for html-webpack-plugin #######
  html_output: '../index.html' # Where cue html is outputted
  html_template: './assets/index.html' # Where to get default cue-html

  ####### For @trinitymirrordigital/ cue-webpack-google-tag-manager-plugin #######
  google_tag_id: GTM-PXC7ST9

  ###### for @trinitymirrordigital/cue-templates-plugin ####
  templates_path: template # Where template code are
  cue_url: https://localhost # dev path for assets
  yaml_output: /etc/escenic/cue-web # where the yaml files are outputted in docker image
  start_up_script: /usr/bin/startup.sh # startup script fired when dev-server starts for @trinitymirrordigital/cue-templates-plugin

development:
  <<: *default
  # Extract and emit a css file
  caching_hash: false # disables cache-busting Hash
  # Reference: https://webpack.js.org/configuration/dev-server/
  dev_server:
    https: false
    http2: false
    host: 0.0.0.0
    hot: true
    port: 3131
    headers:
      Access-Control-Allow-Origin: '*'
      Access-Control-Allow-Methods: 'GET, POST, PUT, DELETE, PATCH, OPTIONS'
      Access-Control-Allow-Headers: 'X-Requested-With, content-type, Authorization'
    static:
      watch:
        aggregate_timeout: 300
        ignored: '**/node_modules/**'
        poll: 1000

production:
  <<: *default
  caching_hash: true # enables cache-busting Hash

For the webpack config:

/* eslint-env node */
const { TemplatePlugin, getFiles } = require('@trinitymirrordigital/cue-templates-plugin');
const webpackSetup = require('@trinitymirrordigital/webpack-config');
const GoogleTagManagerPlugin = require('@trinitymirrordigital/cue-webpack-google-tag-manager-plugin');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const HtmlWebpackHarddiskPlugin = require('html-webpack-harddisk-plugin');
const { merge } = require('webpack-merge');
const { resolve } = require('path');
const webpack = require('webpack');

module.exports = async (env, argv) => {
  const { webpackConfig, config } = await webpackSetup(env, argv);

  const { cueUrl, googleTagId, htmlOutput, htmlTemplate, publicDevOutput, publicOutputPath, publicRootPath, startUpScript, yamlOutput } = config;

  const files = getFiles(config);
  const publicPath = env.development ? publicDevOutput : publicOutputPath;
  return merge(webpackConfig, {
    // stats: 'normal',
    entry: {
      index: resolve('assets/index.js'),
      ...files,
    },
    output: {
      path: resolve(__dirname, '../', 'dist'),
    },
    plugins: [
      new webpack.DefinePlugin({
        VERSION: JSON.stringify(new Date().getTime()),
        ENVIRONMENT: JSON.stringify(argv.mode),
      }),
      new TemplatePlugin({
        cueUrl,
        mode: argv.mode,
        js: publicRootPath,
        publicPath,
        output: yamlOutput,
        shellScript: startUpScript,
      }),
      new HtmlWebpackPlugin({
        filename: htmlOutput,
        alwaysWriteToDisk: true,
        template: resolve(htmlTemplate),
        excludeChunks: Object.keys(files),
        inject: 'body',
        options: {
          minimize: false, //! env.development,
        },
      }),
      // Adds google tag manger to index.html
      new GoogleTagManagerPlugin({
        id: googleTagId,
      }),
      new HtmlWebpackHarddiskPlugin(),
    ],
  });
};

Webpack loaders

A list of loaders included in standard config:

Webpack plugins

A list of plugins included in standard config:

Webpack minimizers (In production only)

A list of minimizers included in standard config:

Copyright (c) 2022 "Reach Shared Services Ltd"

6.0.6-alpha.0

8 months ago

5.4.5

1 year ago

5.4.5-alpha.0

1 year ago

5.3.0

1 year ago

5.4.2

1 year ago

5.4.0

1 year ago

5.4.4-alpha.0

1 year ago

5.1.0

1 year ago

5.0.0-alpha.0

1 year ago

5.2.1

1 year ago

5.2.0

1 year ago

5.0.2

1 year ago

5.0.1

1 year ago

5.0.0

1 year ago

3.7.4

2 years ago

4.0.0-alpha.0

2 years ago

3.6.0

2 years ago

4.0.1

1 year ago

4.0.0

1 year ago

4.0.2

1 year ago

3.4.0

2 years ago

3.4.2

2 years ago

3.4.1

2 years ago

3.5.1

2 years ago

3.5.0

2 years ago

3.0.0

2 years ago

3.1.3

2 years ago

3.1.2

2 years ago

3.1.1

2 years ago

3.1.0

2 years ago

3.1.4

2 years ago

2.0.2-alpha.0

2 years ago

2.0.1

2 years ago

2.0.0

2 years ago

1.6.13-alpha.0

2 years ago

1.6.11-alpha.0

2 years ago

1.6.16-alpha.0

2 years ago

1.6.12-alpha.0

2 years ago

1.6.14-alpha.0

2 years ago

1.6.9-alpha.0

2 years ago

1.6.10-alpha.0

2 years ago

1.6.15-alpha.0

2 years ago

1.6.7-alpha.0

2 years ago

1.6.6-alpha.0

2 years ago

1.6.5-alpha.0

2 years ago

1.6.1

2 years ago

1.6.0

2 years ago

1.5.3

2 years ago

1.5.2

2 years ago

1.4.6-alpha.0

2 years ago

1.4.5

2 years ago

1.5.0

2 years ago

1.4.4

2 years ago

1.4.4-alpha.0

2 years ago

1.4.3-alpha.0

2 years ago

1.4.2-alpha.0

2 years ago

1.4.1-alpha.0

2 years ago

1.4.0-alpha.0

2 years ago

1.3.4-alpha.0

2 years ago

1.3.3-alpha.0

2 years ago

1.3.2-alpha.0

2 years ago

1.3.1-alpha.0

2 years ago

1.3.0

2 years ago

1.2.1-alpha.0

2 years ago

1.2.0-alpha.0

2 years ago

1.1.0

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago

0.15.0

3 years ago

0.14.1

3 years ago

0.14.0

3 years ago

0.13.0

3 years ago

0.12.0

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.3

3 years ago

0.8.2

3 years ago

0.8.1

3 years ago

0.8.0

3 years ago

0.7.0

3 years ago

0.6.0

3 years ago

0.5.1

3 years ago

0.5.0

3 years ago

0.4.1

3 years ago

0.4.0

3 years ago

0.3.0

3 years ago

0.2.2

3 years ago

0.2.1

3 years ago