@rushstack/heft-webpack5-plugin 0.6.0-rc.4 → 0.6.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,12 +1,11 @@
1
1
  # @rushstack/heft-webpack5-plugin
2
2
 
3
- This is a Heft plugin for using Webpack 5.
3
+ This is a Heft plugin for using Webpack 5 during the "bundle" stage.
4
4
 
5
5
  ## Links
6
6
 
7
7
  - [CHANGELOG.md](
8
8
  https://github.com/microsoft/rushstack/blob/main/heft-plugins/heft-webpack5-plugin/CHANGELOG.md) - Find
9
9
  out what's new in the latest version
10
- - [@rushstack/heft](https://www.npmjs.com/package/@rushstack/heft) - Heft is a config-driven toolchain that invokes popular tools such as TypeScript, ESLint, Jest, Webpack, and API Extractor.
11
10
 
12
11
  Heft is part of the [Rush Stack](https://rushstack.io/) family of projects.
@@ -1,114 +1,49 @@
1
- import type { AsyncParallelHook } from 'tapable';
2
- import type { AsyncSeriesBailHook } from 'tapable';
3
- import type { AsyncSeriesHook } from 'tapable';
4
1
  import type { Configuration } from 'webpack-dev-server';
5
- import type { HeftConfiguration } from '@rushstack/heft';
6
- import type { IHeftTaskSession } from '@rushstack/heft';
7
- import type * as TWebpack from 'webpack';
2
+ import type { IBuildStageProperties } from '@rushstack/heft';
3
+ import type { IBundleSubstageProperties } from '@rushstack/heft';
4
+ import type { IHeftPlugin } from '@rushstack/heft';
5
+ import type * as webpack from 'webpack';
8
6
 
9
7
  /**
10
8
  * @public
11
9
  */
12
- export declare type IWebpackConfiguration = IWebpackConfigurationWithDevServer | IWebpackConfigurationWithDevServer[];
10
+ declare const _default: IHeftPlugin<void>;
11
+ export default _default;
13
12
 
14
13
  /**
15
- * The environment passed into the Webpack configuration function. Loosely based
16
- * on the default Webpack environment options, specified here:
17
- * https://webpack.js.org/api/cli/#environment-options
18
- *
19
14
  * @public
20
15
  */
21
- export declare interface IWebpackConfigurationFnEnvironment {
22
- /**
23
- * Whether or not the run is in production mode. Synonym of
24
- * IWebpackConfigurationFnEnvironment.production.
25
- */
26
- prod: boolean;
27
- /**
28
- * Whether or not the run is in production mode. Synonym of
29
- * IWebpackConfigurationFnEnvironment.prod.
30
- */
31
- production: boolean;
32
- /**
33
- * The task session provided to the plugin.
34
- */
35
- taskSession: IHeftTaskSession;
36
- /**
37
- * The Heft configuration provided to the plugin.
38
- */
39
- heftConfiguration: HeftConfiguration;
40
- /**
41
- * The resolved Webpack package.
42
- */
43
- webpack: typeof TWebpack;
44
- }
45
-
46
- /**
47
- * @public
48
- */
49
- export declare interface IWebpackConfigurationWithDevServer extends TWebpack.Configuration {
50
- devServer?: Configuration;
51
- }
52
-
53
- /**
54
- * @public
55
- */
56
- export declare interface IWebpackPluginAccessor {
57
- /**
58
- * Hooks that are called at various points in the Webpack plugin lifecycle.
59
- */
60
- readonly hooks: IWebpackPluginAccessorHooks;
61
- /**
62
- * Parameters that are provided by the Webpack plugin.
63
- */
64
- readonly parameters: IWebpackPluginAccessorParameters;
16
+ export declare interface IWebpackBuildStageProperties extends IBuildStageProperties {
17
+ webpackStats?: webpack.Stats | webpack.MultiStats;
65
18
  }
66
19
 
67
20
  /**
68
21
  * @public
69
22
  */
70
- export declare interface IWebpackPluginAccessorHooks {
23
+ export declare interface IWebpackBundleSubstageProperties extends IBundleSubstageProperties {
71
24
  /**
72
- * A hook that allows for loading custom configurations used by the Webpack
73
- * plugin. If a webpack configuration is provided, this will be populated automatically
74
- * with the exports of the config file. If a webpack configuration is not provided,
75
- * one will be loaded by the Webpack plugin.
25
+ * The configuration used by the Webpack plugin. This must be populated
26
+ * for Webpack to run. If webpackConfigFilePath is specified,
27
+ * this will be populated automatically with the exports of the
28
+ * config file referenced in that property.
76
29
  *
77
30
  * @remarks
78
- * Tapable event handlers can return `false` instead of `undefined` to suppress
79
- * other handlers from creating a configuration object, and prevent webpack from running.
80
- */
81
- readonly onLoadConfiguration: AsyncSeriesBailHook<never, never, never, IWebpackConfiguration | false>;
82
- /**
83
- * A hook that allows for modification of the loaded configuration used by the Webpack
84
- * plugin. If no configuration was loaded, this hook will not be called.
31
+ * Tapable event handlers can return `null` instead of `undefined` to suppress
32
+ * other handlers from creating a configuration object.
85
33
  */
86
- readonly onConfigure: AsyncSeriesHook<IWebpackConfiguration, never, never>;
87
- /**
88
- * A hook that provides the finalized configuration that will be used by Webpack.
89
- * If no configuration was loaded, this hook will not be called.
90
- */
91
- readonly onAfterConfigure: AsyncParallelHook<IWebpackConfiguration, never, never>;
92
- /**
93
- * A hook that provides the stats output from Webpack. If no configuration is loaded,
94
- * this hook will not be called.
95
- */
96
- readonly onEmitStats: AsyncParallelHook<TWebpack.Stats | TWebpack.MultiStats, never, never>;
34
+ webpackConfiguration?: webpack.Configuration | webpack.Configuration[] | null;
97
35
  }
98
36
 
99
37
  /**
100
38
  * @public
101
39
  */
102
- export declare interface IWebpackPluginAccessorParameters {
103
- /**
104
- * Whether or not serve mode was enabled by passing the `--serve` flag.
105
- */
106
- readonly isServeMode: boolean;
107
- }
40
+ export declare type IWebpackConfiguration = IWebpackConfigurationWithDevServer | IWebpackConfigurationWithDevServer[] | undefined;
108
41
 
109
42
  /**
110
43
  * @public
111
44
  */
112
- export declare const PluginName: 'webpack5-plugin';
45
+ export declare interface IWebpackConfigurationWithDevServer extends webpack.Configuration {
46
+ devServer?: Configuration;
47
+ }
113
48
 
114
49
  export { }
@@ -5,7 +5,7 @@
5
5
  "toolPackages": [
6
6
  {
7
7
  "packageName": "@microsoft/api-extractor",
8
- "packageVersion": "7.33.7"
8
+ "packageVersion": "7.34.4"
9
9
  }
10
10
  ]
11
11
  }
@@ -1,19 +1,7 @@
1
- import type * as TWebpack from 'webpack';
2
- import type { IScopedLogger, IHeftTaskSession, HeftConfiguration } from '@rushstack/heft';
3
- import type { IWebpackPluginOptions } from './Webpack5Plugin';
4
- import type { IWebpackConfiguration } from './shared';
5
- interface ILoadWebpackConfigurationOptions extends IWebpackPluginOptions {
6
- taskSession: IHeftTaskSession;
7
- heftConfiguration: HeftConfiguration;
8
- loadWebpackAsyncFn: () => Promise<typeof TWebpack>;
9
- }
1
+ import type { IBuildStageProperties, ScopedLogger } from '@rushstack/heft';
2
+ import { IWebpackConfiguration } from './shared';
10
3
  export declare class WebpackConfigurationLoader {
11
- private readonly _logger;
12
- private readonly _production;
13
- private readonly _serveMode;
14
- constructor(logger: IScopedLogger, production: boolean, serveMode: boolean);
15
- tryLoadWebpackConfigurationAsync(options: ILoadWebpackConfigurationOptions): Promise<IWebpackConfiguration | undefined>;
16
- private _tryLoadWebpackConfigurationInnerAsync;
4
+ static tryLoadWebpackConfigAsync(logger: ScopedLogger, buildFolder: string, buildProperties: IBuildStageProperties): Promise<IWebpackConfiguration | undefined>;
5
+ private static _tryLoadWebpackConfiguration;
17
6
  }
18
- export {};
19
7
  //# sourceMappingURL=WebpackConfigurationLoader.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"WebpackConfigurationLoader.d.ts","sourceRoot":"","sources":["../src/WebpackConfigurationLoader.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,KAAK,QAAQ,MAAM,SAAS,CAAC;AAEzC,OAAO,KAAK,EAAE,aAAa,EAAE,gBAAgB,EAAE,iBAAiB,EAAE,MAAM,iBAAiB,CAAC;AAE1F,OAAO,KAAK,EAAE,qBAAqB,EAAE,MAAM,kBAAkB,CAAC;AAC9D,OAAO,KAAK,EAAE,qBAAqB,EAAsC,MAAM,UAAU,CAAC;AAW1F,UAAU,gCAAiC,SAAQ,qBAAqB;IACtE,WAAW,EAAE,gBAAgB,CAAC;IAC9B,iBAAiB,EAAE,iBAAiB,CAAC;IACrC,kBAAkB,EAAE,MAAM,OAAO,CAAC,OAAO,QAAQ,CAAC,CAAC;CACpD;AAKD,qBAAa,0BAA0B;IACrC,OAAO,CAAC,QAAQ,CAAC,OAAO,CAAgB;IACxC,OAAO,CAAC,QAAQ,CAAC,WAAW,CAAU;IACtC,OAAO,CAAC,QAAQ,CAAC,UAAU,CAAU;gBAElB,MAAM,EAAE,aAAa,EAAE,UAAU,EAAE,OAAO,EAAE,SAAS,EAAE,OAAO;IAMpE,gCAAgC,CAC3C,OAAO,EAAE,gCAAgC,GACxC,OAAO,CAAC,qBAAqB,GAAG,SAAS,CAAC;YAwD/B,sCAAsC;CAmBrD"}
1
+ {"version":3,"file":"WebpackConfigurationLoader.d.ts","sourceRoot":"","sources":["../src/WebpackConfigurationLoader.ts"],"names":[],"mappings":"AAMA,OAAO,KAAK,EAAE,qBAAqB,EAAE,YAAY,EAAE,MAAM,iBAAiB,CAAC;AAE3E,OAAO,EAAE,qBAAqB,EAAE,MAAM,UAAU,CAAC;AAuBjD,qBAAa,0BAA0B;WACjB,yBAAyB,CAC3C,MAAM,EAAE,YAAY,EACpB,WAAW,EAAE,MAAM,EACnB,eAAe,EAAE,qBAAqB,GACrC,OAAO,CAAC,qBAAqB,GAAG,SAAS,CAAC;IAgD7C,OAAO,CAAC,MAAM,CAAC,4BAA4B;CAe5C"}
@@ -28,46 +28,31 @@ Object.defineProperty(exports, "__esModule", { value: true });
28
28
  exports.WebpackConfigurationLoader = void 0;
29
29
  const path = __importStar(require("path"));
30
30
  const node_core_library_1 = require("@rushstack/node-core-library");
31
- const DEFAULT_WEBPACK_CONFIG_PATH = './webpack.config.js';
32
- const DEFAULT_WEBPACK_DEV_CONFIG_PATH = './webpack.dev.config.js';
33
31
  class WebpackConfigurationLoader {
34
- constructor(logger, production, serveMode) {
35
- this._logger = logger;
36
- this._production = production;
37
- this._serveMode = serveMode;
38
- }
39
- async tryLoadWebpackConfigurationAsync(options) {
32
+ static async tryLoadWebpackConfigAsync(logger, buildFolder, buildProperties) {
40
33
  // TODO: Eventually replace this custom logic with a call to this utility in in webpack-cli:
41
34
  // https://github.com/webpack/webpack-cli/blob/next/packages/webpack-cli/lib/groups/ConfigGroup.js
42
- const { taskSession, heftConfiguration, configurationPath, devConfigurationPath, loadWebpackAsyncFn } = options;
35
+ const webpackConfigFiles = await findWebpackConfigAsync(buildFolder);
36
+ const webpackDevConfigFilename = webpackConfigFiles.dev;
37
+ const webpackConfigFilename = webpackConfigFiles.prod;
43
38
  let webpackConfigJs;
44
39
  try {
45
- const buildFolderPath = heftConfiguration.buildFolderPath;
46
- if (this._serveMode) {
47
- const devConfigPath = path.resolve(buildFolderPath, devConfigurationPath || DEFAULT_WEBPACK_DEV_CONFIG_PATH);
48
- this._logger.terminal.writeVerboseLine(`Attempting to load webpack configuration from "${devConfigPath}".`);
49
- webpackConfigJs = await this._tryLoadWebpackConfigurationInnerAsync(devConfigPath);
40
+ if (buildProperties.serveMode && webpackDevConfigFilename) {
41
+ logger.terminal.writeVerboseLine(`Attempting to load webpack configuration from "${webpackDevConfigFilename}".`);
42
+ webpackConfigJs = WebpackConfigurationLoader._tryLoadWebpackConfiguration(buildFolder, webpackDevConfigFilename);
50
43
  }
51
- if (!webpackConfigJs) {
52
- const configPath = path.resolve(buildFolderPath, configurationPath || DEFAULT_WEBPACK_CONFIG_PATH);
53
- this._logger.terminal.writeVerboseLine(`Attempting to load webpack configuration from "${configPath}".`);
54
- webpackConfigJs = await this._tryLoadWebpackConfigurationInnerAsync(configPath);
44
+ if (!webpackConfigJs && webpackConfigFilename) {
45
+ logger.terminal.writeVerboseLine(`Attempting to load webpack configuration from "${webpackConfigFilename}".`);
46
+ webpackConfigJs = WebpackConfigurationLoader._tryLoadWebpackConfiguration(buildFolder, webpackConfigFilename);
55
47
  }
56
48
  }
57
49
  catch (error) {
58
- this._logger.emitError(error);
50
+ logger.emitError(error);
59
51
  }
60
52
  if (webpackConfigJs) {
61
53
  const webpackConfig = webpackConfigJs.default || webpackConfigJs;
62
54
  if (typeof webpackConfig === 'function') {
63
- // Defer loading of webpack until we know for sure that we will need it
64
- return webpackConfig({
65
- prod: this._production,
66
- production: this._production,
67
- taskSession,
68
- heftConfiguration,
69
- webpack: await loadWebpackAsyncFn()
70
- });
55
+ return webpackConfig({ prod: buildProperties.production, production: buildProperties.production });
71
56
  }
72
57
  else {
73
58
  return webpackConfig;
@@ -77,19 +62,14 @@ class WebpackConfigurationLoader {
77
62
  return undefined;
78
63
  }
79
64
  }
80
- async _tryLoadWebpackConfigurationInnerAsync(configurationPath) {
81
- const configExists = await node_core_library_1.FileSystem.existsAsync(configurationPath);
82
- if (configExists) {
65
+ static _tryLoadWebpackConfiguration(buildFolder, configurationFilename) {
66
+ const fullWebpackConfigPath = path.join(buildFolder, configurationFilename);
67
+ if (node_core_library_1.FileSystem.exists(fullWebpackConfigPath)) {
83
68
  try {
84
- return await Promise.resolve().then(() => __importStar(require(configurationPath)));
69
+ return require(fullWebpackConfigPath);
85
70
  }
86
71
  catch (e) {
87
- const error = e;
88
- if (error.code === 'ERR_MODULE_NOT_FOUND') {
89
- // No configuration found, return undefined.
90
- return undefined;
91
- }
92
- throw new Error(`Error loading webpack configuration at "${configurationPath}": ${e}`);
72
+ throw new Error(`Error loading webpack configuration at "${fullWebpackConfigPath}": ${e}`);
93
73
  }
94
74
  }
95
75
  else {
@@ -98,4 +78,34 @@ class WebpackConfigurationLoader {
98
78
  }
99
79
  }
100
80
  exports.WebpackConfigurationLoader = WebpackConfigurationLoader;
81
+ async function findWebpackConfigAsync(buildFolder) {
82
+ try {
83
+ const folderItems = await node_core_library_1.FileSystem.readFolderItemsAsync(buildFolder);
84
+ const dev = [];
85
+ const prod = [];
86
+ for (const folderItem of folderItems) {
87
+ if (folderItem.isFile()) {
88
+ if (folderItem.name.match(/^webpack.dev.config\.(cjs|js|mjs)$/)) {
89
+ dev.push(folderItem.name);
90
+ }
91
+ else if (folderItem.name.match(/^webpack.config\.(cjs|js|mjs)$/)) {
92
+ prod.push(folderItem.name);
93
+ }
94
+ }
95
+ }
96
+ if (dev.length > 1) {
97
+ throw new Error(`Error: Found more than one dev webpack configuration file.`);
98
+ }
99
+ else if (prod.length > 1) {
100
+ throw new Error(`Error: Found more than one non-dev webpack configuration file.`);
101
+ }
102
+ return {
103
+ dev: dev[0],
104
+ prod: prod[0]
105
+ };
106
+ }
107
+ catch (e) {
108
+ throw new Error(`Error finding webpack configuration: ${e}`);
109
+ }
110
+ }
101
111
  //# sourceMappingURL=WebpackConfigurationLoader.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"WebpackConfigurationLoader.js","sourceRoot":"","sources":["../src/WebpackConfigurationLoader.ts"],"names":[],"mappings":";AAAA,4FAA4F;AAC5F,2DAA2D;;;;;;;;;;;;;;;;;;;;;;;;;;AAE3D,2CAA6B;AAE7B,oEAA0D;AAqB1D,MAAM,2BAA2B,GAA0B,qBAAqB,CAAC;AACjF,MAAM,+BAA+B,GAA8B,yBAAyB,CAAC;AAE7F,MAAa,0BAA0B;IAKrC,YAAmB,MAAqB,EAAE,UAAmB,EAAE,SAAkB;QAC/E,IAAI,CAAC,OAAO,GAAG,MAAM,CAAC;QACtB,IAAI,CAAC,WAAW,GAAG,UAAU,CAAC;QAC9B,IAAI,CAAC,UAAU,GAAG,SAAS,CAAC;IAC9B,CAAC;IAEM,KAAK,CAAC,gCAAgC,CAC3C,OAAyC;QAEzC,4FAA4F;QAC5F,kGAAkG;QAElG,MAAM,EAAE,WAAW,EAAE,iBAAiB,EAAE,iBAAiB,EAAE,oBAAoB,EAAE,kBAAkB,EAAE,GACnG,OAAO,CAAC;QACV,IAAI,eAA6C,CAAC;QAElD,IAAI;YACF,MAAM,eAAe,GAAW,iBAAiB,CAAC,eAAe,CAAC;YAClE,IAAI,IAAI,CAAC,UAAU,EAAE;gBACnB,MAAM,aAAa,GAAW,IAAI,CAAC,OAAO,CACxC,eAAe,EACf,oBAAoB,IAAI,+BAA+B,CACxD,CAAC;gBACF,IAAI,CAAC,OAAO,CAAC,QAAQ,CAAC,gBAAgB,CACpC,kDAAkD,aAAa,IAAI,CACpE,CAAC;gBACF,eAAe,GAAG,MAAM,IAAI,CAAC,sCAAsC,CAAC,aAAa,CAAC,CAAC;aACpF;YAED,IAAI,CAAC,eAAe,EAAE;gBACpB,MAAM,UAAU,GAAW,IAAI,CAAC,OAAO,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;CACF;AAxFD,gEAwFC","sourcesContent":["// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.\n// See LICENSE in the project root for license information.\n\nimport * as path from 'path';\nimport type * as TWebpack from 'webpack';\nimport { FileSystem } from '@rushstack/node-core-library';\nimport type { IScopedLogger, IHeftTaskSession, HeftConfiguration } from '@rushstack/heft';\n\nimport type { IWebpackPluginOptions } from './Webpack5Plugin';\nimport type { IWebpackConfiguration, IWebpackConfigurationFnEnvironment } from './shared';\n\ntype IWebpackConfigJsExport =\n | TWebpack.Configuration\n | TWebpack.Configuration[]\n | Promise<TWebpack.Configuration>\n | Promise<TWebpack.Configuration[]>\n | ((env: IWebpackConfigurationFnEnvironment) => TWebpack.Configuration | TWebpack.Configuration[])\n | ((env: IWebpackConfigurationFnEnvironment) => Promise<TWebpack.Configuration | TWebpack.Configuration[]>);\ntype IWebpackConfigJs = IWebpackConfigJsExport | { default: IWebpackConfigJsExport };\n\ninterface ILoadWebpackConfigurationOptions extends IWebpackPluginOptions {\n taskSession: IHeftTaskSession;\n heftConfiguration: HeftConfiguration;\n loadWebpackAsyncFn: () => Promise<typeof TWebpack>;\n}\n\nconst DEFAULT_WEBPACK_CONFIG_PATH: './webpack.config.js' = './webpack.config.js';\nconst DEFAULT_WEBPACK_DEV_CONFIG_PATH: './webpack.dev.config.js' = './webpack.dev.config.js';\n\nexport class WebpackConfigurationLoader {\n private readonly _logger: IScopedLogger;\n private readonly _production: boolean;\n private readonly _serveMode: boolean;\n\n public constructor(logger: IScopedLogger, production: boolean, serveMode: boolean) {\n this._logger = logger;\n this._production = production;\n this._serveMode = serveMode;\n }\n\n public async tryLoadWebpackConfigurationAsync(\n options: ILoadWebpackConfigurationOptions\n ): Promise<IWebpackConfiguration | undefined> {\n // TODO: Eventually replace this custom logic with a call to this utility in in webpack-cli:\n // https://github.com/webpack/webpack-cli/blob/next/packages/webpack-cli/lib/groups/ConfigGroup.js\n\n const { taskSession, heftConfiguration, configurationPath, devConfigurationPath, loadWebpackAsyncFn } =\n options;\n let webpackConfigJs: IWebpackConfigJs | undefined;\n\n try {\n const buildFolderPath: string = heftConfiguration.buildFolderPath;\n if (this._serveMode) {\n const devConfigPath: string = path.resolve(\n buildFolderPath,\n devConfigurationPath || DEFAULT_WEBPACK_DEV_CONFIG_PATH\n );\n this._logger.terminal.writeVerboseLine(\n `Attempting to load webpack configuration from \"${devConfigPath}\".`\n );\n webpackConfigJs = await this._tryLoadWebpackConfigurationInnerAsync(devConfigPath);\n }\n\n if (!webpackConfigJs) {\n const configPath: string = path.resolve(\n buildFolderPath,\n configurationPath || DEFAULT_WEBPACK_CONFIG_PATH\n );\n this._logger.terminal.writeVerboseLine(\n `Attempting to load webpack configuration from \"${configPath}\".`\n );\n webpackConfigJs = await this._tryLoadWebpackConfigurationInnerAsync(configPath);\n }\n } catch (error) {\n this._logger.emitError(error as Error);\n }\n\n if (webpackConfigJs) {\n const webpackConfig: IWebpackConfigJsExport =\n (webpackConfigJs as { default: IWebpackConfigJsExport }).default || webpackConfigJs;\n\n if (typeof webpackConfig === 'function') {\n // Defer loading of webpack until we know for sure that we will need it\n return webpackConfig({\n prod: this._production,\n production: this._production,\n taskSession,\n heftConfiguration,\n webpack: await loadWebpackAsyncFn()\n });\n } else {\n return webpackConfig;\n }\n } else {\n return undefined;\n }\n }\n\n private async _tryLoadWebpackConfigurationInnerAsync(\n configurationPath: string\n ): Promise<IWebpackConfigJs | undefined> {\n const configExists: boolean = await FileSystem.existsAsync(configurationPath);\n if (configExists) {\n try {\n return await import(configurationPath);\n } catch (e) {\n const error: NodeJS.ErrnoException = e as NodeJS.ErrnoException;\n if (error.code === 'ERR_MODULE_NOT_FOUND') {\n // No configuration found, return undefined.\n return undefined;\n }\n throw new Error(`Error loading webpack configuration at \"${configurationPath}\": ${e}`);\n }\n } else {\n return undefined;\n }\n }\n}\n"]}
1
+ {"version":3,"file":"WebpackConfigurationLoader.js","sourceRoot":"","sources":["../src/WebpackConfigurationLoader.ts"],"names":[],"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,MAAM,IAAI,KAAK,CAAC,gEAAgE,CAAC,CAAC;SACnF;QAED,OAAO;YACL,GAAG,EAAE,GAAG,CAAC,CAAC,CAAC;YACX,IAAI,EAAE,IAAI,CAAC,CAAC,CAAC;SACd,CAAC;KACH;IAAC,OAAO,CAAC,EAAE;QACV,MAAM,IAAI,KAAK,CAAC,wCAAwC,CAAC,EAAE,CAAC,CAAC;KAC9D;AACH,CAAC","sourcesContent":["// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.\n// See LICENSE in the project root for license information.\n\nimport * as path from 'path';\nimport { type FolderItem, FileSystem } from '@rushstack/node-core-library';\nimport type * as webpack from 'webpack';\nimport type { IBuildStageProperties, ScopedLogger } from '@rushstack/heft';\n\nimport { IWebpackConfiguration } from './shared';\n\n/**\n * See https://webpack.js.org/api/cli/#environment-options\n */\ninterface IWebpackConfigFunctionEnv {\n prod: boolean;\n production: boolean;\n}\ntype IWebpackConfigJsExport =\n | webpack.Configuration\n | webpack.Configuration[]\n | Promise<webpack.Configuration>\n | Promise<webpack.Configuration[]>\n | ((env: IWebpackConfigFunctionEnv) => webpack.Configuration | webpack.Configuration[])\n | ((env: IWebpackConfigFunctionEnv) => Promise<webpack.Configuration | webpack.Configuration[]>);\ntype IWebpackConfigJs = IWebpackConfigJsExport | { default: IWebpackConfigJsExport };\n\ninterface IWebpackConfigFileNames {\n dev: string | undefined;\n prod: string | undefined;\n}\n\nexport class WebpackConfigurationLoader {\n public static async tryLoadWebpackConfigAsync(\n logger: ScopedLogger,\n buildFolder: string,\n buildProperties: IBuildStageProperties\n ): Promise<IWebpackConfiguration | undefined> {\n // TODO: Eventually replace this custom logic with a call to this utility in in webpack-cli:\n // https://github.com/webpack/webpack-cli/blob/next/packages/webpack-cli/lib/groups/ConfigGroup.js\n\n const webpackConfigFiles: IWebpackConfigFileNames | undefined = await findWebpackConfigAsync(buildFolder);\n const webpackDevConfigFilename: string | undefined = webpackConfigFiles.dev;\n const webpackConfigFilename: string | undefined = webpackConfigFiles.prod;\n\n let webpackConfigJs: IWebpackConfigJs | undefined;\n\n try {\n if (buildProperties.serveMode && webpackDevConfigFilename) {\n logger.terminal.writeVerboseLine(\n `Attempting to load webpack configuration from \"${webpackDevConfigFilename}\".`\n );\n webpackConfigJs = WebpackConfigurationLoader._tryLoadWebpackConfiguration(\n buildFolder,\n webpackDevConfigFilename\n );\n }\n\n if (!webpackConfigJs && webpackConfigFilename) {\n logger.terminal.writeVerboseLine(\n `Attempting to load webpack configuration from \"${webpackConfigFilename}\".`\n );\n webpackConfigJs = WebpackConfigurationLoader._tryLoadWebpackConfiguration(\n buildFolder,\n webpackConfigFilename\n );\n }\n } catch (error) {\n logger.emitError(error as Error);\n }\n\n if (webpackConfigJs) {\n const webpackConfig: IWebpackConfigJsExport =\n (webpackConfigJs as { default: IWebpackConfigJsExport }).default || webpackConfigJs;\n\n if (typeof webpackConfig === 'function') {\n return webpackConfig({ prod: buildProperties.production, production: buildProperties.production });\n } else {\n return webpackConfig;\n }\n } else {\n return undefined;\n }\n }\n\n private static _tryLoadWebpackConfiguration(\n buildFolder: string,\n configurationFilename: string\n ): IWebpackConfigJs | undefined {\n const fullWebpackConfigPath: string = path.join(buildFolder, configurationFilename);\n if (FileSystem.exists(fullWebpackConfigPath)) {\n try {\n return require(fullWebpackConfigPath);\n } catch (e) {\n throw new Error(`Error loading webpack configuration at \"${fullWebpackConfigPath}\": ${e}`);\n }\n } else {\n return undefined;\n }\n }\n}\n\nasync function findWebpackConfigAsync(buildFolder: string): Promise<IWebpackConfigFileNames> {\n try {\n const folderItems: FolderItem[] = await FileSystem.readFolderItemsAsync(buildFolder);\n const dev: string[] = [];\n const prod: string[] = [];\n\n for (const folderItem of folderItems) {\n if (folderItem.isFile()) {\n if (folderItem.name.match(/^webpack.dev.config\\.(cjs|js|mjs)$/)) {\n dev.push(folderItem.name);\n } else if (folderItem.name.match(/^webpack.config\\.(cjs|js|mjs)$/)) {\n prod.push(folderItem.name);\n }\n }\n }\n\n if (dev.length > 1) {\n throw new Error(`Error: Found more than one dev webpack configuration file.`);\n } else if (prod.length > 1) {\n throw new Error(`Error: Found more than one non-dev webpack configuration file.`);\n }\n\n return {\n dev: dev[0],\n prod: prod[0]\n };\n } catch (e) {\n throw new Error(`Error finding webpack configuration: ${e}`);\n }\n}\n"]}
@@ -0,0 +1,14 @@
1
+ import type { HeftConfiguration, HeftSession, IHeftPlugin } from '@rushstack/heft';
2
+ /**
3
+ * @internal
4
+ */
5
+ export declare class WebpackPlugin implements IHeftPlugin {
6
+ readonly pluginName: string;
7
+ private static _webpackVersions;
8
+ private static _getWebpackVersions;
9
+ apply(heftSession: HeftSession, heftConfiguration: HeftConfiguration): void;
10
+ private _runWebpackAsync;
11
+ private _emitErrors;
12
+ private _normalizeError;
13
+ }
14
+ //# sourceMappingURL=WebpackPlugin.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"WebpackPlugin.d.ts","sourceRoot":"","sources":["../src/WebpackPlugin.ts"],"names":[],"mappings":"AAcA,OAAO,KAAK,EACV,iBAAiB,EACjB,WAAW,EAIX,WAAW,EAEZ,MAAM,iBAAiB,CAAC;AAmBzB;;GAEG;AACH,qBAAa,aAAc,YAAW,WAAW;IAC/C,SAAgB,UAAU,EAAE,MAAM,CAAe;IAEjD,OAAO,CAAC,MAAM,CAAC,gBAAgB,CAA+B;IAC9D,OAAO,CAAC,MAAM,CAAC,mBAAmB;IAkB3B,KAAK,CAAC,WAAW,EAAE,WAAW,EAAE,iBAAiB,EAAE,iBAAiB,GAAG,IAAI;YA2CpE,gBAAgB;IAoK9B,OAAO,CAAC,WAAW;IAyCnB,OAAO,CAAC,eAAe;CAqBxB"}
@@ -0,0 +1,266 @@
1
+ "use strict";
2
+ // Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
3
+ // See LICENSE in the project root for license information.
4
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
5
+ if (k2 === undefined) k2 = k;
6
+ var desc = Object.getOwnPropertyDescriptor(m, k);
7
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
8
+ desc = { enumerable: true, get: function() { return m[k]; } };
9
+ }
10
+ Object.defineProperty(o, k2, desc);
11
+ }) : (function(o, m, k, k2) {
12
+ if (k2 === undefined) k2 = k;
13
+ o[k2] = m[k];
14
+ }));
15
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
16
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
17
+ }) : function(o, v) {
18
+ o["default"] = v;
19
+ });
20
+ var __importStar = (this && this.__importStar) || function (mod) {
21
+ if (mod && mod.__esModule) return mod;
22
+ var result = {};
23
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
24
+ __setModuleDefault(result, mod);
25
+ return result;
26
+ };
27
+ Object.defineProperty(exports, "__esModule", { value: true });
28
+ exports.WebpackPlugin = void 0;
29
+ const nodePath = __importStar(require("path"));
30
+ const node_core_library_1 = require("@rushstack/node-core-library");
31
+ const WebpackConfigurationLoader_1 = require("./WebpackConfigurationLoader");
32
+ const webpack = node_core_library_1.Import.lazy('webpack', require);
33
+ const PLUGIN_NAME = 'WebpackPlugin';
34
+ const WEBPACK_DEV_SERVER_PACKAGE_NAME = 'webpack-dev-server';
35
+ const WEBPACK_DEV_SERVER_ENV_VAR_NAME = 'WEBPACK_DEV_SERVER';
36
+ /**
37
+ * @internal
38
+ */
39
+ class WebpackPlugin {
40
+ constructor() {
41
+ this.pluginName = PLUGIN_NAME;
42
+ }
43
+ static _getWebpackVersions() {
44
+ if (!WebpackPlugin._webpackVersions) {
45
+ const webpackDevServerPackageJsonPath = node_core_library_1.Import.resolveModule({
46
+ modulePath: 'webpack-dev-server/package.json',
47
+ baseFolderPath: __dirname
48
+ });
49
+ const webpackDevServerPackageJson = node_core_library_1.PackageJsonLookup.instance.loadPackageJson(webpackDevServerPackageJsonPath);
50
+ WebpackPlugin._webpackVersions = {
51
+ webpackVersion: webpack.version,
52
+ webpackDevServerVersion: webpackDevServerPackageJson.version
53
+ };
54
+ }
55
+ return WebpackPlugin._webpackVersions;
56
+ }
57
+ apply(heftSession, heftConfiguration) {
58
+ heftSession.hooks.build.tap(PLUGIN_NAME, (build) => {
59
+ build.hooks.bundle.tap(PLUGIN_NAME, (bundle) => {
60
+ bundle.hooks.configureWebpack.tap({ name: PLUGIN_NAME, stage: Number.MIN_SAFE_INTEGER }, (webpackConfiguration) => {
61
+ const webpackVersions = WebpackPlugin._getWebpackVersions();
62
+ bundle.properties.webpackVersion = webpack.version;
63
+ bundle.properties.webpackDevServerVersion = webpackVersions.webpackDevServerVersion;
64
+ return webpackConfiguration;
65
+ });
66
+ bundle.hooks.configureWebpack.tapPromise(PLUGIN_NAME, async (existingConfiguration) => {
67
+ const logger = heftSession.requestScopedLogger('configure-webpack');
68
+ if (existingConfiguration) {
69
+ logger.terminal.writeVerboseLine('Skipping loading webpack config file because the webpack config has already been set.');
70
+ return existingConfiguration;
71
+ }
72
+ else {
73
+ return await WebpackConfigurationLoader_1.WebpackConfigurationLoader.tryLoadWebpackConfigAsync(logger, heftConfiguration.buildFolder, build.properties);
74
+ }
75
+ });
76
+ bundle.hooks.run.tapPromise(PLUGIN_NAME, async () => {
77
+ await this._runWebpackAsync(heftSession, heftConfiguration, bundle.properties, build.properties, heftConfiguration.terminalProvider.supportsColor);
78
+ });
79
+ });
80
+ });
81
+ }
82
+ async _runWebpackAsync(heftSession, heftConfiguration, bundleSubstageProperties, buildProperties, supportsColor) {
83
+ const webpackConfiguration = bundleSubstageProperties.webpackConfiguration;
84
+ if (!webpackConfiguration) {
85
+ return;
86
+ }
87
+ const logger = heftSession.requestScopedLogger('webpack');
88
+ const webpackVersions = WebpackPlugin._getWebpackVersions();
89
+ if (bundleSubstageProperties.webpackVersion !== webpackVersions.webpackVersion) {
90
+ logger.emitError(new Error(`The Webpack plugin expected to be configured with Webpack version ${webpackVersions.webpackVersion}, ` +
91
+ `but the configuration specifies version ${bundleSubstageProperties.webpackVersion}. ` +
92
+ 'Are multiple versions of the Webpack plugin present?'));
93
+ }
94
+ if (bundleSubstageProperties.webpackDevServerVersion !== webpackVersions.webpackDevServerVersion) {
95
+ logger.emitError(new Error(`The Webpack plugin expected to be configured with webpack-dev-server version ${webpackVersions.webpackDevServerVersion}, ` +
96
+ `but the configuration specifies version ${bundleSubstageProperties.webpackDevServerVersion}. ` +
97
+ 'Are multiple versions of the Webpack plugin present?'));
98
+ }
99
+ logger.terminal.writeLine(`Using Webpack version ${webpack.version}`);
100
+ let compiler;
101
+ if (Array.isArray(webpackConfiguration)) {
102
+ if (webpackConfiguration.length === 0) {
103
+ logger.terminal.writeLine('The webpack configuration is an empty array - nothing to do.');
104
+ return;
105
+ }
106
+ else {
107
+ compiler = webpack(webpackConfiguration); /* (webpack.Compilation[]) => MultiCompiler */
108
+ }
109
+ }
110
+ else {
111
+ compiler = webpack(webpackConfiguration); /* (webpack.Compilation) => Compiler */
112
+ }
113
+ if (buildProperties.serveMode) {
114
+ const defaultDevServerOptions = {
115
+ host: 'localhost',
116
+ devMiddleware: {
117
+ publicPath: '/',
118
+ stats: {
119
+ cached: false,
120
+ cachedAssets: false,
121
+ colors: supportsColor
122
+ }
123
+ },
124
+ client: {
125
+ logging: 'info',
126
+ webSocketURL: {
127
+ port: 8080
128
+ }
129
+ },
130
+ port: 8080
131
+ };
132
+ let options;
133
+ if (Array.isArray(webpackConfiguration)) {
134
+ const devServerOptions = webpackConfiguration
135
+ .map((configuration) => configuration.devServer)
136
+ .filter((devServer) => !!devServer);
137
+ if (devServerOptions.length > 1) {
138
+ logger.emitWarning(new Error(`Detected multiple webpack devServer configurations, using the first one.`));
139
+ }
140
+ if (devServerOptions.length > 0) {
141
+ options = Object.assign(Object.assign({}, defaultDevServerOptions), devServerOptions[0]);
142
+ }
143
+ else {
144
+ options = defaultDevServerOptions;
145
+ }
146
+ }
147
+ else {
148
+ options = Object.assign(Object.assign({}, defaultDevServerOptions), webpackConfiguration.devServer);
149
+ }
150
+ // Register a plugin to callback after webpack is done with the first compilation
151
+ // so we can move on to post-build
152
+ let firstCompilationDoneCallback;
153
+ const originalBeforeCallback = options.setupMiddlewares;
154
+ options.setupMiddlewares = (middlewares, devServer) => {
155
+ compiler.hooks.done.tap('heft-webpack-plugin', () => {
156
+ if (firstCompilationDoneCallback) {
157
+ firstCompilationDoneCallback();
158
+ firstCompilationDoneCallback = undefined;
159
+ }
160
+ });
161
+ if (originalBeforeCallback) {
162
+ return originalBeforeCallback(middlewares, devServer);
163
+ }
164
+ return middlewares;
165
+ };
166
+ // The webpack-dev-server package has a design flaw, where merely loading its package will set the
167
+ // WEBPACK_DEV_SERVER environment variable -- even if no APIs are accessed. This environment variable
168
+ // causes incorrect behavior if Heft is not running in serve mode. Thus, we need to be careful to call require()
169
+ // only if Heft is in serve mode.
170
+ const WebpackDevServer = require(WEBPACK_DEV_SERVER_PACKAGE_NAME);
171
+ // TODO: the WebpackDevServer accepts a third parameter for a logger. We should make
172
+ // use of that to make logging cleaner
173
+ const webpackDevServer = new WebpackDevServer(options, compiler);
174
+ await new Promise((resolve, reject) => {
175
+ firstCompilationDoneCallback = resolve;
176
+ // Wrap in promise.resolve due to small issue in the type declaration, return type should be
177
+ // webpackDevServer.start(): Promise<void>;
178
+ Promise.resolve(webpackDevServer.start()).catch(reject);
179
+ });
180
+ }
181
+ else {
182
+ if (process.env[WEBPACK_DEV_SERVER_ENV_VAR_NAME]) {
183
+ logger.emitWarning(new Error(`The "${WEBPACK_DEV_SERVER_ENV_VAR_NAME}" environment variable is set, ` +
184
+ 'which will cause problems when webpack is not running in serve mode. ' +
185
+ `(Did a dependency inadvertently load the "${WEBPACK_DEV_SERVER_PACKAGE_NAME}" package?)`));
186
+ }
187
+ let stats;
188
+ if (buildProperties.watchMode) {
189
+ try {
190
+ stats = await node_core_library_1.LegacyAdapters.convertCallbackToPromise(compiler.watch.bind(compiler), {});
191
+ }
192
+ catch (e) {
193
+ logger.emitError(e);
194
+ }
195
+ }
196
+ else {
197
+ try {
198
+ stats = await node_core_library_1.LegacyAdapters.convertCallbackToPromise(compiler.run.bind(compiler));
199
+ await node_core_library_1.LegacyAdapters.convertCallbackToPromise(compiler.close.bind(compiler));
200
+ }
201
+ catch (e) {
202
+ logger.emitError(e);
203
+ }
204
+ }
205
+ if (stats) {
206
+ // eslint-disable-next-line require-atomic-updates
207
+ buildProperties.webpackStats = stats;
208
+ this._emitErrors(logger, heftConfiguration.buildFolder, stats);
209
+ }
210
+ }
211
+ }
212
+ _emitErrors(logger, buildFolder, stats) {
213
+ if (stats.hasErrors() || stats.hasWarnings()) {
214
+ const serializedStats = [stats.toJson('errors-warnings')];
215
+ const errors = [];
216
+ const warnings = [];
217
+ for (const compilationStats of serializedStats) {
218
+ if (compilationStats.warnings) {
219
+ for (const warning of compilationStats.warnings) {
220
+ warnings.push(this._normalizeError(buildFolder, warning));
221
+ }
222
+ }
223
+ if (compilationStats.errors) {
224
+ for (const error of compilationStats.errors) {
225
+ errors.push(this._normalizeError(buildFolder, error));
226
+ }
227
+ }
228
+ if (compilationStats.children) {
229
+ for (const child of compilationStats.children) {
230
+ serializedStats.push(child);
231
+ }
232
+ }
233
+ }
234
+ for (const warning of warnings) {
235
+ logger.emitWarning(warning);
236
+ }
237
+ for (const error of errors) {
238
+ logger.emitError(error);
239
+ }
240
+ }
241
+ }
242
+ _normalizeError(buildFolder, error) {
243
+ if (error instanceof Error) {
244
+ return error;
245
+ }
246
+ else {
247
+ let moduleName = error.moduleName;
248
+ if (!moduleName && error.moduleIdentifier) {
249
+ moduleName = node_core_library_1.Path.convertToSlashes(nodePath.relative(buildFolder, error.moduleIdentifier));
250
+ }
251
+ let formattedError;
252
+ if (error.loc && moduleName) {
253
+ formattedError = `${moduleName}:${error.loc} - ${error.message}`;
254
+ }
255
+ else if (moduleName) {
256
+ formattedError = `${moduleName} - ${error.message}`;
257
+ }
258
+ else {
259
+ formattedError = error.message;
260
+ }
261
+ return new Error(formattedError);
262
+ }
263
+ }
264
+ }
265
+ exports.WebpackPlugin = WebpackPlugin;
266
+ //# sourceMappingURL=WebpackPlugin.js.map