awesome-interview/assets/js/f930e7e8.1e33fc6c.js

1 line
33 KiB
JavaScript

"use strict";(self.webpackChunkjjbook=self.webpackChunkjjbook||[]).push([[6553],{3905:function(e,a,t){t.d(a,{Zo:function(){return d},kt:function(){return m}});var n=t(7294);function r(e,a,t){return a in e?Object.defineProperty(e,a,{value:t,enumerable:!0,configurable:!0,writable:!0}):e[a]=t,e}function l(e,a){var t=Object.keys(e);if(Object.getOwnPropertySymbols){var n=Object.getOwnPropertySymbols(e);a&&(n=n.filter((function(a){return Object.getOwnPropertyDescriptor(e,a).enumerable}))),t.push.apply(t,n)}return t}function o(e){for(var a=1;a<arguments.length;a++){var t=null!=arguments[a]?arguments[a]:{};a%2?l(Object(t),!0).forEach((function(a){r(e,a,t[a])})):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(t)):l(Object(t)).forEach((function(a){Object.defineProperty(e,a,Object.getOwnPropertyDescriptor(t,a))}))}return e}function s(e,a){if(null==e)return{};var t,n,r=function(e,a){if(null==e)return{};var t,n,r={},l=Object.keys(e);for(n=0;n<l.length;n++)t=l[n],a.indexOf(t)>=0||(r[t]=e[t]);return r}(e,a);if(Object.getOwnPropertySymbols){var l=Object.getOwnPropertySymbols(e);for(n=0;n<l.length;n++)t=l[n],a.indexOf(t)>=0||Object.prototype.propertyIsEnumerable.call(e,t)&&(r[t]=e[t])}return r}var i=n.createContext({}),p=function(e){var a=n.useContext(i),t=a;return e&&(t="function"==typeof e?e(a):o(o({},a),e)),t},d=function(e){var a=p(e.components);return n.createElement(i.Provider,{value:a},e.children)},c={inlineCode:"code",wrapper:function(e){var a=e.children;return n.createElement(n.Fragment,{},a)}},u=n.forwardRef((function(e,a){var t=e.components,r=e.mdxType,l=e.originalType,i=e.parentName,d=s(e,["components","mdxType","originalType","parentName"]),u=p(t),m=r,k=u["".concat(i,".").concat(m)]||u[m]||c[m]||l;return t?n.createElement(k,o(o({ref:a},d),{},{components:t})):n.createElement(k,o({ref:a},d))}));function m(e,a){var t=arguments,r=a&&a.mdxType;if("string"==typeof e||r){var l=t.length,o=new Array(l);o[0]=u;var s={};for(var i in a)hasOwnProperty.call(a,i)&&(s[i]=a[i]);s.originalType=e,s.mdxType="string"==typeof e?e:r,o[1]=s;for(var p=2;p<l;p++)o[p]=t[p];return n.createElement.apply(null,o)}return n.createElement.apply(null,t)}u.displayName="MDXCreateElement"},1503:function(e,a,t){t.r(a),t.d(a,{frontMatter:function(){return s},contentTitle:function(){return i},metadata:function(){return p},toc:function(){return d},default:function(){return u}});var n=t(7462),r=t(3366),l=(t(7294),t(3905)),o=["components"],s={sidebar_label:"\u5de5\u7a0b\u5316\uff1a\u8c08\u4e0b webpack loader \u7684\u673a\u5236",sidebar_position:3},i="\u8c08\u4e0b webpack loader \u7684\u673a\u5236",p={unversionedId:"book3/engineer-webpack-loader",id:"book3/engineer-webpack-loader",isDocsHomePage:!1,title:"\u8c08\u4e0b webpack loader \u7684\u673a\u5236",description:"\u76f8\u5173\u95ee\u9898",source:"@site/docs/book3/engineer-webpack-loader.md",sourceDirName:"book3",slug:"/book3/engineer-webpack-loader",permalink:"/awesome-interview/book3/engineer-webpack-loader",tags:[],version:"current",sidebarPosition:3,frontMatter:{sidebar_label:"\u5de5\u7a0b\u5316\uff1a\u8c08\u4e0b webpack loader \u7684\u673a\u5236",sidebar_position:3},sidebar:"tutorialSidebar",previous:{title:"\u6d4f\u89c8\u5668\uff1a\u5982\u4f55\u5b9a\u4f4d\u5185\u5b58\u6cc4\u9732",permalink:"/awesome-interview/book3/browser-memory-leaks"},next:{title:"\u6846\u67b6\uff1aReact Hooks \u5b9e\u73b0\u539f\u7406",permalink:"/awesome-interview/book3/frame-react-hooks"}},d=[{value:"\u76f8\u5173\u95ee\u9898",id:"\u76f8\u5173\u95ee\u9898",children:[]},{value:"\u56de\u7b54\u5173\u952e\u70b9",id:"\u56de\u7b54\u5173\u952e\u70b9",children:[]},{value:"\u77e5\u8bc6\u70b9\u6df1\u5165",id:"\u77e5\u8bc6\u70b9\u6df1\u5165",children:[{value:"1. \u7f16\u5199 webpack loader",id:"1-\u7f16\u5199-webpack-loader",children:[]},{value:"2. webpack loader \u5de5\u4f5c\u673a\u5236",id:"2-webpack-loader-\u5de5\u4f5c\u673a\u5236",children:[]},{value:"3. \u5e38\u89c1 webpack loader \u539f\u7406\u89e3\u6790",id:"3-\u5e38\u89c1-webpack-loader-\u539f\u7406\u89e3\u6790",children:[]}]},{value:"\u53c2\u8003\u8d44\u6599",id:"\u53c2\u8003\u8d44\u6599",children:[]}],c={toc:d};function u(e){var a=e.components,t=(0,r.Z)(e,o);return(0,l.kt)("wrapper",(0,n.Z)({},c,t,{components:a,mdxType:"MDXLayout"}),(0,l.kt)("h1",{id:"\u8c08\u4e0b-webpack-loader-\u7684\u673a\u5236"},"\u8c08\u4e0b webpack loader \u7684\u673a\u5236"),(0,l.kt)("h2",{id:"\u76f8\u5173\u95ee\u9898"},"\u76f8\u5173\u95ee\u9898"),(0,l.kt)("ul",null,(0,l.kt)("li",{parentName:"ul"},"webpack loader \u662f\u5982\u4f55\u5de5\u4f5c\u7684"),(0,l.kt)("li",{parentName:"ul"},"\u5982\u4f55\u7f16\u5199 webpack loader")),(0,l.kt)("h2",{id:"\u56de\u7b54\u5173\u952e\u70b9"},"\u56de\u7b54\u5173\u952e\u70b9"),(0,l.kt)("p",null,(0,l.kt)("inlineCode",{parentName:"p"},"\u8f6c\u6362")," ",(0,l.kt)("inlineCode",{parentName:"p"},"\u751f\u547d\u5468\u671f")," ",(0,l.kt)("inlineCode",{parentName:"p"},"chunk")),(0,l.kt)("p",null,"webpack \u672c\u8eab\u53ea\u80fd\u5904\u7406 JavaScript \u548c JSON \u6587\u4ef6\uff0c\u800c loader \u4e3a webpack \u6dfb\u52a0\u4e86\u5904\u7406\u5176\u4ed6\u7c7b\u578b\u6587\u4ef6\u7684\u80fd\u529b\u3002loader \u5c06\u5176\u4ed6\u7c7b\u578b\u7684\u6587\u4ef6\u8f6c\u6362\u6210\u6709\u6548\u7684 webpack modules\uff08\u5982 ESmodule\u3001CommonJS\u3001AMD\uff09\uff0cwebpack \u80fd\u6d88\u8d39\u8fd9\u4e9b\u6a21\u5757\uff0c\u5e76\u5c06\u5176\u6dfb\u52a0\u5230\u4f9d\u8d56\u5173\u7cfb\u56fe\u4e2d\u3002"),(0,l.kt)("p",null,"loader \u672c\u8d28\u4e0a\u662f\u4e00\u4e2a\u51fd\u6570\uff0c\u8be5\u51fd\u6570\u5bf9\u63a5\u6536\u5230\u7684\u5185\u5bb9\u8fdb\u884c\u8f6c\u6362\uff0c\u8fd4\u56de\u8f6c\u6362\u540e\u7684\u7ed3\u679c\u3002"),(0,l.kt)("p",null,"\u5e38\u89c1\u7684 loader \u6709\uff1a"),(0,l.kt)("ul",null,(0,l.kt)("li",{parentName:"ul"},"raw-loader\uff1a\u52a0\u8f7d\u6587\u4ef6\u539f\u59cb\u5185\u5bb9\u3002"),(0,l.kt)("li",{parentName:"ul"},"file-loader\uff1a\u5c06\u5f15\u7528\u6587\u4ef6\u8f93\u51fa\u5230\u76ee\u6807\u6587\u4ef6\u5939\u4e2d\uff0c\u5728\u4ee3\u7801\u4e2d\u901a\u8fc7\u76f8\u5bf9\u8def\u5f84\u5f15\u7528\u8f93\u51fa\u7684\u6587\u4ef6\u3002"),(0,l.kt)("li",{parentName:"ul"},"url-loader\uff1a\u548c file-loader \u7c7b\u4f3c\uff0c\u4f46\u662f\u80fd\u5728\u6587\u4ef6\u5f88\u5c0f\u7684\u60c5\u51b5\u4e0b\u4ee5 base64 \u7684\u65b9\u5f0f\u5c06\u6587\u4ef6\u5185\u5bb9\u6ce8\u5165\u5230\u4ee3\u7801\u4e2d\u3002"),(0,l.kt)("li",{parentName:"ul"},"babel-loader\uff1a\u5c06 ES \u8f83\u65b0\u7684\u8bed\u6cd5\u8f6c\u6362\u4e3a\u6d4f\u89c8\u5668\u53ef\u4ee5\u517c\u5bb9\u7684\u8bed\u6cd5\u3002"),(0,l.kt)("li",{parentName:"ul"},"style-loader\uff1a\u5c06 CSS \u4ee3\u7801\u6ce8\u5165\u5230 JavaScript \u4e2d\uff0c\u901a\u8fc7 DOM \u64cd\u4f5c\u52a0\u8f7d CSS\u3002"),(0,l.kt)("li",{parentName:"ul"},"css-loader\uff1a\u52a0\u8f7d CSS\uff0c\u652f\u6301\u6a21\u5757\u5316\u3001\u538b\u7f29\u3001\u6587\u4ef6\u5bfc\u5165\u7b49\u7279\u6027\u3002")),(0,l.kt)("p",null,"\u4f7f\u7528 loader \u7684\u65b9\u5f0f\u4e3b\u8981\u6709\u4e24\u79cd\uff1a"),(0,l.kt)("ol",null,(0,l.kt)("li",{parentName:"ol"},"\u5728 webpack.config.js \u6587\u4ef6\u4e2d\u914d\u7f6e\uff0c\u901a\u8fc7\u5728 module.rules \u4e2d\u4f7f\u7528 test \u5339\u914d\u8981\u8f6c\u6362\u7684\u6587\u4ef6\u7c7b\u578b\uff0c\u4f7f\u7528 use \u6307\u5b9a\u8981\u4f7f\u7528\u7684 loader\u3002")),(0,l.kt)("pre",null,(0,l.kt)("code",{parentName:"pre",className:"language-js"},'module.exports = {\n module: {\n rules: [{ test: /\\.ts$/, use: "ts-loader" }],\n },\n};\n')),(0,l.kt)("ol",{start:2},(0,l.kt)("li",{parentName:"ol"},"\u5185\u8054\u4f7f\u7528")),(0,l.kt)("pre",null,(0,l.kt)("code",{parentName:"pre",className:"language-js"},'import Styles from "style-loader!css-loader?modules!./styles.css";\n')),(0,l.kt)("h2",{id:"\u77e5\u8bc6\u70b9\u6df1\u5165"},"\u77e5\u8bc6\u70b9\u6df1\u5165"),(0,l.kt)("h3",{id:"1-\u7f16\u5199-webpack-loader"},"1. \u7f16\u5199 webpack loader"),(0,l.kt)("h4",{id:"11-\u540c\u6b65-loader"},"1.1 \u540c\u6b65 loader"),(0,l.kt)("p",null,"\u540c\u6b65\u8f6c\u6362\u5185\u5bb9\u540e\uff0c\u53ef\u4ee5\u901a\u8fc7 return \u6216\u8c03\u7528 this.callback \u8fd4\u56de\u7ed3\u679c\u3002"),(0,l.kt)("pre",null,(0,l.kt)("code",{parentName:"pre",className:"language-js"},"export default function loader(content, map, meta) {\n return someSyncOperation(content);\n}\n")),(0,l.kt)("p",null,"\u901a\u8fc7 this.callback \u53ef\u4ee5\u8fd4\u56de\u9664\u5185\u5bb9\u4ee5\u5916\u7684\u5176\u4ed6\u4fe1\u606f\uff08\u5982 sourcemap\uff09\u3002"),(0,l.kt)("pre",null,(0,l.kt)("code",{parentName:"pre",className:"language-js"},"export default function loader(content, map, meta) {\n this.callback(null, someSyncOperation(content), map, meta);\n return; // \u5f53\u8c03\u7528 callback() \u65f6\uff0c\u59cb\u7ec8\u8fd4\u56de undefined\n}\n")),(0,l.kt)("h4",{id:"12-\u5f02\u6b65-loader"},"1.2 \u5f02\u6b65 loader"),(0,l.kt)("p",null,"\u901a\u8fc7 this.async \u53ef\u4ee5\u83b7\u53d6\u5f02\u6b65\u64cd\u4f5c\u7684\u56de\u8c03\u51fd\u6570\uff0c\u5e76\u5728\u56de\u8c03\u51fd\u6570\u4e2d\u8fd4\u56de\u7ed3\u679c\u3002"),(0,l.kt)("pre",null,(0,l.kt)("code",{parentName:"pre",className:"language-js"},"export default function (content, map, meta) {\n const callback = this.async();\n someAsyncOperation(content, (err, result, sourceMaps, meta) => {\n if (err) return callback(err);\n callback(null, result, sourceMaps, meta);\n });\n}\n")),(0,l.kt)("p",null,"\u9664\u975e\u8ba1\u7b97\u5f88\u5c0f\uff0c\u5426\u5219\u5bf9\u4e8e Node.js \u8fd9\u79cd\u5355\u7ebf\u7a0b\u73af\u5883\uff0c\u5c3d\u53ef\u80fd\u4f7f\u7528\u5f02\u6b65 loader\u3002"),(0,l.kt)("h4",{id:"13-loader-\u5f00\u53d1\u8f85\u52a9\u5de5\u5177\u53ca-loadercontext"},"1.3 loader \u5f00\u53d1\u8f85\u52a9\u5de5\u5177\u53ca loaderContext"),(0,l.kt)("p",null,(0,l.kt)("inlineCode",{parentName:"p"},"loader-utils")," \u4e0e ",(0,l.kt)("inlineCode",{parentName:"p"},"schema-utils"),"\uff0c\u53ef\u4ee5\u4f7f\u83b7\u53d6\u53ca\u9a8c\u8bc1\u4f20\u9012\u7ed9 loader \u7684\u53c2\u6570\u7684\u5de5\u4f5c\u7b80\u5355\u5316\u3002"),(0,l.kt)("pre",null,(0,l.kt)("code",{parentName:"pre",className:"language-js"},'import { getOptions } from "loader-utils";\nimport { validate } from "schema-utils";\n\nconst schema = {\n type: "object",\n properties: {\n test: {\n type: "string",\n },\n },\n};\n\nexport default function (source) {\n const options = getOptions(this);\n\n validate(schema, options, {\n name: "Example Loader",\n baseDataPath: "options",\n });\n\n // Apply some transformations to the source...\n\n return `export default ${JSON.stringify(source)}`;\n}\n')),(0,l.kt)("p",null,"loader-utils \u4e3b\u8981\u6709\u4ee5\u4e0b\u5de5\u5177\u65b9\u6cd5\uff1a"),(0,l.kt)("ul",null,(0,l.kt)("li",{parentName:"ul"},(0,l.kt)("inlineCode",{parentName:"li"},"parseQuery"),"\uff1a\u89e3\u6790 loader \u7684 query \u53c2\u6570\uff0c\u8fd4\u56de\u4e00\u4e2a\u5bf9\u8c61\u3002"),(0,l.kt)("li",{parentName:"ul"},(0,l.kt)("inlineCode",{parentName:"li"},"stringifyRequest"),"\uff1a\u5c06\u8bf7\u6c42\u7684\u8d44\u6e90\u8f6c\u6362\u4e3a\u53ef\u4ee5\u5728 loader \u751f\u6210\u7684\u4ee3\u7801\u4e2d require \u6216 import \u4f7f\u7528\u7684\u76f8\u5bf9\u8def\u5f84\u5b57\u7b26\u4e32\uff0c\u540c\u65f6\u907f\u514d\u7edd\u5bf9\u8def\u5f84\u5bfc\u81f4\u91cd\u65b0\u8ba1\u7b97 hash \u503c\u3002",(0,l.kt)("pre",{parentName:"li"},(0,l.kt)("code",{parentName:"pre",className:"language-js"},'loaderUtils.stringifyRequest(this, "./test.js");\n// "\\"./test.js\\""\n'))),(0,l.kt)("li",{parentName:"ul"},(0,l.kt)("inlineCode",{parentName:"li"},"urlToRequest"),"\uff1a\u5c06\u8bf7\u6c42\u7684\u8d44\u6e90\u8def\u5f84\u8f6c\u6362\u6210 webpack \u53ef\u4ee5\u5904\u7406\u7684\u5f62\u5f0f\u3002",(0,l.kt)("pre",{parentName:"li"},(0,l.kt)("code",{parentName:"pre",className:"language-js"},'const url = "~path/to/module.js";\nconst request = loaderUtils.urlToRequest(url); // "path/to/module.js"\n'))),(0,l.kt)("li",{parentName:"ul"},(0,l.kt)("inlineCode",{parentName:"li"},"interpolateName"),"\uff1a\u5bf9\u6587\u4ef6\u540d\u6a21\u677f\u8fdb\u884c\u63d2\u503c\u3002",(0,l.kt)("pre",{parentName:"li"},(0,l.kt)("code",{parentName:"pre",className:"language-js"},'// loaderContext.resourcePath = "/absolute/path/to/app/js/hzfe.js"\nloaderUtils.interpolateName(loaderContext, "js/[hash].script.[ext]", { content: ... });\n// => js/9473fdd0d880a43c21b7778d34872157.script.js\n'))),(0,l.kt)("li",{parentName:"ul"},(0,l.kt)("inlineCode",{parentName:"li"},"getHashDigest"),"\uff1a\u83b7\u53d6\u6587\u4ef6\u5185\u5bb9\u7684 hash \u503c\u3002")),(0,l.kt)("p",null,"\u5728\u7f16\u5199 loader \u7684\u8fc7\u7a0b\u4e2d\uff0c\u8fd8\u53ef\u4ee5\u5229\u7528 loaderContext \u5bf9\u8c61\u6765\u83b7\u53d6 loader \u7684\u76f8\u5173\u4fe1\u606f\u548c\u8fdb\u884c\u4e00\u4e9b\u9ad8\u7ea7\u7684\u64cd\u4f5c\uff0c\u5e38\u89c1\u7684\u5c5e\u6027\u548c\u65b9\u6cd5\u6709\uff1a"),(0,l.kt)("ul",null,(0,l.kt)("li",{parentName:"ul"},(0,l.kt)("inlineCode",{parentName:"li"},"this.addDependency"),"\uff1a\u52a0\u5165\u4e00\u4e2a\u6587\u4ef6\uff0c\u4f5c\u4e3a loader \u4ea7\u751f\u7684\u7ed3\u679c\u7684\u4f9d\u8d56\uff0c\u4f7f\u5176\u5728\u6709\u4efb\u4f55\u53d8\u5316\u65f6\u53ef\u4ee5\u88ab\u76d1\u542c\u5230\uff0c\u4ece\u800c\u89e6\u53d1\u91cd\u65b0\u7f16\u8bd1\u3002"),(0,l.kt)("li",{parentName:"ul"},(0,l.kt)("inlineCode",{parentName:"li"},"this.async"),"\uff1a\u544a\u8bc9 loader-runner \u8fd9\u4e2a loader \u5c06\u4f1a\u5f02\u6b65\u7684\u6267\u884c\u56de\u8c03\u3002"),(0,l.kt)("li",{parentName:"ul"},(0,l.kt)("inlineCode",{parentName:"li"},"this.cacheable"),"\uff1a\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c\u5c06 loader \u7684\u5904\u7406\u7ed3\u679c\u6807\u8bb0\u4e3a\u53ef\u7f13\u5b58\u3002\u4f20\u5165 false \u53ef\u4ee5\u5173\u95ed loader \u5904\u7406\u7ed3\u679c\u7684\u7f13\u5b58\u80fd\u529b\u3002"),(0,l.kt)("li",{parentName:"ul"},(0,l.kt)("inlineCode",{parentName:"li"},"this.fs"),"\uff1a\u7528\u4e8e\u8bbf\u95ee compilation \u7684 inputFileSystem \u5c5e\u6027\u3002"),(0,l.kt)("li",{parentName:"ul"},(0,l.kt)("inlineCode",{parentName:"li"},"this.getOptions"),"\uff1a\u63d0\u53d6 loader \u7684\u914d\u7f6e\u9009\u9879\u3002\u4ece webpack 5 \u5f00\u59cb\uff0c\u53ef\u4ee5\u83b7\u53d6\u5230 loader \u4e0a\u4e0b\u6587\u5bf9\u8c61\uff0c\u7528\u4e8e\u66ff\u4ee3 ",(0,l.kt)("inlineCode",{parentName:"li"},"loader-utils")," \u4e2d\u7684 getOptions \u65b9\u6cd5\u3002"),(0,l.kt)("li",{parentName:"ul"},(0,l.kt)("inlineCode",{parentName:"li"},"this.mode"),'\uff1a webpack \u7684\u8fd0\u884c\u6a21\u5f0f\uff0c\u53ef\u4ee5\u662f "development" \u6216 "production"\u3002'),(0,l.kt)("li",{parentName:"ul"},(0,l.kt)("inlineCode",{parentName:"li"},"this.query"),"\uff1a\u5982\u679c loader \u914d\u7f6e\u4e86 options \u5bf9\u8c61\uff0c\u5219\u6307\u5411\u8fd9\u4e2a\u5bf9\u8c61\u3002\u5982\u679c loader \u6ca1\u6709 options\uff0c\u800c\u662f\u4ee5 query \u5b57\u7b26\u4e32\u4f5c\u4e3a\u53c2\u6570\uff0cquery \u5219\u662f\u4e00\u4e2a\u4ee5 ",(0,l.kt)("inlineCode",{parentName:"li"},"?")," \u5f00\u5934\u7684\u5b57\u7b26\u4e32\u3002")),(0,l.kt)("p",null,"\u4ee5\u4e0a\u5185\u5bb9\u662f\u7f16\u5199\u4e00\u4e2a loader \u7684\u5173\u952e\u70b9\uff0c\u60f3\u8981\u5b66\u4e60\u66f4\u8be6\u7ec6\u7684\u5173\u4e8e\u7f16\u5199 loader \u7684\u6307\u5bfc\uff0c\u53ef\u4ee5\u53c2\u8003\u5b98\u65b9\u7684 ",(0,l.kt)("a",{parentName:"p",href:"https://webpack.js.org/contribute/writing-a-loader/#guidelines"},"guidelines"),"\u3002"),(0,l.kt)("h3",{id:"2-webpack-loader-\u5de5\u4f5c\u673a\u5236"},"2. webpack loader \u5de5\u4f5c\u673a\u5236"),(0,l.kt)("h4",{id:"21-\u6839\u636e-modulerules-\u89e3\u6790-loader-\u52a0\u8f7d\u89c4\u5219"},"2.1 \u6839\u636e module.rules \u89e3\u6790 loader \u52a0\u8f7d\u89c4\u5219"),(0,l.kt)("p",null,"\u5f53 webpack \u5904\u7406\u4e00\u4e2a\u6a21\u5757\uff08module\uff09\u65f6\uff0c\u4f1a\u6839\u636e\u914d\u7f6e\u6587\u4ef6\u4e2d ",(0,l.kt)("inlineCode",{parentName:"p"},"module.rules")," \u7684\u89c4\u5219\uff0c\u4f7f\u7528 loader \u5904\u7406\u5bf9\u5e94\u8d44\u6e90\uff0c\u5f97\u5230\u53ef\u4f9b webpack \u4f7f\u7528\u7684 JavaScript \u6a21\u5757\u3002"),(0,l.kt)("p",null,"\u6839\u636e\u5177\u4f53\u7684\u914d\u7f6e\u60c5\u51b5\uff0cloader \u4f1a\u6709\u4e0d\u540c\u7684\u7c7b\u578b\uff0c\u53ef\u4ee5\u5f71\u54cd loader \u7684\u6267\u884c\u987a\u5e8f\u3002\u5177\u4f53\u7c7b\u578b\u5982\u4e0b\u6240\u793a\uff1a"),(0,l.kt)("pre",null,(0,l.kt)("code",{parentName:"pre",className:"language-js"},'rules: [\n // pre \u524d\u7f6e loader\n { enforce: "pre", test: /\\.js$/, loader: "eslint-loader" },\n // normal loader\n { test: /\\.js$/, loader: "babel-loader" },\n // post \u540e\u7f6e loader\n { enforce: "post", test: /\\.js$/, loader: "eslint-loader" },\n];\n')),(0,l.kt)("p",null,"\u4ee5\u53ca\u5185\u8054\u4f7f\u7528\u7684 inline loader\uff1a"),(0,l.kt)("pre",null,(0,l.kt)("code",{parentName:"pre",className:"language-js"},'import "style-loader!css-loader!sass-loader!./hzfe.scss";\n')),(0,l.kt)("p",null,"\u5728\u6b63\u5e38\u7684\u6267\u884c\u6d41\u7a0b\u4e2d\uff0c\u8fd9\u4e9b\u4e0d\u540c\u7c7b\u578b\u7684 loader \u7684\u6267\u884c\u987a\u5e8f\u662f\uff1a",(0,l.kt)("inlineCode",{parentName:"p"},"pre -> normal -> inline -> post"),"\u3002\u5728\u4e0b\u4e00\u8282\u5c06\u4f1a\u63d0\u5230\u7684 pitch \u6d41\u7a0b\u4e2d\uff0c\u8fd9\u4e9b loader \u7684\u6267\u884c\u987a\u5e8f\u662f\u53cd\u8fc7\u6765\u7684\uff1a",(0,l.kt)("inlineCode",{parentName:"p"},"post -> inline -> normal -> pre"),"\u3002"),(0,l.kt)("p",null,"\u5bf9\u4e8e\u5185\u8054 loader\uff0c\u53ef\u4ee5\u901a\u77e5\u4fee\u9970\u524d\u7f00\u6539\u53d8 loader \u7684\u6267\u884c\u987a\u5e8f\uff1a"),(0,l.kt)("pre",null,(0,l.kt)("code",{parentName:"pre",className:"language-js"},'// ! \u524d\u7f00\u4f1a\u7981\u7528 normal loader\nimport { HZFE } from "!./hzfe.js";\n// -! \u524d\u7f00\u4f1a\u7981\u7528 pre loader \u548c normal loader\nimport { HZFE } from "-!./hzfe.js";\n// !! \u524d\u7f00\u4f1a\u7981\u7528 pre\u3001normal \u548c post loader\nimport { HZFE } from "!!./hzfe.js";\n')),(0,l.kt)("p",null,"\u4e00\u822c\u60c5\u51b5\u4e0b\uff0c",(0,l.kt)("inlineCode",{parentName:"p"},"!")," \u524d\u7f00\u548c inline loader \u4e00\u8d77\u4f7f\u7528\u4ec5\u51fa\u73b0\u5728 loader\uff08\u5982 style-loader\uff09\u751f\u6210\u7684\u4ee3\u7801\u4e2d\uff0cwebpack \u5b98\u65b9\u4e0d\u5efa\u8bae\u7528\u6237\u540c\u65f6\u4f7f\u7528 inline loader \u548c ",(0,l.kt)("inlineCode",{parentName:"p"},"!")," \u524d\u7f00\u3002"),(0,l.kt)("p",null,"webpack rules \u4e2d\u914d\u7f6e\u7684 loader \u53ef\u4ee5\u662f\u591a\u4e2a\u94fe\u5f0f\u4e32\u8054\u7684\u3002\u5728\u6b63\u5e38\u6d41\u7a0b\u4e2d\uff0c\u94fe\u5f0f loader \u4f1a\u6309\u7167\u4ece\u540e\u5f80\u524d\u7684\u987a\u5e8f\u6267\u884c\u3002"),(0,l.kt)("ul",null,(0,l.kt)("li",{parentName:"ul"},"\u6700\u540e\u7684 loader \u6700\u5148\u6267\u884c\uff0c\u5b83\u63a5\u6536\u7684\u662f\u8d44\u6e90\u6587\u4ef6\uff08resource file\uff09\u7684\u5185\u5bb9\u3002"),(0,l.kt)("li",{parentName:"ul"},"\u7b2c\u4e00\u4e2a loader \u6700\u540e\u6267\u884c\uff0c\u5b83\u5c06\u8fd4\u56de JavaScript \u6a21\u5757\u548c\u53ef\u9009\u7684 source map\u3002"),(0,l.kt)("li",{parentName:"ul"},"\u4f4d\u4e8e\u4e2d\u95f4\u7684 loader\uff0c\u5bf9\u63a5\u6536\u548c\u8fd4\u56de\u6ca1\u6709\u7279\u5b9a\u8981\u6c42\uff0c\u53ea\u8981\u80fd\u5904\u7406\u4e4b\u524d loader \u8fd4\u56de\u7684\u5185\u5bb9\uff0c\u4ea7\u51fa\u4e0b\u4e00\u4e2a loader \u80fd\u591f\u7406\u89e3\u7684\u5185\u5bb9\u5c31\u53ef\u4ee5\u3002")),(0,l.kt)("h4",{id:"22-loader-runner-\u7684\u6267\u884c\u6d41\u7a0b"},"2.2 loader-runner \u7684\u6267\u884c\u6d41\u7a0b"),(0,l.kt)("p",null,"webpack \u8c03\u7528 loader \u7684\u65f6\u673a\u5728\u89e6\u53d1 compilation \u7684 buildModule \u94a9\u5b50\u4e4b\u540e\u3002webpack \u4f1a\u5728 ",(0,l.kt)("inlineCode",{parentName:"p"},"NormalModule.js")," \u4e2d\uff0c\u8c03\u7528 runLoaders \u8fd0\u884c loader\uff1a"),(0,l.kt)("pre",null,(0,l.kt)("code",{parentName:"pre",className:"language-js"},"runLoaders(\n {\n resource: this.resource, // \u8d44\u6e90\u6587\u4ef6\u7684\u8def\u5f84\uff0c\u53ef\u4ee5\u6709\u67e5\u8be2\u5b57\u7b26\u4e32\u3002\u5982\uff1a'./test.txt?query'\n loaders: this.loaders, // loader \u7684\u8def\u5f84\u3002\n context: loaderContext, // \u4f20\u9012\u7ed9 loader \u7684\u4e0a\u4e0b\u6587\n processResource: (loaderContext, resourcePath, callback) => {\n // \u83b7\u53d6\u8d44\u6e90\u7684\u65b9\u5f0f\uff0c\u6709 scheme \u7684\u6587\u4ef6\u901a\u8fc7 readResourceForScheme \u8bfb\u53d6\uff0c\u5426\u5219\u901a\u8fc7 fs.readFile \u8bfb\u53d6\u3002\n const resource = loaderContext.resource;\n const scheme = getScheme(resource);\n if (scheme) {\n hooks.readResourceForScheme\n .for(scheme)\n .callAsync(resource, this, (err, result) => {\n // ...\n return callback(null, result);\n });\n } else {\n loaderContext.addDependency(resourcePath);\n fs.readFile(resourcePath, callback);\n }\n },\n },\n (err, result) => {\n // \u5f53 loader \u8f6c\u6362\u5b8c\u6210\u540e\uff0c\u4f1a\u5c06\u7ed3\u679c\u8fd4\u56de\u5230 webpack \u4e2d\u7ee7\u7eed\u5904\u7406\u3002\n processResult(err, result.result);\n }\n);\n")),(0,l.kt)("p",null,"runLoaders \u51fd\u6570\u6765\u81ea ",(0,l.kt)("inlineCode",{parentName:"p"},"loader-runner")," \u5305\u3002\u5728\u4ecb\u7ecd runLoaders \u7684\u5177\u4f53\u6d41\u7a0b\u4e4b\u524d\uff0c\u5148\u4ecb\u7ecd\u4e00\u4e0b pitch \u9636\u6bb5\uff0c\u4e0a\u4e00\u8282\u4e2d\u6240\u8bb2\u7684\u8fd9\u79cd\u4ece\u540e\u5f80\u524d\u6267\u884c loader \u7684\u6d41\u7a0b\uff0c\u4e00\u822c\u53eb\u505a normal \u9636\u6bb5\u3002\u4e0e\u4e4b\u76f8\u5bf9\u7684\uff0c\u8fd8\u6709\u4e00\u79cd\u53eb\u505a pitch \u9636\u6bb5\u7684\u6d41\u7a0b\u3002"),(0,l.kt)("p",null,"\u4e00\u4e2a loader \u5982\u679c\u5728\u5bfc\u51fa\u7684\u51fd\u6570\u7684 pitch \u5c5e\u6027\u4e0a\u6302\u5728\u4e86\u65b9\u6cd5\uff0c\u90a3\u8fd9\u4e2a\u65b9\u6cd5\u5c06\u5728 pitch \u9636\u6bb5\u6267\u884c\u3002pitch \u9636\u6bb5\u4e0d\u540c\u4e8e normal \u9636\u6bb5\uff0cpitch \u9636\u6bb5\u7684\u6267\u884c\u987a\u5e8f\u662f\u4ece\u524d\u5f80\u540e\u7684\uff0c\u6574\u4e2a\u6d41\u7a0b\u7c7b\u4f3c\u6d4f\u89c8\u5668\u4e8b\u4ef6\u6a21\u578b\u6216\u6d0b\u8471\u6a21\u578b\uff0cpitch \u9636\u6bb5\u5148\u4ece\u524d\u5f80\u540e\u6267\u884c loader\uff0c\u7136\u540e\u518d\u8fdb\u5165 normal \u9636\u6bb5\u4ece\u540e\u5f80\u524d\u6267\u884c loader\u3002\u6ce8\u610f\uff0cpitch \u9636\u6bb5\u4e00\u822c\u4e0d\u8fd4\u56de\u503c\uff0c\u4e00\u65e6 pitch \u9636\u6bb5\u6709 loader \u8fd4\u56de\u503c\uff0c\u5219\u4ece\u8fd9\u91cc\u5f00\u59cb\u8fdb\u5165\u4ece\u540e\u5f80\u524d\u6267\u884c\u7684 normal \u9636\u6bb5\u3002"),(0,l.kt)("p",null,"loader-runner \u7684\u5177\u4f53\u6d41\u7a0b\u5982\u4e0b\uff1a"),(0,l.kt)("ol",null,(0,l.kt)("li",{parentName:"ol"},(0,l.kt)("p",{parentName:"li"},"\u5904\u7406\u4ece webpack \u63a5\u6536\u7684 context\uff0c\u7ee7\u7eed\u6dfb\u52a0\u5fc5\u8981\u7684\u5c5e\u6027\u548c\u8f85\u52a9\u65b9\u6cd5\u3002")),(0,l.kt)("li",{parentName:"ol"},(0,l.kt)("p",{parentName:"li"},"iteratePitchingLoaders \u5904\u7406 pitch loader\u3002"),(0,l.kt)("p",{parentName:"li"},"\u5982\u679c\u6211\u4eec\u7ed9\u4e00\u4e2a module \u914d\u7f6e\u4e86\u4e09\u4e2a loader\uff0c\u6bcf\u4e2a loader \u90fd\u914d\u7f6e\u4e86 pitch \u51fd\u6570\uff1a"),(0,l.kt)("pre",{parentName:"li"},(0,l.kt)("code",{parentName:"pre",className:"language-js"},'module.exports = {\n //...\n module: {\n rules: [\n {\n //...\n use: ["a-loader", "b-loader", "c-loader"],\n },\n ],\n },\n};\n')),(0,l.kt)("p",{parentName:"li"},"\u90a3\u4e48\u5904\u7406\u8fd9\u4e2a module \u7684\u6d41\u7a0b\u5982\u4e0b\uff1a"),(0,l.kt)("pre",{parentName:"li"},(0,l.kt)("code",{parentName:"pre",className:"language-shell"},"|- a-loader `pitch`\n |- b-loader `pitch`\n |- c-loader `pitch`\n |- requested module is picked up as a dependency\n |- c-loader normal execution\n |- b-loader normal execution\n|- a-loader normal execution\n")),(0,l.kt)("p",{parentName:"li"},"\u5982\u679c b-loader \u5728 pitch \u4e2d\u63d0\u524d\u8fd4\u56de\u4e86\u503c\uff0c\u90a3\u4e48\u6d41\u7a0b\u5982\u4e0b\uff1a"),(0,l.kt)("pre",{parentName:"li"},(0,l.kt)("code",{parentName:"pre",className:"language-shell"},"|- a-loader `pitch`\n |- b-loader `pitch` returns a module\n|- a-loader normal execution\n"))),(0,l.kt)("li",{parentName:"ol"},(0,l.kt)("p",{parentName:"li"},"iterateNormalLoaders \u5904\u7406 normal loader\u3002"),(0,l.kt)("p",{parentName:"li"},"\u5f53 pitch loader \u7684\u6d41\u7a0b\u5904\u7406\u5b8c\u540e\uff0c\u5c31\u6765\u5230\u4e86\u5904\u7406 normal loader \u7684\u6d41\u7a0b\u3002\u5904\u7406 normal loader \u7684\u6d41\u7a0b\u548c pitch loader \u76f8\u4f3c\uff0c\u53ea\u662f\u4ece\u540e\u5f80\u524d\u8fed\u4ee3\u3002"),(0,l.kt)("p",{parentName:"li"},"iterateNormalLoaders \u548c iteratePitchingLoaders \u90fd\u4f1a\u8c03\u7528 runSyncOrAsync \u6765\u6267\u884c loader\u3002runSyncOrAsync \u4f1a\u63d0\u4f9b context.async\uff0c\u8fd9\u662f\u4e00\u4e2a\u8fd4\u56de callback \u7684 async \u51fd\u6570\uff0c\u7528\u4e8e\u5f02\u6b65\u5904\u7406\u3002"))),(0,l.kt)("h3",{id:"3-\u5e38\u89c1-webpack-loader-\u539f\u7406\u89e3\u6790"},"3. \u5e38\u89c1 webpack loader \u539f\u7406\u89e3\u6790"),(0,l.kt)("p",null,"loader \u672c\u8eab\u7684\u64cd\u4f5c\u5e76\u4e0d\u590d\u6742\uff0c\u5c31\u662f\u4e00\u4e2a\u8d1f\u8d23\u8f6c\u6362\u5176\u4ed6\u8d44\u6e90\u5230 JavaScript \u6a21\u5757\u7684\u51fd\u6570\u3002"),(0,l.kt)("h4",{id:"31-raw-loader-\u5206\u6790"},"3.1 raw-loader \u5206\u6790"),(0,l.kt)("p",null,"\u8be5 loader \u662f\u529f\u80fd\u975e\u5e38\u7b80\u5355\u7684\u540c\u6b65 loader\uff0c\u5b83\u7684\u6838\u5fc3\u6b65\u9aa4\u662f\u4ece\u6587\u4ef6\u539f\u59cb\u5185\u5bb9\u4e2d\u53d6\u5f97\u5e8f\u5217\u5316\u7684\u5b57\u7b26\u4e32\uff0c\u4fee\u590d JSON \u5e8f\u5217\u5316\u7279\u6b8a\u5b57\u7b26\u65f6\u7684 bug\uff0c\u6dfb\u52a0\u5bfc\u51fa\u8bed\u53e5\uff0c\u4f7f\u5176\u6210\u4e3a JavaScript \u6a21\u5757\u3002"),(0,l.kt)("p",null,"\u8be5 loader \u5728 webpack 5 \u4e2d\u5df2\u5e9f\u5f03\uff0c\u76f4\u63a5\u4f7f\u7528 asset modules \u7684\u529f\u80fd\u4ee3\u66ff\u5373\u53ef\u3002\u8be5 loader \u6e90\u7801\u5982\u4e0b\uff1a"),(0,l.kt)("pre",null,(0,l.kt)("code",{parentName:"pre",className:"language-js"},'import { getOptions } from "loader-utils";\nimport { validate } from "schema-utils";\n\nimport schema from "./options.json";\n\nexport default function rawLoader(source) {\n const options = getOptions(this);\n\n validate(schema, options, {\n name: "Raw Loader",\n baseDataPath: "options",\n });\n\n const json = JSON.stringify(source)\n .replace(/\\u2028/g, "\\\\u2028")\n .replace(/\\u2029/g, "\\\\u2029");\n\n const esModule =\n typeof options.esModule !== "undefined" ? options.esModule : true;\n\n return `${esModule ? "export default" : "module.exports ="} ${json};`;\n}\n')),(0,l.kt)("h4",{id:"32-babel-loader-\u5206\u6790"},"3.2 babel-loader \u5206\u6790"),(0,l.kt)("p",null,"babel loader \u662f\u4e00\u4e2a\u7efc\u5408\u4e86\u540c\u6b65\u548c\u5f02\u6b65\u7684 loader\uff0c\u5728\u4f7f\u7528\u7f13\u5b58\u914d\u7f6e\u65f6\u4ee5\u5f02\u6b65\u6a21\u5f0f\u8fd0\u884c\uff0c\u5426\u5219\u4ee5\u540c\u6b65\u65b9\u5f0f\u8fd0\u884c\u3002\u8be5 loader \u7684\u4e3b\u8981\u6e90\u7801\u5982\u4e0b\uff1a"),(0,l.kt)("pre",null,(0,l.kt)("code",{parentName:"pre",className:"language-js"},"// imports ...\n// ...\n\nconst transpile = function (source, options) {\n // ...\n\n let result;\n try {\n result = babel.transform(source, options);\n } catch (error) {\n // ...\n }\n // ...\n\n return {\n code: code,\n map: map,\n metadata: metadata,\n };\n};\n\n// ...\n\nmodule.exports = function (source, inputSourceMap) {\n // ...\n\n if (cacheDirectory) {\n const callback = this.async();\n return cache(\n {\n directory: cacheDirectory,\n identifier: cacheIdentifier,\n source: source,\n options: options,\n transform: transpile,\n },\n (err, { code, map, metadata } = {}) => {\n if (err) return callback(err);\n\n metadataSubscribers.forEach((s) => passMetadata(s, this, metadata));\n\n return callback(null, code, map);\n }\n );\n }\n\n const { code, map, metadata } = transpile(source, options);\n\n this.callback(null, code, map);\n};\n")),(0,l.kt)("p",null,"babel-loader \u901a\u8fc7 callback \u4f20\u9012\u4e86\u7ecf\u8fc7 babel.transform \u8f6c\u6362\u540e\u7684\u4ee3\u7801\u53ca source map\u3002"),(0,l.kt)("h4",{id:"33-style-loader-\u4e0e-css-loader-\u5206\u6790"},"3.3 style-loader \u4e0e css-loader \u5206\u6790"),(0,l.kt)("p",null,"style-loader \u8d1f\u8d23\u5c06\u6837\u5f0f\u63d2\u5165\u5230 DOM \u4e2d\uff0c\u4f7f\u6837\u5f0f\u5bf9\u9875\u9762\u751f\u6548\u3002css-loader \u4e3b\u8981\u8d1f\u8d23\u5904\u7406 import\u3001url \u8def\u5f84\u7b49\u5916\u90e8\u5f15\u7528\u3002"),(0,l.kt)("p",null,"style-loader \u53ea\u6709 pitch \u51fd\u6570\u3002css-loader \u662f normal module\u3002\u6574\u4e2a\u6267\u884c\u6d41\u7a0b\u662f\u5148\u6267\u884c style-loader \u9636\u6bb5\uff0cstyle-loader \u4f1a\u521b\u5efa\u5f62\u5982 ",(0,l.kt)("inlineCode",{parentName:"p"},"require(!!./hzfe.css)")," \u7684\u4ee3\u7801\u8fd4\u56de\u7ed9 webpack\u3002webpack \u4f1a\u518d\u6b21\u8c03\u7528 css-loader \u5904\u7406\u6837\u5f0f\uff0ccss-loader \u4f1a\u8fd4\u56de\u5305\u542b runtime \u7684 js \u6a21\u5757\u7ed9 webpack \u53bb\u89e3\u6790\u3002style-loader \u5728\u4e0a\u4e00\u6b65\u6ce8\u5165 ",(0,l.kt)("inlineCode",{parentName:"p"},"require(!!./hzfe.css)")," \u7684\u540c\u65f6\uff0c\u4e5f\u6ce8\u5165\u4e86\u6dfb\u52a0 style \u6807\u7b7e\u7684\u4ee3\u7801\u3002\u8fd9\u6837\uff0c\u5728\u8fd0\u884c\u65f6\uff08\u6d4f\u89c8\u5668\u4e2d\uff09\uff0cstyle-loader \u5c31\u53ef\u4ee5\u628a css-loader \u7684\u6837\u5f0f\u63d2\u5165\u5230\u9875\u9762\u4e2d\u3002"),(0,l.kt)("p",null,"\u5e38\u89c1\u7684\u7591\u95ee\u5c31\u662f\u4e3a\u4ec0\u4e48\u4e0d\u6309\u7167 normal \u6a21\u5f0f\u7ec4\u7ec7 style-loader \u548c css-loader\u3002"),(0,l.kt)("p",null,"\u9996\u5148 css-loader \u8fd4\u56de\u7684\u662f\u5f62\u5982\u8fd9\u6837\u7684\u4ee3\u7801\uff1a"),(0,l.kt)("pre",null,(0,l.kt)("code",{parentName:"pre",className:"language-js"},'import ___CSS_LOADER_API_IMPORT___ from "../node_modules/_css-loader@5.1.3@css-loader/dist/runtime/api.js";\nvar ___CSS_LOADER_EXPORT___ = ___CSS_LOADER_API_IMPORT___(function (i) {\n return i[1];\n});\n// Module\n___CSS_LOADER_EXPORT___.push([\n module.id,\n ".hzfe{\\r\\n height: 100px;\\r\\n}",\n "",\n]);\n// Exports\nexport default ___CSS_LOADER_EXPORT___;\n')),(0,l.kt)("p",null,"style-loader \u65e0\u6cd5\u5728\u7f16\u8bd1\u65f6\u83b7\u53d6 CSS \u76f8\u5173\u7684\u5185\u5bb9\uff0c\u56e0\u4e3a style-loader \u65e0\u6cd5\u5904\u7406 css-loader \u751f\u6210\u7ed3\u679c\u7684 runtime \u4f9d\u8d56\u3002style-loader \u4e5f\u65e0\u6cd5\u5728\u8fd0\u884c\u65f6\u83b7\u53d6 CSS \u76f8\u5173\u7684\u5185\u5bb9\uff0c\u56e0\u4e3a\u65e0\u8bba\u600e\u6837\u62fc\u63a5\u8fd0\u884c\u65f6\u4ee3\u7801\uff0c\u90fd\u65e0\u6cd5\u83b7\u53d6\u5230 CSS \u7684\u5185\u5bb9\u3002"),(0,l.kt)("p",null,"\u4f5c\u4e3a\u66ff\u4ee3\uff0cstyle-loader \u91c7\u7528\u4e86 pitch \u65b9\u6848\uff0cstyle-loader \u7684\u6838\u5fc3\u529f\u80fd\u5982\u4e0b\u6240\u793a\uff1a"),(0,l.kt)("pre",null,(0,l.kt)("code",{parentName:"pre",className:"language-js",metastring:'title="style-loader"',title:'"style-loader"'},'module.exports.pitch = function (request) {\n var result = [\n // \u751f\u6210 require CSS \u6587\u4ef6\u7684\u8bed\u53e5\uff0c\u4ea4\u7ed9 css-loader \u89e3\u6790 \u5f97\u5230\u5305\u542b CSS \u5185\u5bb9\u7684 JS \u6a21\u5757\n // \u5176\u4e2d !! \u662f\u4e3a\u4e86\u907f\u514d webpack \u89e3\u6790\u65f6\u9012\u5f52\u8c03\u7528 style-loader\n `var content=require("${loaderUtils.stringifyRequest(this, `!!${request}`)}")`,\n // \u5728\u8fd0\u884c\u65f6\u8c03\u7528 addStyle \u628a CSS \u5185\u5bb9\u63d2\u5165\u5230 DOM \u4e2d\n `require("${loaderUtils.stringifyRequest(this, `!${path.join(__dirname, "add-style.js")}`)}")(content)`\n // \u5982\u679c\u53d1\u73b0\u542f\u7528\u4e86 CSS modules\uff0c\u5219\u9ed8\u8ba4\u5bfc\u51fa\u5b83\n "if(content.locals) module.exports = content.locals",\n ];\n return result.join(";");\n};\n')),(0,l.kt)("pre",null,(0,l.kt)("code",{parentName:"pre",className:"language-js",metastring:'title="add-style.js"',title:'"add-style.js"'},'module.exports = function (content) {\n var style = document.createElement("style");\n style.innerHTML = content;\n document.head.appendChild(style);\n};\n')),(0,l.kt)("p",null,"\u5728 pitch \u9636\u6bb5\uff0cstyle-loader \u751f\u6210 require CSS \u4ee5\u53ca\u6ce8\u5165 runtime \u7684\u4ee3\u7801\u3002\u8be5\u7ed3\u679c\u4f1a\u8fd4\u56de\u7ed9 webpack \u8fdb\u4e00\u6b65\u89e3\u6790\uff0ccss-loader \u8fd4\u56de\u7684\u7ed3\u679c\u4f1a\u4f5c\u4e3a\u6a21\u5757\u5728\u8fd0\u884c\u65f6\u5bfc\u5165\uff0c\u5728\u8fd0\u884c\u65f6\u80fd\u591f\u83b7\u5f97 CSS \u7684\u5185\u5bb9\uff0c\u7136\u540e\u8c03\u7528 add-style.js \u628a CSS \u5185\u5bb9\u63d2\u5165\u5230 DOM \u4e2d\u3002"),(0,l.kt)("h2",{id:"\u53c2\u8003\u8d44\u6599"},"\u53c2\u8003\u8d44\u6599"),(0,l.kt)("ol",null,(0,l.kt)("li",{parentName:"ol"},(0,l.kt)("a",{parentName:"li",href:"https://webpack.js.org/contribute/writing-a-loader/"},"writting a loader")),(0,l.kt)("li",{parentName:"ol"},(0,l.kt)("a",{parentName:"li",href:"https://webpack.js.org/api/loaders/"},"Loader Interface")),(0,l.kt)("li",{parentName:"ol"},(0,l.kt)("a",{parentName:"li",href:"https://github.com/webpack/loader-runner"},"loader runner"))))}u.isMDXComponent=!0}}]);