@nx/react 0.0.0-pr-29176-d3c6c29 → 0.0.0-pr-29314-bfd9d0c
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/generators.json +4 -2
- package/package.json +6 -6
- package/plugins/bundle-rollup.js +2 -2
- package/plugins/nx-react-webpack-plugin/lib/apply-react-config.js +3 -1
- package/src/executors/module-federation-dev-server/schema.json +14 -10
- package/src/executors/module-federation-ssr-dev-server/schema.json +13 -9
- package/src/executors/module-federation-static-server/module-federation-static-server.impl.js +5 -5
- package/src/executors/module-federation-static-server/schema.json +1 -1
- package/src/generators/application/application.d.ts +3 -3
- package/src/generators/application/application.js +59 -194
- package/src/generators/application/files/base-rsbuild/src/app/__fileName__.spec.tsx__tmpl__ +10 -0
- package/src/generators/application/files/base-rsbuild/src/assets/.gitkeep +0 -0
- package/src/generators/application/files/base-rsbuild/src/favicon.ico +0 -0
- package/src/generators/application/files/base-rsbuild/src/index.html +14 -0
- package/src/generators/application/files/base-rsbuild/src/main.tsx__tmpl__ +32 -0
- package/src/generators/application/files/base-rsbuild/tsconfig.app.json__tmpl__ +31 -0
- package/src/generators/application/files/style-tailwind/src/app/__fileName__.tsx__tmpl__ +1 -1
- package/src/generators/application/lib/add-e2e.js +12 -1
- package/src/generators/application/lib/add-linting.d.ts +3 -0
- package/src/generators/application/lib/add-linting.js +48 -0
- package/src/generators/application/lib/bundlers/add-rsbuild.d.ts +4 -0
- package/src/generators/application/lib/bundlers/add-rsbuild.js +63 -0
- package/src/generators/application/lib/bundlers/add-rspack.d.ts +5 -0
- package/src/generators/application/lib/bundlers/add-rspack.js +55 -0
- package/src/generators/application/lib/bundlers/add-vite.d.ts +4 -0
- package/src/generators/application/lib/bundlers/add-vite.js +67 -0
- package/src/generators/application/lib/bundlers/add-webpack.d.ts +3 -0
- package/src/generators/application/lib/bundlers/add-webpack.js +18 -0
- package/src/generators/application/lib/create-application-files.js +5 -0
- package/src/generators/application/schema.d.ts +1 -1
- package/src/generators/application/schema.json +4 -4
- package/src/generators/component/component.js +19 -32
- package/src/generators/component/files/{__fileName__.tsx__tmpl__ → __fileName__.__ext__} +5 -5
- package/src/generators/component/lib/normalize-options.js +7 -4
- package/src/generators/component/schema.d.ts +9 -3
- package/src/generators/component/schema.json +6 -6
- package/src/generators/component-story/component-story.js +1 -2
- package/src/generators/federate-module/schema.json +14 -8
- package/src/generators/hook/files/{__fileName__.ts__tmpl__ → __fileName__.__ext__} +3 -1
- package/src/generators/hook/hook.js +21 -22
- package/src/generators/hook/schema.d.ts +4 -0
- package/src/generators/hook/schema.json +8 -4
- package/src/generators/host/schema.json +9 -8
- package/src/generators/library/lib/add-rollup-build-target.js +30 -27
- package/src/generators/library/library.js +6 -1
- package/src/generators/library/schema.json +4 -0
- package/src/generators/redux/files/js/__fileName__.__ext__ +114 -0
- package/src/generators/redux/files/{__fileName__.slice.spec.ts__tmpl__ → js/__fileName__.spec.__ext__} +1 -1
- package/src/generators/redux/files/ts/__fileName__.spec.__ext__ +56 -0
- package/src/generators/redux/redux.js +19 -16
- package/src/generators/redux/schema.d.ts +10 -2
- package/src/generators/redux/schema.json +8 -4
- package/src/generators/remote/lib/update-host-with-remote.js +2 -2
- package/src/generators/remote/schema.json +9 -8
- package/src/generators/stories/stories.js +5 -2
- package/src/generators/storybook-configuration/configuration.js +2 -2
- package/src/utils/has-rsbuild-plugin.d.ts +2 -0
- package/src/utils/has-rsbuild-plugin.js +10 -0
- /package/src/generators/component/files/{__fileName__.spec.tsx__tmpl__ → __fileName__.spec.__ext__} +0 -0
- /package/src/generators/hook/files/{__fileName__.spec.tsx__tmpl__ → __fileName__.spec.__specExt__} +0 -0
- /package/src/generators/redux/files/{__fileName__.slice.ts__tmpl__ → ts/__fileName__.__ext__} +0 -0
|
@@ -3,11 +3,15 @@
|
|
|
3
3
|
"cli": "nx",
|
|
4
4
|
"$id": "NxReactFederateModule",
|
|
5
5
|
"title": "Federate Module",
|
|
6
|
-
"description": "Create a federated module, which can be loaded by a
|
|
6
|
+
"description": "Create a federated module, which can be loaded by a Consumer (host) via a Producer (remote).",
|
|
7
7
|
"examples": [
|
|
8
8
|
{
|
|
9
|
-
"command": "nx g federate-module MyModule --path=./src/component/my-cmp.ts --remote=
|
|
10
|
-
"description": "Create a federated module from
|
|
9
|
+
"command": "nx g federate-module MyModule --path=./src/component/my-cmp.ts --remote=myRemote --remoteDirectory=apps/myRemote",
|
|
10
|
+
"description": "Create a federated module from myRemote, that exposes my-cmp from ./src/component/my-cmp.ts as MyModule."
|
|
11
|
+
},
|
|
12
|
+
{
|
|
13
|
+
"command": "nx g federate-module MyModule --path=./src/component/my-cmp.ts --producer=myProducer --producerDirectory=apps/myProducer",
|
|
14
|
+
"description": "Create a federated module from myProducer, that exposes my-cmp from ./src/component/my-cmp.ts as MyModule."
|
|
11
15
|
}
|
|
12
16
|
],
|
|
13
17
|
"type": "object",
|
|
@@ -30,12 +34,14 @@
|
|
|
30
34
|
},
|
|
31
35
|
"remote": {
|
|
32
36
|
"type": "string",
|
|
33
|
-
"description": "The name of the remote.",
|
|
34
|
-
"x-prompt": "What is/should the remote be named?"
|
|
37
|
+
"description": "The name of the Producer (remote).",
|
|
38
|
+
"x-prompt": "What is/should the Producer (remote) be named?",
|
|
39
|
+
"alias": "producer"
|
|
35
40
|
},
|
|
36
41
|
"remoteDirectory": {
|
|
37
|
-
"description": "The directory of the new remote application if one needs to be created.",
|
|
38
|
-
"type": "string"
|
|
42
|
+
"description": "The directory of the new Producer (remote) application if one needs to be created.",
|
|
43
|
+
"type": "string",
|
|
44
|
+
"alias": "producerDirectory"
|
|
39
45
|
},
|
|
40
46
|
"style": {
|
|
41
47
|
"description": "The file extension to be used for style files.",
|
|
@@ -70,7 +76,7 @@
|
|
|
70
76
|
},
|
|
71
77
|
"host": {
|
|
72
78
|
"type": "string",
|
|
73
|
-
"description": "The host
|
|
79
|
+
"description": "The Consumer (host) application for this Producer (remote)."
|
|
74
80
|
},
|
|
75
81
|
"bundler": {
|
|
76
82
|
"description": "The bundler to use.",
|
|
@@ -1,12 +1,14 @@
|
|
|
1
1
|
import { useState, useCallback } from 'react'
|
|
2
2
|
|
|
3
|
+
<%_ if (isTs) { _%>
|
|
3
4
|
// eslint-disable-next-line @typescript-eslint/no-empty-interface
|
|
4
5
|
export interface <%= hookTypeName %> {
|
|
5
6
|
count: number;
|
|
6
7
|
increment: () => void;
|
|
7
8
|
}
|
|
9
|
+
<%_ } _%>
|
|
8
10
|
|
|
9
|
-
export function <%= hookName %>()
|
|
11
|
+
export function <%= hookName %>()<% if (isTs) { %>: <%= hookTypeName %><% } %> {
|
|
10
12
|
const [count, setCount] = useState(0)
|
|
11
13
|
const increment = useCallback(() => setCount((x) => x + 1), [])
|
|
12
14
|
return { count, increment }
|
|
@@ -3,10 +3,10 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.hookGenerator = hookGenerator;
|
|
4
4
|
// TODO(jack): Remove inline renderHook function when RTL releases with its own version
|
|
5
5
|
const devkit_1 = require("@nx/devkit");
|
|
6
|
-
const
|
|
6
|
+
const artifact_name_and_directory_utils_1 = require("@nx/devkit/src/generators/artifact-name-and-directory-utils");
|
|
7
7
|
const ensure_typescript_1 = require("@nx/js/src/utils/typescript/ensure-typescript");
|
|
8
8
|
const path_1 = require("path");
|
|
9
|
-
const
|
|
9
|
+
const ast_utils_1 = require("../../utils/ast-utils");
|
|
10
10
|
async function hookGenerator(host, schema) {
|
|
11
11
|
const options = await normalizeOptions(host, schema);
|
|
12
12
|
createFiles(host, options);
|
|
@@ -14,21 +14,15 @@ async function hookGenerator(host, schema) {
|
|
|
14
14
|
return await (0, devkit_1.formatFiles)(host);
|
|
15
15
|
}
|
|
16
16
|
function createFiles(host, options) {
|
|
17
|
+
const specExt = options.fileExtensionType === 'ts' ? 'tsx' : 'js';
|
|
17
18
|
(0, devkit_1.generateFiles)(host, (0, path_1.join)(__dirname, './files'), options.directory, {
|
|
18
19
|
...options,
|
|
19
|
-
|
|
20
|
+
ext: options.fileExtension,
|
|
21
|
+
specExt,
|
|
22
|
+
isTs: options.fileExtensionType === 'ts',
|
|
20
23
|
});
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
if (options.skipTests && /.*spec.ts/.test(c.path)) {
|
|
24
|
-
deleteFile = true;
|
|
25
|
-
}
|
|
26
|
-
if (deleteFile) {
|
|
27
|
-
host.delete(c.path);
|
|
28
|
-
}
|
|
29
|
-
}
|
|
30
|
-
if (options.js) {
|
|
31
|
-
(0, devkit_1.toJS)(host);
|
|
24
|
+
if (options.skipTests) {
|
|
25
|
+
host.delete((0, devkit_1.joinPathFragments)(options.directory, `${options.fileName}.spec.${specExt}`));
|
|
32
26
|
}
|
|
33
27
|
}
|
|
34
28
|
let tsModule;
|
|
@@ -39,20 +33,23 @@ function addExportsToBarrel(host, options) {
|
|
|
39
33
|
const workspace = (0, devkit_1.getProjects)(host);
|
|
40
34
|
const isApp = workspace.get(options.projectName).projectType === 'application';
|
|
41
35
|
if (options.export && !isApp) {
|
|
42
|
-
const indexFilePath = (0, devkit_1.joinPathFragments)(options.projectSourceRoot, options.js ? 'index.js' : 'index.ts');
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
const indexSourceFile = tsModule.createSourceFile(indexFilePath, indexSource, tsModule.ScriptTarget.Latest, true);
|
|
46
|
-
const changes = (0, devkit_1.applyChangesToString)(indexSource, (0, ast_utils_1.addImport)(indexSourceFile, `export * from './${options.directory}/${options.fileName}';`));
|
|
47
|
-
host.write(indexFilePath, changes);
|
|
36
|
+
const indexFilePath = (0, devkit_1.joinPathFragments)(options.projectSourceRoot, options.fileExtensionType === 'js' ? 'index.js' : 'index.ts');
|
|
37
|
+
if (!host.exists(indexFilePath)) {
|
|
38
|
+
return;
|
|
48
39
|
}
|
|
40
|
+
const indexSource = host.read(indexFilePath, 'utf-8');
|
|
41
|
+
const indexSourceFile = tsModule.createSourceFile(indexFilePath, indexSource, tsModule.ScriptTarget.Latest, true);
|
|
42
|
+
const changes = (0, devkit_1.applyChangesToString)(indexSource, (0, ast_utils_1.addImport)(indexSourceFile, `export * from './${options.directory}/${options.fileName}';`));
|
|
43
|
+
host.write(indexFilePath, changes);
|
|
49
44
|
}
|
|
50
45
|
}
|
|
51
46
|
async function normalizeOptions(host, options) {
|
|
52
|
-
const { artifactName, directory, fileName: hookFilename, project: projectName, } = await (0, artifact_name_and_directory_utils_1.determineArtifactNameAndDirectoryOptions)(host, {
|
|
47
|
+
const { artifactName, directory, fileName: hookFilename, fileExtension, fileExtensionType, project: projectName, } = await (0, artifact_name_and_directory_utils_1.determineArtifactNameAndDirectoryOptions)(host, {
|
|
53
48
|
path: options.path,
|
|
54
49
|
name: options.name,
|
|
55
|
-
|
|
50
|
+
allowedFileExtensions: ['js', 'ts'],
|
|
51
|
+
fileExtension: options.js ? 'js' : 'ts',
|
|
52
|
+
js: options.js,
|
|
56
53
|
});
|
|
57
54
|
const { className } = (0, devkit_1.names)(hookFilename);
|
|
58
55
|
// if name is provided, use it as is for the hook name, otherwise prepend
|
|
@@ -74,6 +71,8 @@ async function normalizeOptions(host, options) {
|
|
|
74
71
|
hookName,
|
|
75
72
|
hookTypeName,
|
|
76
73
|
fileName: hookFilename,
|
|
74
|
+
fileExtension,
|
|
75
|
+
fileExtensionType,
|
|
77
76
|
projectSourceRoot,
|
|
78
77
|
projectName,
|
|
79
78
|
};
|
|
@@ -8,17 +8,21 @@
|
|
|
8
8
|
"examples": [
|
|
9
9
|
{
|
|
10
10
|
"description": "Generate a hook with the exported symbol matching the file name. It results in the hook `useFoo` at `mylib/src/lib/foo.ts`",
|
|
11
|
-
"command": "nx g @nx/react:hook mylib/src/lib/foo"
|
|
11
|
+
"command": "nx g @nx/react:hook mylib/src/lib/foo.ts"
|
|
12
12
|
},
|
|
13
13
|
{
|
|
14
14
|
"description": "Generate a hook with the exported symbol different from the file name. It results in the hook `useCustom` at `mylib/src/lib/foo.ts`",
|
|
15
|
-
"command": "nx g @nx/react:hook mylib/src/lib/foo --name=useCustom"
|
|
15
|
+
"command": "nx g @nx/react:hook mylib/src/lib/foo.ts --name=useCustom"
|
|
16
|
+
},
|
|
17
|
+
{
|
|
18
|
+
"description": "Generate a hook without providing the file extension. It results in the hook `useFoo` at `mylib/src/lib/foo.ts`",
|
|
19
|
+
"command": "nx g @nx/react:hook mylib/src/lib/foo"
|
|
16
20
|
}
|
|
17
21
|
],
|
|
18
22
|
"properties": {
|
|
19
23
|
"path": {
|
|
20
24
|
"type": "string",
|
|
21
|
-
"description": "The file path to the hook
|
|
25
|
+
"description": "The file path to the hook. Relative to the current working directory.",
|
|
22
26
|
"$default": {
|
|
23
27
|
"$source": "argv",
|
|
24
28
|
"index": 0
|
|
@@ -33,7 +37,7 @@
|
|
|
33
37
|
"js": {
|
|
34
38
|
"type": "boolean",
|
|
35
39
|
"description": "Generate JavaScript files rather than TypeScript files.",
|
|
36
|
-
"
|
|
40
|
+
"x-deprecated": "Provide the full file path including the file extension in the `path` option. This option will be removed in Nx v21."
|
|
37
41
|
},
|
|
38
42
|
"skipTests": {
|
|
39
43
|
"type": "boolean",
|
|
@@ -2,8 +2,8 @@
|
|
|
2
2
|
"$schema": "https://json-schema.org/schema",
|
|
3
3
|
"$id": "GeneratorReactHost",
|
|
4
4
|
"cli": "nx",
|
|
5
|
-
"title": "Generate Module Federation Setup for React Host App",
|
|
6
|
-
"description": "Create Module Federation configuration files for given React Host Application.",
|
|
5
|
+
"title": "Generate Module Federation Setup for React Consumer (Host) App",
|
|
6
|
+
"description": "Create Module Federation configuration files for given React Consumer (Host) Application.",
|
|
7
7
|
"type": "object",
|
|
8
8
|
"properties": {
|
|
9
9
|
"directory": {
|
|
@@ -18,7 +18,7 @@
|
|
|
18
18
|
},
|
|
19
19
|
"name": {
|
|
20
20
|
"type": "string",
|
|
21
|
-
"description": "The name of the host application to generate the Module Federation configuration",
|
|
21
|
+
"description": "The name of the Consumer (host) application to generate the Module Federation configuration",
|
|
22
22
|
"pattern": "^[a-zA-Z][^:]*$",
|
|
23
23
|
"x-priority": "important"
|
|
24
24
|
},
|
|
@@ -74,7 +74,7 @@
|
|
|
74
74
|
},
|
|
75
75
|
"dynamic": {
|
|
76
76
|
"type": "boolean",
|
|
77
|
-
"description": "Should the host application use dynamic federation?",
|
|
77
|
+
"description": "Should the Consumer (host) application use dynamic federation?",
|
|
78
78
|
"default": false
|
|
79
79
|
},
|
|
80
80
|
"skipFormat": {
|
|
@@ -141,18 +141,19 @@
|
|
|
141
141
|
},
|
|
142
142
|
"remotes": {
|
|
143
143
|
"type": "array",
|
|
144
|
-
"description": "A list of remote application names that the host application should consume.",
|
|
144
|
+
"description": "A list of Producer (remote) application names that the Consumer (host) application should consume.",
|
|
145
145
|
"default": [],
|
|
146
|
-
"x-priority": "important"
|
|
146
|
+
"x-priority": "important",
|
|
147
|
+
"alias": "producers"
|
|
147
148
|
},
|
|
148
149
|
"devServerPort": {
|
|
149
150
|
"type": "number",
|
|
150
|
-
"description": "The port for the dev server of the remote app.",
|
|
151
|
+
"description": "The port for the dev server of the Producer (remote) app.",
|
|
151
152
|
"default": 4200,
|
|
152
153
|
"x-priority": "important"
|
|
153
154
|
},
|
|
154
155
|
"ssr": {
|
|
155
|
-
"description": "Whether to configure SSR for the host application",
|
|
156
|
+
"description": "Whether to configure SSR for the Consumer (host) application",
|
|
156
157
|
"type": "boolean",
|
|
157
158
|
"default": false
|
|
158
159
|
},
|
|
@@ -32,33 +32,36 @@ async function addRollupBuildTarget(host, options) {
|
|
|
32
32
|
: p.plugin === '@nx/rollup/plugin');
|
|
33
33
|
if (hasRollupPlugin) {
|
|
34
34
|
// New behavior, using rollup config file and inferred target.
|
|
35
|
-
host.write((0, devkit_1.joinPathFragments)(options.projectRoot, 'rollup.config.
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
})
|
|
61
|
-
|
|
35
|
+
host.write((0, devkit_1.joinPathFragments)(options.projectRoot, 'rollup.config.cjs'), `const { withNx } = require('@nx/rollup/with-nx');
|
|
36
|
+
const url = require('@rollup/plugin-url');
|
|
37
|
+
const svg = require('@svgr/rollup');
|
|
38
|
+
|
|
39
|
+
module.exports = withNx(
|
|
40
|
+
{
|
|
41
|
+
main: '${(0, maybe_js_1.maybeJs)(options, './src/index.ts')}',
|
|
42
|
+
outputPath: '${options.isUsingTsSolutionConfig
|
|
43
|
+
? './dist'
|
|
44
|
+
: (0, devkit_1.joinPathFragments)((0, devkit_1.offsetFromRoot)(options.projectRoot), 'dist', options.projectRoot)}',
|
|
45
|
+
tsConfig: './tsconfig.lib.json',
|
|
46
|
+
compiler: '${options.compiler ?? 'babel'}',
|
|
47
|
+
external: ${JSON.stringify(external)},
|
|
48
|
+
format: ['esm'],
|
|
49
|
+
assets:[{ input: '.', output: '.', glob: 'README.md'}],
|
|
50
|
+
}, {
|
|
51
|
+
// Provide additional rollup configuration here. See: https://rollupjs.org/configuration-options
|
|
52
|
+
plugins: [
|
|
53
|
+
svg({
|
|
54
|
+
svgo: false,
|
|
55
|
+
titleProp: true,
|
|
56
|
+
ref: true,
|
|
57
|
+
}),
|
|
58
|
+
url({
|
|
59
|
+
limit: 10000, // 10kB
|
|
60
|
+
}),
|
|
61
|
+
],
|
|
62
|
+
}
|
|
63
|
+
);
|
|
64
|
+
`);
|
|
62
65
|
}
|
|
63
66
|
else {
|
|
64
67
|
// Legacy behavior, there is a target in project.json using rollup executor.
|
|
@@ -55,6 +55,7 @@ async function libraryGeneratorInternal(host, schema) {
|
|
|
55
55
|
: undefined;
|
|
56
56
|
(0, devkit_1.writeJson)(host, `${options.projectRoot}/package.json`, {
|
|
57
57
|
name: options.importPath,
|
|
58
|
+
version: '0.0.1',
|
|
58
59
|
main: sourceEntry,
|
|
59
60
|
types: sourceEntry,
|
|
60
61
|
nx: {
|
|
@@ -63,6 +64,7 @@ async function libraryGeneratorInternal(host, schema) {
|
|
|
63
64
|
sourceRoot: `${options.projectRoot}/src`,
|
|
64
65
|
tags: options.parsedTags?.length ? options.parsedTags : undefined,
|
|
65
66
|
},
|
|
67
|
+
files: options.publishable ? ['dist', '!**/*.tsbuildinfo'] : undefined,
|
|
66
68
|
});
|
|
67
69
|
}
|
|
68
70
|
else {
|
|
@@ -215,12 +217,15 @@ async function libraryGeneratorInternal(host, schema) {
|
|
|
215
217
|
}, options.linter === 'eslint'
|
|
216
218
|
? ['eslint.config.js', 'eslint.config.cjs', 'eslint.config.mjs']
|
|
217
219
|
: undefined);
|
|
220
|
+
if (options.isUsingTsSolutionConfig) {
|
|
221
|
+
(0, ts_solution_setup_1.addProjectToTsSolutionWorkspace)(host, options.projectRoot);
|
|
222
|
+
}
|
|
218
223
|
if (!options.skipFormat) {
|
|
219
224
|
await (0, devkit_1.formatFiles)(host);
|
|
220
225
|
}
|
|
221
226
|
// Always run install to link packages.
|
|
222
227
|
if (options.isUsingTsSolutionConfig) {
|
|
223
|
-
tasks.push(() => (0, devkit_1.installPackagesTask)(host));
|
|
228
|
+
tasks.push(() => (0, devkit_1.installPackagesTask)(host, true));
|
|
224
229
|
}
|
|
225
230
|
tasks.push(() => {
|
|
226
231
|
(0, log_show_project_command_1.logShowProjectCommand)(options.name);
|
|
@@ -52,6 +52,10 @@
|
|
|
52
52
|
"value": "less",
|
|
53
53
|
"label": "LESS [ https://lesscss.org ]"
|
|
54
54
|
},
|
|
55
|
+
{
|
|
56
|
+
"value": "tailwind",
|
|
57
|
+
"label": "tailwind [ https://tailwindcss.com/ ]"
|
|
58
|
+
},
|
|
55
59
|
{
|
|
56
60
|
"value": "styled-components",
|
|
57
61
|
"label": "styled-components [ https://styled-components.com ]"
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
import {
|
|
2
|
+
createAsyncThunk,
|
|
3
|
+
createEntityAdapter,
|
|
4
|
+
createSelector,
|
|
5
|
+
createSlice,
|
|
6
|
+
} from '@reduxjs/toolkit';
|
|
7
|
+
|
|
8
|
+
export const <%= constantName %>_FEATURE_KEY = '<%= propertyName %>';
|
|
9
|
+
export const <%= propertyName %>Adapter = createEntityAdapter();
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Export an effect using createAsyncThunk from
|
|
13
|
+
* the Redux Toolkit: https://redux-toolkit.js.org/api/createAsyncThunk
|
|
14
|
+
*
|
|
15
|
+
* e.g.
|
|
16
|
+
* ```
|
|
17
|
+
* import React, { useEffect } from 'react';
|
|
18
|
+
* import { useDispatch } from 'react-redux';
|
|
19
|
+
*
|
|
20
|
+
* // ...
|
|
21
|
+
*
|
|
22
|
+
* const dispatch = useDispatch();
|
|
23
|
+
* useEffect(() => {
|
|
24
|
+
* dispatch(fetch<%= className %>())
|
|
25
|
+
* }, [dispatch]);
|
|
26
|
+
* ```
|
|
27
|
+
*/
|
|
28
|
+
export const fetch<%= className %> = createAsyncThunk(
|
|
29
|
+
'<%= propertyName %>/fetchStatus',
|
|
30
|
+
async (_, thunkAPI) => {
|
|
31
|
+
/**
|
|
32
|
+
* Replace this with your custom fetch call.
|
|
33
|
+
* For example, `return myApi.get<%= className %>s()`;
|
|
34
|
+
* Right now we just return an empty array.
|
|
35
|
+
*/
|
|
36
|
+
return Promise.resolve([]);
|
|
37
|
+
}
|
|
38
|
+
);
|
|
39
|
+
|
|
40
|
+
export const initial<%= className %>State = <%= propertyName %>Adapter.getInitialState({
|
|
41
|
+
loadingStatus: 'not loaded',
|
|
42
|
+
error: null
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
export const <%= propertyName %>Slice = createSlice({
|
|
46
|
+
name: <%= constantName %>_FEATURE_KEY,
|
|
47
|
+
initialState: initial<%= className %>State,
|
|
48
|
+
reducers: {
|
|
49
|
+
add: <%= propertyName %>Adapter.addOne,
|
|
50
|
+
remove: <%= propertyName %>Adapter.removeOne
|
|
51
|
+
// ...
|
|
52
|
+
},
|
|
53
|
+
extraReducers: builder => {
|
|
54
|
+
builder
|
|
55
|
+
.addCase(fetch<%= className %>.pending, (state) => {
|
|
56
|
+
state.loadingStatus = 'loading';
|
|
57
|
+
})
|
|
58
|
+
.addCase(fetch<%= className %>.fulfilled, (state, action) => {
|
|
59
|
+
<%= propertyName %>Adapter.setAll(state, action.payload);
|
|
60
|
+
state.loadingStatus = 'loaded';
|
|
61
|
+
})
|
|
62
|
+
.addCase(fetch<%= className %>.rejected, (state, action) => {
|
|
63
|
+
state.loadingStatus = 'error';
|
|
64
|
+
state.error = action.error.message;
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
/*
|
|
70
|
+
* Export reducer for store configuration.
|
|
71
|
+
*/
|
|
72
|
+
export const <%= propertyName %>Reducer = <%= propertyName %>Slice.reducer;
|
|
73
|
+
|
|
74
|
+
/*
|
|
75
|
+
* Export action creators to be dispatched. For use with the `useDispatch` hook.
|
|
76
|
+
*
|
|
77
|
+
* e.g.
|
|
78
|
+
* ```
|
|
79
|
+
* import React, { useEffect } from 'react';
|
|
80
|
+
* import { useDispatch } from 'react-redux';
|
|
81
|
+
*
|
|
82
|
+
* // ...
|
|
83
|
+
*
|
|
84
|
+
* const dispatch = useDispatch();
|
|
85
|
+
* useEffect(() => {
|
|
86
|
+
* dispatch(<%= propertyName %>Actions.add({ id: 1 }))
|
|
87
|
+
* }, [dispatch]);
|
|
88
|
+
* ```
|
|
89
|
+
*
|
|
90
|
+
* See: https://react-redux.js.org/next/api/hooks#usedispatch
|
|
91
|
+
*/
|
|
92
|
+
export const <%= propertyName %>Actions = <%= propertyName %>Slice.actions;
|
|
93
|
+
|
|
94
|
+
/*
|
|
95
|
+
* Export selectors to query state. For use with the `useSelector` hook.
|
|
96
|
+
*
|
|
97
|
+
* e.g.
|
|
98
|
+
* ```
|
|
99
|
+
* import { useSelector } from 'react-redux';
|
|
100
|
+
*
|
|
101
|
+
* // ...
|
|
102
|
+
*
|
|
103
|
+
* const entities = useSelector(selectAll<%= className %>);
|
|
104
|
+
* ```
|
|
105
|
+
*
|
|
106
|
+
* See: https://react-redux.js.org/next/api/hooks#useselector
|
|
107
|
+
*/
|
|
108
|
+
const { selectAll, selectEntities } = <%= propertyName %>Adapter.getSelectors();
|
|
109
|
+
|
|
110
|
+
export const get<%= className %>State = (rootState) => rootState[<%= constantName %>_FEATURE_KEY];
|
|
111
|
+
|
|
112
|
+
export const selectAll<%= className %> = createSelector(get<%= className %>State, selectAll);
|
|
113
|
+
|
|
114
|
+
export const select<%= className %>Entities = createSelector(get<%= className %>State, selectEntities);
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { fetch<%= className %>, <%= propertyName %>Adapter, <%= propertyName %>Reducer } from './<%= fileName
|
|
1
|
+
import { fetch<%= className %>, <%= propertyName %>Adapter, <%= propertyName %>Reducer } from './<%= fileName %>';
|
|
2
2
|
|
|
3
3
|
describe('<%= propertyName %> reducer', () => {
|
|
4
4
|
it('should handle initial state', () => {
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { fetch<%= className %>, <%= propertyName %>Adapter, <%= propertyName %>Reducer } from './<%= fileName %>';
|
|
2
|
+
|
|
3
|
+
describe('<%= propertyName %> reducer', () => {
|
|
4
|
+
it('should handle initial state', () => {
|
|
5
|
+
const expected = <%= propertyName %>Adapter.getInitialState({
|
|
6
|
+
loadingStatus: 'not loaded',
|
|
7
|
+
error: null
|
|
8
|
+
});
|
|
9
|
+
|
|
10
|
+
expect(<%= propertyName %>Reducer(undefined, { type: '' })).toEqual(expected);
|
|
11
|
+
});
|
|
12
|
+
|
|
13
|
+
it('should handle fetch<%= className %>', () => {
|
|
14
|
+
let state = <%= propertyName %>Reducer(
|
|
15
|
+
undefined,
|
|
16
|
+
fetch<%= className %>.pending('')
|
|
17
|
+
);
|
|
18
|
+
|
|
19
|
+
expect(state).toEqual(
|
|
20
|
+
expect.objectContaining({
|
|
21
|
+
loadingStatus: 'loading',
|
|
22
|
+
error: null,
|
|
23
|
+
entities: {},
|
|
24
|
+
ids: []
|
|
25
|
+
})
|
|
26
|
+
);
|
|
27
|
+
|
|
28
|
+
state = <%= propertyName %>Reducer(
|
|
29
|
+
state,
|
|
30
|
+
fetch<%= className %>.fulfilled([{ id: 1 }], '')
|
|
31
|
+
);
|
|
32
|
+
|
|
33
|
+
expect(state).toEqual(
|
|
34
|
+
expect.objectContaining({
|
|
35
|
+
loadingStatus: 'loaded',
|
|
36
|
+
error: null,
|
|
37
|
+
entities: { 1: { id: 1 } },
|
|
38
|
+
ids: [1]
|
|
39
|
+
})
|
|
40
|
+
);
|
|
41
|
+
|
|
42
|
+
state = <%= propertyName %>Reducer(
|
|
43
|
+
state,
|
|
44
|
+
fetch<%= className %>.rejected(new Error('Uh oh'), '')
|
|
45
|
+
);
|
|
46
|
+
|
|
47
|
+
expect(state).toEqual(
|
|
48
|
+
expect.objectContaining({
|
|
49
|
+
loadingStatus: 'error',
|
|
50
|
+
error: 'Uh oh',
|
|
51
|
+
entities: { 1: { id: 1 } },
|
|
52
|
+
ids: [1]
|
|
53
|
+
})
|
|
54
|
+
);
|
|
55
|
+
});
|
|
56
|
+
});
|
|
@@ -20,13 +20,10 @@ async function reduxGenerator(host, schema) {
|
|
|
20
20
|
return installTask;
|
|
21
21
|
}
|
|
22
22
|
function generateReduxFiles(host, options) {
|
|
23
|
-
(0, devkit_1.generateFiles)(host, (0, devkit_1.joinPathFragments)(__dirname, '
|
|
23
|
+
(0, devkit_1.generateFiles)(host, (0, devkit_1.joinPathFragments)(__dirname, 'files', options.fileExtensionType), options.projectDirectory, {
|
|
24
24
|
...options,
|
|
25
|
-
|
|
25
|
+
ext: options.fileExtension,
|
|
26
26
|
});
|
|
27
|
-
if (options.js) {
|
|
28
|
-
(0, devkit_1.toJS)(host);
|
|
29
|
-
}
|
|
30
27
|
}
|
|
31
28
|
function addReduxPackageDependencies(host) {
|
|
32
29
|
return (0, devkit_1.addDependenciesToPackageJson)(host, {
|
|
@@ -38,16 +35,17 @@ function addExportsToBarrel(host, options) {
|
|
|
38
35
|
if (!tsModule) {
|
|
39
36
|
tsModule = (0, ensure_typescript_1.ensureTypescript)();
|
|
40
37
|
}
|
|
41
|
-
const indexFilePath =
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
const indexSourceFile = tsModule.createSourceFile(indexFilePath, indexSource, tsModule.ScriptTarget.Latest, true);
|
|
45
|
-
const statePath = options.path
|
|
46
|
-
? `./lib/${options.path}/${options.fileName}`
|
|
47
|
-
: `./lib/${options.fileName}`;
|
|
48
|
-
const changes = (0, devkit_1.applyChangesToString)(indexSource, (0, ast_utils_1.addImport)(indexSourceFile, `export * from '${statePath}.slice';`));
|
|
49
|
-
host.write(indexFilePath, changes);
|
|
38
|
+
const indexFilePath = (0, devkit_1.joinPathFragments)(options.projectSourcePath, options.fileExtensionType === 'js' ? 'index.js' : 'index.ts');
|
|
39
|
+
if (!host.exists(indexFilePath)) {
|
|
40
|
+
return;
|
|
50
41
|
}
|
|
42
|
+
const indexSource = host.read(indexFilePath, 'utf-8');
|
|
43
|
+
const indexSourceFile = tsModule.createSourceFile(indexFilePath, indexSource, tsModule.ScriptTarget.Latest, true);
|
|
44
|
+
const statePath = options.path
|
|
45
|
+
? `./lib/${options.path}/${options.fileName}`
|
|
46
|
+
: `./lib/${options.fileName}`;
|
|
47
|
+
const changes = (0, devkit_1.applyChangesToString)(indexSource, (0, ast_utils_1.addImport)(indexSourceFile, `export * from '${statePath}.slice';`));
|
|
48
|
+
host.write(indexFilePath, changes);
|
|
51
49
|
}
|
|
52
50
|
function addStoreConfiguration(host, options) {
|
|
53
51
|
if (!options.appProjectSourcePath) {
|
|
@@ -77,10 +75,13 @@ function updateReducerConfiguration(host, options) {
|
|
|
77
75
|
host.write(options.appMainFilePath, changes);
|
|
78
76
|
}
|
|
79
77
|
async function normalizeOptions(host, options) {
|
|
80
|
-
const { artifactName: name, directory, fileName, project: projectName, } = await (0, artifact_name_and_directory_utils_1.determineArtifactNameAndDirectoryOptions)(host, {
|
|
78
|
+
const { artifactName: name, directory, fileName, fileExtension, fileExtensionType, project: projectName, } = await (0, artifact_name_and_directory_utils_1.determineArtifactNameAndDirectoryOptions)(host, {
|
|
81
79
|
path: options.path,
|
|
82
80
|
name: options.name,
|
|
83
|
-
|
|
81
|
+
suffix: 'slice',
|
|
82
|
+
allowedFileExtensions: ['js', 'ts'],
|
|
83
|
+
fileExtension: options.js ? 'js' : 'ts',
|
|
84
|
+
js: options.js,
|
|
84
85
|
});
|
|
85
86
|
let appProjectSourcePath;
|
|
86
87
|
let appMainFilePath;
|
|
@@ -117,6 +118,8 @@ async function normalizeOptions(host, options) {
|
|
|
117
118
|
...options,
|
|
118
119
|
...extraNames,
|
|
119
120
|
fileName,
|
|
121
|
+
fileExtension,
|
|
122
|
+
fileExtensionType,
|
|
120
123
|
constantName: (0, devkit_1.names)(name).constantName.toUpperCase(),
|
|
121
124
|
projectDirectory: directory,
|
|
122
125
|
projectType,
|
|
@@ -1,11 +1,17 @@
|
|
|
1
|
+
import type { FileExtensionType } from '@nx/devkit/src/generators/artifact-name-and-directory-utils';
|
|
2
|
+
|
|
1
3
|
export interface Schema {
|
|
2
4
|
path: string;
|
|
3
5
|
name?: string;
|
|
4
6
|
appProject?: string;
|
|
5
|
-
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* @deprecated Provide the full file path including the file extension in the `path` option. This option will be removed in Nx v21.
|
|
10
|
+
*/
|
|
11
|
+
js?: boolean;
|
|
6
12
|
}
|
|
7
13
|
|
|
8
|
-
interface NormalizedSchema extends Schema {
|
|
14
|
+
interface NormalizedSchema extends Omit<Schema, 'js'> {
|
|
9
15
|
projectType: string;
|
|
10
16
|
projectDirectory: string;
|
|
11
17
|
projectSourcePath: string;
|
|
@@ -16,4 +22,6 @@ interface NormalizedSchema extends Schema {
|
|
|
16
22
|
constantName: string;
|
|
17
23
|
propertyName: string;
|
|
18
24
|
fileName: string;
|
|
25
|
+
fileExtension: string;
|
|
26
|
+
fileExtensionType: FileExtensionType;
|
|
19
27
|
}
|