@nx/webpack 17.1.0-beta.3 → 17.1.0-beta.5

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.
Files changed (33) hide show
  1. package/index.d.ts +2 -0
  2. package/index.js +5 -1
  3. package/package.json +4 -4
  4. package/src/executors/dev-server/dev-server.impl.js +18 -5
  5. package/src/executors/webpack/lib/normalize-options.d.ts +1 -2
  6. package/src/executors/webpack/lib/normalize-options.js +3 -52
  7. package/src/executors/webpack/schema.d.ts +4 -4
  8. package/src/executors/webpack/webpack.impl.js +11 -4
  9. package/src/plugins/generate-package-json-plugin.d.ts +6 -4
  10. package/src/plugins/generate-package-json-plugin.js +11 -14
  11. package/src/plugins/nx-typescript-webpack-plugin/nx-tsconfig-paths-webpack-plugin.d.ts +8 -0
  12. package/src/plugins/nx-typescript-webpack-plugin/nx-tsconfig-paths-webpack-plugin.js +31 -0
  13. package/src/plugins/nx-webpack-plugin/lib/apply-base-config.d.ts +3 -0
  14. package/src/plugins/nx-webpack-plugin/lib/apply-base-config.js +288 -0
  15. package/src/plugins/nx-webpack-plugin/lib/apply-web-config.d.ts +3 -0
  16. package/src/plugins/nx-webpack-plugin/lib/apply-web-config.js +355 -0
  17. package/src/plugins/nx-webpack-plugin/lib/compiler-loaders.d.ts +53 -0
  18. package/src/plugins/nx-webpack-plugin/lib/compiler-loaders.js +78 -0
  19. package/src/plugins/nx-webpack-plugin/lib/get-terser-ecma-version.d.ts +1 -0
  20. package/src/plugins/nx-webpack-plugin/lib/get-terser-ecma-version.js +35 -0
  21. package/src/plugins/nx-webpack-plugin/lib/instantiate-script-plugins.d.ts +3 -0
  22. package/src/plugins/nx-webpack-plugin/lib/instantiate-script-plugins.js +42 -0
  23. package/src/plugins/nx-webpack-plugin/lib/normalize-options.d.ts +3 -0
  24. package/src/plugins/nx-webpack-plugin/lib/normalize-options.js +116 -0
  25. package/src/plugins/nx-webpack-plugin/lib/stylesheet-loaders.d.ts +73 -0
  26. package/src/plugins/nx-webpack-plugin/lib/stylesheet-loaders.js +115 -0
  27. package/src/plugins/nx-webpack-plugin/nx-webpack-plugin-options.d.ts +87 -0
  28. package/src/plugins/nx-webpack-plugin/nx-webpack-plugin-options.js +2 -0
  29. package/src/plugins/nx-webpack-plugin/nx-webpack-plugin.d.ts +18 -0
  30. package/src/plugins/nx-webpack-plugin/nx-webpack-plugin.js +52 -0
  31. package/src/utils/with-nx.d.ts +0 -53
  32. package/src/utils/with-nx.js +14 -361
  33. package/src/utils/with-web.js +9 -488
@@ -0,0 +1,116 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.normalizeAssets = exports.normalizeOptions = void 0;
4
+ const path_1 = require("path");
5
+ const fs_1 = require("fs");
6
+ const devkit_1 = require("@nx/devkit");
7
+ function normalizeOptions(options) {
8
+ const combinedOptions = {};
9
+ const isProd = process.env.NODE_ENV === 'production';
10
+ const projectName = process.env.NX_TASK_TARGET_PROJECT;
11
+ const targetName = process.env.NX_TASK_TARGET_TARGET;
12
+ const configurationName = process.env.NX_TASK_TARGET_CONFIGURATION;
13
+ // Since this is invoked by the executor, the graph has already been created and cached.
14
+ const projectGraph = (0, devkit_1.readCachedProjectGraph)();
15
+ const projectNode = projectGraph.nodes[projectName];
16
+ const targetConfig = projectNode.data.targets[targetName];
17
+ // Merge options from `@nx/webpack:webpack` into plugin options.
18
+ // Options from `@nx/webpack:webpack` take precedence.
19
+ const originalTargetOptions = targetConfig.options;
20
+ if (configurationName) {
21
+ Object.assign(originalTargetOptions, targetConfig.configurations?.[configurationName]);
22
+ }
23
+ // This could be called from dev-server which means we need to read `buildTarget` to get actual build options.
24
+ // Otherwise, the options are passed from the `@nx/webpack:webpack` executor.
25
+ if (originalTargetOptions.buildTarget) {
26
+ const buildTargetOptions = targetConfig.options;
27
+ if (configurationName) {
28
+ Object.assign(buildTargetOptions, targetConfig.configurations?.[configurationName]);
29
+ }
30
+ Object.assign(combinedOptions, buildTargetOptions);
31
+ }
32
+ else {
33
+ Object.assign(combinedOptions, originalTargetOptions, options);
34
+ }
35
+ const sourceRoot = projectNode.data.sourceRoot ?? projectNode.data.root;
36
+ if (!options.main)
37
+ throw new Error(`Missing "main" option for the entry file. Set this option in your Nx webpack plugin.`);
38
+ return {
39
+ ...options,
40
+ assets: options.assets
41
+ ? normalizeAssets(options.assets, devkit_1.workspaceRoot, sourceRoot)
42
+ : [],
43
+ baseHref: options.baseHref ?? '/',
44
+ commonChunk: options.commonChunk ?? true,
45
+ compiler: options.compiler ?? 'babel',
46
+ configurationName,
47
+ deleteOutputPath: options.deleteOutputPath ?? true,
48
+ extractCss: options.extractCss ?? true,
49
+ fileReplacements: normalizeFileReplacements(devkit_1.workspaceRoot, options.fileReplacements),
50
+ generateIndexHtml: options.generateIndexHtml ?? true,
51
+ main: options.main,
52
+ namedChunks: options.namedChunks ?? !isProd,
53
+ optimization: options.optimization ?? isProd,
54
+ outputFileName: options.outputFileName ?? 'main.js',
55
+ outputHashing: options.outputHashing ?? (isProd ? 'all' : 'none'),
56
+ outputPath: options.outputPath,
57
+ projectGraph,
58
+ projectName,
59
+ projectRoot: projectNode.data.root,
60
+ root: devkit_1.workspaceRoot,
61
+ runtimeChunk: options.runtimeChunk ?? true,
62
+ scripts: options.scripts ?? [],
63
+ sourceMap: options.sourceMap ?? !isProd,
64
+ sourceRoot,
65
+ styles: options.styles ?? [],
66
+ target: options.target ?? 'web',
67
+ targetName,
68
+ vendorChunk: options.vendorChunk ?? !isProd,
69
+ };
70
+ }
71
+ exports.normalizeOptions = normalizeOptions;
72
+ function normalizeAssets(assets, root, sourceRoot) {
73
+ return assets.map((asset) => {
74
+ if (typeof asset === 'string') {
75
+ const assetPath = (0, devkit_1.normalizePath)(asset);
76
+ const resolvedAssetPath = (0, path_1.resolve)(root, assetPath);
77
+ const resolvedSourceRoot = (0, path_1.resolve)(root, sourceRoot);
78
+ if (!resolvedAssetPath.startsWith(resolvedSourceRoot)) {
79
+ throw new Error(`The ${resolvedAssetPath} asset path must start with the project source root: ${sourceRoot}`);
80
+ }
81
+ const isDirectory = (0, fs_1.statSync)(resolvedAssetPath).isDirectory();
82
+ const input = isDirectory
83
+ ? resolvedAssetPath
84
+ : (0, path_1.dirname)(resolvedAssetPath);
85
+ const output = (0, path_1.relative)(resolvedSourceRoot, (0, path_1.resolve)(root, input));
86
+ const glob = isDirectory ? '**/*' : (0, path_1.basename)(resolvedAssetPath);
87
+ return {
88
+ input,
89
+ output,
90
+ glob,
91
+ };
92
+ }
93
+ else {
94
+ if (asset.output.startsWith('..')) {
95
+ throw new Error('An asset cannot be written to a location outside of the output path.');
96
+ }
97
+ const assetPath = (0, devkit_1.normalizePath)(asset.input);
98
+ const resolvedAssetPath = (0, path_1.resolve)(root, assetPath);
99
+ return {
100
+ ...asset,
101
+ input: resolvedAssetPath,
102
+ // Now we remove starting slash to make Webpack place it from the output root.
103
+ output: asset.output.replace(/^\//, ''),
104
+ };
105
+ }
106
+ });
107
+ }
108
+ exports.normalizeAssets = normalizeAssets;
109
+ function normalizeFileReplacements(root, fileReplacements) {
110
+ return fileReplacements
111
+ ? fileReplacements.map((fileReplacement) => ({
112
+ replace: (0, path_1.resolve)(root, fileReplacement.replace),
113
+ with: (0, path_1.resolve)(root, fileReplacement.with),
114
+ }))
115
+ : [];
116
+ }
@@ -0,0 +1,73 @@
1
+ import { getCSSModuleLocalIdent } from '../../../utils/get-css-module-local-ident';
2
+ import { NormalizedNxWebpackPluginOptions } from '../nx-webpack-plugin-options';
3
+ interface PostcssOptions {
4
+ (loader: any): any;
5
+ config?: string;
6
+ }
7
+ export declare function getCommonLoadersForCssModules(options: NormalizedNxWebpackPluginOptions, includePaths: string[]): ({
8
+ loader: any;
9
+ options?: undefined;
10
+ } | {
11
+ loader: string;
12
+ options: {
13
+ modules: {
14
+ mode: string;
15
+ getLocalIdent: typeof getCSSModuleLocalIdent;
16
+ };
17
+ importLoaders: number;
18
+ implementation?: undefined;
19
+ postcssOptions?: undefined;
20
+ };
21
+ } | {
22
+ loader: string;
23
+ options: {
24
+ implementation: any;
25
+ postcssOptions: PostcssOptions;
26
+ modules?: undefined;
27
+ importLoaders?: undefined;
28
+ };
29
+ })[];
30
+ export declare function getCommonLoadersForGlobalCss(options: NormalizedNxWebpackPluginOptions, includePaths: string[]): ({
31
+ loader: any;
32
+ options?: undefined;
33
+ } | {
34
+ loader: string;
35
+ options: {
36
+ url: boolean;
37
+ implementation?: undefined;
38
+ postcssOptions?: undefined;
39
+ };
40
+ } | {
41
+ loader: string;
42
+ options: {
43
+ implementation: any;
44
+ postcssOptions: PostcssOptions;
45
+ url?: undefined;
46
+ };
47
+ })[];
48
+ export declare function getCommonLoadersForGlobalStyle(options: NormalizedNxWebpackPluginOptions, includePaths: string[]): ({
49
+ loader: any;
50
+ options: {
51
+ esModule: boolean;
52
+ url?: undefined;
53
+ implementation?: undefined;
54
+ postcssOptions?: undefined;
55
+ };
56
+ } | {
57
+ loader: string;
58
+ options: {
59
+ url: boolean;
60
+ esModule?: undefined;
61
+ implementation?: undefined;
62
+ postcssOptions?: undefined;
63
+ };
64
+ } | {
65
+ loader: string;
66
+ options: {
67
+ implementation: any;
68
+ postcssOptions: PostcssOptions;
69
+ esModule?: undefined;
70
+ url?: undefined;
71
+ };
72
+ })[];
73
+ export {};
@@ -0,0 +1,115 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getCommonLoadersForGlobalStyle = exports.getCommonLoadersForGlobalCss = exports.getCommonLoadersForCssModules = void 0;
4
+ const path = require("path");
5
+ const autoprefixer = require("autoprefixer");
6
+ const postcssImports = require("postcss-import");
7
+ const MiniCssExtractPlugin = require("mini-css-extract-plugin");
8
+ const get_css_module_local_ident_1 = require("../../../utils/get-css-module-local-ident");
9
+ const hash_format_1 = require("../../../utils/hash-format");
10
+ const postcss_cli_resources_1 = require("../../../utils/webpack/plugins/postcss-cli-resources");
11
+ function getCommonLoadersForCssModules(options, includePaths) {
12
+ // load component css as raw strings
13
+ return [
14
+ {
15
+ loader: options.extractCss
16
+ ? MiniCssExtractPlugin.loader
17
+ : require.resolve('style-loader'),
18
+ },
19
+ {
20
+ loader: require.resolve('css-loader'),
21
+ options: {
22
+ modules: {
23
+ mode: 'local',
24
+ getLocalIdent: get_css_module_local_ident_1.getCSSModuleLocalIdent,
25
+ },
26
+ importLoaders: 1,
27
+ },
28
+ },
29
+ {
30
+ loader: require.resolve('postcss-loader'),
31
+ options: {
32
+ implementation: require('postcss'),
33
+ postcssOptions: postcssOptionsCreator(options, {
34
+ includePaths,
35
+ forCssModules: true,
36
+ }),
37
+ },
38
+ },
39
+ ];
40
+ }
41
+ exports.getCommonLoadersForCssModules = getCommonLoadersForCssModules;
42
+ function getCommonLoadersForGlobalCss(options, includePaths) {
43
+ return [
44
+ {
45
+ loader: options.extractCss
46
+ ? MiniCssExtractPlugin.loader
47
+ : require.resolve('style-loader'),
48
+ },
49
+ { loader: require.resolve('css-loader'), options: { url: false } },
50
+ {
51
+ loader: require.resolve('postcss-loader'),
52
+ options: {
53
+ implementation: require('postcss'),
54
+ postcssOptions: postcssOptionsCreator(options, {
55
+ includePaths,
56
+ }),
57
+ },
58
+ },
59
+ ];
60
+ }
61
+ exports.getCommonLoadersForGlobalCss = getCommonLoadersForGlobalCss;
62
+ function getCommonLoadersForGlobalStyle(options, includePaths) {
63
+ return [
64
+ {
65
+ loader: MiniCssExtractPlugin.loader,
66
+ options: { esModule: true },
67
+ },
68
+ { loader: require.resolve('css-loader'), options: { url: false } },
69
+ {
70
+ loader: require.resolve('postcss-loader'),
71
+ options: {
72
+ implementation: require('postcss'),
73
+ postcssOptions: postcssOptionsCreator(options, {
74
+ includePaths,
75
+ }),
76
+ },
77
+ },
78
+ ];
79
+ }
80
+ exports.getCommonLoadersForGlobalStyle = getCommonLoadersForGlobalStyle;
81
+ function postcssOptionsCreator(options, { includePaths, forCssModules = false, }) {
82
+ const hashFormat = (0, hash_format_1.getOutputHashFormat)(options.outputHashing);
83
+ // PostCSS options depend on the webpack loader, but we need to set the `config` path as a string due to this check:
84
+ // https://github.com/webpack-contrib/postcss-loader/blob/0d342b1/src/utils.js#L36
85
+ const postcssOptions = (loader) => ({
86
+ map: options.sourceMap &&
87
+ options.sourceMap !== 'hidden' && {
88
+ inline: true,
89
+ annotation: false,
90
+ },
91
+ plugins: [
92
+ postcssImports({
93
+ addModulesDirectories: includePaths,
94
+ resolve: (url) => (url.startsWith('~') ? url.slice(1) : url),
95
+ }),
96
+ ...(forCssModules
97
+ ? []
98
+ : [
99
+ (0, postcss_cli_resources_1.PostcssCliResources)({
100
+ baseHref: options.baseHref,
101
+ deployUrl: options.deployUrl,
102
+ loader,
103
+ filename: `[name]${hashFormat.file}.[ext]`,
104
+ }),
105
+ autoprefixer(),
106
+ ]),
107
+ ],
108
+ });
109
+ // If a path to postcssConfig is passed in, set it for app and all libs, otherwise
110
+ // use automatic detection.
111
+ if (typeof options.postcssConfig === 'string') {
112
+ postcssOptions.config = path.join(options.root, options.postcssConfig);
113
+ }
114
+ return postcssOptions;
115
+ }
@@ -0,0 +1,87 @@
1
+ import { ProjectGraph } from '@nx/devkit';
2
+ import { AssetGlob } from '@nx/js/src/utils/assets/assets';
3
+ export interface AssetGlobPattern {
4
+ glob: string;
5
+ input: string;
6
+ output: string;
7
+ ignore?: string[];
8
+ }
9
+ export interface ExtraEntryPointClass {
10
+ bundleName?: string;
11
+ inject?: boolean;
12
+ input: string;
13
+ lazy?: boolean;
14
+ }
15
+ export interface FileReplacement {
16
+ replace: string;
17
+ with: string;
18
+ }
19
+ export interface AdditionalEntryPoint {
20
+ entryName: string;
21
+ entryPath: string;
22
+ }
23
+ export interface TransformerPlugin {
24
+ name: string;
25
+ options: Record<string, unknown>;
26
+ }
27
+ export type TransformerEntry = string | TransformerPlugin;
28
+ export interface OptimizationOptions {
29
+ scripts: boolean;
30
+ styles: boolean;
31
+ }
32
+ export interface NxWebpackPluginOptions {
33
+ main: string;
34
+ outputPath: string;
35
+ tsConfig: string;
36
+ additionalEntryPoints?: AdditionalEntryPoint[];
37
+ assets?: Array<AssetGlob | string>;
38
+ babelConfig?: string;
39
+ babelUpwardRootMode?: boolean;
40
+ baseHref?: string;
41
+ commonChunk?: boolean;
42
+ compiler?: 'babel' | 'swc' | 'tsc';
43
+ crossOrigin?: 'none' | 'anonymous' | 'use-credentials';
44
+ deleteOutputPath?: boolean;
45
+ deployUrl?: string;
46
+ externalDependencies?: 'all' | 'none' | string[];
47
+ extractCss?: boolean;
48
+ extractLicenses?: boolean;
49
+ fileReplacements?: FileReplacement[];
50
+ generateIndexHtml?: boolean;
51
+ generatePackageJson?: boolean;
52
+ index?: string;
53
+ memoryLimit?: number;
54
+ namedChunks?: boolean;
55
+ optimization?: boolean | OptimizationOptions;
56
+ outputFileName?: string;
57
+ outputHashing?: any;
58
+ poll?: number;
59
+ polyfills?: string;
60
+ postcssConfig?: string;
61
+ progress?: boolean;
62
+ runtimeChunk?: boolean;
63
+ scripts?: Array<ExtraEntryPointClass | string>;
64
+ skipTypeChecking?: boolean;
65
+ sourceMap?: boolean | 'hidden';
66
+ ssr?: boolean;
67
+ statsJson?: boolean;
68
+ stylePreprocessorOptions?: any;
69
+ styles?: Array<ExtraEntryPointClass | string>;
70
+ subresourceIntegrity?: boolean;
71
+ target?: string | string[];
72
+ transformers?: TransformerEntry[];
73
+ vendorChunk?: boolean;
74
+ verbose?: boolean;
75
+ watch?: boolean;
76
+ }
77
+ export interface NormalizedNxWebpackPluginOptions extends NxWebpackPluginOptions {
78
+ projectName: string;
79
+ root: string;
80
+ projectRoot: string;
81
+ sourceRoot: string;
82
+ configurationName: string;
83
+ targetName: string;
84
+ projectGraph: ProjectGraph;
85
+ outputFileName: string;
86
+ assets: AssetGlobPattern[];
87
+ }
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,18 @@
1
+ import { Compiler } from 'webpack';
2
+ import { NxWebpackPluginOptions } from './nx-webpack-plugin-options';
3
+ /**
4
+ * This plugin provides features to build Node and Web applications.
5
+ * - TS support (including tsconfig paths)
6
+ * - Different compiler options
7
+ * - Assets handling
8
+ * - Stylesheets handling
9
+ * - index.html and package.json generation
10
+ *
11
+ * Web-only features, such as stylesheets and images, are only supported when `target` is 'web' or 'webworker'.
12
+ */
13
+ export declare class NxWebpackPlugin {
14
+ private readonly options;
15
+ constructor(options: NxWebpackPluginOptions);
16
+ apply(compiler: Compiler): void;
17
+ private readExecutorOptions;
18
+ }
@@ -0,0 +1,52 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.NxWebpackPlugin = void 0;
4
+ const normalize_options_1 = require("./lib/normalize-options");
5
+ const fs_1 = require("../../utils/fs");
6
+ const apply_base_config_1 = require("./lib/apply-base-config");
7
+ const apply_web_config_1 = require("./lib/apply-web-config");
8
+ /**
9
+ * This plugin provides features to build Node and Web applications.
10
+ * - TS support (including tsconfig paths)
11
+ * - Different compiler options
12
+ * - Assets handling
13
+ * - Stylesheets handling
14
+ * - index.html and package.json generation
15
+ *
16
+ * Web-only features, such as stylesheets and images, are only supported when `target` is 'web' or 'webworker'.
17
+ */
18
+ class NxWebpackPlugin {
19
+ constructor(options) {
20
+ this.options = (0, normalize_options_1.normalizeOptions)({
21
+ ...options,
22
+ ...this.readExecutorOptions(),
23
+ });
24
+ }
25
+ apply(compiler) {
26
+ const target = this.options.target ?? compiler.options.target;
27
+ this.options.outputPath ??= compiler.options.output?.path;
28
+ if (typeof target === 'string') {
29
+ this.options.target = target;
30
+ }
31
+ if (this.options.deleteOutputPath) {
32
+ (0, fs_1.deleteOutputDir)(this.options.root, this.options.outputPath);
33
+ }
34
+ (0, apply_base_config_1.applyBaseConfig)(this.options, compiler.options);
35
+ if (compiler.options.target) {
36
+ this.options.target = compiler.options.target;
37
+ }
38
+ if (this.options.target === 'web' || this.options.target === 'webworker') {
39
+ (0, apply_web_config_1.applyWebConfig)(this.options, compiler.options);
40
+ }
41
+ }
42
+ readExecutorOptions() {
43
+ const fromExecutor = process.env['NX_WEBPACK_EXECUTOR_RAW_OPTIONS'] ?? '{}';
44
+ try {
45
+ return JSON.parse(fromExecutor);
46
+ }
47
+ catch {
48
+ return {};
49
+ }
50
+ }
51
+ }
52
+ exports.NxWebpackPlugin = NxWebpackPlugin;
@@ -1,4 +1,3 @@
1
- import { NormalizedWebpackExecutorOptions } from '../executors/webpack/schema';
2
1
  import { NxWebpackPlugin } from './config';
3
2
  export interface WithNxOptions {
4
3
  skipTypeChecking?: boolean;
@@ -8,55 +7,3 @@ export interface WithNxOptions {
8
7
  * @returns {NxWebpackPlugin}
9
8
  */
10
9
  export declare function withNx(pluginOptions?: WithNxOptions): NxWebpackPlugin;
11
- export declare function createLoaderFromCompiler(options: NormalizedWebpackExecutorOptions): {
12
- test: RegExp;
13
- loader: string;
14
- exclude: RegExp;
15
- options: {
16
- cwd: string;
17
- emitDecoratorMetadata: boolean;
18
- isModern: boolean;
19
- isTest: boolean;
20
- envName: string;
21
- cacheDirectory: boolean;
22
- cacheCompression: boolean;
23
- };
24
- } | {
25
- test: RegExp;
26
- loader: string;
27
- exclude: RegExp;
28
- options: {
29
- jsc: {
30
- parser: {
31
- syntax: string;
32
- decorators: boolean;
33
- tsx: boolean;
34
- };
35
- transform: {
36
- react: {
37
- runtime: string;
38
- };
39
- };
40
- loose: boolean;
41
- };
42
- configFile?: undefined;
43
- transpileOnly?: undefined;
44
- experimentalWatchApi?: undefined;
45
- getCustomTransformers?: undefined;
46
- };
47
- } | {
48
- test: RegExp;
49
- loader: string;
50
- exclude: RegExp;
51
- options: {
52
- configFile: string;
53
- transpileOnly: boolean;
54
- experimentalWatchApi: boolean;
55
- getCustomTransformers: (program: any) => {
56
- before: any;
57
- after: any;
58
- afterDeclarations: any;
59
- };
60
- jsc?: undefined;
61
- };
62
- };