All Projects → yoriiis → chunks-webpack-plugin

yoriiis / chunks-webpack-plugin

Licence: MIT license
Create HTML files with entrypoints and chunks relations to serve your bundles

Programming Languages

javascript
184084 projects - #8 most used programming language
typescript
32286 projects
CSS
56736 projects

Projects that are alternatives of or similar to chunks-webpack-plugin

Webpack Mpa
Multiple page application setup with Webpack using SASS, PostCSS, ES6+, etc
Stars: ✭ 33 (+50%)
Mutual labels:  webpack-configuration
Preact Minimal
🚀 Minimal preact structure
Stars: ✭ 136 (+518.18%)
Mutual labels:  webpack-configuration
Webpack Chain
A chaining API to generate and simplify the modification of Webpack configurations.
Stars: ✭ 2,821 (+12722.73%)
Mutual labels:  webpack-configuration
Angular Cli Webpack
Webpack configuration modifier for @angular/cli
Stars: ✭ 72 (+227.27%)
Mutual labels:  webpack-configuration
Webpack Core Usage
webpack2完整系列课程,欢迎阅读。同时欢迎移步我的react全家桶文章全集: https://github.com/liangklfangl/react-article-bucket
Stars: ✭ 94 (+327.27%)
Mutual labels:  webpack-configuration
React Webpack4 Cook
💯The most powerful webpack4 tutorial in the universe
Stars: ✭ 152 (+590.91%)
Mutual labels:  webpack-configuration
Awesome Cms Core
Awesome CMS Core is an open source CMS built using ASP.Net Core & ReactJS with module seperation concern in mind and provide lastest trend of technology like .Net Core, React, Webpack, SASS, Background Job, Message Queue.
Stars: ✭ 352 (+1500%)
Mutual labels:  webpack-configuration
Frontend Webpack Boilerplate
Simple starter webpack 5 project template supporting SASS/PostCSS, Babel ES7, browser syncing, code linting. Easy project setup having multiple features and developer friendly tools.
Stars: ✭ 242 (+1000%)
Mutual labels:  webpack-configuration
Serverless Webpack
Serverless plugin to bundle your lambdas with Webpack
Stars: ✭ 1,595 (+7150%)
Mutual labels:  webpack-configuration
Gulp Webpack Starter
Gulp Webpack Starter - fast static website builder. The starter uses gulp toolkit and webpack bundler. Download to get an awesome development experience!
Stars: ✭ 199 (+804.55%)
Mutual labels:  webpack-configuration
Agency Webpack Mix Config
👨‍💻 A capable website/webapp boilerplate ready for the web agency battlefield. Creates a static site with Twig templating by default. Supports Craft/Wordpress/Laravel after a few adjustments.
Stars: ✭ 83 (+277.27%)
Mutual labels:  webpack-configuration
Angular Librarian
An Angular 2+ scaffolding setup for creating libraries
Stars: ✭ 92 (+318.18%)
Mutual labels:  webpack-configuration
Webpack Config Handbook
Minimum Webpack config handbook & examples
Stars: ✭ 165 (+650%)
Mutual labels:  webpack-configuration
Vscode Ts Webpack Node Debug Example
VSCode TypeScript Webpack Node Debug Example
Stars: ✭ 66 (+200%)
Mutual labels:  webpack-configuration
Wpk
a friendly, intuitive & intelligent CLI for webpack
Stars: ✭ 232 (+954.55%)
Mutual labels:  webpack-configuration
Easywebpack
A Simple, Powerful Webpack Front-End Development Solution
Stars: ✭ 452 (+1954.55%)
Mutual labels:  webpack-configuration
Sharejs
💻js小技巧、react、webpack、redux、javascript及其它前端干货,持续更新ing
Stars: ✭ 141 (+540.91%)
Mutual labels:  webpack-configuration
Maps
TGM Maps used on the Warzone server (2017 - 2022).
Stars: ✭ 22 (+0%)
Mutual labels:  chunks
Webpack Config
Helps to load, extend and merge webpack configs
Stars: ✭ 244 (+1009.09%)
Mutual labels:  webpack-configuration
Baumeister
👷 The aim of this project is to help you to build your things. From Bootstrap themes over static websites to single page applications.
Stars: ✭ 171 (+677.27%)
Mutual labels:  webpack-configuration

ChunksWebpackPlugin

GitHub Workflow Status (branch) Coverage Status Mutation testing badge Npm downloads

The ChunksWebpackPlugin creates HTML files with entry points and chunks relations to serve your webpack bundles. It is suitable with multi-page applications that contain multiple entry points.

Since webpack 4, SplitChunksPlugin offers the possibility to optimizes all chunks. It can be particularly powerful, because it means that chunks can be shared even between async and non-async chunks. See the webpack documentation of splitChunks.chunks for details.

splitChunks.chunks option can be set to automatically generate new chunks associated with an entry point. For example, entry points a.js and b.js share common code with the file vendors~a~b.js.

With multiple entry points, it can be difficult to identify relation between the auto-generated chunks and entry points.

ChunksWebpackPlugin parses the entrypoints Map from the webpack compilation to get all valid entry points and associated files. Then, it generates HTML files which include all assets filtered by an entry point and the chunks-manifest.json file.

Zero configuration

It works without configuration. For advanced usage, see the using configuration section.

Installation

ChunksWebpackPlugin is available on npm as chunks-webpack-plugin and as chunks-webpack-plugin on GitHub.

npm install chunks-webpack-plugin --save-dev
yarn add chunks-webpack-plugin --dev

Environment

ChunksWebpackPlugin was built for Node.js LTS 14 and webpack 5.

Example

The project includes a minimalist example in the ./example directory. Run the npm run build:example command to execute the Webpack example and see ChunksWebpackPlugin implementation in action with SplitChunks.

Basic usage

ChunksWebpackPlugin will generate two HTML files for each entry point. Each filename contains the entry point name, the {{entry}} placeholder is automatically replaced.

  • {{entry}}-styles.html: contains all HTML <link> tags
  • {{entry}}-scripts.html: contains all HTML <script> tags

First, let's add the plugin to the webpack configuration.

const ChunksWebpackPlugin = require('chunks-webpack-plugin');
const path = require('path');

module.exports = {
  entry: 'main.js',
  output: {
    filename: 'bundle.js',
    path: path.resolve(__dirname, './dist')
  },
  plugins: [new ChunksWebpackPlugin()]
};

HTML files are built in the output path directory with the rest of the webpack output.

Now you can include the generated HTML files into your HTML page templates. You can do it with e.g. Twig.

main-styles.html

<link rel="stylesheet" href="main.css" />

main-scripts.html

<script src="main.js"></script>

Using a configuration

You can pass a configuration object to ChunksWebpackPlugin to override the default settings.

filename

string = '[name]-[type].html'

Tells the plugin whether to personalize the filename of the generated files. Files are processed by the webpack compilation and generated in the output path directory.

💡 [name] is automatically replaced by the entrypoint name and [type] by styles|scripts.

The filename can contain directories, which will be created automatically.

new ChunksWebpackPlugin({
  filename: 'templates/[name]-[type].html'
});

templateStyle

string = '<link rel="stylesheet" href="{{chunk}}" />'

Tells the plugin whether to personalize the default template for the HTML <style> tags. For example, add additional attributes or a CDN prefix.

new ChunksWebpackPlugin({
  templateStyle: '<link rel="stylesheet" href="https://cdn.domain.com/{{chunk}}" />'
});

💡 Keep the {{chunk}} placeholder, it is automatically replaced by the concatenation of the webpack public path and the chunk filename.

templateScript

string = '<script src="{{chunk}}"></script>'

Tells the plugin whether to personalize the default template for the HTML <script> tags. For example, add additional attributes or a CDN prefix.

new ChunksWebpackPlugin({
  templateScript: '<script defer src="{{chunk}}"></script>'
});

💡 Keep the {{chunk}} placeholder, it is automatically replaced by the concatenation of the webpack public path and the chunk filename.

outputPath

string = null

Tells the plugin whether to personalize the output path of generated files (need absolute path). By default the plugin will use options.output.path from the Webpack configuration.

💡 Can be used to generate files outside the webpack output path directory. With this option, files are not processed by the webpack compilation.

new ChunksWebpackPlugin({
  outputPath: path.resolve(__dirname, `./templates`)
});

customFormatTags

boolean: false function (chunksSorted, Entrypoint) => object

Tells the plugin whether to personalize the default behavior for generating your own templates. The function is called for each entry point. Can be used to add a custom behavior for a specific entry point.

new ChunksWebpackPlugin({
  customFormatTags: (chunksSorted, Entrypoint) => {
    // Generate all HTML style tags with a CDN prefix
    const styles = chunksSorted.styles
      .map((chunkCss) => `<link rel="stylesheet" href="https://cdn.domain.com/${chunkCss}" />`)
      .join('');

    // Generate all HTML style tags with CDN prefix and defer attribute
    const scripts = chunksSorted.scripts
      .map((chunkJs) => `<script defer src="https://cdn.domain.com/${chunkJs}"></script>`)
      .join('');

    return { styles, scripts };
  }
});

💡 The function provides more flexibility by replacing the default behavior. Follow the example above to make sure it works.

The function must return an object with the following format:

return {
  styles: '',
  scripts: ''
};

chunksSorted

object

The list of the chunks sorted by type: styles and scripts.

Entrypoint

object

The object is included in every single ChunkGroup. The variable contains all information about the current entry point; log it on the console for more details.

💡 Use this variable only for a full customization if the chunksSorted variable does not meet your needs.

generateChunksManifest

boolean = false

Tells the plugin whether to generate the chunks-manifest.json. The file contains the list of all chunks grouped by entry points.

new ChunksWebpackPlugin({
  generateChunksManifest: true
});

Example of the output of the chunks-manifest.json file:

{
  "app-a": {
    "styles": ["/dist/css/vendors~app-a~app-b.css", "/dist/css/app-a.css"],
    "scripts": ["/dist/js/vendors~app-a~app-b.js", "/dist/js/app-a.js"]
  },
  "app-b": {
    "styles": ["/dist/css/vendors~app-a~app-b.css", "/dist/css/app-b.css"],
    "scripts": ["/dist/js/vendors~app-a~app-b.js", "/dist/js/app-b.js"]
  }
}

generateChunksFiles

boolean = true

Tells the plugin whether to generate the HTML files.

new ChunksWebpackPlugin({
  generateChunksFiles: false
});

💡 When set to false, HTML files will not be generated. It can only be useful together with generateChunksManifest option set to true for custom generation of the HTML files.

Caching

The webpack caching feature allows you to generate HTML files that include hash in the filename.

const ChunksWebpackPlugin = require('chunks-webpack-plugin');
const path = require('path');

module.exports = {
  entry: 'main.js',
  output: {
    filename: 'bundle.[contenthash].js',
    path: path.resolve(__dirname, './dist')
  },
  plugins: [new ChunksWebpackPlugin()]
};

This will generate the following HTML files with hash in the filename.

main-styles.html

<link rel="stylesheet" href="main.72dd90acdd3d4a4a1fd4.css" />

main-scripts.html

<script src="main.72dd90acdd3d4a4a1fd4.js"></script>

Multiple entry points

Example of the webpack configuration with multiple entry points which share common code with the splitChunks option.

const ChunksWebpackPlugin = require('chunks-webpack-plugin');
const path = require('path');

module.exports = {
  entry: {
    home: 'home.js',
    news: 'news.js'
  },
  output: {
    filename: 'bundle.js',
    path: path.resolve(__dirname, './dist')
  },
  plugins: [new ChunksWebpackPlugin()],
  optimization: {
    splitChunks: {
      chunks: 'all'
    }
  }
};

The plugin will generate all files in the ./dist/ directory:

home-styles.html

<link rel="stylesheet" href="vendors~home~news.css" /> <link rel="stylesheet" href="home.css" />

home-scripts.html

<script src="vendors~home~news.js"></script>
<script src="home.js"></script>

news-styles.html

<link rel="stylesheet" href="vendors~home~news.css" /> <link rel="stylesheet" href="news.css" />

news-scripts.html

<script src="vendors~home~news.js"></script>
<script src="news.js"></script>

Licence

ChunksWebpackPlugin is licensed under the MIT License.

Created with by @yoriiis.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].