@touk/federated-types 1.1.0-beta.1 → 1.2.0-beta.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/README.md +13 -9
  2. package/cli.js +42 -31
  3. package/package.json +24 -27
package/README.md CHANGED
@@ -14,9 +14,10 @@ npm install @touk/federated-types
14
14
 
15
15
  You'll also need to place a `federation.config.json` in each package being federated. It will contain the remote name and exported members. These properties are used in Webpack's `ModuleFederationPlugin` configuration object. An example:
16
16
 
17
- #### `federation.config.json`
18
17
 
19
18
  ```json
19
+ //federation.config.json
20
+
20
21
  {
21
22
  "name": "app2",
22
23
  "exposes": {
@@ -27,9 +28,9 @@ You'll also need to place a `federation.config.json` in each package being feder
27
28
 
28
29
  It's recommended that you spread these properties into your ModuleFederationPlugin configuration, like so:
29
30
 
30
- #### `webpack.config.js`
31
31
 
32
32
  ```javascript
33
+ //webpack.config.js
33
34
 
34
35
  const deps = require('../package.json').dependencies;
35
36
  const federationConfig = require('./federation.config.json');
@@ -54,9 +55,10 @@ module.exports = {
54
55
 
55
56
  Then you can call `make-federated-types` from your `scripts` block in your package's `package.json` file:
56
57
 
57
- #### `package.json`
58
58
 
59
- ```json
59
+ ```javascript
60
+ //package.json
61
+
60
62
  scripts: {
61
63
  "make-types": "make-federated-types"
62
64
  }
@@ -66,19 +68,21 @@ This will write new package to the `node_modules/@types/__federated_types` in yo
66
68
 
67
69
  If you would rather specify a directory in which to write the typing files, you can pass an `--outputDir` parameter to the command like so:
68
70
 
69
- #### `package.json`
70
71
 
71
- ```json
72
+ ```javascript
73
+ //package.json
74
+
72
75
  scripts: {
73
76
  "make-types": "make-federated-types --outputDir ../../my_types/"
74
77
  }
75
78
  ```
76
-
77
- #### `package.json`
79
+ You can add an `--saveToNodeModules` parameter to save in both places.
78
80
 
79
81
  If you would like to specify custom path to the config, you can pass `--config` parameter like so:
80
82
 
81
- ```json
83
+ ```javascript
84
+ //package.json
85
+
82
86
  scripts: {
83
87
  "make-types": "make-federated-types --config ./path/to/my/config.json --outputDir ../../my_types/"
84
88
  }
package/cli.js CHANGED
@@ -6,8 +6,8 @@ const findNodeModules = require('find-node-modules');
6
6
  const ts = require('typescript');
7
7
 
8
8
  const formatHost = {
9
- getCurrentDirectory: ts.sys.getCurrentDirectory,
10
- getNewLine: () => ts.sys.newLine
9
+ getCurrentDirectory: ts.sys.getCurrentDirectory,
10
+ getNewLine: () => ts.sys.newLine
11
11
  };
12
12
 
13
13
  function reportDiagnostic(diagnostic) {
@@ -16,16 +16,26 @@ function reportDiagnostic(diagnostic) {
16
16
 
17
17
  const [nodeModules] = findNodeModules({ cwd: process.argv[1], relative: false });
18
18
 
19
+ const hasArg = (argName) => {
20
+ const argIndex = process.argv.indexOf(argName);
21
+ return argIndex !== -1;
22
+ };
19
23
  const getArg = (argName) => {
20
24
  const argIndex = process.argv.indexOf(argName);
21
25
  return argIndex !== -1 ? process.argv[argIndex + 1] : null;
22
26
  };
23
27
 
28
+ const nodeModulesOutputDir = path.resolve(nodeModules, '@types/__federated_types/');
29
+ const saveToNodeMoulesArg = hasArg('--saveToNodeModules');
24
30
  const outDirArg = getArg('--outputDir');
25
31
 
26
32
  const outputDir = outDirArg
27
33
  ? path.resolve('./', outDirArg)
28
- : path.resolve(nodeModules, '@types/__federated_types/');
34
+ : nodeModulesOutputDir;
35
+
36
+ const outputDirs = outputDir !== nodeModulesOutputDir && saveToNodeMoulesArg
37
+ ? [nodeModulesOutputDir, outputDir]
38
+ : [outputDir];
29
39
 
30
40
  const configPathArg = getArg('--config');
31
41
  const configPath = configPathArg
@@ -69,7 +79,6 @@ console.log(`Using config file: ${federationConfigPath}`);
69
79
  const federationConfig = require(federationConfigPath);
70
80
  const compileFiles = Object.values(federationConfig.exposes);
71
81
  const compileKeys = Object.keys(federationConfig.exposes);
72
- const outFile = path.resolve(outputDir, `${federationConfig.name}.d.ts`);
73
82
 
74
83
  function getModuleDeclareName(exposeName) {
75
84
  // windows paths 🤦
@@ -77,6 +86,7 @@ function getModuleDeclareName(exposeName) {
77
86
  }
78
87
 
79
88
  try {
89
+ const outFile = path.resolve(outputDir, `${federationConfig.name}.d.ts`);
80
90
  if (fs.existsSync(outFile)) {
81
91
  fs.unlinkSync(outFile);
82
92
  }
@@ -128,39 +138,40 @@ try {
128
138
  ].join('\n');
129
139
  });
130
140
 
131
- console.log('writing typing file:', outFile);
141
+ outputDirs.forEach(_outputDir => {
142
+ const _outFile = path.resolve(_outputDir, `${federationConfig.name}.d.ts`);
143
+ console.log('writing typing file:', _outFile);
132
144
 
133
- fs.writeFileSync(outFile, typing);
145
+ fs.writeFileSync(_outFile, typing);
134
146
 
135
- // if we are writing to the node_modules/@types directory, add a package.json file
136
- if (outputDir.includes(path.join('node_modules', '@types'))) {
137
- const packageJsonPath = path.resolve(outputDir, 'package.json');
147
+ console.debug(`using output dir: ${_outputDir}`);
148
+ // if we are writing to the node_modules/@types directory, add a package.json file
149
+ if (_outputDir.includes(path.join('node_modules', '@types'))) {
150
+ const packageJsonPath = path.resolve(_outputDir, 'package.json');
138
151
 
139
- if (!fs.existsSync(packageJsonPath)) {
140
- console.debug('writing package.json:', packageJsonPath);
141
- fs.copyFileSync(path.resolve(__dirname, 'typings.package.tmpl.json'), packageJsonPath);
142
- } else {
143
- console.debug(packageJsonPath, 'already exists');
152
+ if (!fs.existsSync(packageJsonPath)) {
153
+ console.debug('writing package.json:', packageJsonPath);
154
+ fs.copyFileSync(path.resolve(__dirname, 'typings.package.tmpl.json'), packageJsonPath);
155
+ } else {
156
+ console.debug(packageJsonPath, 'already exists');
157
+ }
144
158
  }
145
- } else {
146
- console.debug('not writing to node modules, dont need a package.json');
147
- }
148
159
 
149
- // write/update the index.d.ts file
150
- const indexPath = path.resolve(outputDir, 'index.d.ts');
151
- const importStatement = `export * from './${federationConfig.name}';`;
152
-
153
- if (!fs.existsSync(indexPath)) {
154
- console.log('creating index.d.ts file');
155
- fs.writeFileSync(indexPath, `${importStatement}\n`);
156
- } else {
157
- console.log('updating index.d.ts file');
158
- const contents = fs.readFileSync(indexPath);
159
- if (!contents.includes(importStatement)) {
160
- fs.writeFileSync(indexPath, `${contents}${importStatement}\n`);
161
- }
162
- }
160
+ // write/update the index.d.ts file
161
+ const indexPath = path.resolve(_outputDir, 'index.d.ts');
162
+ const importStatement = `export * from './${federationConfig.name}';`;
163
163
 
164
+ if (!fs.existsSync(indexPath)) {
165
+ console.log('creating index.d.ts file');
166
+ fs.writeFileSync(indexPath, `${importStatement}\n`);
167
+ } else {
168
+ console.log('updating index.d.ts file');
169
+ const contents = fs.readFileSync(indexPath);
170
+ if (!contents.includes(importStatement)) {
171
+ fs.writeFileSync(indexPath, `${contents}${importStatement}\n`);
172
+ }
173
+ }
174
+ });
164
175
  console.debug('Success!');
165
176
  } catch (e) {
166
177
  console.error(`ERROR:`, e);
package/package.json CHANGED
@@ -1,20 +1,25 @@
1
1
  {
2
2
  "name": "@touk/federated-types",
3
- "version": "1.1.0-beta.1",
4
3
  "description": "share typings for your federated typescript packages around your mono-repo",
5
- "main": "cli.js",
6
- "scripts": {
7
- "test": "echo \"Error: no test specified\" && exit 0",
8
- "semantic-release": "semantic-release"
9
- },
4
+ "version": "1.2.0-beta.1",
10
5
  "author": "Julian Wielga <j@touk.pl>",
6
+ "bin": {
7
+ "make-federated-types": "./cli.js"
8
+ },
11
9
  "contributors": [
12
10
  "Jonathan Greene <jgreene@pixability.com>"
13
11
  ],
14
- "license": "MIT",
15
- "repository": {
16
- "type": "git",
17
- "url": "https://github.com/touk/federated-types.git"
12
+ "dependencies": {
13
+ "find-node-modules": "2.1.3"
14
+ },
15
+ "devDependencies": {
16
+ "@commitlint/cli": "13.2.1",
17
+ "@commitlint/config-conventional": "13.2.0",
18
+ "@semantic-release/changelog": "5.0.1",
19
+ "@semantic-release/git": "9.0.1",
20
+ "husky": "7.0.4",
21
+ "prettier": "2.7.1",
22
+ "semantic-release": "17.4.7"
18
23
  },
19
24
  "files": [
20
25
  "cli.js",
@@ -22,26 +27,18 @@
22
27
  ],
23
28
  "keywords": [
24
29
  "module-federation",
30
+ "mono-repo",
25
31
  "typescript",
26
- "typings",
27
- "mono-repo"
32
+ "typings"
28
33
  ],
29
- "bin": {
30
- "make-federated-types": "./cli.js"
31
- },
34
+ "license": "MIT",
35
+ "main": "cli.js",
32
36
  "peerDependencies": {
33
- "typescript": "^4.0.5"
37
+ "typescript": ">4.0.0"
34
38
  },
35
- "devDependencies": {
36
- "@commitlint/cli": "^13.1.0",
37
- "@commitlint/config-conventional": "^13.1.0",
38
- "@semantic-release/changelog": "^5.0.1",
39
- "@semantic-release/git": "^9.0.0",
40
- "husky": "^7.0.1",
41
- "prettier": "^2.1.2",
42
- "semantic-release": "^17.4.4"
43
- },
44
- "dependencies": {
45
- "find-node-modules": "^2.1.2"
39
+ "repository": "touk/federated-types.git",
40
+ "scripts": {
41
+ "semantic-release": "semantic-release",
42
+ "test": "echo \"Error: no test specified\" && exit 0"
46
43
  }
47
44
  }