web 3d图形渲染器
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

763 lines
15 KiB

  1. <div align="center">
  2. <a href="https://github.com/webpack/webpack">
  3. <img width="200" height="200" src="https://webpack.js.org/assets/icon-square-big.svg">
  4. </a>
  5. </div>
  6. [![npm][npm]][npm-url]
  7. [![node][node]][node-url]
  8. [![deps][deps]][deps-url]
  9. [![tests][tests]][tests-url]
  10. [![coverage][cover]][cover-url]
  11. [![chat][chat]][chat-url]
  12. [![size][size]][size-url]
  13. # file-loader
  14. The `file-loader` resolves `import`/`require()` on a file into a url and emits the file into the output directory.
  15. ## Getting Started
  16. To begin, you'll need to install `file-loader`:
  17. ```console
  18. $ npm install file-loader --save-dev
  19. ```
  20. Import (or `require`) the target file(s) in one of the bundle's files:
  21. **file.js**
  22. ```js
  23. import img from './file.png';
  24. ```
  25. Then add the loader to your `webpack` config. For example:
  26. **webpack.config.js**
  27. ```js
  28. module.exports = {
  29. module: {
  30. rules: [
  31. {
  32. test: /\.(png|jpe?g|gif)$/i,
  33. use: [
  34. {
  35. loader: 'file-loader',
  36. },
  37. ],
  38. },
  39. ],
  40. },
  41. };
  42. ```
  43. And run `webpack` via your preferred method. This will emit `file.png` as a file
  44. in the output directory (with the specified naming convention, if options are
  45. specified to do so) and returns the public URI of the file.
  46. > ℹ️ By default the filename of the resulting file is the hash of the file's contents with the original extension of the required resource.
  47. ## Options
  48. ### `name`
  49. Type: `String|Function`
  50. Default: `'[contenthash].[ext]'`
  51. Specifies a custom filename template for the target file(s) using the query
  52. parameter `name`. For example, to emit a file from your `context` directory into
  53. the output directory retaining the full directory structure, you might use:
  54. #### `String`
  55. **webpack.config.js**
  56. ```js
  57. module.exports = {
  58. module: {
  59. rules: [
  60. {
  61. test: /\.(png|jpe?g|gif)$/i,
  62. loader: 'file-loader',
  63. options: {
  64. name: '[path][name].[ext]',
  65. },
  66. },
  67. ],
  68. },
  69. };
  70. ```
  71. #### `Function`
  72. **webpack.config.js**
  73. ```js
  74. module.exports = {
  75. module: {
  76. rules: [
  77. {
  78. test: /\.(png|jpe?g|gif)$/i,
  79. loader: 'file-loader',
  80. options: {
  81. name(resourcePath, resourceQuery) {
  82. // `resourcePath` - `/absolute/path/to/file.js`
  83. // `resourceQuery` - `?foo=bar`
  84. if (process.env.NODE_ENV === 'development') {
  85. return '[path][name].[ext]';
  86. }
  87. return '[contenthash].[ext]';
  88. },
  89. },
  90. },
  91. ],
  92. },
  93. };
  94. ```
  95. > ℹ️ By default the path and name you specify will output the file in that same directory, and will also use the same URI path to access the file.
  96. ### `outputPath`
  97. Type: `String|Function`
  98. Default: `undefined`
  99. Specify a filesystem path where the target file(s) will be placed.
  100. #### `String`
  101. **webpack.config.js**
  102. ```js
  103. module.exports = {
  104. module: {
  105. rules: [
  106. {
  107. test: /\.(png|jpe?g|gif)$/i,
  108. loader: 'file-loader',
  109. options: {
  110. outputPath: 'images',
  111. },
  112. },
  113. ],
  114. },
  115. };
  116. ```
  117. #### `Function`
  118. **webpack.config.js**
  119. ```js
  120. module.exports = {
  121. module: {
  122. rules: [
  123. {
  124. test: /\.(png|jpe?g|gif)$/i,
  125. loader: 'file-loader',
  126. options: {
  127. outputPath: (url, resourcePath, context) => {
  128. // `resourcePath` is original absolute path to asset
  129. // `context` is directory where stored asset (`rootContext`) or `context` option
  130. // To get relative path you can use
  131. // const relativePath = path.relative(context, resourcePath);
  132. if (/my-custom-image\.png/.test(resourcePath)) {
  133. return `other_output_path/${url}`;
  134. }
  135. if (/images/.test(context)) {
  136. return `image_output_path/${url}`;
  137. }
  138. return `output_path/${url}`;
  139. },
  140. },
  141. },
  142. ],
  143. },
  144. };
  145. ```
  146. ### `publicPath`
  147. Type: `String|Function`
  148. Default: [`__webpack_public_path__`](https://webpack.js.org/api/module-variables/#__webpack_public_path__-webpack-specific-)+outputPath
  149. Specifies a custom public path for the target file(s).
  150. #### `String`
  151. **webpack.config.js**
  152. ```js
  153. module.exports = {
  154. module: {
  155. rules: [
  156. {
  157. test: /\.(png|jpe?g|gif)$/i,
  158. loader: 'file-loader',
  159. options: {
  160. publicPath: 'assets',
  161. },
  162. },
  163. ],
  164. },
  165. };
  166. ```
  167. #### `Function`
  168. **webpack.config.js**
  169. ```js
  170. module.exports = {
  171. module: {
  172. rules: [
  173. {
  174. test: /\.(png|jpe?g|gif)$/i,
  175. loader: 'file-loader',
  176. options: {
  177. publicPath: (url, resourcePath, context) => {
  178. // `resourcePath` is original absolute path to asset
  179. // `context` is directory where stored asset (`rootContext`) or `context` option
  180. // To get relative path you can use
  181. // const relativePath = path.relative(context, resourcePath);
  182. if (/my-custom-image\.png/.test(resourcePath)) {
  183. return `other_public_path/${url}`;
  184. }
  185. if (/images/.test(context)) {
  186. return `image_output_path/${url}`;
  187. }
  188. return `public_path/${url}`;
  189. },
  190. },
  191. },
  192. ],
  193. },
  194. };
  195. ```
  196. ### `postTransformPublicPath`
  197. Type: `Function`
  198. Default: `undefined`
  199. Specifies a custom function to post-process the generated public path. This can be used to prepend or append dynamic global variables that are only available at runtime, like `__webpack_public_path__`. This would not be possible with just `publicPath`, since it stringifies the values.
  200. **webpack.config.js**
  201. ```js
  202. module.exports = {
  203. module: {
  204. rules: [
  205. {
  206. test: /\.(png|jpg|gif)$/i,
  207. loader: 'file-loader',
  208. options: {
  209. publicPath: '/some/path/',
  210. postTransformPublicPath: (p) => `__webpack_public_path__ + ${p}`,
  211. },
  212. },
  213. ],
  214. },
  215. };
  216. ```
  217. ### `context`
  218. Type: `String`
  219. Default: [`context`](https://webpack.js.org/configuration/entry-context/#context)
  220. Specifies a custom file context.
  221. ```js
  222. module.exports = {
  223. module: {
  224. rules: [
  225. {
  226. test: /\.(png|jpe?g|gif)$/i,
  227. use: [
  228. {
  229. loader: 'file-loader',
  230. options: {
  231. context: 'project',
  232. },
  233. },
  234. ],
  235. },
  236. ],
  237. },
  238. };
  239. ```
  240. ### `emitFile`
  241. Type: `Boolean`
  242. Default: `true`
  243. If true, emits a file (writes a file to the filesystem). If false, the loader
  244. will return a public URI but **will not** emit the file. It is often useful to
  245. disable this option for server-side packages.
  246. **file.js**
  247. ```js
  248. // bundle file
  249. import img from './file.png';
  250. ```
  251. **webpack.config.js**
  252. ```js
  253. module.exports = {
  254. module: {
  255. rules: [
  256. {
  257. test: /\.css$/i,
  258. use: [
  259. {
  260. loader: 'file-loader',
  261. options: {
  262. emitFile: false,
  263. },
  264. },
  265. ],
  266. },
  267. ],
  268. },
  269. };
  270. ```
  271. ### `regExp`
  272. Type: `RegExp`
  273. Default: `undefined`
  274. Specifies a Regular Expression to one or many parts of the target file path.
  275. The capture groups can be reused in the `name` property using `[N]`
  276. [placeholder](https://github.com/webpack-contrib/file-loader#placeholders).
  277. **file.js**
  278. ```js
  279. import img from './customer01/file.png';
  280. ```
  281. **webpack.config.js**
  282. ```js
  283. module.exports = {
  284. module: {
  285. rules: [
  286. {
  287. test: /\.(png|jpe?g|gif)$/i,
  288. use: [
  289. {
  290. loader: 'file-loader',
  291. options: {
  292. regExp: /\/([a-z0-9]+)\/[a-z0-9]+\.png$/i,
  293. name: '[1]-[name].[ext]',
  294. },
  295. },
  296. ],
  297. },
  298. ],
  299. },
  300. };
  301. ```
  302. > ℹ️ If `[0]` is used, it will be replaced by the entire tested string, whereas `[1]` will contain the first capturing parenthesis of your regex and so on...
  303. ### `esModule`
  304. Type: `Boolean`
  305. Default: `true`
  306. By default, `file-loader` generates JS modules that use the ES modules syntax.
  307. There are some cases in which using ES modules is beneficial, like in the case of [module concatenation](https://webpack.js.org/plugins/module-concatenation-plugin/) and [tree shaking](https://webpack.js.org/guides/tree-shaking/).
  308. You can enable a CommonJS module syntax using:
  309. **webpack.config.js**
  310. ```js
  311. module.exports = {
  312. module: {
  313. rules: [
  314. {
  315. test: /\.css$/,
  316. use: [
  317. {
  318. loader: 'file-loader',
  319. options: {
  320. esModule: false,
  321. },
  322. },
  323. ],
  324. },
  325. ],
  326. },
  327. };
  328. ```
  329. ## Placeholders
  330. Full information about placeholders you can find [here](https://github.com/webpack/loader-utils#interpolatename).
  331. ### `[ext]`
  332. Type: `String`
  333. Default: `file.extname`
  334. The file extension of the target file/resource.
  335. ### `[name]`
  336. Type: `String`
  337. Default: `file.basename`
  338. The basename of the file/resource.
  339. ### `[path]`
  340. Type: `String`
  341. Default: `file.directory`
  342. The path of the resource relative to the webpack/config `context`.
  343. ### `[folder]`
  344. Type: `String`
  345. Default: `file.folder`
  346. The folder of the resource is in.
  347. ### `[query]`
  348. Type: `String`
  349. Default: `file.query`
  350. The query of the resource, i.e. `?foo=bar`.
  351. ### `[emoji]`
  352. Type: `String`
  353. Default: `undefined`
  354. A random emoji representation of `content`.
  355. ### `[emoji:<length>]`
  356. Type: `String`
  357. Default: `undefined`
  358. Same as above, but with a customizable number of emojis
  359. ### `[hash]`
  360. Type: `String`
  361. Default: `md4`
  362. Specifies the hash method to use for hashing the file content.
  363. ### `[contenthash]`
  364. Type: `String`
  365. Default: `md4`
  366. Specifies the hash method to use for hashing the file content.
  367. ### `[<hashType>:hash:<digestType>:<length>]`
  368. Type: `String`
  369. The hash of options.content (Buffer) (by default it's the hex digest of the hash).
  370. #### `digestType`
  371. Type: `String`
  372. Default: `'hex'`
  373. The [digest](https://en.wikipedia.org/wiki/Cryptographic_hash_function) that the
  374. hash function should use. Valid values include: base26, base32, base36,
  375. base49, base52, base58, base62, base64, and hex.
  376. #### `hashType`
  377. Type: `String`
  378. Default: `'md4'`
  379. The type of hash that the has function should use. Valid values include: `md4`, `md5`, `sha1`, `sha256`, and `sha512`.
  380. #### `length`
  381. Type: `Number`
  382. Default: `undefined`
  383. Users may also specify a length for the computed hash.
  384. ### `[N]`
  385. Type: `String`
  386. Default: `undefined`
  387. The n-th match obtained from matching the current file name against the `regExp`.
  388. ## Examples
  389. ### Names
  390. The following examples show how one might use `file-loader` and what the result would be.
  391. **file.js**
  392. ```js
  393. import png from './image.png';
  394. ```
  395. **webpack.config.js**
  396. ```js
  397. module.exports = {
  398. module: {
  399. rules: [
  400. {
  401. test: /\.(png|jpe?g|gif)$/i,
  402. use: [
  403. {
  404. loader: 'file-loader',
  405. options: {
  406. name: 'dirname/[contenthash].[ext]',
  407. },
  408. },
  409. ],
  410. },
  411. ],
  412. },
  413. };
  414. ```
  415. Result:
  416. ```bash
  417. # result
  418. dirname/0dcbbaa701328ae351f.png
  419. ```
  420. ---
  421. **file.js**
  422. ```js
  423. import png from './image.png';
  424. ```
  425. **webpack.config.js**
  426. ```js
  427. module.exports = {
  428. module: {
  429. rules: [
  430. {
  431. test: /\.(png|jpe?g|gif)$/i,
  432. use: [
  433. {
  434. loader: 'file-loader',
  435. options: {
  436. name: '[sha512:hash:base64:7].[ext]',
  437. },
  438. },
  439. ],
  440. },
  441. ],
  442. },
  443. };
  444. ```
  445. Result:
  446. ```bash
  447. # result
  448. gdyb21L.png
  449. ```
  450. ---
  451. **file.js**
  452. ```js
  453. import png from './path/to/file.png';
  454. ```
  455. **webpack.config.js**
  456. ```js
  457. module.exports = {
  458. module: {
  459. rules: [
  460. {
  461. test: /\.(png|jpe?g|gif)$/i,
  462. use: [
  463. {
  464. loader: 'file-loader',
  465. options: {
  466. name: '[path][name].[ext]?[contenthash]',
  467. },
  468. },
  469. ],
  470. },
  471. ],
  472. },
  473. };
  474. ```
  475. Result:
  476. ```bash
  477. # result
  478. path/to/file.png?e43b20c069c4a01867c31e98cbce33c9
  479. ```
  480. ### CDN
  481. The following examples show how to use `file-loader` for CDN uses query params.
  482. **file.js**
  483. ```js
  484. import png from './directory/image.png?width=300&height=300';
  485. ```
  486. **webpack.config.js**
  487. ```js
  488. module.exports = {
  489. output: {
  490. publicPath: 'https://cdn.example.com/',
  491. },
  492. module: {
  493. rules: [
  494. {
  495. test: /\.(png|jpe?g|gif)$/i,
  496. use: [
  497. {
  498. loader: 'file-loader',
  499. options: {
  500. name: '[path][name].[ext][query]',
  501. },
  502. },
  503. ],
  504. },
  505. ],
  506. },
  507. };
  508. ```
  509. Result:
  510. ```bash
  511. # result
  512. https://cdn.example.com/directory/image.png?width=300&height=300
  513. ```
  514. ### Dynamic public path depending on environment variable at run time
  515. An application might want to configure different CDN hosts depending on an environment variable that is only available when running the application. This can be an advantage, as only one build of the application is necessary, which behaves differently depending on environment variables of the deployment environment. Since file-loader is applied when compiling the application, and not when running it, the environment variable cannot be used in the file-loader configuration. A way around this is setting the `__webpack_public_path__` to the desired CDN host depending on the environment variable at the entrypoint of the application. The option `postTransformPublicPath` can be used to configure a custom path depending on a variable like `__webpack_public_path__`.
  516. **main.js**
  517. ```js
  518. const assetPrefixForNamespace = (namespace) => {
  519. switch (namespace) {
  520. case 'prod':
  521. return 'https://cache.myserver.net/web';
  522. case 'uat':
  523. return 'https://cache-uat.myserver.net/web';
  524. case 'st':
  525. return 'https://cache-st.myserver.net/web';
  526. case 'dev':
  527. return 'https://cache-dev.myserver.net/web';
  528. default:
  529. return '';
  530. }
  531. };
  532. const namespace = process.env.NAMESPACE;
  533. __webpack_public_path__ = `${assetPrefixForNamespace(namespace)}/`;
  534. ```
  535. **file.js**
  536. ```js
  537. import png from './image.png';
  538. ```
  539. **webpack.config.js**
  540. ```js
  541. module.exports = {
  542. module: {
  543. rules: [
  544. {
  545. test: /\.(png|jpg|gif)$/i,
  546. loader: 'file-loader',
  547. options: {
  548. name: '[name].[contenthash].[ext]',
  549. outputPath: 'static/assets/',
  550. publicPath: 'static/assets/',
  551. postTransformPublicPath: (p) => `__webpack_public_path__ + ${p}`,
  552. },
  553. },
  554. ],
  555. },
  556. };
  557. ```
  558. Result when run with `NAMESPACE=prod` env variable:
  559. ```bash
  560. # result
  561. https://cache.myserver.net/web/static/assets/image.somehash.png
  562. ```
  563. Result when run with `NAMESPACE=dev` env variable:
  564. ```bash
  565. # result
  566. https://cache-dev.myserver.net/web/static/assets/image.somehash.png
  567. ```
  568. ## Contributing
  569. Please take a moment to read our contributing guidelines if you haven't yet done so.
  570. [CONTRIBUTING](./.github/CONTRIBUTING.md)
  571. ## License
  572. [MIT](./LICENSE)
  573. [npm]: https://img.shields.io/npm/v/file-loader.svg
  574. [npm-url]: https://npmjs.com/package/file-loader
  575. [node]: https://img.shields.io/node/v/file-loader.svg
  576. [node-url]: https://nodejs.org
  577. [deps]: https://david-dm.org/webpack-contrib/file-loader.svg
  578. [deps-url]: https://david-dm.org/webpack-contrib/file-loader
  579. [tests]: https://github.com/webpack-contrib/file-loader/workflows/file-loader/badge.svg
  580. [tests-url]: https://github.com/webpack-contrib/file-loader/actions
  581. [cover]: https://codecov.io/gh/webpack-contrib/file-loader/branch/master/graph/badge.svg
  582. [cover-url]: https://codecov.io/gh/webpack-contrib/file-loader
  583. [chat]: https://img.shields.io/badge/gitter-webpack%2Fwebpack-brightgreen.svg
  584. [chat-url]: https://gitter.im/webpack/webpack
  585. [size]: https://packagephobia.now.sh/badge?p=file-loader
  586. [size-url]: https://packagephobia.now.sh/result?p=file-loader