Skip to content

Commit 578f9e8

Browse files
authored
Merge pull request #55 from TouK/dev
dev > master
2 parents 92ce207 + 0ed826a commit 578f9e8

File tree

12 files changed

+2903
-2692
lines changed

12 files changed

+2903
-2692
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ jobs:
5252
key: ${{ runner.os }}-${{ hashFiles('**/package-lock.json') }}
5353
- uses: actions/setup-node@v1
5454
with:
55-
node-version: 12
55+
node-version: 14
5656
- run: npm ci
5757
- run: npm run build --if-present
5858
- name: Release

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
.idea
22
node_modules
3+
/test-results/

.npmrc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
engine-strict=true
2+
legacy-peer-deps=true

.nvmrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
v16.15.1

CHANGELOG.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,15 @@
1+
# [1.2.0-beta.1](https://github.com/touk/federated-types/compare/v1.1.0...v1.2.0-beta.1) (2022-10-31)
2+
3+
4+
### Bug Fixes
5+
6+
* npm audit fix ([e95a125](https://github.com/touk/federated-types/commit/e95a125541584a95038d209f285ef28ea60a38d2))
7+
8+
9+
### Features
10+
11+
* "--saveToNodeModules" param ([8ca1214](https://github.com/touk/federated-types/commit/8ca1214759cc2659e546c5e99d3121374947be4b))
12+
113
# [1.1.0](https://github.com/touk/federated-types/compare/v1.0.0...v1.1.0) (2021-08-02)
214

315

README.md

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,10 @@ npm install @touk/federated-types
1414

1515
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:
1616

17-
#### `federation.config.json`
1817

1918
```json
19+
//federation.config.json
20+
2021
{
2122
"name": "app2",
2223
"exposes": {
@@ -27,9 +28,9 @@ You'll also need to place a `federation.config.json` in each package being feder
2728

2829
It's recommended that you spread these properties into your ModuleFederationPlugin configuration, like so:
2930

30-
#### `webpack.config.js`
3131

3232
```javascript
33+
//webpack.config.js
3334

3435
const deps = require('../package.json').dependencies;
3536
const federationConfig = require('./federation.config.json');
@@ -54,9 +55,10 @@ module.exports = {
5455

5556
Then you can call `make-federated-types` from your `scripts` block in your package's `package.json` file:
5657

57-
#### `package.json`
5858

59-
```json
59+
```javascript
60+
//package.json
61+
6062
scripts: {
6163
"make-types": "make-federated-types"
6264
}
@@ -66,19 +68,21 @@ This will write new package to the `node_modules/@types/__federated_types` in yo
6668

6769
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:
6870

69-
#### `package.json`
7071

71-
```json
72+
```javascript
73+
//package.json
74+
7275
scripts: {
7376
"make-types": "make-federated-types --outputDir ../../my_types/"
7477
}
7578
```
76-
77-
#### `package.json`
79+
You can add an `--saveToNodeModules` parameter to save in both places.
7880

7981
If you would like to specify custom path to the config, you can pass `--config` parameter like so:
8082

81-
```json
83+
```javascript
84+
//package.json
85+
8286
scripts: {
8387
"make-types": "make-federated-types --config ./path/to/my/config.json --outputDir ../../my_types/"
8488
}

cli.js

Lines changed: 42 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ const findNodeModules = require('find-node-modules');
66
const ts = require('typescript');
77

88
const formatHost = {
9-
getCurrentDirectory: ts.sys.getCurrentDirectory,
10-
getNewLine: () => ts.sys.newLine
9+
getCurrentDirectory: ts.sys.getCurrentDirectory,
10+
getNewLine: () => ts.sys.newLine
1111
};
1212

1313
function reportDiagnostic(diagnostic) {
@@ -16,16 +16,26 @@ function reportDiagnostic(diagnostic) {
1616

1717
const [nodeModules] = findNodeModules({ cwd: process.argv[1], relative: false });
1818

19+
const hasArg = (argName) => {
20+
const argIndex = process.argv.indexOf(argName);
21+
return argIndex !== -1;
22+
};
1923
const getArg = (argName) => {
2024
const argIndex = process.argv.indexOf(argName);
2125
return argIndex !== -1 ? process.argv[argIndex + 1] : null;
2226
};
2327

28+
const nodeModulesOutputDir = path.resolve(nodeModules, '@types/__federated_types/');
29+
const saveToNodeMoulesArg = hasArg('--saveToNodeModules');
2430
const outDirArg = getArg('--outputDir');
2531

2632
const outputDir = outDirArg
2733
? path.resolve('./', outDirArg)
28-
: path.resolve(nodeModules, '@types/__federated_types/');
34+
: nodeModulesOutputDir;
35+
36+
const outputDirs = outputDir !== nodeModulesOutputDir && saveToNodeMoulesArg
37+
? [nodeModulesOutputDir, outputDir]
38+
: [outputDir];
2939

3040
const configPathArg = getArg('--config');
3141
const configPath = configPathArg
@@ -69,14 +79,14 @@ console.log(`Using config file: ${federationConfigPath}`);
6979
const federationConfig = require(federationConfigPath);
7080
const compileFiles = Object.values(federationConfig.exposes);
7181
const compileKeys = Object.keys(federationConfig.exposes);
72-
const outFile = path.resolve(outputDir, `${federationConfig.name}.d.ts`);
7382

7483
function getModuleDeclareName(exposeName) {
7584
// windows paths 🤦
7685
return path.join(federationConfig.name, exposeName).replace(/[\\/]/g, '/');
7786
}
7887

7988
try {
89+
const outFile = path.resolve(outputDir, `${federationConfig.name}.d.ts`);
8090
if (fs.existsSync(outFile)) {
8191
fs.unlinkSync(outFile);
8292
}
@@ -128,39 +138,40 @@ try {
128138
].join('\n');
129139
});
130140

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);
132144

133-
fs.writeFileSync(outFile, typing);
145+
fs.writeFileSync(_outFile, typing);
134146

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');
138151

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+
}
144158
}
145-
} else {
146-
console.debug('not writing to node modules, dont need a package.json');
147-
}
148159

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}';`;
163163

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+
});
164175
console.debug('Success!');
165176
} catch (e) {
166177
console.error(`ERROR:`, e);

0 commit comments

Comments
 (0)