1.0.12 • Published 4 years ago

webpack-inject-chunk-filename-plugin v1.0.12

Weekly downloads
34
License
ISC
Repository
github
Last release
4 years ago

webpack-inject-chunk-filename-plugin

Webpack Plugin that injects chunks filename to outputs or any files you define.

Install

npm i webpack-inject-chunk-filename-plugin -D

Usage

Inject chunks to another chunk

Inject chunk's filename to outputs. you should make sure each chunks have name, so that the plugin can find it.

Suppose you have two chunks, app and lib. You want to inject chunk lib's output filename to chunk app's output source.

First, add a placeholder such as inject-tag-lib in chunk app's source file

import lib from './lib'
console.log('inject-tag-lib')

Second, configure plugin

const InjectChunkFilenamePlugin = require('webpack-inject-chunk-filename-plugin')

module.exports = {
  context: __dirname,
  entry: {
    app: './example.js' // make sure your chunk have name
  },
  output: {
    libraryTarget: 'umd',
    path: dist,
    filename: 'bundle.js'
  },
  plugins: [
    new InjectChunkFilenamePlugin([ // configure plugin here
      {
        targetChunk: 'app',
        rules: [
          {
            regex: /inject-tag-lib/,
            injectChunk: 'lib'
          }
        ]
      }
    ])
  ],
  optimization: { // your split chunks
    splitChunks: {
      chunks: 'all',
      minSize: 0,
      cacheGroups: {
        lib: {
          name: 'lib', // make sure your chunk have name
          test: /lib/,
          filename: 'lib.[hash:8].js'
        }
      }
    }
  }
}

Last, build and see what you got

In chunk app's output source, inject-tag-lib will be replaced will lib.51be9832.js

...
console.log('lib.51be9832.js')
...

Inject chunks to a template file you want

Using targetTemplate in your config, targetTemplate.entry is your template file's absolute path and targetTemplate.filename is the output filename.

const InjectChunkFilenamePlugin = require('webpack-inject-chunk-filename-plugin')


module.exports = {
  context: __dirname,
  entry: {
    app: './example.js'
  },
  output: {
    libraryTarget: 'umd',
    path: dist,
    filename: 'bundle.[hash:8].js'
  },
  plugins: [
    new InjectChunkFilenamePlugin([
      {
        targetTemplate: {
          entry: resolve('./template.js'),
          filename: 'template.[hash:8].js'
        },
        rules: [
          {
            regex: /inject-tag-lib1/,
            injectChunk: 'lib1'
          },
          {
            regex: /inject-tag-lib2/,
            injectChunk: 'lib2'
          }
        ]
      }
    ])
  ],
  optimization: {
    splitChunks: {
      chunks: 'all',
      minSize: 0,
      cacheGroups: {
        lib1: {
          name: 'lib1',
          test: /lib1/,
          filename: 'lib1.[hash:8].js'
        },
        lib2: {
          name: 'lib2',
          test: /lib2/,
          filename: 'lib2.[hash:8].js'
        }
      }
    }
  }
}

Options

NameTypeDescription
targetChunkstringchunk to be injected filename
targetTemplate.entrystringtemplate file's absolute path
targetTemplate.filenamestringtemplate file's output filename
rulesn.regexobjectregex match chunk placeholder
rulesn.injectChunkstringchunk will be injected
1.0.11

4 years ago

1.0.12

4 years ago

1.0.10

4 years ago

1.0.9

4 years ago

1.0.8

4 years ago

1.0.7

4 years ago

1.0.6

4 years ago

1.0.5

4 years ago

1.0.4

4 years ago

1.0.3

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago