Skip to content
This repository was archived by the owner on Apr 28, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 68 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,10 @@ And run `webpack` via your preferred method.

## Options

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

### `flags`

Expand Down Expand Up @@ -134,6 +135,70 @@ module.exports = {
};
```

### `name`

Type: `String|Function`
Default: `'[contenthash].[ext]'`

Specifies a custom filename template for the target file(s).

#### `String`

**webpack.config.js**

```js
module.exports = {
target: 'node',
node: {
__dirname: false,
},
module: {
rules: [
{
test: /\.node$/,
loader: 'node-loader',
options: {
name: '[path][name].[ext]',
},
},
],
},
};
```

#### `Function`

**webpack.config.js**

```js
module.exports = {
target: 'node',
node: {
__dirname: false,
},
module: {
rules: [
{
test: /\.node$/,
loader: 'node-loader',
options: {
name(resourcePath, resourceQuery) {
// `resourcePath` - `/absolute/path/to/file.js`
// `resourceQuery` - `?foo=bar`

if (process.env.NODE_ENV === 'development') {
return '[path][name].[ext]';
}

return '[contenthash].[ext]';
},
},
},
],
},
};
```

## Contributing

Please take a moment to read our contributing guidelines if you haven't yet done so.
Expand Down
12 changes: 9 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@
MIT License http://www.opensource.org/licenses/mit-license.php
Author Tobias Koppers @sokra
*/
import path from 'path';

import { getOptions } from 'loader-utils';
import { getOptions, interpolateName } from 'loader-utils';
import validateOptions from 'schema-utils';

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

const name = path.basename(this.resourcePath);
const name = interpolateName(
this,
typeof options.name !== 'undefined' ? options.name : '[contenthash].[ext]',
{
context: this.rootContext,
content,
}
);

this.emitFile(name, content);

Expand Down
10 changes: 10 additions & 0 deletions src/options.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
{
"type": "object",
"properties": {
"name": {
"anyOf": [
{
"type": "string"
},
{
"instanceof": "Function"
}
]
},
"flags": {
"type": "integer"
}
Expand Down
34 changes: 26 additions & 8 deletions test/__snapshots__/validate-options.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -15,50 +15,68 @@ exports[`validate options should throw an error on the "flags" option with "true
- options.flags should be a integer."
`;

exports[`validate options should throw an error on the "name" option with "false" value 1`] = `
"Invalid options object. Node Loader has been initialized using an options object that does not match the API schema.
- options.name should be one of these:
string | function
Details:
* options.name should be a string.
* options.name should be an instance of function."
`;

exports[`validate options should throw an error on the "name" option with "true" value 1`] = `
"Invalid options object. Node Loader has been initialized using an options object that does not match the API schema.
- options.name should be one of these:
string | function
Details:
* options.name should be a string.
* options.name should be an instance of function."
`;

exports[`validate options should throw an error on the "unknown" option with "/test/" value 1`] = `
"Invalid options object. Node Loader has been initialized using an options object that does not match the API schema.
- options has an unknown property 'unknown'. These properties are valid:
object { flags? }"
object { name?, flags? }"
`;

exports[`validate options should throw an error on the "unknown" option with "[]" value 1`] = `
"Invalid options object. Node Loader has been initialized using an options object that does not match the API schema.
- options has an unknown property 'unknown'. These properties are valid:
object { flags? }"
object { name?, flags? }"
`;

exports[`validate options should throw an error on the "unknown" option with "{"foo":"bar"}" value 1`] = `
"Invalid options object. Node Loader has been initialized using an options object that does not match the API schema.
- options has an unknown property 'unknown'. These properties are valid:
object { flags? }"
object { name?, flags? }"
`;

exports[`validate options should throw an error on the "unknown" option with "{}" value 1`] = `
"Invalid options object. Node Loader has been initialized using an options object that does not match the API schema.
- options has an unknown property 'unknown'. These properties are valid:
object { flags? }"
object { name?, flags? }"
`;

exports[`validate options should throw an error on the "unknown" option with "1" value 1`] = `
"Invalid options object. Node Loader has been initialized using an options object that does not match the API schema.
- options has an unknown property 'unknown'. These properties are valid:
object { flags? }"
object { name?, flags? }"
`;

exports[`validate options should throw an error on the "unknown" option with "false" value 1`] = `
"Invalid options object. Node Loader has been initialized using an options object that does not match the API schema.
- options has an unknown property 'unknown'. These properties are valid:
object { flags? }"
object { name?, flags? }"
`;

exports[`validate options should throw an error on the "unknown" option with "test" value 1`] = `
"Invalid options object. Node Loader has been initialized using an options object that does not match the API schema.
- options has an unknown property 'unknown'. These properties are valid:
object { flags? }"
object { name?, flags? }"
`;

exports[`validate options should throw an error on the "unknown" option with "true" value 1`] = `
"Invalid options object. Node Loader has been initialized using an options object that does not match the API schema.
- options has an unknown property 'unknown'. These properties are valid:
object { flags? }"
object { name?, flags? }"
`;
5 changes: 4 additions & 1 deletion test/loader.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
describe('loader', () => {
it('should work', async () => {
const compiler = getCompiler('simple.js', {
name: '[name].[ext]',
flags: 1,
});
const stats = await compile(compiler);
Expand All @@ -29,7 +30,9 @@ describe('loader', () => {
});

it('should throw an error on broken "node" addon', async () => {
const compiler = getCompiler('broken.js');
const compiler = getCompiler('broken.js', {
name: '[name].[ext]',
});
const stats = await compile(compiler);

expect(getModuleSource('./broken.node', stats)).toMatchSnapshot('module');
Expand Down
10 changes: 10 additions & 0 deletions test/validate-options.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,22 @@
* @jest-environment node
*/

import path from 'path';
import os from 'os';

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

describe('validate options', () => {
const tests = {
name: {
success: [
'[name].[ext]',
(resourcePath) => {
return path.basename(resourcePath);
},
],
failure: [true, false],
},
flags: {
success: [os.constants.dlopen.RTLD_NOW],
failure: [true, false, 'test'],
Expand Down