UNPKG

4.79 kBMarkdownView Raw
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
7[![npm][npm]][npm-url]
8[![node][node]][node-url]
9[![deps][deps]][deps-url]
10[![tests][tests]][tests-url]
11[![coverage][cover]][cover-url]
12[![chat][chat]][chat-url]
13[![size][size]][size-url]
14
15# val-loader
16
17A webpack loader which executes a given module, and returns the result of the
18execution at build-time, when the module is required in the bundle. In this way,
19the loader changes a module from code to a result.
20
21Another way to view `val-loader`, is that it allows a user a way to make their
22own custom loader logic, without having to write a custom loader.
23
24## Getting Started
25
26To begin, you'll need to install `val-loader`:
27
28```console
29$ npm install val-loader --save-dev
30```
31
32Then add the loader to your `webpack` config. For example:
33
34**target-file.js**
35
36```js
37module.exports = () => {
38 return { code: 'module.exports = 42;' };
39};
40```
41
42**webpack.config.js**
43
44```js
45module.exports = {
46 module: {
47 rules: [
48 {
49 test: /target-file.js$/,
50 use: [
51 {
52 loader: `val-loader`,
53 },
54 ],
55 },
56 ],
57 },
58};
59```
60
61**src/entry.js**
62
63```js
64const answer = require('target-file');
65```
66
67And run `webpack` via your preferred method.
68
69## Return Object Properties
70
71Targeted modules of this loader must export either a `Function` or `Promise`
72that returns an object containing a `code` property at a minimum, but can
73contain any number of additional properties.
74
75### `code`
76
77Type: `String|Buffer`
78Default: `undefined`
79_Required_
80
81Code passed along to webpack or the next loader that will replace the module.
82
83### `sourceMap`
84
85Type: `Object`
86Default: `undefined`
87
88A source map passed along to webpack or the next loader.
89
90### `ast`
91
92Type: `Array[Object]`
93Default: `undefined`
94
95An [Abstract Syntax Tree](https://en.wikipedia.org/wiki/Abstract_syntax_tree)
96that will be passed to the next loader. Useful to speed up the build time if the
97next loader uses the same AST.
98
99### `dependencies`
100
101Type: `Array[String]`
102Default: `[]`
103
104An array of absolute, native paths to file dependencies that should be watched
105by webpack for changes.
106
107### `contextDependencies`
108
109Type: `Array[String]`
110Default: `[]`
111
112An array of absolute, native paths to directory dependencies that should be
113watched by webpack for changes.
114
115### `cacheable`
116
117Type: `Boolean`
118Default: `false`
119
120If `true`, specifies that the code can be re-used in watch mode if none of the
121`dependencies` have changed.
122
123## Examples
124
125In this example the loader is configured to operator on a file name of
126`years-in-ms.js`, execute the code, and store the result in the bundle as the
127result of the execution. This example passes `years` as an `option`, which
128corresponds to the `years` parameter in the target module exported function:
129
130**years-in-ms.js**
131
132```js
133module.exports = function yearsInMs({ years }) {
134 const value = years * 365 * 24 * 60 * 60 * 1000;
135 // NOTE: this return value will replace the module in the bundle
136 return { code: 'module.exports = ' + value };
137};
138```
139
140**webpack.config.js**
141
142```js
143module.exports = {
144 module: {
145 rules: [
146 {
147 test: require.resolve('src/years-in-ms.js'),
148 use: [
149 {
150 loader: 'val-loader',
151 options: {
152 years: 10,
153 },
154 },
155 ],
156 },
157 ],
158 },
159};
160```
161
162In the bundle, requiring the module then returns:
163
164```js
165// ... bundle code ...
166
167const tenYearsMs = require('years-in-ms'); // 315360000000
168```
169
170```js
171// ... bundle code ...
172
173require('val-loader!tenyearsinms') == 315360000000;
174```
175
176## Contributing
177
178Please take a moment to read our contributing guidelines if you haven't yet done so.
179
180[CONTRIBUTING](./.github/CONTRIBUTING.md)
181
182## License
183
184[MIT](./LICENSE)
185
186[npm]: https://img.shields.io/npm/v/val-loader.svg
187[npm-url]: https://npmjs.com/package/val-loader
188[node]: https://img.shields.io/node/v/val-loader.svg
189[node-url]: https://nodejs.org
190[deps]: https://david-dm.org/webpack-contrib/val-loader.svg
191[deps-url]: https://david-dm.org/webpack-contrib/val-loader
192[tests]: https://dev.azure.com/webpack-contrib/val-loader/_apis/build/status/webpack-contrib.val-loader?branchName=master
193[tests-url]: https://dev.azure.com/webpack-contrib/val-loader/_build/latest?definitionId=2&branchName=master
194[cover]: https://codecov.io/gh/webpack-contrib/val-loader/branch/master/graph/badge.svg
195[cover-url]: https://codecov.io/gh/webpack-contrib/val-loader
196[chat]: https://img.shields.io/badge/gitter-webpack%2Fwebpack-brightgreen.svg
197[chat-url]: https://gitter.im/webpack/webpack
198[size]: https://packagephobia.now.sh/badge?p=val-loader
199[size-url]: https://packagephobia.now.sh/result?p=val-loader