项目作者: leegeunhyeok

项目描述 :
Inject webpack assets list into your HTML template
高级语言: TypeScript
项目地址: git://github.com/leegeunhyeok/inject-assets-list-webpack-plugin.git
创建时间: 2020-11-09T04:12:05Z
项目社区:https://github.com/leegeunhyeok/inject-assets-list-webpack-plugin

开源协议:MIT License

下载


npm
node
npm





Inject Assets List Webpack Plugin


Inject assets list into your HTML template


Install

  1. npm i --save-dev inject-assets-list-webpack-plugin
  1. yarn add --dev inject-assets-list-webpack-plugin

Usage

The plugin will generate an JS array for you that includes all your webpack
assets(RawSource) in the <head> using <script> tags. Just add the plugin to your webpack
config as follows:

webpack.config.js

  1. // !! HtmlWebpackPlugin required
  2. const HtmlWebpackPlugin = require('html-webpack-plugin');
  3. const InjectAssetsListWebpackPlugin = require('inject-assets-list-webpack-plugin');
  4. module.exports = {
  5. entry: 'index.js',
  6. publicPath: '/',
  7. output: {
  8. path: __dirname + '/dist',
  9. filename: 'bundle.js',
  10. },
  11. plugins: [new HtmlWebpackPlugin(), new InjectAssetsListWebpackPlugin()],
  12. };

This will generate a file dist/index.html containing the following

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8" />
  5. <title>Webpack App</title>
  6. <script type="text/javascript">
  7. var __assets = [
  8. '/img/apple.707709ec.png',
  9. '/img/banana.51a48343.png',
  10. /* Webpack assets */
  11. ];
  12. </script>
  13. </head>
  14. <body>
  15. <script src="bundle.js"></script>
  16. </body>
  17. </html>

Assets list value format: <publicPath>name.ext (eg. /img/banana.51a48343.png)

Options

You can pass a hash of configuration options to inject-assets-list-webpack-plugin.
Allowed values are as follows:

Name Type Default Description
name {String} __assets The name to use for the global variable
allowPattern {RegExp} undefined Regular expression for allow assets name
ignorePattern {RegExp} undefined Regular expression for ignoring assets

Here’s an example webpack config illustrating how to use these options

webpack.config.js

  1. module.exports = {
  2. entry: 'index.js',
  3. publicPath: '/',
  4. output: {
  5. path: __dirname + '/dist',
  6. filename: 'index.js',
  7. },
  8. plugins: [
  9. new HtmlWebpackPlugin(),
  10. new InjectAssetsListWebpackPlugin({
  11. name: 'myAssets',
  12. allowPattern: /(png|jpg)/, // Allow `png`, `jpg`
  13. ignorePattern: /(gif|ttf)/, // ignoring `gif`, `ttf` files
  14. }),
  15. ],
  16. };

Sample assets

  1. assets
  2. ├─ img
  3. ├── apple.png
  4. ├── animation.gif
  5. └── banana.png
  6. ├─ font
  7. └── my-font.ttf
  8. └─ content
  9. ├── post_1.jpg
  10. ├── post_2.jpg
  11. └── post_3.jpg

Result

  1. // in <script>
  2. var myAssets = [
  3. '/img/apple.707709ec.png',
  4. '/img/banana.51a48343.png',
  5. '/content/post_1.6b60786f.jpg',
  6. '/content/post_2.26053162.jpg',
  7. '/content/post_3.a416371c.jpg',
  8. ];

You can uses assets list list like this.

  1. function preload() {
  2. Promise.allSettled(myAssets.map((uri) => fetch(uri))).then(() => {
  3. console.log(`${myAssets.length} resource(s) loaded.`);
  4. });
  5. }
  6. preload(); // 5 resource(s) loaded.

Development

  1. # Install dependencies
  2. npm i
  3. # Build module
  4. npm run build

Changelog

  • 1.0.3 (2020.11.09)
    • Remove RawSource filtering logic
  • 1.0.2 (2020.11.09)
    • FIXED: Combine default options at initialization
    • Update README.md
  • 1.0.1 (2020.11.09)
    • Add allowPattern option
    • Change name property to optional
  • 1.0.0 (2020.11.09)
    • First Release!

Contributors

This project exists thanks to all the people who contribute.

You’re free to contribute to this project by submitting issues and/or pull requests.