{"id":13671294,"url":"https://github.com/markshapiro/webpack-merge-and-include-globally","last_synced_at":"2025-04-27T14:33:16.739Z","repository":{"id":19732492,"uuid":"87751211","full_name":"markshapiro/webpack-merge-and-include-globally","owner":"markshapiro","description":"Merge multiple files (js, css..) and include by running them \"as is\". Supports minify by custom transform and wildcard paths.","archived":false,"fork":false,"pushed_at":"2023-01-06T01:56:16.000Z","size":1706,"stargazers_count":103,"open_issues_count":42,"forks_count":26,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-04-19T00:55:13.247Z","etag":null,"topics":["global-variables","minify","webpack"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/markshapiro.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-04-10T00:58:14.000Z","updated_at":"2025-02-16T15:56:36.000Z","dependencies_parsed_at":"2023-01-13T22:15:12.883Z","dependency_job_id":null,"html_url":"https://github.com/markshapiro/webpack-merge-and-include-globally","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/markshapiro%2Fwebpack-merge-and-include-globally","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/markshapiro%2Fwebpack-merge-and-include-globally/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/markshapiro%2Fwebpack-merge-and-include-globally/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/markshapiro%2Fwebpack-merge-and-include-globally/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/markshapiro","download_url":"https://codeload.github.com/markshapiro/webpack-merge-and-include-globally/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":251154426,"owners_count":21544499,"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":["global-variables","minify","webpack"],"created_at":"2024-08-02T09:01:05.341Z","updated_at":"2025-04-27T14:33:16.045Z","avatar_url":"https://github.com/markshapiro.png","language":"JavaScript","funding_links":[],"categories":["JavaScript"],"sub_categories":[],"readme":"# MERGE INTO SINGLE FILE PLUGIN FOR WEBPACK\n\nWebpack plugin to merge your source files together into single file, to be included in index.html, and achieving same effect as you would by including them all separately through `\u003cscript\u003e` or `\u003clink\u003e`.\n\n### Getting Started\n\n```bash\nnpm install --save-dev webpack-merge-and-include-globally\n```\n\n### Usage\n\nLets say you want to make libraries like `jquery`, `moment` (including 3 languages) and `toastr` available globally, and you're struggling to make them global with webpack or just importing them (in cases they aren't written well) because require() wraps the code into new scope and you want to execute it against a global scope, and you don't want to do this:\n```html\n  \u003cscript src=\"/node_modules/jquery/dist/jquery.min.js\"\u003e\u003c/script\u003e\n  \u003cscript src=\"/node_modules/moment/moment.js\"\u003e\u003c/script\u003e\n  \u003cscript src=\"/node_modules/moment/locale/cs.js\"\u003e\u003c/script\u003e\n  \u003cscript src=\"/node_modules/moment/locale/de.js\"\u003e\u003c/script\u003e\n  \u003cscript src=\"/node_modules/moment/locale/nl.js\"\u003e\u003c/script\u003e\n  \u003cscript src=\"/node_modules/toastr/build/toastr.min.js\"\u003e\u003c/script\u003e\n  \n  \u003clink rel=\"stylesheet\" href=\"/node_modules/toastr/build/toastr.min.css\"\u003e\n```\nbecause your `node_modules` is not available in production.\n\u003cbr/\u003ewith this plugin you can achieve the desired effect this way:\n```javascript\n\n  const MergeIntoSingleFilePlugin = require('webpack-merge-and-include-globally');\n  \n  module.exports = {\n    ...\n    plugins: [\n        new MergeIntoSingleFilePlugin({\n            files: {\n                \"vendor.js\": [\n                    'node_modules/jquery/dist/jquery.min.js',\n                    //  will work too\n                    //  'node_modules/jquery/**/*.min.js',\n                    'node_modules/moment/moment.js',\n                    'node_modules/moment/locale/cs.js',\n                    'node_modules/moment/locale/de.js',\n                    'node_modules/moment/locale/nl.js',\n                    'node_modules/toastr/build/toastr.min.js'\n                ],\n                \"vendor.css\": [\n                    'node_modules/toastr/build/toastr.min.css'\n                ]\n            }\n        }),\n    ]\n\n```\nthis generates 2 files with merged js and css content, include them into your `index.html` to take effect:\n``` html\n  \u003cscript src=\"./vendor.js\"\u003e\u003c/script\u003e\n  \u003clink rel=\"stylesheet\" href=\"./vendor.css\"\u003e\n```\nnow `jQuery`, `moment` and `toastr` are available globally throughout your application.\n\n### Options\n\n#### files (as object)\n\nObject that maps file names to array of all files (can also be defined by wildcard path) that will be merged together and saved under each file name.\n\u003cbr/\u003eFor example to merge `jquery`, `classnames` and `humps` into `vendor.js`, do:\n```javascript\nnew MergeIntoSingle({\n  files: {\n    'vendor.js': [\n      'node_modules/jquery/**/*.min.js',\n      'node_modules/classnames/index.js',\n      'node_modules/humps/humps.js'\n    ],\n    'style.css': [\n      'example/test.css'\n    ]\n  }\n})\n```\n\n#### transform\n\nObject that maps resulting file names to tranform methods that will be applied on merged content before saving. Use to minify / uglify the result.\n\u003cbr/\u003eFor example to minify the final merge result of `vendor.js`, do:\n```javascript\nnew MergeIntoSingle({\n  files: { 'vendor.js': [...] },\n  transform: {\n    'vendor.js': code =\u003e require(\"uglify-js\").minify(code).code\n  }\n})\n```\n\n#### files (as array)\n\nAlternative way to specify files as array of `src` \u0026 `dest`, for flexibility to transform and create multiple destination files for same source when you need to generate additional map file for example.\n```javascript\nnew MergeIntoSingle({\n  files: [{\n    src:[\n      'node_modules/jquery/**/*.min.js',\n      'node_modules/classnames/index.js',\n      'node_modules/humps/humps.js'\n    ],\n    dest: code =\u003e {\n      const min = uglifyJS.minify(code, {sourceMap: {\n        filename: 'vendor.js',\n        url: 'vendor.js.map'\n      }});\n      return {\n        'vendor.js':min.code,\n        'vendor.js.map': min.map\n      }\n    },\n\n    // also possible:\n    //\n    // dest: 'vendor.js'\n  },{\n    src: ['example/test.css'],\n    dest: 'style.css'\n\n    // also possible:\n    //\n    // dest: code =\u003e ({\n    //   'style.css':new CleanCSS({}).minify(code).styles\n    // })\n  }]\n})\n```\n\n#### hash\ndefault: false\n\nset `true` to append version hash before file extension.\n\nyou can get names of generated files mapped to original by passing callback function as second argument to plugin: \n```javascript\nnew MergeIntoSingle({ ... }, filesMap =\u003e { ... }),\n```\n\n#### transformFileName\ndefault: undefined\n\nalso you can pass function for change output file name with hash\n```javascript\nnew MergeIntoSingle({\n  ...,\n  transformFileName: (fileNameBase, extension, hash) =\u003e `${fileName}.[${hash}]${extension}`,\n  // bundle.[somehash].js\n}),\n\n//or\n\nnew MergeIntoSingle({\n  ...,\n  transformFileName: (fileNameBase, extension, hash) =\u003e `${fileNameBase}${extension}?hash=${hash}`,\n  // bundle.js?hash=somehash\n}),\n\n```\n\n#### encoding\n\ndefault: 'utf-8'\n\nencoding of node.js reading\n\n#### chunks\n\ndefault: undefined\n\narray of entry points (strings) for which this plugin should run only\n\n#### separator\n\ndefault: '\\n'\n\nstring used between files when joining them together\n\n### Working Example\n\nworking example already included in project.\n\u003cbr/\u003eto test first install `npm i`, then run `npm run start` to see it in action\n\u003cbr/\u003eand `npm run build` to build prod files with vendor file and `index.html`.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmarkshapiro%2Fwebpack-merge-and-include-globally","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmarkshapiro%2Fwebpack-merge-and-include-globally","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmarkshapiro%2Fwebpack-merge-and-include-globally/lists"}