Skip to content
This repository was archived by the owner on Apr 28, 2025. It is now read-only.

Commit afc43f8

Browse files
feat: name option (#20)
1 parent a654cd7 commit afc43f8

File tree

6 files changed

+127
-15
lines changed

6 files changed

+127
-15
lines changed

README.md

+68-3
Original file line numberDiff line numberDiff line change
@@ -92,9 +92,10 @@ And run `webpack` via your preferred method.
9292

9393
## Options
9494

95-
| Name | Type | Default | Description |
96-
| :-------------------: | :--------: | :---------: | :---------------------------------------------------- |
97-
| **[`flags`](#flags)** | `{Number}` | `undefined` | Enables/Disables `url`/`image-set` functions handling |
95+
| Name | Type | Default | Description |
96+
| :-------------------: | :------------------: | :---------------------: | :----------------------------------------------------------- |
97+
| **[`flags`](#flags)** | `{Number}` | `undefined` | Enables/Disables `url`/`image-set` functions handling |
98+
| **[`name`](#name)** | `{String\|Function}` | `'[contenthash].[ext]'` | Specifies a custom filename template for the target file(s). |
9899

99100
### `flags`
100101

@@ -134,6 +135,70 @@ module.exports = {
134135
};
135136
```
136137

138+
### `name`
139+
140+
Type: `String|Function`
141+
Default: `'[contenthash].[ext]'`
142+
143+
Specifies a custom filename template for the target file(s).
144+
145+
#### `String`
146+
147+
**webpack.config.js**
148+
149+
```js
150+
module.exports = {
151+
target: 'node',
152+
node: {
153+
__dirname: false,
154+
},
155+
module: {
156+
rules: [
157+
{
158+
test: /\.node$/,
159+
loader: 'node-loader',
160+
options: {
161+
name: '[path][name].[ext]',
162+
},
163+
},
164+
],
165+
},
166+
};
167+
```
168+
169+
#### `Function`
170+
171+
**webpack.config.js**
172+
173+
```js
174+
module.exports = {
175+
target: 'node',
176+
node: {
177+
__dirname: false,
178+
},
179+
module: {
180+
rules: [
181+
{
182+
test: /\.node$/,
183+
loader: 'node-loader',
184+
options: {
185+
name(resourcePath, resourceQuery) {
186+
// `resourcePath` - `/absolute/path/to/file.js`
187+
// `resourceQuery` - `?foo=bar`
188+
189+
if (process.env.NODE_ENV === 'development') {
190+
return '[path][name].[ext]';
191+
}
192+
193+
return '[contenthash].[ext]';
194+
},
195+
},
196+
},
197+
],
198+
},
199+
};
200+
```
201+
137202
## Contributing
138203

139204
Please take a moment to read our contributing guidelines if you haven't yet done so.

src/index.js

+9-3
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,8 @@
22
MIT License http://www.opensource.org/licenses/mit-license.php
33
Author Tobias Koppers @sokra
44
*/
5-
import path from 'path';
65

7-
import { getOptions } from 'loader-utils';
6+
import { getOptions, interpolateName } from 'loader-utils';
87
import validateOptions from 'schema-utils';
98

109
import schema from './options.json';
@@ -17,7 +16,14 @@ export default function loader(content) {
1716
baseDataPath: 'options',
1817
});
1918

20-
const name = path.basename(this.resourcePath);
19+
const name = interpolateName(
20+
this,
21+
typeof options.name !== 'undefined' ? options.name : '[contenthash].[ext]',
22+
{
23+
context: this.rootContext,
24+
content,
25+
}
26+
);
2127

2228
this.emitFile(name, content);
2329

src/options.json

+10
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,16 @@
11
{
22
"type": "object",
33
"properties": {
4+
"name": {
5+
"anyOf": [
6+
{
7+
"type": "string"
8+
},
9+
{
10+
"instanceof": "Function"
11+
}
12+
]
13+
},
414
"flags": {
515
"type": "integer"
616
}

test/__snapshots__/validate-options.test.js.snap

+26-8
Original file line numberDiff line numberDiff line change
@@ -15,50 +15,68 @@ exports[`validate options should throw an error on the "flags" option with "true
1515
- options.flags should be a integer."
1616
`;
1717

18+
exports[`validate options should throw an error on the "name" option with "false" value 1`] = `
19+
"Invalid options object. Node Loader has been initialized using an options object that does not match the API schema.
20+
- options.name should be one of these:
21+
string | function
22+
Details:
23+
* options.name should be a string.
24+
* options.name should be an instance of function."
25+
`;
26+
27+
exports[`validate options should throw an error on the "name" option with "true" value 1`] = `
28+
"Invalid options object. Node Loader has been initialized using an options object that does not match the API schema.
29+
- options.name should be one of these:
30+
string | function
31+
Details:
32+
* options.name should be a string.
33+
* options.name should be an instance of function."
34+
`;
35+
1836
exports[`validate options should throw an error on the "unknown" option with "/test/" value 1`] = `
1937
"Invalid options object. Node Loader has been initialized using an options object that does not match the API schema.
2038
- options has an unknown property 'unknown'. These properties are valid:
21-
object { flags? }"
39+
object { name?, flags? }"
2240
`;
2341
2442
exports[`validate options should throw an error on the "unknown" option with "[]" value 1`] = `
2543
"Invalid options object. Node Loader has been initialized using an options object that does not match the API schema.
2644
- options has an unknown property 'unknown'. These properties are valid:
27-
object { flags? }"
45+
object { name?, flags? }"
2846
`;
2947
3048
exports[`validate options should throw an error on the "unknown" option with "{"foo":"bar"}" value 1`] = `
3149
"Invalid options object. Node Loader has been initialized using an options object that does not match the API schema.
3250
- options has an unknown property 'unknown'. These properties are valid:
33-
object { flags? }"
51+
object { name?, flags? }"
3452
`;
3553
3654
exports[`validate options should throw an error on the "unknown" option with "{}" value 1`] = `
3755
"Invalid options object. Node Loader has been initialized using an options object that does not match the API schema.
3856
- options has an unknown property 'unknown'. These properties are valid:
39-
object { flags? }"
57+
object { name?, flags? }"
4058
`;
4159
4260
exports[`validate options should throw an error on the "unknown" option with "1" value 1`] = `
4361
"Invalid options object. Node Loader has been initialized using an options object that does not match the API schema.
4462
- options has an unknown property 'unknown'. These properties are valid:
45-
object { flags? }"
63+
object { name?, flags? }"
4664
`;
4765
4866
exports[`validate options should throw an error on the "unknown" option with "false" value 1`] = `
4967
"Invalid options object. Node Loader has been initialized using an options object that does not match the API schema.
5068
- options has an unknown property 'unknown'. These properties are valid:
51-
object { flags? }"
69+
object { name?, flags? }"
5270
`;
5371
5472
exports[`validate options should throw an error on the "unknown" option with "test" value 1`] = `
5573
"Invalid options object. Node Loader has been initialized using an options object that does not match the API schema.
5674
- options has an unknown property 'unknown'. These properties are valid:
57-
object { flags? }"
75+
object { name?, flags? }"
5876
`;
5977
6078
exports[`validate options should throw an error on the "unknown" option with "true" value 1`] = `
6179
"Invalid options object. Node Loader has been initialized using an options object that does not match the API schema.
6280
- options has an unknown property 'unknown'. These properties are valid:
63-
object { flags? }"
81+
object { name?, flags? }"
6482
`;

test/loader.test.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import {
1414
describe('loader', () => {
1515
it('should work', async () => {
1616
const compiler = getCompiler('simple.js', {
17+
name: '[name].[ext]',
1718
flags: 1,
1819
});
1920
const stats = await compile(compiler);
@@ -29,7 +30,9 @@ describe('loader', () => {
2930
});
3031

3132
it('should throw an error on broken "node" addon', async () => {
32-
const compiler = getCompiler('broken.js');
33+
const compiler = getCompiler('broken.js', {
34+
name: '[name].[ext]',
35+
});
3336
const stats = await compile(compiler);
3437

3538
expect(getModuleSource('./broken.node', stats)).toMatchSnapshot('module');

test/validate-options.test.js

+10
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,22 @@
22
* @jest-environment node
33
*/
44

5+
import path from 'path';
56
import os from 'os';
67

78
import { getCompiler, compile } from './helpers';
89

910
describe('validate options', () => {
1011
const tests = {
12+
name: {
13+
success: [
14+
'[name].[ext]',
15+
(resourcePath) => {
16+
return path.basename(resourcePath);
17+
},
18+
],
19+
failure: [true, false],
20+
},
1121
flags: {
1222
success: [os.constants.dlopen.RTLD_NOW],
1323
failure: [true, false, 'test'],

0 commit comments

Comments
 (0)