@rushstack/heft-webpack4-plugin 0.5.16 → 0.6.0-dev.0

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.
@@ -28,30 +28,46 @@ 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 WEBPACK_CONFIG_FILENAME = 'webpack.config.js';
32
- const WEBPACK_DEV_CONFIG_FILENAME = 'webpack.dev.config.js';
31
+ const DEFAULT_WEBPACK_CONFIG_PATH = './webpack.config.js';
32
+ const DEFAULT_WEBPACK_DEV_CONFIG_PATH = './webpack.dev.config.js';
33
33
  class WebpackConfigurationLoader {
34
- static async tryLoadWebpackConfigAsync(logger, buildFolder, buildProperties) {
34
+ constructor(logger, production, serveMode) {
35
+ this._logger = logger;
36
+ this._production = production;
37
+ this._serveMode = serveMode;
38
+ }
39
+ async tryLoadWebpackConfigurationAsync(options) {
35
40
  // TODO: Eventually replace this custom logic with a call to this utility in in webpack-cli:
36
41
  // https://github.com/webpack/webpack-cli/blob/next/packages/webpack-cli/lib/groups/ConfigGroup.js
42
+ const { taskSession, heftConfiguration, configurationPath, devConfigurationPath, loadWebpackAsyncFn } = options;
37
43
  let webpackConfigJs;
38
44
  try {
39
- if (buildProperties.serveMode) {
40
- logger.terminal.writeVerboseLine(`Attempting to load webpack configuration from "${WEBPACK_DEV_CONFIG_FILENAME}".`);
41
- webpackConfigJs = WebpackConfigurationLoader._tryLoadWebpackConfiguration(buildFolder, WEBPACK_DEV_CONFIG_FILENAME);
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);
42
50
  }
43
51
  if (!webpackConfigJs) {
44
- logger.terminal.writeVerboseLine(`Attempting to load webpack configuration from "${WEBPACK_CONFIG_FILENAME}".`);
45
- webpackConfigJs = WebpackConfigurationLoader._tryLoadWebpackConfiguration(buildFolder, WEBPACK_CONFIG_FILENAME);
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);
46
55
  }
47
56
  }
48
57
  catch (error) {
49
- logger.emitError(error);
58
+ this._logger.emitError(error);
50
59
  }
51
60
  if (webpackConfigJs) {
52
61
  const webpackConfig = webpackConfigJs.default || webpackConfigJs;
53
62
  if (typeof webpackConfig === 'function') {
54
- return webpackConfig({ prod: buildProperties.production, production: buildProperties.production });
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
71
  }
56
72
  else {
57
73
  return webpackConfig;
@@ -61,14 +77,19 @@ class WebpackConfigurationLoader {
61
77
  return undefined;
62
78
  }
63
79
  }
64
- static _tryLoadWebpackConfiguration(buildFolder, configurationFilename) {
65
- const fullWebpackConfigPath = path.join(buildFolder, configurationFilename);
66
- if (node_core_library_1.FileSystem.exists(fullWebpackConfigPath)) {
80
+ async _tryLoadWebpackConfigurationInnerAsync(configurationPath) {
81
+ const configExists = await node_core_library_1.FileSystem.existsAsync(configurationPath);
82
+ if (configExists) {
67
83
  try {
68
- return require(fullWebpackConfigPath);
84
+ return await Promise.resolve().then(() => __importStar(require(configurationPath)));
69
85
  }
70
86
  catch (e) {
71
- throw new Error(`Error loading webpack configuration at "${fullWebpackConfigPath}": ${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
93
  }
73
94
  }
74
95
  else {
@@ -1 +1 @@
1
- {"version":3,"file":"WebpackConfigurationLoader.js","sourceRoot":"","sources":["../src/WebpackConfigurationLoader.ts"],"names":[],"mappings":";AAAA,4FAA4F;AAC5F,2DAA2D;;;;;;;;;;;;;;;;;;;;;;;;;;AAE3D,2CAA6B;AAC7B,oEAA0D;AAsB1D,MAAM,uBAAuB,GAAW,mBAAmB,CAAC;AAC5D,MAAM,2BAA2B,GAAW,uBAAuB,CAAC;AAEpE,MAAa,0BAA0B;IAC9B,MAAM,CAAC,KAAK,CAAC,yBAAyB,CAC3C,MAAoB,EACpB,WAAmB,EACnB,eAAsC;QAEtC,4FAA4F;QAC5F,kGAAkG;QAElG,IAAI,eAA6C,CAAC;QAElD,IAAI;YACF,IAAI,eAAe,CAAC,SAAS,EAAE;gBAC7B,MAAM,CAAC,QAAQ,CAAC,gBAAgB,CAC9B,kDAAkD,2BAA2B,IAAI,CAClF,CAAC;gBACF,eAAe,GAAG,0BAA0B,CAAC,4BAA4B,CACvE,WAAW,EACX,2BAA2B,CAC5B,CAAC;aACH;YAED,IAAI,CAAC,eAAe,EAAE;gBACpB,MAAM,CAAC,QAAQ,CAAC,gBAAgB,CAC9B,kDAAkD,uBAAuB,IAAI,CAC9E,CAAC;gBACF,eAAe,GAAG,0BAA0B,CAAC,4BAA4B,CACvE,WAAW,EACX,uBAAuB,CACxB,CAAC;aACH;SACF;QAAC,OAAO,KAAK,EAAE;YACd,MAAM,CAAC,SAAS,CAAC,KAAc,CAAC,CAAC;SAClC;QAED,IAAI,eAAe,EAAE;YACnB,MAAM,aAAa,GAChB,eAAuD,CAAC,OAAO,IAAI,eAAe,CAAC;YAEtF,IAAI,OAAO,aAAa,KAAK,UAAU,EAAE;gBACvC,OAAO,aAAa,CAAC,EAAE,IAAI,EAAE,eAAe,CAAC,UAAU,EAAE,UAAU,EAAE,eAAe,CAAC,UAAU,EAAE,CAAC,CAAC;aACpG;iBAAM;gBACL,OAAO,aAAa,CAAC;aACtB;SACF;aAAM;YACL,OAAO,SAAS,CAAC;SAClB;IACH,CAAC;IAEO,MAAM,CAAC,4BAA4B,CACzC,WAAmB,EACnB,qBAA6B;QAE7B,MAAM,qBAAqB,GAAW,IAAI,CAAC,IAAI,CAAC,WAAW,EAAE,qBAAqB,CAAC,CAAC;QACpF,IAAI,8BAAU,CAAC,MAAM,CAAC,qBAAqB,CAAC,EAAE;YAC5C,IAAI;gBACF,OAAO,OAAO,CAAC,qBAAqB,CAAC,CAAC;aACvC;YAAC,OAAO,CAAC,EAAE;gBACV,MAAM,IAAI,KAAK,CAAC,2CAA2C,qBAAqB,MAAM,CAAC,EAAE,CAAC,CAAC;aAC5F;SACF;aAAM;YACL,OAAO,SAAS,CAAC;SAClB;IACH,CAAC;CACF;AAhED,gEAgEC","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 { 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\nconst WEBPACK_CONFIG_FILENAME: string = 'webpack.config.js';\nconst WEBPACK_DEV_CONFIG_FILENAME: string = 'webpack.dev.config.js';\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 let webpackConfigJs: IWebpackConfigJs | undefined;\n\n try {\n if (buildProperties.serveMode) {\n logger.terminal.writeVerboseLine(\n `Attempting to load webpack configuration from \"${WEBPACK_DEV_CONFIG_FILENAME}\".`\n );\n webpackConfigJs = WebpackConfigurationLoader._tryLoadWebpackConfiguration(\n buildFolder,\n WEBPACK_DEV_CONFIG_FILENAME\n );\n }\n\n if (!webpackConfigJs) {\n logger.terminal.writeVerboseLine(\n `Attempting to load webpack configuration from \"${WEBPACK_CONFIG_FILENAME}\".`\n );\n webpackConfigJs = WebpackConfigurationLoader._tryLoadWebpackConfiguration(\n buildFolder,\n WEBPACK_CONFIG_FILENAME\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"]}
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,CACrC,eAAe,EACf,iBAAiB,IAAI,2BAA2B,CACjD,CAAC;gBACF,IAAI,CAAC,OAAO,CAAC,QAAQ,CAAC,gBAAgB,CACpC,kDAAkD,UAAU,IAAI,CACjE,CAAC;gBACF,eAAe,GAAG,MAAM,IAAI,CAAC,sCAAsC,CAAC,UAAU,CAAC,CAAC;aACjF;SACF;QAAC,OAAO,KAAK,EAAE;YACd,IAAI,CAAC,OAAO,CAAC,SAAS,CAAC,KAAc,CAAC,CAAC;SACxC;QAED,IAAI,eAAe,EAAE;YACnB,MAAM,aAAa,GAChB,eAAuD,CAAC,OAAO,IAAI,eAAe,CAAC;YAEtF,IAAI,OAAO,aAAa,KAAK,UAAU,EAAE;gBACvC,uEAAuE;gBACvE,OAAO,aAAa,CAAC;oBACnB,IAAI,EAAE,IAAI,CAAC,WAAW;oBACtB,UAAU,EAAE,IAAI,CAAC,WAAW;oBAC5B,WAAW;oBACX,iBAAiB;oBACjB,OAAO,EAAE,MAAM,kBAAkB,EAAE;iBACpC,CAAC,CAAC;aACJ;iBAAM;gBACL,OAAO,aAAa,CAAC;aACtB;SACF;aAAM;YACL,OAAO,SAAS,CAAC;SAClB;IACH,CAAC;IAEO,KAAK,CAAC,sCAAsC,CAClD,iBAAyB;QAEzB,MAAM,YAAY,GAAY,MAAM,8BAAU,CAAC,WAAW,CAAC,iBAAiB,CAAC,CAAC;QAC9E,IAAI,YAAY,EAAE;YAChB,IAAI;gBACF,OAAO,wDAAa,iBAAiB,GAAC,CAAC;aACxC;YAAC,OAAO,CAAC,EAAE;gBACV,MAAM,KAAK,GAA0B,CAA0B,CAAC;gBAChE,IAAI,KAAK,CAAC,IAAI,KAAK,sBAAsB,EAAE;oBACzC,4CAA4C;oBAC5C,OAAO,SAAS,CAAC;iBAClB;gBACD,MAAM,IAAI,KAAK,CAAC,2CAA2C,iBAAiB,MAAM,CAAC,EAAE,CAAC,CAAC;aACxF;SACF;aAAM;YACL,OAAO,SAAS,CAAC;SAClB;IACH,CAAC;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 './Webpack4Plugin';\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"]}
package/lib/index.d.ts CHANGED
@@ -1,8 +1,3 @@
1
- import type { IHeftPlugin } from '@rushstack/heft';
2
- export { IWebpackConfigurationWithDevServer, IWebpackConfiguration, IWebpackBuildStageProperties, IWebpackBundleSubstageProperties } from './shared';
3
- /**
4
- * @public
5
- */
6
- declare const _default: IHeftPlugin<void>;
7
- export default _default;
1
+ export { PLUGIN_NAME as PluginName } from './Webpack4Plugin';
2
+ export type { IWebpackConfigurationWithDevServer, IWebpackConfiguration, IWebpackConfigurationFnEnvironment, IWebpackPluginAccessor, IWebpackPluginAccessorHooks } from './shared';
8
3
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,iBAAiB,CAAC;AAInD,OAAO,EACL,kCAAkC,EAClC,qBAAqB,EACrB,4BAA4B,EAC5B,gCAAgC,EACjC,MAAM,UAAU,CAAC;AAElB;;GAEG;;AACH,wBAAkD"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,WAAW,IAAI,UAAU,EAAE,MAAM,kBAAkB,CAAC;AAE7D,YAAY,EACV,kCAAkC,EAClC,qBAAqB,EACrB,kCAAkC,EAClC,sBAAsB,EACtB,2BAA2B,EAC5B,MAAM,UAAU,CAAC"}
package/lib/index.js CHANGED
@@ -2,9 +2,7 @@
2
2
  // Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
3
3
  // See LICENSE in the project root for license information.
4
4
  Object.defineProperty(exports, "__esModule", { value: true });
5
- const WebpackPlugin_1 = require("./WebpackPlugin");
6
- /**
7
- * @public
8
- */
9
- exports.default = new WebpackPlugin_1.WebpackPlugin();
5
+ exports.PluginName = void 0;
6
+ var Webpack4Plugin_1 = require("./Webpack4Plugin");
7
+ Object.defineProperty(exports, "PluginName", { enumerable: true, get: function () { return Webpack4Plugin_1.PLUGIN_NAME; } });
10
8
  //# sourceMappingURL=index.js.map
package/lib/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";AAAA,4FAA4F;AAC5F,2DAA2D;;AAI3D,mDAAgD;AAShD;;GAEG;AACH,kBAAe,IAAI,6BAAa,EAAiB,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 type { IHeftPlugin } from '@rushstack/heft';\n\nimport { WebpackPlugin } from './WebpackPlugin';\n\nexport {\n IWebpackConfigurationWithDevServer,\n IWebpackConfiguration,\n IWebpackBuildStageProperties,\n IWebpackBundleSubstageProperties\n} from './shared';\n\n/**\n * @public\n */\nexport default new WebpackPlugin() as IHeftPlugin;\n"]}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";AAAA,4FAA4F;AAC5F,2DAA2D;;;AAE3D,mDAA6D;AAApD,4GAAA,WAAW,OAAc","sourcesContent":["// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.\n// See LICENSE in the project root for license information.\n\nexport { PLUGIN_NAME as PluginName } from './Webpack4Plugin';\n\nexport type {\n IWebpackConfigurationWithDevServer,\n IWebpackConfiguration,\n IWebpackConfigurationFnEnvironment,\n IWebpackPluginAccessor,\n IWebpackPluginAccessorHooks\n} from './shared';\n"]}
@@ -0,0 +1,25 @@
1
+ {
2
+ "$schema": "http://json-schema.org/draft-04/schema#",
3
+ "title": "Webpack 4 Plugin Configuration",
4
+ "description": "Defines options for Webpack 4 plugin execution.",
5
+ "type": "object",
6
+
7
+ "additionalProperties": false,
8
+
9
+ "properties": {
10
+ "$schema": {
11
+ "description": "Part of the JSON Schema standard, this optional keyword declares the URL of the schema that the file conforms to. Editors may download the schema and use it to perform syntax highlighting.",
12
+ "type": "string"
13
+ },
14
+
15
+ "devConfigurationPath": {
16
+ "description": "Specifies a relative path to the Webpack dev configuration, which is used in \"serve\" mode. The default value is \"./webpack.dev.config.js\".",
17
+ "type": "string"
18
+ },
19
+
20
+ "configurationPath": {
21
+ "description": "Specifies a relative path to the Webpack configuration. The default value is \"./webpack.config.js\".",
22
+ "type": "string"
23
+ }
24
+ }
25
+ }
package/lib/shared.d.ts CHANGED
@@ -1,44 +1,94 @@
1
- import type * as webpack from 'webpack';
1
+ import type * as TWebpack from 'webpack';
2
2
  declare module 'webpack' {
3
- type MultiStats = webpack.compilation.MultiStats;
3
+ type MultiStats = TWebpack.compilation.MultiStats;
4
4
  type StatsOptions = unknown;
5
- type StatsCompilation = webpack.compilation.Compilation;
5
+ type StatsCompilation = TWebpack.compilation.Compilation;
6
6
  interface Compiler {
7
7
  watching?: unknown;
8
8
  }
9
9
  }
10
10
  import type { Configuration as WebpackDevServerConfiguration } from 'webpack-dev-server';
11
- import type { IBuildStageProperties, IBundleSubstageProperties } from '@rushstack/heft';
11
+ import type { AsyncParallelHook, AsyncSeriesBailHook, AsyncSeriesHook } from 'tapable';
12
+ import type { IHeftTaskSession, HeftConfiguration } from '@rushstack/heft';
12
13
  /**
14
+ * The environment passed into the Webpack configuration function. Loosely based
15
+ * on the default Webpack environment options, specified here:
16
+ * https://webpack.js.org/api/cli/#environment-options
17
+ *
13
18
  * @public
14
19
  */
15
- export interface IWebpackConfigurationWithDevServer extends webpack.Configuration {
20
+ export interface IWebpackConfigurationFnEnvironment {
21
+ /**
22
+ * Whether or not the run is in production mode. Synonym of
23
+ * IWebpackConfigurationFnEnvironment.production.
24
+ */
25
+ prod: boolean;
26
+ /**
27
+ * Whether or not the run is in production mode. Synonym of
28
+ * IWebpackConfigurationFnEnvironment.prod.
29
+ */
30
+ production: boolean;
31
+ /**
32
+ * The task session provided to the plugin.
33
+ */
34
+ taskSession: IHeftTaskSession;
35
+ /**
36
+ * The Heft configuration provided to the plugin.
37
+ */
38
+ heftConfiguration: HeftConfiguration;
39
+ /**
40
+ * The resolved Webpack package.
41
+ */
42
+ webpack: typeof TWebpack;
43
+ }
44
+ /**
45
+ * @public
46
+ */
47
+ export interface IWebpackConfigurationWithDevServer extends TWebpack.Configuration {
16
48
  devServer?: WebpackDevServerConfiguration;
17
49
  }
18
50
  /**
19
51
  * @public
20
52
  */
21
- export declare type IWebpackConfiguration = IWebpackConfigurationWithDevServer | IWebpackConfigurationWithDevServer[] | undefined;
53
+ export declare type IWebpackConfiguration = IWebpackConfigurationWithDevServer | IWebpackConfigurationWithDevServer[];
22
54
  /**
23
55
  * @public
24
56
  */
25
- export interface IWebpackBundleSubstageProperties extends IBundleSubstageProperties {
57
+ export interface IWebpackPluginAccessorHooks {
26
58
  /**
27
- * The configuration used by the Webpack plugin. This must be populated
28
- * for Webpack to run. If webpackConfigFilePath is specified,
29
- * this will be populated automatically with the exports of the
30
- * config file referenced in that property.
59
+ * A hook that allows for loading custom configurations used by the Webpack
60
+ * plugin. If a webpack configuration is provided, this will be populated automatically
61
+ * with the exports of the config file. If a webpack configuration is not provided,
62
+ * one will be loaded by the Webpack plugin.
31
63
  *
32
64
  * @remarks
33
- * Tapable event handlers can return `null` instead of `undefined` to suppress
34
- * other handlers from creating a configuration object.
65
+ * Tapable event handlers can return `false` instead of `undefined` to suppress
66
+ * other handlers from creating a configuration object, and prevent webpack from running.
35
67
  */
36
- webpackConfiguration?: webpack.Configuration | webpack.Configuration[] | null;
68
+ readonly onLoadConfiguration: AsyncSeriesBailHook<never, never, never, IWebpackConfiguration | false>;
69
+ /**
70
+ * A hook that allows for modification of the loaded configuration used by the Webpack
71
+ * plugin. If no configuration was loaded, this hook will not be called.
72
+ */
73
+ readonly onConfigure: AsyncSeriesHook<IWebpackConfiguration, never, never>;
74
+ /**
75
+ * A hook that provides the finalized configuration that will be used by Webpack.
76
+ * If no configuration was loaded, this hook will not be called.
77
+ */
78
+ readonly onAfterConfigure: AsyncParallelHook<IWebpackConfiguration, never, never>;
79
+ /**
80
+ * A hook that provides the stats output from Webpack. If no configuration is loaded,
81
+ * this hook will not be called.
82
+ */
83
+ readonly onEmitStats: AsyncParallelHook<TWebpack.Stats | TWebpack.compilation.MultiStats, never, never>;
37
84
  }
38
85
  /**
39
86
  * @public
40
87
  */
41
- export interface IWebpackBuildStageProperties extends IBuildStageProperties {
42
- webpackStats?: webpack.Stats | webpack.compilation.MultiStats;
88
+ export interface IWebpackPluginAccessor {
89
+ /**
90
+ * Hooks that are called at various points in the Webpack plugin lifecycle.
91
+ */
92
+ hooks: IWebpackPluginAccessorHooks;
43
93
  }
44
94
  //# sourceMappingURL=shared.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"shared.d.ts","sourceRoot":"","sources":["../src/shared.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,KAAK,OAAO,MAAM,SAAS,CAAC;AAExC,OAAO,QAAQ,SAAS,CAAC;IACvB,KAAY,UAAU,GAAG,OAAO,CAAC,WAAW,CAAC,UAAU,CAAC;IACxD,KAAY,YAAY,GAAG,OAAO,CAAC;IACnC,KAAY,gBAAgB,GAAG,OAAO,CAAC,WAAW,CAAC,WAAW,CAAC;IAG/D,UAAiB,QAAQ;QACvB,QAAQ,CAAC,EAAE,OAAO,CAAC;KACpB;CACF;AACD,OAAO,KAAK,EAAE,aAAa,IAAI,6BAA6B,EAAE,MAAM,oBAAoB,CAAC;AACzF,OAAO,KAAK,EAAE,qBAAqB,EAAE,yBAAyB,EAAE,MAAM,iBAAiB,CAAC;AAExF;;GAEG;AACH,MAAM,WAAW,kCAAmC,SAAQ,OAAO,CAAC,aAAa;IAC/E,SAAS,CAAC,EAAE,6BAA6B,CAAC;CAC3C;AAED;;GAEG;AACH,oBAAY,qBAAqB,GAC7B,kCAAkC,GAClC,kCAAkC,EAAE,GACpC,SAAS,CAAC;AAEd;;GAEG;AACH,MAAM,WAAW,gCAAiC,SAAQ,yBAAyB;IACjF;;;;;;;;;OASG;IAGH,oBAAoB,CAAC,EAAE,OAAO,CAAC,aAAa,GAAG,OAAO,CAAC,aAAa,EAAE,GAAG,IAAI,CAAC;CAC/E;AAED;;GAEG;AACH,MAAM,WAAW,4BAA6B,SAAQ,qBAAqB;IACzE,YAAY,CAAC,EAAE,OAAO,CAAC,KAAK,GAAG,OAAO,CAAC,WAAW,CAAC,UAAU,CAAC;CAC/D"}
1
+ {"version":3,"file":"shared.d.ts","sourceRoot":"","sources":["../src/shared.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,KAAK,QAAQ,MAAM,SAAS,CAAC;AAEzC,OAAO,QAAQ,SAAS,CAAC;IACvB,KAAY,UAAU,GAAG,QAAQ,CAAC,WAAW,CAAC,UAAU,CAAC;IACzD,KAAY,YAAY,GAAG,OAAO,CAAC;IACnC,KAAY,gBAAgB,GAAG,QAAQ,CAAC,WAAW,CAAC,WAAW,CAAC;IAGhE,UAAiB,QAAQ;QACvB,QAAQ,CAAC,EAAE,OAAO,CAAC;KACpB;CACF;AACD,OAAO,KAAK,EAAE,aAAa,IAAI,6BAA6B,EAAE,MAAM,oBAAoB,CAAC;AACzF,OAAO,KAAK,EAAE,iBAAiB,EAAE,mBAAmB,EAAE,eAAe,EAAE,MAAM,SAAS,CAAC;AACvF,OAAO,KAAK,EAAE,gBAAgB,EAAE,iBAAiB,EAAE,MAAM,iBAAiB,CAAC;AAE3E;;;;;;GAMG;AACH,MAAM,WAAW,kCAAkC;IACjD;;;OAGG;IACH,IAAI,EAAE,OAAO,CAAC;IACd;;;OAGG;IACH,UAAU,EAAE,OAAO,CAAC;IAGpB;;OAEG;IACH,WAAW,EAAE,gBAAgB,CAAC;IAC9B;;OAEG;IACH,iBAAiB,EAAE,iBAAiB,CAAC;IACrC;;OAEG;IACH,OAAO,EAAE,OAAO,QAAQ,CAAC;CAC1B;AAED;;GAEG;AACH,MAAM,WAAW,kCAAmC,SAAQ,QAAQ,CAAC,aAAa;IAChF,SAAS,CAAC,EAAE,6BAA6B,CAAC;CAC3C;AAED;;GAEG;AACH,oBAAY,qBAAqB,GAAG,kCAAkC,GAAG,kCAAkC,EAAE,CAAC;AAE9G;;GAEG;AACH,MAAM,WAAW,2BAA2B;IAC1C;;;;;;;;;OASG;IACH,QAAQ,CAAC,mBAAmB,EAAE,mBAAmB,CAAC,KAAK,EAAE,KAAK,EAAE,KAAK,EAAE,qBAAqB,GAAG,KAAK,CAAC,CAAC;IACtG;;;OAGG;IACH,QAAQ,CAAC,WAAW,EAAE,eAAe,CAAC,qBAAqB,EAAE,KAAK,EAAE,KAAK,CAAC,CAAC;IAC3E;;;OAGG;IACH,QAAQ,CAAC,gBAAgB,EAAE,iBAAiB,CAAC,qBAAqB,EAAE,KAAK,EAAE,KAAK,CAAC,CAAC;IAClF;;;OAGG;IACH,QAAQ,CAAC,WAAW,EAAE,iBAAiB,CAAC,QAAQ,CAAC,KAAK,GAAG,QAAQ,CAAC,WAAW,CAAC,UAAU,EAAE,KAAK,EAAE,KAAK,CAAC,CAAC;CACzG;AAED;;GAEG;AACH,MAAM,WAAW,sBAAsB;IACrC;;OAEG;IACH,KAAK,EAAE,2BAA2B,CAAC;CACpC"}
package/lib/shared.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"shared.js","sourceRoot":"","sources":["../src/shared.ts"],"names":[],"mappings":";AAAA,4FAA4F;AAC5F,2DAA2D","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 type * as webpack from 'webpack';\n// Compensate for webpack-dev-server referencing constructs from webpack 5\ndeclare module 'webpack' {\n export type MultiStats = webpack.compilation.MultiStats;\n export type StatsOptions = unknown;\n export type StatsCompilation = webpack.compilation.Compilation;\n\n // eslint-disable-next-line @typescript-eslint/naming-convention\n export interface Compiler {\n watching?: unknown;\n }\n}\nimport type { Configuration as WebpackDevServerConfiguration } from 'webpack-dev-server';\nimport type { IBuildStageProperties, IBundleSubstageProperties } from '@rushstack/heft';\n\n/**\n * @public\n */\nexport interface IWebpackConfigurationWithDevServer extends webpack.Configuration {\n devServer?: WebpackDevServerConfiguration;\n}\n\n/**\n * @public\n */\nexport type IWebpackConfiguration =\n | IWebpackConfigurationWithDevServer\n | IWebpackConfigurationWithDevServer[]\n | undefined;\n\n/**\n * @public\n */\nexport interface IWebpackBundleSubstageProperties extends IBundleSubstageProperties {\n /**\n * The configuration used by the Webpack plugin. This must be populated\n * for Webpack to run. If webpackConfigFilePath is specified,\n * this will be populated automatically with the exports of the\n * config file referenced in that property.\n *\n * @remarks\n * Tapable event handlers can return `null` instead of `undefined` to suppress\n * other handlers from creating a configuration object.\n */\n // We are inheriting this problem from Tapable's API\n // eslint-disable-next-line @rushstack/no-new-null\n webpackConfiguration?: webpack.Configuration | webpack.Configuration[] | null;\n}\n\n/**\n * @public\n */\nexport interface IWebpackBuildStageProperties extends IBuildStageProperties {\n webpackStats?: webpack.Stats | webpack.compilation.MultiStats;\n}\n"]}
1
+ {"version":3,"file":"shared.js","sourceRoot":"","sources":["../src/shared.ts"],"names":[],"mappings":";AAAA,4FAA4F;AAC5F,2DAA2D","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 type * as TWebpack from 'webpack';\n// Compensate for webpack-dev-server referencing constructs from webpack 5\ndeclare module 'webpack' {\n export type MultiStats = TWebpack.compilation.MultiStats;\n export type StatsOptions = unknown;\n export type StatsCompilation = TWebpack.compilation.Compilation;\n\n // eslint-disable-next-line @typescript-eslint/naming-convention\n export interface Compiler {\n watching?: unknown;\n }\n}\nimport type { Configuration as WebpackDevServerConfiguration } from 'webpack-dev-server';\nimport type { AsyncParallelHook, AsyncSeriesBailHook, AsyncSeriesHook } from 'tapable';\nimport type { IHeftTaskSession, HeftConfiguration } from '@rushstack/heft';\n\n/**\n * The environment passed into the Webpack configuration function. Loosely based\n * on the default Webpack environment options, specified here:\n * https://webpack.js.org/api/cli/#environment-options\n *\n * @public\n */\nexport interface IWebpackConfigurationFnEnvironment {\n /**\n * Whether or not the run is in production mode. Synonym of\n * IWebpackConfigurationFnEnvironment.production.\n */\n prod: boolean;\n /**\n * Whether or not the run is in production mode. Synonym of\n * IWebpackConfigurationFnEnvironment.prod.\n */\n production: boolean;\n\n // Non-standard environment options\n /**\n * The task session provided to the plugin.\n */\n taskSession: IHeftTaskSession;\n /**\n * The Heft configuration provided to the plugin.\n */\n heftConfiguration: HeftConfiguration;\n /**\n * The resolved Webpack package.\n */\n webpack: typeof TWebpack;\n}\n\n/**\n * @public\n */\nexport interface IWebpackConfigurationWithDevServer extends TWebpack.Configuration {\n devServer?: WebpackDevServerConfiguration;\n}\n\n/**\n * @public\n */\nexport type IWebpackConfiguration = IWebpackConfigurationWithDevServer | IWebpackConfigurationWithDevServer[];\n\n/**\n * @public\n */\nexport interface IWebpackPluginAccessorHooks {\n /**\n * A hook that allows for loading custom configurations used by the Webpack\n * plugin. If a webpack configuration is provided, this will be populated automatically\n * with the exports of the config file. If a webpack configuration is not provided,\n * one will be loaded by the Webpack plugin.\n *\n * @remarks\n * Tapable event handlers can return `false` instead of `undefined` to suppress\n * other handlers from creating a configuration object, and prevent webpack from running.\n */\n readonly onLoadConfiguration: AsyncSeriesBailHook<never, never, never, IWebpackConfiguration | false>;\n /**\n * A hook that allows for modification of the loaded configuration used by the Webpack\n * plugin. If no configuration was loaded, this hook will not be called.\n */\n readonly onConfigure: AsyncSeriesHook<IWebpackConfiguration, never, never>;\n /**\n * A hook that provides the finalized configuration that will be used by Webpack.\n * If no configuration was loaded, this hook will not be called.\n */\n readonly onAfterConfigure: AsyncParallelHook<IWebpackConfiguration, never, never>;\n /**\n * A hook that provides the stats output from Webpack. If no configuration is loaded,\n * this hook will not be called.\n */\n readonly onEmitStats: AsyncParallelHook<TWebpack.Stats | TWebpack.compilation.MultiStats, never, never>;\n}\n\n/**\n * @public\n */\nexport interface IWebpackPluginAccessor {\n /**\n * Hooks that are called at various points in the Webpack plugin lifecycle.\n */\n hooks: IWebpackPluginAccessorHooks;\n}\n"]}
@@ -5,7 +5,7 @@
5
5
  "toolPackages": [
6
6
  {
7
7
  "packageName": "@microsoft/api-extractor",
8
- "packageVersion": "7.31.1"
8
+ "packageVersion": "7.31.2"
9
9
  }
10
10
  ]
11
11
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rushstack/heft-webpack4-plugin",
3
- "version": "0.5.16",
3
+ "version": "0.6.0-dev.0",
4
4
  "description": "Heft plugin for Webpack 4",
5
5
  "repository": {
6
6
  "type": "git",
@@ -9,7 +9,7 @@
9
9
  },
10
10
  "homepage": "https://rushstack.io/pages/heft/overview/",
11
11
  "main": "lib/index.js",
12
- "types": "dist/heft-webpack4-plugin.d.ts",
12
+ "types": "lib/index.d.ts",
13
13
  "license": "MIT",
14
14
  "peerDependenciesMeta": {
15
15
  "@types/webpack": {
@@ -17,18 +17,21 @@
17
17
  }
18
18
  },
19
19
  "peerDependencies": {
20
- "@rushstack/heft": "^0.47.10",
20
+ "@rushstack/heft": "^0.48.0-dev.0",
21
21
  "@types/webpack": "^4",
22
22
  "webpack": "~4.44.2"
23
23
  },
24
24
  "dependencies": {
25
+ "@rushstack/debug-certificate-manager": "1.1.76",
25
26
  "@rushstack/node-core-library": "3.52.0",
27
+ "@types/tapable": "1.0.6",
28
+ "tapable": "1.1.3",
26
29
  "webpack-dev-server": "~4.9.3"
27
30
  },
28
31
  "devDependencies": {
29
32
  "@rushstack/eslint-config": "3.0.1",
30
- "@rushstack/heft-node-rig": "1.10.12",
31
- "@rushstack/heft": "0.47.10",
33
+ "@rushstack/heft-node-rig": "1.11.0-dev.0",
34
+ "@rushstack/heft": "0.48.0-dev.0",
32
35
  "@types/node": "12.20.24",
33
36
  "@types/webpack": "4.41.32",
34
37
  "webpack": "~4.44.2"
@@ -36,7 +39,7 @@
36
39
  "scripts": {
37
40
  "build": "heft build --clean",
38
41
  "start": "heft test --clean --watch",
39
- "_phase:build": "heft build --clean",
40
- "_phase:test": "heft test --no-build"
42
+ "_phase:build": "heft run --only build -- --clean",
43
+ "_phase:test": "heft run --only test -- --clean"
41
44
  }
42
45
  }
@@ -1,13 +0,0 @@
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
- }
13
- //# sourceMappingURL=WebpackPlugin.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"WebpackPlugin.d.ts","sourceRoot":"","sources":["../src/WebpackPlugin.ts"],"names":[],"mappings":"AAWA,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;YA0CpE,gBAAgB;IAgK9B,OAAO,CAAC,WAAW;CAapB"}
@@ -1,198 +0,0 @@
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
- Object.defineProperty(exports, "__esModule", { value: true });
5
- exports.WebpackPlugin = void 0;
6
- const node_core_library_1 = require("@rushstack/node-core-library");
7
- const WebpackConfigurationLoader_1 = require("./WebpackConfigurationLoader");
8
- const webpack = node_core_library_1.Import.lazy('webpack', require);
9
- const PLUGIN_NAME = 'WebpackPlugin';
10
- const WEBPACK_DEV_SERVER_PACKAGE_NAME = 'webpack-dev-server';
11
- const WEBPACK_DEV_SERVER_ENV_VAR_NAME = 'WEBPACK_DEV_SERVER';
12
- /**
13
- * @internal
14
- */
15
- class WebpackPlugin {
16
- constructor() {
17
- this.pluginName = PLUGIN_NAME;
18
- }
19
- static _getWebpackVersions() {
20
- if (!WebpackPlugin._webpackVersions) {
21
- const webpackDevServerPackageJsonPath = node_core_library_1.Import.resolveModule({
22
- modulePath: 'webpack-dev-server/package.json',
23
- baseFolderPath: __dirname
24
- });
25
- const webpackDevServerPackageJson = node_core_library_1.PackageJsonLookup.instance.loadPackageJson(webpackDevServerPackageJsonPath);
26
- WebpackPlugin._webpackVersions = {
27
- webpackVersion: webpack.version,
28
- webpackDevServerVersion: webpackDevServerPackageJson.version
29
- };
30
- }
31
- return WebpackPlugin._webpackVersions;
32
- }
33
- apply(heftSession, heftConfiguration) {
34
- heftSession.hooks.build.tap(PLUGIN_NAME, (build) => {
35
- build.hooks.bundle.tap(PLUGIN_NAME, (bundle) => {
36
- bundle.hooks.configureWebpack.tap({ name: PLUGIN_NAME, stage: Number.MIN_SAFE_INTEGER }, (webpackConfiguration) => {
37
- const webpackVersions = WebpackPlugin._getWebpackVersions();
38
- bundle.properties.webpackVersion = webpack.version;
39
- bundle.properties.webpackDevServerVersion = webpackVersions.webpackDevServerVersion;
40
- return webpackConfiguration;
41
- });
42
- bundle.hooks.configureWebpack.tapPromise(PLUGIN_NAME, async (existingConfiguration) => {
43
- const logger = heftSession.requestScopedLogger('configure-webpack');
44
- if (existingConfiguration) {
45
- logger.terminal.writeVerboseLine('Skipping loading webpack config file because the webpack config has already been set.');
46
- return existingConfiguration;
47
- }
48
- else {
49
- return await WebpackConfigurationLoader_1.WebpackConfigurationLoader.tryLoadWebpackConfigAsync(logger, heftConfiguration.buildFolder, build.properties);
50
- }
51
- });
52
- bundle.hooks.run.tapPromise(PLUGIN_NAME, async () => {
53
- await this._runWebpackAsync(heftSession, bundle.properties, build.properties, heftConfiguration.terminalProvider.supportsColor);
54
- });
55
- });
56
- });
57
- }
58
- async _runWebpackAsync(heftSession, bundleSubstageProperties, buildProperties, supportsColor) {
59
- const webpackConfiguration = bundleSubstageProperties.webpackConfiguration;
60
- if (!webpackConfiguration) {
61
- return;
62
- }
63
- const logger = heftSession.requestScopedLogger('webpack');
64
- const webpackVersions = WebpackPlugin._getWebpackVersions();
65
- if (bundleSubstageProperties.webpackVersion !== webpackVersions.webpackVersion) {
66
- logger.emitError(new Error(`The Webpack plugin expected to be configured with Webpack version ${webpackVersions.webpackVersion}, ` +
67
- `but the configuration specifies version ${bundleSubstageProperties.webpackVersion}. ` +
68
- 'Are multiple versions of the Webpack plugin present?'));
69
- }
70
- if (bundleSubstageProperties.webpackDevServerVersion !== webpackVersions.webpackDevServerVersion) {
71
- logger.emitError(new Error(`The Webpack plugin expected to be configured with webpack-dev-server version ${webpackVersions.webpackDevServerVersion}, ` +
72
- `but the configuration specifies version ${bundleSubstageProperties.webpackDevServerVersion}. ` +
73
- 'Are multiple versions of the Webpack plugin present?'));
74
- }
75
- logger.terminal.writeLine(`Using Webpack version ${webpack.version}`);
76
- let compiler;
77
- if (Array.isArray(webpackConfiguration)) {
78
- if (webpackConfiguration.length === 0) {
79
- logger.terminal.writeLine('The webpack configuration received is an empty array - nothing to do.');
80
- return;
81
- }
82
- else {
83
- compiler = webpack(webpackConfiguration); /* (webpack.Compilation[]) => MultiCompiler */
84
- }
85
- }
86
- else {
87
- compiler = webpack(webpackConfiguration); /* (webpack.Compilation) => Compiler */
88
- }
89
- if (buildProperties.serveMode) {
90
- const defaultDevServerOptions = {
91
- host: 'localhost',
92
- devMiddleware: {
93
- publicPath: '/',
94
- stats: {
95
- cached: false,
96
- cachedAssets: false,
97
- colors: supportsColor
98
- }
99
- },
100
- client: {
101
- logging: 'info'
102
- },
103
- port: 8080
104
- };
105
- let options;
106
- if (Array.isArray(webpackConfiguration)) {
107
- const devServerOptions = webpackConfiguration
108
- .map((configuration) => configuration.devServer)
109
- .filter((devServer) => !!devServer);
110
- if (devServerOptions.length > 1) {
111
- logger.emitWarning(new Error(`Detected multiple webpack devServer configurations, using the first one.`));
112
- }
113
- if (devServerOptions.length > 0) {
114
- options = Object.assign(Object.assign({}, defaultDevServerOptions), devServerOptions[0]);
115
- }
116
- else {
117
- options = defaultDevServerOptions;
118
- }
119
- }
120
- else {
121
- options = Object.assign(Object.assign({}, defaultDevServerOptions), webpackConfiguration.devServer);
122
- }
123
- // Register a plugin to callback after webpack is done with the first compilation
124
- // so we can move on to post-build
125
- let firstCompilationDoneCallback;
126
- const originalBeforeCallback = options.onBeforeSetupMiddleware;
127
- options.onBeforeSetupMiddleware = (server) => {
128
- compiler.hooks.done.tap('heft-webpack-plugin', () => {
129
- if (firstCompilationDoneCallback) {
130
- firstCompilationDoneCallback();
131
- firstCompilationDoneCallback = undefined;
132
- }
133
- });
134
- if (originalBeforeCallback) {
135
- return originalBeforeCallback(server);
136
- }
137
- };
138
- // The webpack-dev-server package has a design flaw, where merely loading its package will set the
139
- // WEBPACK_DEV_SERVER environment variable -- even if no APIs are accessed. This environment variable
140
- // causes incorrect behavior if Heft is not running in serve mode. Thus, we need to be careful to call require()
141
- // only if Heft is in serve mode.
142
- const WebpackDevServer = require(WEBPACK_DEV_SERVER_PACKAGE_NAME);
143
- // TODO: the WebpackDevServer accepts a third parameter for a logger. We should make
144
- // use of that to make logging cleaner
145
- const webpackDevServer = new WebpackDevServer(compiler, options);
146
- await new Promise((resolve, reject) => {
147
- firstCompilationDoneCallback = resolve;
148
- webpackDevServer.listen(options.port, options.host, (error) => {
149
- if (error) {
150
- reject(error);
151
- }
152
- });
153
- });
154
- }
155
- else {
156
- if (process.env[WEBPACK_DEV_SERVER_ENV_VAR_NAME]) {
157
- logger.emitWarning(new Error(`The "${WEBPACK_DEV_SERVER_ENV_VAR_NAME}" environment variable is set, ` +
158
- 'which will cause problems when webpack is not running in serve mode. ' +
159
- `(Did a dependency inadvertently load the "${WEBPACK_DEV_SERVER_PACKAGE_NAME}" package?)`));
160
- }
161
- let stats;
162
- if (buildProperties.watchMode) {
163
- try {
164
- stats = await node_core_library_1.LegacyAdapters.convertCallbackToPromise(compiler.watch.bind(compiler), {});
165
- }
166
- catch (e) {
167
- logger.emitError(e);
168
- }
169
- }
170
- else {
171
- try {
172
- stats = await node_core_library_1.LegacyAdapters.convertCallbackToPromise(compiler.run.bind(compiler));
173
- }
174
- catch (e) {
175
- logger.emitError(e);
176
- }
177
- }
178
- if (stats) {
179
- // eslint-disable-next-line require-atomic-updates
180
- buildProperties.webpackStats = stats;
181
- this._emitErrors(logger, stats);
182
- }
183
- }
184
- }
185
- _emitErrors(logger, stats) {
186
- if (stats.hasErrors() || stats.hasWarnings()) {
187
- const serializedStats = stats.toJson('errors-warnings');
188
- for (const warning of serializedStats.warnings) {
189
- logger.emitWarning(warning instanceof Error ? warning : new Error(warning));
190
- }
191
- for (const error of serializedStats.errors) {
192
- logger.emitError(error instanceof Error ? error : new Error(error));
193
- }
194
- }
195
- }
196
- }
197
- exports.WebpackPlugin = WebpackPlugin;
198
- //# sourceMappingURL=WebpackPlugin.js.map