Skip to content

Commit e11fb12

Browse files
committed
add platform target properties to compiler.
add 2 methods `compiler.getPlatformTargetInfo` and `compiler.setPlatformTargetInfo` to allow get and set platform target properties. by default resolve this properties from target option.
1 parent c586c7b commit e11fb12

6 files changed

Lines changed: 144 additions & 6 deletions

File tree

lib/Compiler.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ const { isSourceEqual } = require("./util/source");
4545
/** @typedef {import("./FileSystemInfo").FileSystemInfoEntry} FileSystemInfoEntry */
4646
/** @typedef {import("./Module")} Module */
4747
/** @typedef {import("./Module").BuildInfo} BuildInfo */
48+
/** @typedef {import("./config/target").PlatformTargetProperties} PlatformTargetProperties */
4849
/** @typedef {import("./logging/createConsoleLogger").LoggingFunction} LoggingFunction */
4950
/** @typedef {import("./util/WeakTupleMap")} WeakTupleMap */
5051
/** @typedef {import("./util/fs").IStats} IStats */
@@ -265,6 +266,16 @@ class Compiler {
265266
/** @type {LoggingFunction | undefined} */
266267
this.infrastructureLogger = undefined;
267268

269+
/** @type {PlatformTargetProperties} */
270+
this.target = {
271+
web: null,
272+
browser: null,
273+
webworker: null,
274+
node: null,
275+
nwjs: null,
276+
electron: null
277+
};
278+
268279
this.options = options;
269280

270281
this.context = context;
@@ -302,6 +313,20 @@ class Compiler {
302313
this._assetEmittingPreviousFiles = new Set();
303314
}
304315

316+
/**
317+
* @returns {Readonly<PlatformTargetProperties>} platform target properties
318+
*/
319+
getPlatformTargetInfo() {
320+
return this.target;
321+
}
322+
323+
/**
324+
* @param {PlatformTargetProperties} platform platform target properties
325+
*/
326+
setPlatformTargetInfo(platform) {
327+
this.target = platform;
328+
}
329+
305330
/**
306331
* @param {string} name cache name
307332
* @returns {CacheFacade} the cache facade instance

lib/PlatformPlugin.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
MIT License http://www.opensource.org/licenses/mit-license.php
3+
Authors Ivan Kopeykin @vankop
4+
*/
5+
6+
"use strict";
7+
8+
/** @typedef {import("./Compiler")} Compiler */
9+
/** @typedef {import("./config/target").PlatformTargetProperties} PlatformTargetProperties */
10+
11+
/**
12+
* Should be used only for "target === false" or
13+
* when you want to overwrite platform target properties
14+
*/
15+
class PlatformPlugin {
16+
/**
17+
* @param {Partial<PlatformTargetProperties>} platform target properties
18+
*/
19+
constructor(platform) {
20+
/** @type {Partial<PlatformTargetProperties>} */
21+
this.platform = platform;
22+
}
23+
24+
/**
25+
* Apply the plugin
26+
* @param {Compiler} compiler the compiler instance
27+
* @returns {void}
28+
*/
29+
apply(compiler) {
30+
compiler.hooks.environment.tap("PlatformPlugin", () => {
31+
compiler.setPlatformTargetInfo({
32+
...compiler.getPlatformTargetInfo(),
33+
...this.platform
34+
});
35+
});
36+
}
37+
}
38+
39+
module.exports = PlatformPlugin;

lib/config/defaults.js

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ const {
6363
/** @typedef {import("../../declarations/WebpackOptions").WebpackOptionsNormalized} WebpackOptions */
6464
/** @typedef {import("../Compiler")} Compiler */
6565
/** @typedef {import("../Module")} Module */
66+
/** @typedef {import("./target").PlatformTargetProperties} PlatformTargetProperties */
6667
/** @typedef {import("./target").TargetProperties} TargetProperties */
6768

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

148149
/**
149150
* @param {WebpackOptions} options options to be modified
150-
* @returns {void}
151+
* @returns {PlatformTargetProperties|false} platform target properties
151152
*/
152-
const applyWebpackOptionsDefaults = options => {
153+
const applyWebpackOptionsDefaultsAndResolveTargetProperties = options => {
153154
F(options, "context", () => process.cwd());
154155
F(options, "target", () => {
155156
return getDefaultTarget(/** @type {string} */ (options.context));
@@ -167,6 +168,15 @@ const applyWebpackOptionsDefaults = options => {
167168
/** @type {Context} */ (options.context)
168169
);
169170

171+
if (targetProperties) {
172+
D(targetProperties, "web", null);
173+
D(targetProperties, "browser", null);
174+
D(targetProperties, "webworker", null);
175+
D(targetProperties, "node", null);
176+
D(targetProperties, "nwjs", null);
177+
D(targetProperties, "electron", null);
178+
}
179+
170180
const development = mode === "development";
171181
const production = mode === "production" || !mode;
172182

@@ -315,6 +325,16 @@ const applyWebpackOptionsDefaults = options => {
315325
getResolveLoaderDefaults({ cache }),
316326
options.resolveLoader
317327
);
328+
329+
if (targetProperties === false) return false;
330+
return {
331+
web: targetProperties.web,
332+
browser: targetProperties.browser,
333+
webworker: targetProperties.webworker,
334+
node: targetProperties.node,
335+
nwjs: targetProperties.nwjs,
336+
electron: targetProperties.electron
337+
};
318338
};
319339

320340
/**
@@ -1595,4 +1615,7 @@ const applyInfrastructureLoggingDefaults = infrastructureLogging => {
15951615
};
15961616

15971617
exports.applyWebpackOptionsBaseDefaults = applyWebpackOptionsBaseDefaults;
1598-
exports.applyWebpackOptionsDefaults = applyWebpackOptionsDefaults;
1618+
exports.applyWebpackOptionsDefaults =
1619+
applyWebpackOptionsDefaultsAndResolveTargetProperties;
1620+
exports.applyWebpackOptionsDefaultsAndResolvePlatformTargetProperties =
1621+
applyWebpackOptionsDefaultsAndResolveTargetProperties;

lib/index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,9 @@ module.exports = mergeExports(fn, {
282282
get Parser() {
283283
return require("./Parser");
284284
},
285+
get PlatformPlugin() {
286+
return require("./PlatformPlugin");
287+
},
285288
get PrefetchPlugin() {
286289
return require("./PrefetchPlugin");
287290
},

lib/webpack.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ const Compiler = require("./Compiler");
1212
const MultiCompiler = require("./MultiCompiler");
1313
const WebpackOptionsApply = require("./WebpackOptionsApply");
1414
const {
15-
applyWebpackOptionsDefaults,
15+
applyWebpackOptionsDefaultsAndResolvePlatformTargetProperties,
1616
applyWebpackOptionsBaseDefaults
1717
} = require("./config/defaults");
1818
const { getNormalizedWebpackOptions } = require("./config/normalization");
@@ -79,7 +79,11 @@ const createCompiler = rawOptions => {
7979
}
8080
}
8181
}
82-
applyWebpackOptionsDefaults(options);
82+
const platform =
83+
applyWebpackOptionsDefaultsAndResolvePlatformTargetProperties(options);
84+
if (platform) {
85+
compiler.setPlatformTargetInfo(platform);
86+
}
8387
compiler.hooks.environment.call();
8488
compiler.hooks.afterEnvironment.call();
8589
new WebpackOptionsApply().process(options, compiler);

types.d.ts

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2308,6 +2308,7 @@ declare class Compiler {
23082308
fsStartTime?: number;
23092309
resolverFactory: ResolverFactory;
23102310
infrastructureLogger?: (arg0: string, arg1: LogTypeEnum, arg2: any[]) => void;
2311+
target: PlatformTargetProperties;
23112312
options: WebpackOptionsNormalized;
23122313
context: string;
23132314
requestShortener: RequestShortener;
@@ -2324,6 +2325,8 @@ declare class Compiler {
23242325
running: boolean;
23252326
idle: boolean;
23262327
watchMode: boolean;
2328+
getPlatformTargetInfo(): Readonly<PlatformTargetProperties>;
2329+
setPlatformTargetInfo(platform: PlatformTargetProperties): void;
23272330
getCache(name: string): CacheFacade;
23282331
getInfrastructureLogger(name: string | (() => string)): WebpackLogger;
23292332
watch(watchOptions: WatchOptions, handler: RunCallback<Stats>): Watching;
@@ -10397,6 +10400,46 @@ declare interface PitchLoaderDefinitionFunction<
1039710400
data: object
1039810401
): string | void | Buffer | Promise<string | Buffer>;
1039910402
}
10403+
declare class PlatformPlugin {
10404+
constructor(platform: Partial<PlatformTargetProperties>);
10405+
platform: Partial<PlatformTargetProperties>;
10406+
10407+
/**
10408+
* Apply the plugin
10409+
*/
10410+
apply(compiler: Compiler): void;
10411+
}
10412+
declare interface PlatformTargetProperties {
10413+
/**
10414+
* web platform, importing of http(s) and std: is available
10415+
*/
10416+
web: null | boolean;
10417+
10418+
/**
10419+
* browser platform, running in a normal web browser
10420+
*/
10421+
browser: null | boolean;
10422+
10423+
/**
10424+
* (Web)Worker platform, running in a web/shared/service worker
10425+
*/
10426+
webworker: null | boolean;
10427+
10428+
/**
10429+
* node platform, require of node built-in modules is available
10430+
*/
10431+
node: null | boolean;
10432+
10433+
/**
10434+
* nwjs platform, require of legacy nw.gui is available
10435+
*/
10436+
nwjs: null | boolean;
10437+
10438+
/**
10439+
* electron platform, require of some electron built-in modules is available
10440+
*/
10441+
electron: null | boolean;
10442+
}
1040010443
type Plugin =
1040110444
| undefined
1040210445
| null
@@ -15058,7 +15101,7 @@ declare namespace exports {
1505815101
) => WebpackOptionsNormalized;
1505915102
export const applyWebpackOptionsDefaults: (
1506015103
options: WebpackOptionsNormalized
15061-
) => void;
15104+
) => false | PlatformTargetProperties;
1506215105
}
1506315106
export namespace dependencies {
1506415107
export {
@@ -15410,6 +15453,7 @@ declare namespace exports {
1541015453
NormalModuleReplacementPlugin,
1541115454
MultiCompiler,
1541215455
Parser,
15456+
PlatformPlugin,
1541315457
PrefetchPlugin,
1541415458
ProgressPlugin,
1541515459
ProvidePlugin,

0 commit comments

Comments
 (0)