@touk/federated-types 1.1.0 → 1.2.0-beta.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +13 -9
- package/cli.js +43 -31
- package/package.json +27 -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
|
-
```
|
|
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
|
-
```
|
|
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
|
-
```
|
|
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
|
-
|
|
10
|
-
|
|
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
|
-
:
|
|
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,41 @@ try {
|
|
|
128
138
|
].join('\n');
|
|
129
139
|
});
|
|
130
140
|
|
|
131
|
-
|
|
141
|
+
outputDirs.forEach(_outputDir => {
|
|
142
|
+
const _outFile = path.resolve(_outputDir, `${federationConfig.name}.d.ts`);
|
|
132
143
|
|
|
133
|
-
|
|
144
|
+
console.log('writing typing file:', _outFile);
|
|
145
|
+
fs.mkdirSync(path.dirname(_outFile), { recursive: true });
|
|
146
|
+
fs.writeFileSync(_outFile, typing);
|
|
134
147
|
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
148
|
+
console.debug(`using output dir: ${_outputDir}`);
|
|
149
|
+
// if we are writing to the node_modules/@types directory, add a package.json file
|
|
150
|
+
if (_outputDir.includes(path.join('node_modules', '@types'))) {
|
|
151
|
+
const packageJsonPath = path.resolve(_outputDir, 'package.json');
|
|
138
152
|
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
153
|
+
if (!fs.existsSync(packageJsonPath)) {
|
|
154
|
+
console.debug('writing package.json:', packageJsonPath);
|
|
155
|
+
fs.copyFileSync(path.resolve(__dirname, 'typings.package.tmpl.json'), packageJsonPath);
|
|
156
|
+
} else {
|
|
157
|
+
console.debug(packageJsonPath, 'already exists');
|
|
158
|
+
}
|
|
144
159
|
}
|
|
145
|
-
} else {
|
|
146
|
-
console.debug('not writing to node modules, dont need a package.json');
|
|
147
|
-
}
|
|
148
160
|
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
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
|
-
}
|
|
161
|
+
// write/update the index.d.ts file
|
|
162
|
+
const indexPath = path.resolve(_outputDir, 'index.d.ts');
|
|
163
|
+
const importStatement = `export * from './${federationConfig.name}';`;
|
|
163
164
|
|
|
165
|
+
if (!fs.existsSync(indexPath)) {
|
|
166
|
+
console.log('creating index.d.ts file');
|
|
167
|
+
fs.writeFileSync(indexPath, `${importStatement}\n`);
|
|
168
|
+
} else {
|
|
169
|
+
console.log('updating index.d.ts file');
|
|
170
|
+
const contents = fs.readFileSync(indexPath);
|
|
171
|
+
if (!contents.includes(importStatement)) {
|
|
172
|
+
fs.writeFileSync(indexPath, `${contents}${importStatement}\n`);
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
});
|
|
164
176
|
console.debug('Success!');
|
|
165
177
|
} catch (e) {
|
|
166
178
|
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",
|
|
4
3
|
"description": "share typings for your federated typescript packages around your mono-repo",
|
|
5
|
-
"
|
|
6
|
-
"scripts": {
|
|
7
|
-
"test": "echo \"Error: no test specified\" && exit 0",
|
|
8
|
-
"semantic-release": "semantic-release"
|
|
9
|
-
},
|
|
4
|
+
"version": "1.2.0-beta.2",
|
|
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
|
-
"
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
12
|
+
"dependencies": {
|
|
13
|
+
"find-node-modules": "2.1.3"
|
|
14
|
+
},
|
|
15
|
+
"devDependencies": {
|
|
16
|
+
"@commitlint/cli": "17.1.2",
|
|
17
|
+
"@commitlint/config-conventional": "13.2.0",
|
|
18
|
+
"@semantic-release/changelog": "6.0.1",
|
|
19
|
+
"@semantic-release/git": "10.0.1",
|
|
20
|
+
"husky": "8.0.1",
|
|
21
|
+
"prettier": "2.7.1",
|
|
22
|
+
"semantic-release": "19.0.5"
|
|
18
23
|
},
|
|
19
24
|
"files": [
|
|
20
25
|
"cli.js",
|
|
@@ -22,26 +27,21 @@
|
|
|
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
|
-
"
|
|
30
|
-
|
|
31
|
-
},
|
|
34
|
+
"license": "MIT",
|
|
35
|
+
"main": "cli.js",
|
|
32
36
|
"peerDependencies": {
|
|
33
|
-
"typescript": "
|
|
37
|
+
"typescript": ">4.0.0"
|
|
34
38
|
},
|
|
35
|
-
"
|
|
36
|
-
|
|
37
|
-
"
|
|
38
|
-
"
|
|
39
|
-
"@semantic-release/git": "^9.0.0",
|
|
40
|
-
"husky": "^7.0.1",
|
|
41
|
-
"prettier": "^2.1.2",
|
|
42
|
-
"semantic-release": "^17.4.4"
|
|
39
|
+
"repository": "touk/federated-types.git",
|
|
40
|
+
"scripts": {
|
|
41
|
+
"semantic-release": "semantic-release",
|
|
42
|
+
"test": "npm run make-types -workspaces --if-present"
|
|
43
43
|
},
|
|
44
|
-
"
|
|
45
|
-
"
|
|
46
|
-
|
|
44
|
+
"workspaces": [
|
|
45
|
+
"./packages/*"
|
|
46
|
+
]
|
|
47
47
|
}
|