1 | /*
|
---|
2 | MIT License http://www.opensource.org/licenses/mit-license.php
|
---|
3 | Author Tobias Koppers @sokra
|
---|
4 | */
|
---|
5 |
|
---|
6 | "use strict";
|
---|
7 |
|
---|
8 | const Dependency = require("../Dependency");
|
---|
9 | const DependencyTemplate = require("../DependencyTemplate");
|
---|
10 | const memoize = require("../util/memoize");
|
---|
11 |
|
---|
12 | /** @typedef {import("../Module")} Module */
|
---|
13 |
|
---|
14 | const getRawModule = memoize(() => require("../RawModule"));
|
---|
15 |
|
---|
16 | class ModuleDependency extends Dependency {
|
---|
17 | /**
|
---|
18 | * @param {string} request request path which needs resolving
|
---|
19 | */
|
---|
20 | constructor(request) {
|
---|
21 | super();
|
---|
22 | this.request = request;
|
---|
23 | this.userRequest = request;
|
---|
24 | this.range = undefined;
|
---|
25 | // assertions must be serialized by subclasses that use it
|
---|
26 | /** @type {Record<string, any> | undefined} */
|
---|
27 | this.assertions = undefined;
|
---|
28 | }
|
---|
29 |
|
---|
30 | /**
|
---|
31 | * @returns {string | null} an identifier to merge equal requests
|
---|
32 | */
|
---|
33 | getResourceIdentifier() {
|
---|
34 | let str = `module${this.request}`;
|
---|
35 | if (this.assertions !== undefined) {
|
---|
36 | str += JSON.stringify(this.assertions);
|
---|
37 | }
|
---|
38 | return str;
|
---|
39 | }
|
---|
40 |
|
---|
41 | /**
|
---|
42 | * @param {string} context context directory
|
---|
43 | * @returns {Module} a module
|
---|
44 | */
|
---|
45 | createIgnoredModule(context) {
|
---|
46 | const RawModule = getRawModule();
|
---|
47 | return new RawModule(
|
---|
48 | "/* (ignored) */",
|
---|
49 | `ignored|${context}|${this.request}`,
|
---|
50 | `${this.request} (ignored)`
|
---|
51 | );
|
---|
52 | }
|
---|
53 |
|
---|
54 | serialize(context) {
|
---|
55 | const { write } = context;
|
---|
56 | write(this.request);
|
---|
57 | write(this.userRequest);
|
---|
58 | write(this.range);
|
---|
59 | super.serialize(context);
|
---|
60 | }
|
---|
61 |
|
---|
62 | deserialize(context) {
|
---|
63 | const { read } = context;
|
---|
64 | this.request = read();
|
---|
65 | this.userRequest = read();
|
---|
66 | this.range = read();
|
---|
67 | super.deserialize(context);
|
---|
68 | }
|
---|
69 | }
|
---|
70 |
|
---|
71 | ModuleDependency.Template = DependencyTemplate;
|
---|
72 |
|
---|
73 | module.exports = ModuleDependency;
|
---|