UNPKG

3.33 kBMarkdownView Raw
1<div align="center">
2 <img width="200" height="200"
3 src="https://cdn3.iconfinder.com/data/icons/lexter-flat-colorfull-file-formats/56/raw-256.png">
4 <a href="https://github.com/webpack/webpack">
5 <img width="200" height="200"
6 src="https://webpack.js.org/assets/icon-square-big.svg">
7 </a>
8</div>
9
10[![npm][npm]][npm-url]
11[![node][node]][node-url]
12[![deps][deps]][deps-url]
13[![tests][tests]][tests-url]
14[![coverage][cover]][cover-url]
15[![chat][chat]][chat-url]
16[![size][size]][size-url]
17
18# raw-loader
19
20A loader for webpack that allows importing files as a String.
21
22## Getting Started
23
24To begin, you'll need to install `raw-loader`:
25
26```console
27$ npm install raw-loader --save-dev
28```
29
30Then add the loader to your `webpack` config. For example:
31
32**file.js**
33
34```js
35import txt from './file.txt';
36```
37
38**webpack.config.js**
39
40```js
41// webpack.config.js
42module.exports = {
43 module: {
44 rules: [
45 {
46 test: /\.txt$/i,
47 use: 'raw-loader',
48 },
49 ],
50 },
51};
52```
53
54And run `webpack` via your preferred method.
55
56## Options
57
58| Name | Type | Default | Description |
59| :-------------------------: | :---------: | :-----: | :--------------------- |
60| **[`esModule`](#esmodule)** | `{Boolean}` | `true` | Uses ES modules syntax |
61
62### `esModule`
63
64Type: `Boolean`
65Default: `true`
66
67By default, `raw-loader` generates JS modules that use the ES modules syntax.
68There 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/).
69
70You can enable a CommonJS module syntax using:
71
72**webpack.config.js**
73
74```js
75module.exports = {
76 module: {
77 rules: [
78 {
79 test: /\.txt$/i,
80 use: [
81 {
82 loader: 'raw-loader',
83 options: {
84 esModule: false,
85 },
86 },
87 ],
88 },
89 ],
90 },
91};
92```
93
94## Examples
95
96### Inline
97
98```js
99import txt from 'raw-loader!./file.txt';
100```
101
102Beware, if you already define loader(s) for extension(s) in `webpack.config.js` you should use:
103
104```js
105import css from '!!raw-loader!./file.txt'; // Adding `!!` to a request will disable all loaders specified in the configuration
106```
107
108## Contributing
109
110Please take a moment to read our contributing guidelines if you haven't yet done so.
111
112[CONTRIBUTING](./.github/CONTRIBUTING.md)
113
114## License
115
116[MIT](./LICENSE)
117
118[npm]: https://img.shields.io/npm/v/raw-loader.svg
119[npm-url]: https://npmjs.com/package/raw-loader
120[node]: https://img.shields.io/node/v/raw-loader.svg
121[node-url]: https://nodejs.org
122[deps]: https://david-dm.org/webpack-contrib/raw-loader.svg
123[deps-url]: https://david-dm.org/webpack-contrib/raw-loader
124[tests]: https://github.com/webpack-contrib/raw-loader/workflows/raw-loader/badge.svg
125[tests-url]: https://github.com/webpack-contrib/raw-loader/actions
126[cover]: https://codecov.io/gh/webpack-contrib/raw-loader/branch/master/graph/badge.svg
127[cover-url]: https://codecov.io/gh/webpack-contrib/raw-loader
128[chat]: https://img.shields.io/badge/gitter-webpack%2Fwebpack-brightgreen.svg
129[chat-url]: https://gitter.im/webpack/webpack
130[size]: https://packagephobia.now.sh/badge?p=raw-loader
131[size-url]: https://packagephobia.now.sh/result?p=raw-loader