Skip to content

Commit

Permalink
fix issues with absolutePath, allOf and anyOf
Browse files Browse the repository at this point in the history
make DllReferencePlugin schema more expressive
typing fixes
  • Loading branch information
sokra committed Sep 21, 2018
1 parent 8c31f2a commit c768182
Show file tree
Hide file tree
Showing 8 changed files with 553 additions and 172 deletions.
4 changes: 4 additions & 0 deletions declarations.d.ts
Expand Up @@ -224,6 +224,10 @@ declare module "@webassemblyjs/ast" {
// It's referenced from "ruleSet-conditions" in schemas/WebpackOptions.json
interface RuleSetConditionsRecursive
extends Array<import("./declarations/WebpackOptions").RuleSetCondition> {}
interface RuleSetConditionsAbsoluteRecursive
extends Array<
import("./declarations/WebpackOptions").RuleSetConditionAbsolute
> {}

/**
* Global variable declarations
Expand Down
71 changes: 55 additions & 16 deletions declarations/WebpackOptions.d.ts
Expand Up @@ -87,7 +87,7 @@ export type RuleSetConditionOrConditions = RuleSetCondition | RuleSetConditions;
export type RuleSetCondition =
| RegExp
| string
| Function
| ((value: string) => boolean)
| RuleSetConditions
| {
/**
Expand Down Expand Up @@ -120,6 +120,55 @@ export type RuleSetCondition =
* via the `definition` "RuleSetConditions".
*/
export type RuleSetConditions = RuleSetConditionsRecursive;
/**
* One or multiple rule conditions
*
* This interface was referenced by `WebpackOptions`'s JSON-Schema
* via the `definition` "RuleSetConditionOrConditionsAbsolute".
*/
export type RuleSetConditionOrConditionsAbsolute =
| RuleSetConditionAbsolute
| RuleSetConditionsAbsolute;
/**
* This interface was referenced by `WebpackOptions`'s JSON-Schema
* via the `definition` "RuleSetConditionAbsolute".
*/
export type RuleSetConditionAbsolute =
| RegExp
| string
| ((value: string) => boolean)
| RuleSetConditionsAbsolute
| {
/**
* Logical AND
*/
and?: RuleSetConditionsAbsolute;
/**
* Exclude all modules matching any of these conditions
*/
exclude?: RuleSetConditionOrConditionsAbsolute;
/**
* Exclude all modules matching not any of these conditions
*/
include?: RuleSetConditionOrConditionsAbsolute;
/**
* Logical NOT
*/
not?: RuleSetConditionsAbsolute;
/**
* Logical OR
*/
or?: RuleSetConditionsAbsolute;
/**
* Exclude all modules matching any of these conditions
*/
test?: RuleSetConditionOrConditionsAbsolute;
};
/**
* This interface was referenced by `WebpackOptions`'s JSON-Schema
* via the `definition` "RuleSetConditionsAbsolute".
*/
export type RuleSetConditionsAbsolute = RuleSetConditionsAbsoluteRecursive;
/**
* This interface was referenced by `WebpackOptions`'s JSON-Schema
* via the `definition` "RuleSetLoader".
Expand Down Expand Up @@ -461,21 +510,15 @@ export interface RuleSetRule {
/**
* Shortcut for resource.exclude
*/
exclude?: RuleSetConditionOrConditions & {
[k: string]: any;
};
exclude?: RuleSetConditionOrConditionsAbsolute;
/**
* Shortcut for resource.include
*/
include?: RuleSetConditionOrConditions & {
[k: string]: any;
};
include?: RuleSetConditionOrConditionsAbsolute;
/**
* Match the issuer of the module (The module pointing to this module)
*/
issuer?: RuleSetConditionOrConditions & {
[k: string]: any;
};
issuer?: RuleSetConditionOrConditionsAbsolute;
/**
* Shortcut for use.loader
*/
Expand Down Expand Up @@ -509,9 +552,7 @@ export interface RuleSetRule {
/**
* Match the resource path of the module
*/
resource?: RuleSetConditionOrConditions & {
[k: string]: any;
};
resource?: RuleSetConditionOrConditionsAbsolute;
/**
* Match the resource query of the module
*/
Expand All @@ -527,9 +568,7 @@ export interface RuleSetRule {
/**
* Shortcut for resource.test
*/
test?: RuleSetConditionOrConditions & {
[k: string]: any;
};
test?: RuleSetConditionOrConditionsAbsolute;
/**
* Module type to use for the module
*/
Expand Down
122 changes: 119 additions & 3 deletions declarations/plugins/DllReferencePlugin.d.ts
Expand Up @@ -4,6 +4,122 @@
* Run `yarn special-lint-fix` to update
*/

export type DllReferencePluginOptions = {
[k: string]: any;
};
export type DllReferencePluginOptions =
| {
/**
* (absolute path) context of requests in the manifest (or content property)
*/
context?: string;
/**
* Extensions used to resolve modules in the dll bundle (only used when using 'scope')
*/
extensions?: string[];
/**
* An object containing content and name or a string to the absolute path of the JSON manifest to be loaded upon compilation
*/
manifest: DllReferencePluginOptionsManifest | string;
/**
* The name where the dll is exposed (external name, defaults to manifest.name)
*/
name?: string;
/**
* Prefix which is used for accessing the content of the dll
*/
scope?: string;
/**
* How the dll is exposed (libraryTarget, defaults to manifest.type)
*/
sourceType?: DllReferencePluginOptionsSourceType;
/**
* The way how the export of the dll bundle is used
*/
type?: "require" | "object";
}
| {
/**
* The mappings from request to module info
*/
content: DllReferencePluginOptionsContent;
/**
* (absolute path) context of requests in the manifest (or content property)
*/
context?: string;
/**
* Extensions used to resolve modules in the dll bundle (only used when using 'scope')
*/
extensions?: string[];
/**
* The name where the dll is exposed (external name)
*/
name: string;
/**
* Prefix which is used for accessing the content of the dll
*/
scope?: string;
/**
* How the dll is exposed (libraryTarget)
*/
sourceType?: DllReferencePluginOptionsSourceType;
/**
* The way how the export of the dll bundle is used
*/
type?: "require" | "object";
};
/**
* The type how the dll is exposed (external type)
*/
export type DllReferencePluginOptionsSourceType =
| "var"
| "assign"
| "this"
| "window"
| "global"
| "commonjs"
| "commonjs2"
| "commonjs-module"
| "amd"
| "umd"
| "umd2"
| "jsonp";

/**
* An object containing content, name and type
*/
export interface DllReferencePluginOptionsManifest {
/**
* The mappings from request to module info
*/
content: DllReferencePluginOptionsContent;
/**
* The name where the dll is exposed (external name)
*/
name?: string;
/**
* The type how the dll is exposed (external type)
*/
type?: DllReferencePluginOptionsSourceType;
}
/**
* The mappings from request to module info
*/
export interface DllReferencePluginOptionsContent {
/**
* Module info
*/
[k: string]: {
/**
* Meta information about the module
*/
buildMeta?: {
[k: string]: any;
};
/**
* Information about the provided exports of the module
*/
exports?: true | string[];
/**
* Module ID
*/
id: number | string;
};
}
111 changes: 65 additions & 46 deletions lib/DllReferencePlugin.js
Expand Up @@ -17,6 +17,7 @@ const validateOptions = require("schema-utils");
const schema = require("../schemas/plugins/DllReferencePlugin.json");

/** @typedef {import("../declarations/plugins/DllReferencePlugin").DllReferencePluginOptions} DllReferencePluginOptions */
/** @typedef {import("../declarations/plugins/DllReferencePlugin").DllReferencePluginOptionsManifest} DllReferencePluginOptionsManifest */

class DllReferencePlugin {
/**
Expand Down Expand Up @@ -45,77 +46,95 @@ class DllReferencePlugin {
compiler.hooks.beforeCompile.tapAsync(
"DllReferencePlugin",
(params, callback) => {
const manifest = this.options.manifest;
if (typeof manifest === "string") {
params.compilationDependencies.add(manifest);
compiler.inputFileSystem.readFile(manifest, (err, result) => {
if (err) return callback(err);
// Catch errors parsing the manifest so that blank
// or malformed manifest files don't kill the process.
try {
params["dll reference " + manifest] = parseJson(
result.toString("utf-8")
);
} catch (e) {
// Store the error in the params so that it can
// be added as a compilation error later on.
const manifestPath = makePathsRelative(
compiler.options.context,
manifest
);
params[
"dll reference parse error " + manifest
] = new DllManifestError(manifestPath, e.message);
}
return callback();
});
} else {
return callback();
if ("manifest" in this.options) {
const manifest = this.options.manifest;
if (typeof manifest === "string") {
params.compilationDependencies.add(manifest);
compiler.inputFileSystem.readFile(manifest, (err, result) => {
if (err) return callback(err);
// Catch errors parsing the manifest so that blank
// or malformed manifest files don't kill the process.
try {
params["dll reference " + manifest] = parseJson(
result.toString("utf-8")
);
} catch (e) {
// Store the error in the params so that it can
// be added as a compilation error later on.
const manifestPath = makePathsRelative(
compiler.options.context,
manifest
);
params[
"dll reference parse error " + manifest
] = new DllManifestError(manifestPath, e.message);
}
return callback();
});
return;
}
}
return callback();
}
);

compiler.hooks.compile.tap("DllReferencePlugin", params => {
let manifest = this.options.manifest;
if (typeof manifest === "string") {
// If there was an error parsing the manifest
// file, exit now because the error will be added
// as a compilation error in the "compilation" hook.
if (params["dll reference parse error " + manifest]) {
return;
let name = this.options.name;
let sourceType = this.options.sourceType;
let content =
"content" in this.options ? this.options.content : undefined;
if ("manifest" in this.options) {
let manifestParameter = this.options.manifest;
let manifest;
if (typeof manifestParameter === "string") {
// If there was an error parsing the manifest
// file, exit now because the error will be added
// as a compilation error in the "compilation" hook.
if (params["dll reference parse error " + manifestParameter]) {
return;
}
manifest =
/** @type {DllReferencePluginOptionsManifest} */ (params[
"dll reference " + manifestParameter
]);
} else {
manifest = manifestParameter;
}
if (manifest) {
if (!name) name = manifest.name;
if (!sourceType) sourceType = manifest.type;
if (!content) content = manifest.content;
}
manifest = params["dll reference " + manifest];
}
const name = this.options.name || manifest.name;
const sourceType =
this.options.sourceType || (manifest && manifest.type) || "var";
const externals = {};
const source = "dll-reference " + name;
externals[source] = name;
const normalModuleFactory = params.normalModuleFactory;
new ExternalModuleFactoryPlugin(sourceType, externals).apply(
new ExternalModuleFactoryPlugin(sourceType || "var", externals).apply(
normalModuleFactory
);
new DelegatedModuleFactoryPlugin({
source: source,
type: this.options.type,
scope: this.options.scope,
context: this.options.context || compiler.options.context,
content: this.options.content || manifest.content,
content,
extensions: this.options.extensions
}).apply(normalModuleFactory);
});

compiler.hooks.compilation.tap(
"DllReferencePlugin",
(compilation, params) => {
let manifest = this.options.manifest;
if (typeof manifest === "string") {
// If there was an error parsing the manifest file, add the
// error as a compilation error to make the compilation fail.
let e = params["dll reference parse error " + manifest];
if (e) {
compilation.errors.push(e);
if ("manifest" in this.options) {
let manifest = this.options.manifest;
if (typeof manifest === "string") {
// If there was an error parsing the manifest file, add the
// error as a compilation error to make the compilation fail.
let e = params["dll reference parse error " + manifest];
if (e) {
compilation.errors.push(e);
}
}
}
}
Expand Down

0 comments on commit c768182

Please sign in to comment.