Skip to content

Commit

Permalink
add platform target properties to compiler.
Browse files Browse the repository at this point in the history
add 2 methods `compiler.getPlatformTargetInfo` and `compiler.setPlatformTargetInfo` to allow get and set platform target properties. by default resolve this properties from target option.
  • Loading branch information
vankop committed May 1, 2024
1 parent c586c7b commit e11fb12
Show file tree
Hide file tree
Showing 6 changed files with 144 additions and 6 deletions.
25 changes: 25 additions & 0 deletions lib/Compiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ const { isSourceEqual } = require("./util/source");
/** @typedef {import("./FileSystemInfo").FileSystemInfoEntry} FileSystemInfoEntry */
/** @typedef {import("./Module")} Module */
/** @typedef {import("./Module").BuildInfo} BuildInfo */
/** @typedef {import("./config/target").PlatformTargetProperties} PlatformTargetProperties */
/** @typedef {import("./logging/createConsoleLogger").LoggingFunction} LoggingFunction */
/** @typedef {import("./util/WeakTupleMap")} WeakTupleMap */
/** @typedef {import("./util/fs").IStats} IStats */
Expand Down Expand Up @@ -265,6 +266,16 @@ class Compiler {
/** @type {LoggingFunction | undefined} */
this.infrastructureLogger = undefined;

/** @type {PlatformTargetProperties} */
this.target = {
web: null,
browser: null,
webworker: null,
node: null,
nwjs: null,
electron: null
};

this.options = options;

this.context = context;
Expand Down Expand Up @@ -302,6 +313,20 @@ class Compiler {
this._assetEmittingPreviousFiles = new Set();
}

/**
* @returns {Readonly<PlatformTargetProperties>} platform target properties
*/
getPlatformTargetInfo() {
return this.target;
}

/**
* @param {PlatformTargetProperties} platform platform target properties
*/
setPlatformTargetInfo(platform) {
this.target = platform;
}

/**
* @param {string} name cache name
* @returns {CacheFacade} the cache facade instance
Expand Down
39 changes: 39 additions & 0 deletions lib/PlatformPlugin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
MIT License http://www.opensource.org/licenses/mit-license.php
Authors Ivan Kopeykin @vankop
*/

"use strict";

/** @typedef {import("./Compiler")} Compiler */
/** @typedef {import("./config/target").PlatformTargetProperties} PlatformTargetProperties */

/**
* Should be used only for "target === false" or
* when you want to overwrite platform target properties
*/
class PlatformPlugin {
/**
* @param {Partial<PlatformTargetProperties>} platform target properties
*/
constructor(platform) {
/** @type {Partial<PlatformTargetProperties>} */
this.platform = platform;
}

/**
* Apply the plugin
* @param {Compiler} compiler the compiler instance
* @returns {void}
*/
apply(compiler) {
compiler.hooks.environment.tap("PlatformPlugin", () => {
compiler.setPlatformTargetInfo({
...compiler.getPlatformTargetInfo(),
...this.platform
});
});
}
}

module.exports = PlatformPlugin;
29 changes: 26 additions & 3 deletions lib/config/defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ const {
/** @typedef {import("../../declarations/WebpackOptions").WebpackOptionsNormalized} WebpackOptions */
/** @typedef {import("../Compiler")} Compiler */
/** @typedef {import("../Module")} Module */
/** @typedef {import("./target").PlatformTargetProperties} PlatformTargetProperties */
/** @typedef {import("./target").TargetProperties} TargetProperties */

const NODE_MODULES_REGEXP = /[\\/]node_modules[\\/]/i;
Expand Down Expand Up @@ -147,9 +148,9 @@ const applyWebpackOptionsBaseDefaults = options => {

/**
* @param {WebpackOptions} options options to be modified
* @returns {void}
* @returns {PlatformTargetProperties|false} platform target properties
*/
const applyWebpackOptionsDefaults = options => {
const applyWebpackOptionsDefaultsAndResolveTargetProperties = options => {
F(options, "context", () => process.cwd());
F(options, "target", () => {
return getDefaultTarget(/** @type {string} */ (options.context));
Expand All @@ -167,6 +168,15 @@ const applyWebpackOptionsDefaults = options => {
/** @type {Context} */ (options.context)
);

if (targetProperties) {
D(targetProperties, "web", null);
D(targetProperties, "browser", null);
D(targetProperties, "webworker", null);
D(targetProperties, "node", null);
D(targetProperties, "nwjs", null);
D(targetProperties, "electron", null);
}

const development = mode === "development";
const production = mode === "production" || !mode;

Expand Down Expand Up @@ -315,6 +325,16 @@ const applyWebpackOptionsDefaults = options => {
getResolveLoaderDefaults({ cache }),
options.resolveLoader
);

if (targetProperties === false) return false;
return {
web: targetProperties.web,
browser: targetProperties.browser,
webworker: targetProperties.webworker,
node: targetProperties.node,
nwjs: targetProperties.nwjs,
electron: targetProperties.electron
};
};

/**
Expand Down Expand Up @@ -1595,4 +1615,7 @@ const applyInfrastructureLoggingDefaults = infrastructureLogging => {
};

exports.applyWebpackOptionsBaseDefaults = applyWebpackOptionsBaseDefaults;
exports.applyWebpackOptionsDefaults = applyWebpackOptionsDefaults;
exports.applyWebpackOptionsDefaults =
applyWebpackOptionsDefaultsAndResolveTargetProperties;
exports.applyWebpackOptionsDefaultsAndResolvePlatformTargetProperties =
applyWebpackOptionsDefaultsAndResolveTargetProperties;
3 changes: 3 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,9 @@ module.exports = mergeExports(fn, {
get Parser() {
return require("./Parser");
},
get PlatformPlugin() {
return require("./PlatformPlugin");
},
get PrefetchPlugin() {
return require("./PrefetchPlugin");
},
Expand Down
8 changes: 6 additions & 2 deletions lib/webpack.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const Compiler = require("./Compiler");
const MultiCompiler = require("./MultiCompiler");
const WebpackOptionsApply = require("./WebpackOptionsApply");
const {
applyWebpackOptionsDefaults,
applyWebpackOptionsDefaultsAndResolvePlatformTargetProperties,
applyWebpackOptionsBaseDefaults
} = require("./config/defaults");
const { getNormalizedWebpackOptions } = require("./config/normalization");
Expand Down Expand Up @@ -79,7 +79,11 @@ const createCompiler = rawOptions => {
}
}
}
applyWebpackOptionsDefaults(options);
const platform =
applyWebpackOptionsDefaultsAndResolvePlatformTargetProperties(options);
if (platform) {
compiler.setPlatformTargetInfo(platform);
}
compiler.hooks.environment.call();
compiler.hooks.afterEnvironment.call();
new WebpackOptionsApply().process(options, compiler);
Expand Down
46 changes: 45 additions & 1 deletion types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2308,6 +2308,7 @@ declare class Compiler {
fsStartTime?: number;
resolverFactory: ResolverFactory;
infrastructureLogger?: (arg0: string, arg1: LogTypeEnum, arg2: any[]) => void;
target: PlatformTargetProperties;
options: WebpackOptionsNormalized;
context: string;
requestShortener: RequestShortener;
Expand All @@ -2324,6 +2325,8 @@ declare class Compiler {
running: boolean;
idle: boolean;
watchMode: boolean;
getPlatformTargetInfo(): Readonly<PlatformTargetProperties>;
setPlatformTargetInfo(platform: PlatformTargetProperties): void;
getCache(name: string): CacheFacade;
getInfrastructureLogger(name: string | (() => string)): WebpackLogger;
watch(watchOptions: WatchOptions, handler: RunCallback<Stats>): Watching;
Expand Down Expand Up @@ -10397,6 +10400,46 @@ declare interface PitchLoaderDefinitionFunction<
data: object
): string | void | Buffer | Promise<string | Buffer>;
}
declare class PlatformPlugin {
constructor(platform: Partial<PlatformTargetProperties>);
platform: Partial<PlatformTargetProperties>;

/**
* Apply the plugin
*/
apply(compiler: Compiler): void;
}
declare interface PlatformTargetProperties {
/**
* web platform, importing of http(s) and std: is available
*/
web: null | boolean;

/**
* browser platform, running in a normal web browser
*/
browser: null | boolean;

/**
* (Web)Worker platform, running in a web/shared/service worker
*/
webworker: null | boolean;

/**
* node platform, require of node built-in modules is available
*/
node: null | boolean;

/**
* nwjs platform, require of legacy nw.gui is available
*/
nwjs: null | boolean;

/**
* electron platform, require of some electron built-in modules is available
*/
electron: null | boolean;
}
type Plugin =
| undefined
| null
Expand Down Expand Up @@ -15058,7 +15101,7 @@ declare namespace exports {
) => WebpackOptionsNormalized;
export const applyWebpackOptionsDefaults: (
options: WebpackOptionsNormalized
) => void;
) => false | PlatformTargetProperties;
}
export namespace dependencies {
export {
Expand Down Expand Up @@ -15410,6 +15453,7 @@ declare namespace exports {
NormalModuleReplacementPlugin,
MultiCompiler,
Parser,
PlatformPlugin,
PrefetchPlugin,
ProgressPlugin,
ProvidePlugin,
Expand Down

0 comments on commit e11fb12

Please sign in to comment.