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 |
|
17 | A webpack loader which executes a given module, and returns the result of the
|
18 | execution at build-time, when the module is required in the bundle. In this way,
|
19 | the loader changes a module from code to a result.
|
20 |
|
21 | Another way to view `val-loader`, is that it allows a user a way to make their
|
22 | own custom loader logic, without having to write a custom loader.
|
23 |
|
24 | ## Getting Started
|
25 |
|
26 | To begin, you'll need to install `val-loader`:
|
27 |
|
28 | ```console
|
29 | $ npm install val-loader --save-dev
|
30 | ```
|
31 |
|
32 | Then add the loader to your `webpack` config. For example:
|
33 |
|
34 | **target-file.js**
|
35 |
|
36 | ```js
|
37 | module.exports = () => {
|
38 | return { code: 'module.exports = 42;' };
|
39 | };
|
40 | ```
|
41 |
|
42 | **webpack.config.js**
|
43 |
|
44 | ```js
|
45 | module.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
|
64 | const answer = require('target-file');
|
65 | ```
|
66 |
|
67 | And run `webpack` via your preferred method.
|
68 |
|
69 | ## Return Object Properties
|
70 |
|
71 | Targeted modules of this loader must export either a `Function` or `Promise`
|
72 | that returns an object containing a `code` property at a minimum, but can
|
73 | contain any number of additional properties.
|
74 |
|
75 | ### `code`
|
76 |
|
77 | Type: `String|Buffer`
|
78 | Default: `undefined`
|
79 | _Required_
|
80 |
|
81 | Code passed along to webpack or the next loader that will replace the module.
|
82 |
|
83 | ### `sourceMap`
|
84 |
|
85 | Type: `Object`
|
86 | Default: `undefined`
|
87 |
|
88 | A source map passed along to webpack or the next loader.
|
89 |
|
90 | ### `ast`
|
91 |
|
92 | Type: `Array[Object]`
|
93 | Default: `undefined`
|
94 |
|
95 | An [Abstract Syntax Tree](https://en.wikipedia.org/wiki/Abstract_syntax_tree)
|
96 | that will be passed to the next loader. Useful to speed up the build time if the
|
97 | next loader uses the same AST.
|
98 |
|
99 | ### `dependencies`
|
100 |
|
101 | Type: `Array[String]`
|
102 | Default: `[]`
|
103 |
|
104 | An array of absolute, native paths to file dependencies that should be watched
|
105 | by webpack for changes.
|
106 |
|
107 | ### `contextDependencies`
|
108 |
|
109 | Type: `Array[String]`
|
110 | Default: `[]`
|
111 |
|
112 | An array of absolute, native paths to directory dependencies that should be
|
113 | watched by webpack for changes.
|
114 |
|
115 | ### `cacheable`
|
116 |
|
117 | Type: `Boolean`
|
118 | Default: `false`
|
119 |
|
120 | If `true`, specifies that the code can be re-used in watch mode if none of the
|
121 | `dependencies` have changed.
|
122 |
|
123 | ## Examples
|
124 |
|
125 | In 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
|
127 | result of the execution. This example passes `years` as an `option`, which
|
128 | corresponds to the `years` parameter in the target module exported function:
|
129 |
|
130 | **years-in-ms.js**
|
131 |
|
132 | ```js
|
133 | module.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
|
143 | module.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 |
|
162 | In the bundle, requiring the module then returns:
|
163 |
|
164 | ```js
|
165 | // ... bundle code ...
|
166 |
|
167 | const tenYearsMs = require('years-in-ms'); // 315360000000
|
168 | ```
|
169 |
|
170 | ```js
|
171 | // ... bundle code ...
|
172 |
|
173 | require('val-loader!tenyearsinms') == 315360000000;
|
174 | ```
|
175 |
|
176 | ## Contributing
|
177 |
|
178 | Please 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
|