\u5c01\u5305\u8f6f\u4ef6<\/a>\u6e90\u6253\u5305\u4e3a\u4e00\u4e2a\u6216\u51e0\u4e2aJS\u6587\u4ef6\uff0c\u4ece\u800c\u4f18\u5316\u7f51\u9875\u6027\u80fd\uff0c\u63d0\u9ad8\u7528\u6237\u4f53\u9a8c\u3002<\/p>\n\u4e8c\u3001\u539f\u7406<\/p>\n
\u5c06\u7f51\u9875\u6253\u5305\u6210JavaScript\u7684\u4e3b\u8981\u539f\u7406\u662f\u4f7f\u7528\u7279\u5b9a\u7684\u6253\u5305\u5de5\u5177\u6216\u811a\u672c\uff0c\u5c06HTML\u3001CSS\u3001JavaScript\u7b49\u6587\u4ef6\u53ca\u5176\u4f9d\u8d56\u7684\u8d44\u6e90\u6587\u4ef6\u5408\u5e76\u3001\u538b\u7f29\u6210\u4e00\u4e2a\u6216\u591a\u4e2aJS\u6587\u4ef6\uff0c\u6700\u540e\u901a\u8fc7\u4e00\u4e2a\u7b80\u5355\u7684HTML\u6587\u4ef6\u8fdb\u884c\u52a0\u8f7d\u3002\u8fd9\u6837\u5728\u8bf7\u6c42\u7f51\u9875\u65f6\uff0c\u6d4f\u89c8\u5668\u53ea\u9700\u4e0b\u8f7d\u4e00\u4e2a\u6216\u5c11\u91cf\u7684JS\u6587\u4ef6\uff0c\u5373\u53ef\u5feb\u901f\u52a0\u8f7d\u5e76\u663e\u793a\u5b8c\u6574\u7684\u7f51\u9875\u3002<\/p>\n
\u4e09\u3001\u5173\u952e\u6280\u672f\u548c\u6b65\u9aa4<\/p>\n
1. \u9009\u62e9\u4e00\u4e2a\u9002\u5f53\u7684\u6253\u5305\u5de5\u5177\u3002\u6709\u8bb8\u591a\u4f18\u79c0\u7684\u6253\u5305\u5de5\u5177\u53ef\u4f9b\u9009\u62e9\uff0c\u4f8b\u5982Webpack\u3001Parcel\u548cRollup\u7b49\u3002<\/p>\n
2. \u914d\u7f6e\u6253\u5305\u5de5\u5177\u3002\u521b\u5efa\u4e00\u4e2a\u914d\u7f6e\u6587\u4ef6\uff0c\u544a\u8bc9\u6253\u5305\u5de5\u5177\u5982\u4f55\u627e\u5230\u76f8\u5173\u8d44\u6e90\uff0c\u4ee5\u53ca\u5982\u4f55\u8fdb\u884c\u5408\u5e76\u3001\u538b\u7f29\u548c\u4f18\u5316\u3002<\/p>\n
3. \u7f16\u5199\u6a21\u5757\u5316\u7684\u4ee3\u7801\u3002\u6309\u7167\u6a21\u5757\u5316\u7684\u539f\u5219\u6765\u7f16\u5199\u4f60\u7684HTML\u3001CSS\u548cJavaScript\u4ee3\u7801\uff0c\u4f7f\u5176\u66f4\u5bb9\u6613\u5b9e\u73b0\u8d44\u6e90\u7684\u5408\u5e76\u548c\u4f18\u5316\u3002<\/p>\n
4. \u5b89\u88c5\u5e76\u8fd0\u884c\u6253\u5305\u5de5\u5177\u3002\u4f7f\u7528\u547d\u4ee4\u884c\u6216\u56fe\u5f62\u754c\u9762\u5b89\u88c5\u6253\u5305\u5de5\u5177\uff0c\u5e76\u6839\u636e\u914d\u7f6e\u6587\u4ef6\u8fd0\u884c\u6253\u5305\u8fc7\u7a0b\u3002<\/p>\n
5. \u5c06\u6253\u5305\u540e\u7684JS\u6587\u4ef6\u96c6\u6210\u5230HTML\u4e2d\u3002\u5728HTML\u6587\u4ef6\u4e2d\u5f15\u7528\u6253\u5305\u540e\u751f\u6210\u7684JS\u6587\u4ef6\uff0c\u7136\u540e\u8fd0\u884c\u670d\u52a1\u5668\u68c0\u67e5\u7ed3\u679c\u3002<\/p>\n
\u56db\u3001\u5b9e\u4f8b\u5206\u6790<\/p>\n
\u4e0b\u9762\u4ee5Webpack\u4e3a\u4f8b\uff0c\u6f14\u793a\u4e00\u4e2a\u7b80\u5355\u7684\u5c06\u7f51\u9875\u6253\u5305\u6210JS\u7684\u8fc7\u7a0b\uff1a<\/p>\n
1. \u5b89\u88c5Webpack\u53ca\u76f8\u5173\u4f9d\u8d56\uff1a<\/p>\n
“`bash<\/p>\n
npm install webpack webpack-cli webpack-dev-server html-webpack-plugin –save-dev<\/p>\n
“`<\/p>\n
2. \u5728\u9879\u76ee\u6839\u76ee\u5f55\u4e0b\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a`webpack.config.js`\u7684\u914d\u7f6e\u6587\u4ef6\uff1a<\/p>\n
“`javascript<\/p>\n
const HtmlWebpackPlugin = require(‘html-webpack-plugin’);<\/p>\n
const path = require(‘path’);<\/p>\n
module.exports = {<\/p>\n
entry: ‘.\/src\/index.js’, \/\/ \u9879\u76ee\u5165\u53e3\u6587\u4ef6<\/p>\n
output: {<\/p>\n
path: path.resolve(__dirname, ‘dist’), \/\/ \u8f93\u51fa\u76ee\u5f55<\/p>\n
filename: ‘bundle.js’, \/\/ \u8f93\u51fa\u6587\u4ef6\u540d<\/p>\n
},<\/p>\n
plugins: [<\/p>\n
new HtmlWebpackPlugin({<\/p>\n
<\/figure>\n<\/p>\n template: ‘.\/src\/index.html’, \/\/ \u6a21\u7248\u6587\u4ef6<\/p>\n
filename: ‘index.html’, \/\/ \u8f93\u51fa\u6587\u4ef6\u540d<\/p>\n
}),<\/p>\n
],<\/p>\n
module: {<\/p>\n
rules: [<\/p>\n
\/\/ \u5c06\u6240\u6709\u7684HTML\u3001CSS\u548cJS\u6587\u4ef6\u5904\u7406\u4e3aJS\u8d44\u6e90<\/p>\n
],<\/p>\n
},<\/p>\n
};<\/p>\n
“`<\/p>\n
3. \u5728`src`\u6587\u4ef6\u5939\u4e0b\u521b\u5efa\u5165\u53e3\u6587\u4ef6`index.js`\uff0c\u5e76\u5f15\u5165\u76f8\u5173\u8d44\u6e90\uff1a<\/p>\n
“`javascript<\/p>\n
import ‘.\/main.html’;<\/p>\n
import ‘.\/style.css’;<\/p>\n
import ‘.\/app.js’;<\/p>\n
“`<\/p>\n
4. \u5728`package.json`\u4e2d\u914d\u7f6e\u6253\u5305\u811a\u672c\uff1a<\/p>\n
“`json<\/p>\n
“scripts”: {<\/p>\n
“build”: “webpack”,<\/p>\n
“start”: “webpack serve”<\/p>\n
}<\/p>\n
“`<\/p>\n
5. \u8fd0\u884c`npm run build`\u8fdb\u884c\u6253\u5305\uff0c\u7136\u540e\u4f7f\u7528`npm start`\u8fd0\u884c\u670d\u52a1\u5668\uff0c\u67e5\u770b\u6253\u5305\u540e\u7684\u7f51\u9875\u6548\u679c\u3002<\/p>\n
\u603b\u7ed3\uff1a\u5c06\u7f51\u9875\u6253\u5305\u6210JavaScript\u5177\u6709\u4f18\u5316\u6027\u80fd\u3001\u63d0\u9ad8\u7528\u6237\u4f53\u9a8c\u7684\u4f18\u52bf\u3002\u901a\u8fc7\u4f7f\u7528\u9002\u5f53\u7684\u6253\u5305\u5de5\u5177\u548c\u7f16\u5199\u6a21\u5757\u5316\u4ee3\u7801\uff0c\u60a8\u53ef\u4ee5\u8f7b\u677e\u5b9e\u73b0\u8fd9\u4e00\u76ee\u6807\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"
\u6807\u9898\uff1a\u5c06\u7f51\u9875\u6253\u5305\u6210JavaScript\uff1a\u539f\u7406\u4e0e\u8be6\u7ec6\u4ecb\u7ecd\u968f\u7740\u4e92\u8054\u7f51\u7684\u5feb\u901f\u53d1\u5c55\uff0cWeb\u5f00\u53d1\u7684\u6280\u672f\u548c\u6846\u67b6\u4e5f\u5728\u4e0d\u65ad\u521b\u65b0\u548c\u6f14\u8fdb\u3002\u5728\u8fd9\u4e2a\u8fc7\u7a0b\u4e2d\uff0c\u4e3a\u4e86\u8ba9\u7f51\u9875\u52a0\u8f7d\u901f\u5ea6\u66f4\u5feb\u3001\u4f53\u9a8c\u66f4\u987a\u7545\uff0c\u5c06\u7f51\u9875\u6253\u5305\u6210JavaScrip<\/p>\n","protected":false},"author":22,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[10595,10594,52,16,149],"topic":[],"class_list":["post-5720","post","type-post","status-publish","format-standard","hentry","category-appkf","tag-webapp","tag-appapp","tag-52","tag-16","tag-149"],"_links":{"self":[{"href":"https:\/\/www.yimenyun.cn\/emen\/wp-json\/wp\/v2\/posts\/5720","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.yimenyun.cn\/emen\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.yimenyun.cn\/emen\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.yimenyun.cn\/emen\/wp-json\/wp\/v2\/users\/22"}],"replies":[{"embeddable":true,"href":"https:\/\/www.yimenyun.cn\/emen\/wp-json\/wp\/v2\/comments?post=5720"}],"version-history":[{"count":0,"href":"https:\/\/www.yimenyun.cn\/emen\/wp-json\/wp\/v2\/posts\/5720\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.yimenyun.cn\/emen\/wp-json\/wp\/v2\/media?parent=5720"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.yimenyun.cn\/emen\/wp-json\/wp\/v2\/categories?post=5720"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.yimenyun.cn\/emen\/wp-json\/wp\/v2\/tags?post=5720"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.yimenyun.cn\/emen\/wp-json\/wp\/v2\/topic?post=5720"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}