{"id":31367,"date":"2024-11-25T15:46:29","date_gmt":"2024-11-25T07:46:29","guid":{"rendered":"https:\/\/fwq.ai\/blog\/31367\/"},"modified":"2024-11-25T15:46:29","modified_gmt":"2024-11-25T07:46:29","slug":"%e5%a6%82%e4%bd%95%e6%93%8d%e4%bd%9cwebpack%e5%a4%84%e7%90%86%e6%96%87%e4%bb%b6","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/31367\/","title":{"rendered":"\u5982\u4f55\u64cd\u4f5cwebpack\u5904\u7406\u6587\u4ef6"},"content":{"rendered":"<p style=\"text-align: left\">\u8fd9\u6b21\u7ed9\u5927\u5bb6\u5e26\u6765\u5982\u4f55\u64cd\u4f5cwebpack\uff0c\u64cd\u4f5cwebpack\u5904\u7406\u6587\u4ef6\u7684\u6709\u54ea\u4e9b\uff0c\u4e0b\u9762\u5c31\u662f\u5b9e\u6218\u6848\u4f8b\uff0c\u4e00\u8d77\u6765\u770b\u4e00\u4e0b\u3002<\/p>\n<p style=\"text-align: left\">\u4f7f\u7528webpack\u6253\u5305\uff0c\u6700\u723d\u7684\u4e8b\u60c5\u83ab\u8fc7\u4e8e\u53ef\u4ee5\u76f4\u63a5\u6587\u4ef6\u4e86\uff0c\u4f46\u662f\u8fd9<\/p>\n<p style=\"text-align: left\">\u540c\u65f6\u5e26\u6765\u4e86\u4e00\u4e2a\u95ee\u9898\uff0c\u5c31\u662f\u6240\u6709\u7684\u6587\u4ef6\u6574\u5408\u5230\u4e00\u8d77\uff0c\u90a3\u8fd9\u4e00\u4e2a\u5305\u5c31\u592a\u5927\u4e86\u3002<\/p>\n<p style=\"text-align: left\"><span style=\"color: #ff0000\"><strong>\u57fa\u4e8e\u6b64\uff1a<\/strong><\/span>\u4e0b\u9762\u6211\u4eec\u6765\u4e86\u89e3\u4e0bwebpack\u7684\u6253\u5305\uff08\u4e3b\u8981\u662f\u5c06\u5982\u4f55\u5c06\u6211\u4eec\u9700\u8981\u7684\u5185\u5bb9\u6a21\u5757\uff0c\u5206\u5f00\u6253\u5305\uff0c<\/p>\n<p style=\"text-align: left\">\u5e76\u4e14\u6309\u7167\u6211\u4eec\u81ea\u5df1\u8bbe\u5b9a\u7684\u5b58\u653e\u8def\u5f84\u8fdb\u884c\u5b58\u653e\uff09<\/p>\n<p style=\"text-align: left\">\u9996\u5148\u5728webpack.config.js\u6587\u4ef6\u4e2d<\/p>\n<p style=\"text-align: left\"><span style=\"background-color: #ccffcc\"><strong>entry\u5165\u53e3\u51fd\u6570\u51fa\u8868\u793a\u51fa\u54ea\u4e9b\u662f\u9700\u8981\u5355\u72ec\u6253\u5305\u6210\u4e00\u4e2ajs\u5305\u7684\uff1a<\/strong><\/span><\/p>\n<pre>entry: {\r\n  main: path.resolve(dirname,'src\/index.js'),\r\n  jq: ['jquery'],\r\n  react: ['react'],\r\n  redom: ['react-dom']\r\n},\r\noutput: {\r\n  path: path.resolve(dirname,'dist'),\r\n  publishPath: 'dist\/',\r\n  filename: '[name].js'\r\n },\r\nplugin: [\r\n  new webpack.optimize.CommonsChunkPlugin('jq','jq.js'),\r\n  new webpack.optimize.CommonsChunkPlugin('vendors','vendors.js'),\r\n  new webpack.optimize.CommonsChunkPlugin('redom','redom.js')\r\n]<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p style=\"text-align: left\">\u5982\u4e0a\u914d\u7f6e\uff0c\u8fd9\u6837\u5728\u6587\u4ef6\u751f\u6210\u7684\u65f6\u5019dist\u76ee\u5f55\u4e0b\u5c31\u4f1a\u589e\u52a0jq.js,vendors.js,redom.js\u8fd9\u4e09\u4e2ajs\u4e86<\/p>\n<p style=\"text-align: left\">\u4ee5\u4e0a\u89e3\u51b3\u4e86\u6211\u4eec\u8981\u6253\u5305\u591a\u4e2a\u5305\u7684\u95ee\u9898\uff1b<\/p>\n<p style=\"text-align: left\">\u90a3\u5982\u679c\u6211\u4eec\u60f3\u8981\u5c06\u6253\u5305\u597d\u7684js\u5b58\u653e\u5728\u6307\u5b9a\u7684\u4f4d\u7f6e\u53c8\u8981\u5982\u4f55\u8fdb\u884c\u64cd\u4f5c\u5462\uff0c\u4e0d\u7528\u7740\u6025\uff0c\u4e0b\u9762\u5c31\u6765\u5177\u4f53<\/p>\n<p style=\"text-align: left\">\u7684\u5b9e\u73b0\u4ee5\u4e0b\uff08\u5176\u5b9e\u533a\u522b\u4e3b\u8981\u662f\u5728entry\u5165\u53e3\u51fd\u6570\u90a3\u513f\uff09\uff1a<\/p>\n<pre>entry: {\r\n  '.\/common\/main': path.resolve(dirname,'src\/index.js'),\r\n  '.\/jquery\/jq': ['jquery'],\r\n  '.\/react\/react': ['react'],\r\n  '.\/reactdom\/redom': ['react-dom']\r\n },<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p style=\"text-align: left\">\u5982\u4e0a\u6240\u793a\u8fd9\u6837\u56db\u4e2ajs\u5c31\u4f1a\u5206\u522b\u5b58\u653e\u5230\u6211\u4eec\u5236\u5b9a\u7684dist\u4e0b\u7684\u56db\u4e2a\u6587\u4ef6\u5939\u4e2d\u4e86\u3002\uff08\u6807\u6ce8\uff1a\u8fd9\u65f6\u5019\u5c31\u4e0d<\/p>\n<p style=\"text-align: left\">\u9700\u8981\u7528\u5230webpack.optimize.CommonsChunkPlugin\u63d2\u4ef6\u4e86\uff09<\/p>\n<p style=\"text-align: left\">\u6700\u540e\u8d34\u4e0a\u5de6\u53f3\u7684webpack.config.js\u4ee3\u7801\uff1a<\/p>\n<pre>var webpack = require('webpack');\r\nvar path = require('path');\r\nmodule.exports = {\r\n entry: {\r\n  '.\/common\/main': path.resolve(dirname,'src\/index.js'),\r\n  '.\/jquery\/jq': ['jquery'],\r\n  '.\/react\/react': ['react'],\r\n  '.\/reactdom\/redom': ['react-dom']\r\n },\r\n output: {\r\n  path: path.resolve(dirname,'dist'),\r\n  publishPath: 'dist\/',\r\n  filename: '[name].js'\r\n },\r\n module: {\r\n  loaders: [\r\n   {\r\n    test: \/.scss$\/,\r\n    loader: 'style!css!sass'\r\n   },\r\n   {\r\n    test: \/.js$\/,\r\n    exclude: \/node_modules|vue\/dist|vue-router\/|vue-loader\/|vue-hot-reload-api\/|presets\/\/,\r\n    loader: 'babel'\r\n   },\r\n   {\r\n    test: \/.(png|jpg|gif)$\/,\r\n    loader: 'url?limit=40000'\r\n   }\r\n  ]\r\n },\r\n babel: {\r\n  presets: ['es2015','stage-0','react'],\r\n  plugins: ['transform-runtime',[\"antd\",{\"style\": \"css\"}]]\r\n },\r\n resolve: ['js','jsx','css'],\r\n plugins:[\r\n  new webpack.ProvidePlugin({\r\n   $:\"jquery\",\r\n   jQuery:\"jquery\",\r\n   \"window.jQuery\":\"jquery\"\r\n  })\r\n \/\/ new webpack.optimize.CommonsChunkPlugin('jq','jq.js'),\r\n \/\/ new webpack.optimize.CommonsChunkPlugin('vendors','vendors.js'),\r\n \/\/ new webpack.optimize.CommonsChunkPlugin('redom','redom.js')\r\n ]\r\n};<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u76f8\u4fe1\u770b\u4e86\u672c\u6587\u6848\u4f8b\u4f60\u5df2\u7ecf\u638c\u63e1\u4e86\u65b9\u6cd5\uff0c\u66f4\u591a\u7cbe\u5f69\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n<p>\u63a8\u8350\u9605\u8bfb\uff1a<\/p>\n<\/p>\n<p><\/p>\n<p style=\"text-align: left\"><\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u5982\u4f55\u64cd\u4f5cwebpack\u5904\u7406\u6587\u4ef6\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u8fd9\u6b21\u7ed9\u5927\u5bb6\u5e26\u6765\u5982\u4f55\u64cd\u4f5cwebpack\uff0c\u64cd\u4f5cwebpack\u5904\u7406\u6587\u4ef6\u7684\u6709\u54ea\u4e9b\uff0c\u4e0b\u9762\u5c31\u662f\u5b9e\u6218\u6848\u4f8b\uff0c\u4e00\u8d77\u6765\u770b\u4e00\u4e0b\u3002 \u4f7f\u7528webpack\u6253\u5305\uff0c\u6700\u723d\u7684\u4e8b\u60c5\u83ab\u8fc7\u4e8e\u53ef\u4ee5\u76f4\u63a5\u6587\u4ef6\u4e86\uff0c\u4f46\u662f\u8fd9 \u540c\u65f6\u5e26\u6765\u4e86\u4e00\u4e2a\u95ee\u9898\uff0c\u5c31\u662f\u6240\u6709\u7684\u6587\u4ef6\u6574\u5408\u5230\u4e00\u8d77\uff0c\u90a3\u8fd9\u4e00\u4e2a\u5305\u5c31\u592a\u5927\u4e86\u3002 \u57fa\u4e8e\u6b64\uff1a\u4e0b\u9762\u6211\u4eec\u6765\u4e86\u89e3\u4e0bwebpack\u7684\u6253\u5305\uff08\u4e3b\u8981\u662f\u5c06\u5982\u4f55\u5c06\u6211\u4eec\u9700\u8981\u7684\u5185\u5bb9\u6a21\u5757\uff0c\u5206\u5f00\u6253\u5305\uff0c \u5e76\u4e14\u6309\u7167\u6211\u4eec\u81ea\u5df1\u8bbe\u5b9a\u7684\u5b58\u653e\u8def\u5f84\u8fdb\u884c\u5b58\u653e\uff09 \u9996\u5148\u5728webpack.config.js\u6587\u4ef6\u4e2d entry\u5165\u53e3\u51fd\u6570\u51fa\u8868\u793a\u51fa\u54ea\u4e9b\u662f\u9700\u8981\u5355\u72ec\u6253\u5305\u6210\u4e00\u4e2ajs\u5305\u7684\uff1a entry: { main: path.resolve(dirname,&#8217;src\/index.js&#8217;), jq: [&#8216;jquery&#8217;], react: [&#8216;react&#8217;], redom: [&#8216;react-dom&#8217;] }, output: { path: path.resolve(dirname,&#8217;dist&#8217;), publishPath: &#8216;dist\/&#8217;, filename: &#8216;[name].js&#8217; }, plugin: [ new webpack.optimize.CommonsChunkPlugin(&#8216;jq&#8217;,&#8217;jq.js&#8217;), new webpack.optimize.CommonsChunkPlugin(&#8216;vendors&#8217;,&#8217;vendors.js&#8217;), new webpack.optimize.CommonsChunkPlugin(&#8216;redom&#8217;,&#8217;redom.js&#8217;) ] \u767b\u5f55\u540e\u590d\u5236 \u5982\u4e0a\u914d\u7f6e\uff0c\u8fd9\u6837\u5728\u6587\u4ef6\u751f\u6210\u7684\u65f6\u5019dist\u76ee\u5f55\u4e0b\u5c31\u4f1a\u589e\u52a0jq.js,vendors.js,redom.js\u8fd9\u4e09\u4e2ajs\u4e86 \u4ee5\u4e0a\u89e3\u51b3\u4e86\u6211\u4eec\u8981\u6253\u5305\u591a\u4e2a\u5305\u7684\u95ee\u9898\uff1b \u90a3\u5982\u679c\u6211\u4eec\u60f3\u8981\u5c06\u6253\u5305\u597d\u7684js\u5b58\u653e\u5728\u6307\u5b9a\u7684\u4f4d\u7f6e\u53c8\u8981\u5982\u4f55\u8fdb\u884c\u64cd\u4f5c\u5462\uff0c\u4e0d\u7528\u7740\u6025\uff0c\u4e0b\u9762\u5c31\u6765\u5177\u4f53 \u7684\u5b9e\u73b0\u4ee5\u4e0b\uff08\u5176\u5b9e\u533a\u522b\u4e3b\u8981\u662f\u5728entry\u5165\u53e3\u51fd\u6570\u90a3\u513f\uff09\uff1a entry: { &#8216;.\/common\/main&#8217;: path.resolve(dirname,&#8217;src\/index.js&#8217;), &#8216;.\/jquery\/jq&#8217;: [&#8216;jquery&#8217;], &#8216;.\/react\/react&#8217;: [&#8216;react&#8217;], &#8216;.\/reactdom\/redom&#8217;: [&#8216;react-dom&#8217;] }, \u767b\u5f55\u540e\u590d\u5236 \u5982\u4e0a\u6240\u793a\u8fd9\u6837\u56db\u4e2ajs\u5c31\u4f1a\u5206\u522b\u5b58\u653e\u5230\u6211\u4eec\u5236\u5b9a\u7684dist\u4e0b\u7684\u56db\u4e2a\u6587\u4ef6\u5939\u4e2d\u4e86\u3002\uff08\u6807\u6ce8\uff1a\u8fd9\u65f6\u5019\u5c31\u4e0d \u9700\u8981\u7528\u5230webpack.optimize.CommonsChunkPlugin\u63d2\u4ef6\u4e86\uff09 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[19],"tags":[],"class_list":["post-31367","post","type-post","status-publish","format-standard","hentry","category-19"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/31367","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=31367"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/31367\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=31367"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=31367"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=31367"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}