{"id":13671315,"url":"https://github.com/byronbyyuan/modify--chunk-webpack-plugin","last_synced_at":"2025-04-27T14:33:18.328Z","repository":{"id":57300581,"uuid":"147284245","full_name":"byronbyyuan/modify--chunk-webpack-plugin","owner":"byronbyyuan","description":null,"archived":false,"fork":false,"pushed_at":"2018-09-10T02:24:34.000Z","size":66,"stargazers_count":3,"open_issues_count":0,"forks_count":0,"subscribers_count":0,"default_branch":"master","last_synced_at":"2025-04-20T20:33:43.750Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/byronbyyuan.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-09-04T03:37:30.000Z","updated_at":"2020-06-30T09:23:55.000Z","dependencies_parsed_at":"2022-09-09T21:02:32.639Z","dependency_job_id":null,"html_url":"https://github.com/byronbyyuan/modify--chunk-webpack-plugin","commit_stats":null,"previous_names":["14138993/custom-chunk-webpack-plugin"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/byronbyyuan%2Fmodify--chunk-webpack-plugin","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/byronbyyuan%2Fmodify--chunk-webpack-plugin/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/byronbyyuan%2Fmodify--chunk-webpack-plugin/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/byronbyyuan%2Fmodify--chunk-webpack-plugin/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/byronbyyuan","download_url":"https://codeload.github.com/byronbyyuan/modify--chunk-webpack-plugin/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":251154442,"owners_count":21544501,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-08-02T09:01:05.910Z","updated_at":"2025-04-27T14:33:18.025Z","avatar_url":"https://github.com/byronbyyuan.png","language":"JavaScript","funding_links":[],"categories":["JavaScript"],"sub_categories":[],"readme":"# modify-chunk-webpack-plugin\n\n##  作用是什么？\n#### HtmlWebpackPlugin 能很好地帮我们注入打包的入口文件,但我们可能有需要更细粒化操作比如：    \n+ 需要将某个chunk内联。\n+ 需要移动某个chunk的位置，由body移动到head。\n+ 甚至是不需要某个chunk，这种能够自定义操作出口的文件的可能。\n+ 添加一段路径或者内联一段代码.\n#### webpack 出口的publicPath一旦设置,所有的资源都会加上该路径，可能我们需要自定义chunk路径:\n+ 有时候可能说某个出口chunk,必如来自第三方包的出口文件需要开启cdn，但其他的chunk不需要\n  \n## options参数\n| key         | Description(优先级*)    |    value(type)                          |     default                  |\n| :--------:  | :-----------------:     |   :------------------------------------:|   :------------------------:  |\n| basePath    | 添加的路径前缀          |                url                       |    ``                         |\n| cssChunk    | 需要添加chunk名称*      |  chunkName/all/[chunkName]              |    ``                         |\n| jsChunk     | 需要添加chunk名称*      |  chunkName/all/[chunkName]              |    ``                      |         \n| customBase  | 自定义设置路径的函数**   |  回调函数 function(返回值)               |    ``                      |\n| start       | 添加一个文件的路径的位置 |  start/end                               |    end                        |\n| addPath     | 添加文件的对象*         |Object{js/css}                            |     ``                         |\n| customAdd   | 自定义添加文件的函数**  |  回调函数 function                       |    ``                          |\n| InlineChunk | 将chunk变为内联        |  chunkName/[chunkName]                   |    ``                        |\n\n### webpack.config.js\n```\nconst customChunkWebpackPlugin = require('custom-chunk-webpack-plugin')\n{\n  entry: 'index.js',\n  output: {\n    path: __dirname + '/dist',\n    filename: 'index_bundle.js'\n  },\n  plugins: [\n    new customChunkWebpackPlugin({\n      basePath:'http://...',\n      jsChunk:'vendors',\n      cssChunk:'all',\n      InlineChunk:'runtime',\n    })\n  ]\n}\n```\n\n### 添加前缀实例\n``` \n    new customChunkWebpackPlugin({\n      basePath:'http://...',\n      jsChunk:['vendors','app'],\n      cssChunk:'all',\n    })\n```\n``` \n    new customChunkWebpackPlugin({\n      basePath:'http://...',//无效\n      jsChunk:['vendors','app'],//无效\n      cssChunk:'all',//无效\n      customBase:asset =\u003e{ //有效 函数优先级最高\n        asset['js'][0] = 'http://...' + asset['js'][0] \n        return asset\n      },      \n    })\n```\n\n### chunk转内联\n``` \n    new customChunkWebpackPlugin({\n        InlineChunk:'runtime'\n    })\n```\n``` \n    new customChunkWebpackPlugin({\n        InlineChunk:['runtime','app']\n    })\n```\n\n\n### 添加路径\n``` \n    // 这种方式添加的所有都基于start的值来,默认是end 添加到当前已有的chunk后面\n    // js只会添加到body无法移动到head\n    new customChunkWebpackPlugin({\n      start:'start', \n      addPath:{\n        js:'http://cdn.comxxx.js',\n        css:['http://cdn.com/xx/xx.css','/xx/xx.css']\n      },\n    })\n```\n``` \n    //参数head和body都是之后的html标签所需要加载的内容的数组项\n    //你可以通过head.set 或者body.set 的方法来插入你要的内容\n    //如果你需要更自由的方式 那你可以直接操作这两个对象\n    new customChunkWebpackPlugin({\n        //set的方式body与head使用方式相同 以head为例,参数只能是数组 否则无效\n        //插入的位置同样以start来作为位置的标识\n        customAdd:(head,body)=\u003e{\n            head.set([          \n                {\n                    type:'Inline', //存在src的值 也会只处理Inline\n                    innerHTML:'var a = 1', //到这一部的代码是无法再压缩的\n                    tag:'css', //插件不会去检查是否合格，很明显这是错误的 他将生成 style标签 但值是js\n                    src:'http://xxxx.xxx'\n                },\n                {\n                    type:'Inline',\n                    innerHTML:'var a = 1', //到这一部的代码是无法再压缩的\n                    tag:'js',\n                },                          \n                {\n                    tag:'js',\n                    src:'http://xxxx.xxx',\n                },\n                {\n                    start:'start', \n                    tag:'js',\n                    src:'http://xxxx.xxx',            \n                }\n          ])\n        }\n    })\n```\n``` \n    // 自定义操作head和body\n    // 插入节点的模板\n    let cssTemplate = {\n        tagName: 'link',\n        selfClosingTag: false,\n        voidTag: true,\n        attributes:{ href: '', rel: 'stylesheet' }\n    }\n    let jsTemplate = {\n        tagName:'script',\n        closeTag: true,\n        attributes:{ type: 'text/javascript',src: ''}\n    }\n    let InlinejsTemplate = {...this.jsTemplate,attributes:{ type: 'text/javascript' },innerHTML:''}\n    let InlinecssTemplate = {\n        tagName:'style',\n        closeTag: true,\n        attributes:{ type: 'text/css' },\n        innerHTML:''\n    }    \n    new customChunkWebpackPlugin({\n        customAdd:(head,body)=\u003e{\n            head.push({\n                tagName:'script',\n                closeTag: true,\n                attributes:{ type: 'text/javascript',src: ''http://xxxx.xxx''}                \n            })\n        }\n    })\n```\n\n\n### 缺陷 \n\n#### 无法做到异步加载的chunk自定义，只能做到出口的chunk修改，下个版本将考虑实现更全面更细化的控制的可能。\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbyronbyyuan%2Fmodify--chunk-webpack-plugin","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbyronbyyuan%2Fmodify--chunk-webpack-plugin","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbyronbyyuan%2Fmodify--chunk-webpack-plugin/lists"}