Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add platform target properties to compiler. #18378

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 11 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 {Readonly<PlatformTargetProperties>} */
this.platform = {
web: null,
browser: null,
webworker: null,
node: null,
nwjs: null,
electron: null
};

this.options = options;

this.context = context;
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.platform = {
...compiler.platform,
...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
};
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's return all targetProperties and set them here https://github.com/webpack/webpack/pull/18378/files#diff-7979ef8b26e853797876ef65fffd2669a8c088f6ae7853028ed4b876f9f0be65R85, I imagine that in the future we may need more properties of our targets

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rest of the target properties related to environment, they are accessible in output.environment

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

};

/**
Expand Down Expand Up @@ -1596,4 +1616,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.platform = platform;
}
compiler.hooks.environment.call();
compiler.hooks.afterEnvironment.call();
new WebpackOptionsApply().process(options, compiler);
Expand Down
29 changes: 29 additions & 0 deletions test/Compiler.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,12 @@ describe("Compiler", () => {
callback();
}
});
it("default platform info", done => {
const platform = compiler.platform;
expect(platform.web).toBe(true);
expect(platform.node).toBe(false);
done();
});
describe("purgeInputFileSystem", () => {
it("invokes purge() if inputFileSystem.purge", done => {
const mockPurge = jest.fn();
Expand Down Expand Up @@ -287,6 +293,29 @@ describe("Compiler", () => {
});
});
});

it("PlatformPlugin", done => {
const webpack = require("..");
const compiler = webpack({
entry: "./c",
context: path.join(__dirname, "fixtures"),
output: {
path: "/directory"
},
plugins: [
new (require("../lib/PlatformPlugin"))({ node: true }),
compiler => {
compiler.hooks.afterEnvironment.tap("test", () => {
const platform = compiler.platform;
expect(platform.node).toBe(true);
expect(platform.web).toBe(true);
});
}
]
});
compiler.close(done);
});

it("should not emit on errors", done => {
const webpack = require("..");
compiler = webpack({
Expand Down
44 changes: 43 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;
platform: Readonly<PlatformTargetProperties>;
options: WebpackOptionsNormalized;
context: string;
requestShortener: RequestShortener;
Expand Down Expand Up @@ -10397,6 +10398,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 +15099,7 @@ declare namespace exports {
) => WebpackOptionsNormalized;
export const applyWebpackOptionsDefaults: (
options: WebpackOptionsNormalized
) => void;
) => false | PlatformTargetProperties;
}
export namespace dependencies {
export {
Expand Down Expand Up @@ -15410,6 +15451,7 @@ declare namespace exports {
NormalModuleReplacementPlugin,
MultiCompiler,
Parser,
PlatformPlugin,
PrefetchPlugin,
ProgressPlugin,
ProvidePlugin,
Expand Down