Skip to content
This repository was archived by the owner on Apr 8, 2020. It is now read-only.

Commit 117c1a6

Browse files
Implement workaround for #1066
1 parent 53f5a77 commit 117c1a6

File tree

4 files changed

+84
-4
lines changed

4 files changed

+84
-4
lines changed

src/Microsoft.AspNetCore.SpaServices/npm/aspnet-webpack-react/package.json

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
{
22
"name": "aspnet-webpack-react",
3-
"version": "2.0.0",
3+
"version": "2.0.1",
44
"description": "Helpers for using Webpack with React in ASP.NET Core projects. Works in conjunction with the Microsoft.AspNetCore.SpaServices NuGet package.",
55
"main": "index.js",
66
"scripts": {
77
"prepublish": "rimraf *.d.ts && tsc && echo 'Finished building NPM package \"aspnet-webpack-react\"'",
8-
"test": "echo \"Error: no test specified\" && exit 1"
8+
"test": "echo \"Error: no test specified\" && exit 1",
9+
"postinstall": "node scripts/postinstall.js"
910
},
1011
"author": "Microsoft",
1112
"license": "Apache-2.0",
@@ -16,8 +17,12 @@
1617
"type": "git",
1718
"url": "https://github.com/aspnet/JavaScriptServices.git"
1819
},
20+
"dependencies": {
21+
"@types/webpack": "2.2.15",
22+
"hjson": "2.4.3"
23+
},
1924
"devDependencies": {
20-
"@types/webpack": "^2.2.0",
25+
"@types/react": "15.0.29",
2126
"rimraf": "^2.5.4",
2227
"typescript": "^2.0.0",
2328
"webpack": "^2.2.0"
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
var fs = require('fs');
2+
var path = require('path');
3+
var Hjson = require('hjson');
4+
5+
// This logic is a workaround for #1066.
6+
// See the comment in index.ts for details.
7+
8+
function findInDirOrAncestor(targetFilename, rootDir) {
9+
var candidateFilename = path.join(rootDir, targetFilename);
10+
if (fs.existsSync(candidateFilename)) {
11+
return candidateFilename;
12+
}
13+
14+
var parentDir = path.join(rootDir, '..');
15+
return parentDir !== rootDir ? findInDirOrAncestor(targetFilename, parentDir) : null;
16+
}
17+
18+
function findTsConfigFile() {
19+
var rootDir = path.join(__dirname, '..', '..'); // Start 2 levels up because this package has a tsconfig file of its own
20+
var tsConfigFile = 'tsconfig.json';
21+
var tsConfigFileName = findInDirOrAncestor(tsConfigFile, rootDir);
22+
if (!tsConfigFileName) {
23+
console.error('Could not locate ' + tsConfigFile + ' in ' + rootDir + ' or any ancestor directory.');
24+
}
25+
return tsConfigFileName;
26+
}
27+
28+
function ensureTsConfigContainsTypesEntry(packageName) {
29+
var tsConfigFileName = findTsConfigFile();
30+
if (tsConfigFileName) {
31+
var parsedTsConfig = Hjson.rt.parse(fs.readFileSync(tsConfigFileName, 'utf8'));
32+
parsedTsConfig.compilerOptions = parsedTsConfig.compilerOptions || {};
33+
parsedTsConfig.compilerOptions.types = parsedTsConfig.compilerOptions.types || [];
34+
35+
if (parsedTsConfig.compilerOptions.types.indexOf(packageName) < 0) {
36+
parsedTsConfig.compilerOptions.types.push(packageName);
37+
38+
var hjsonOptions = {
39+
bracesSameLine: true,
40+
multiline: 'off',
41+
quotes: 'all',
42+
separator: true,
43+
space: 2
44+
};
45+
fs.writeFileSync(tsConfigFileName, Hjson.rt.stringify(parsedTsConfig, hjsonOptions), 'utf8');
46+
}
47+
}
48+
}
49+
50+
try {
51+
ensureTsConfigContainsTypesEntry('aspnet-webpack-react');
52+
} catch(ex) {
53+
console.error(ex);
54+
process.exit(0); // Don't break installation
55+
}

src/Microsoft.AspNetCore.SpaServices/npm/aspnet-webpack-react/src/index.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,23 @@ export { addReactHotModuleReplacementConfig } from './HotModuleReplacement';
44
// compatibility with aspnet-webpack 1.x. In aspnet-webpack 2.0, we can drop the old name (and also deprecate
55
// some other no-longer-supported functionality, such as LoadViaWebpack).
66
export { addReactHotModuleReplacementConfig as addReactHotModuleReplacementBabelTransform } from './HotModuleReplacement';
7+
8+
// Workaround for #1066
9+
//
10+
// The issue is that @types/[email protected] is incompatible with @types/[email protected]
11+
// This is a problem because the ReactReduxSpa template that ships in 2.0.0-preview2 is pinned
12+
// to @types/[email protected] but does *not* declare a direct dependency on @types/react-router,
13+
// which means we end up grabbing the latest @types/react-router.
14+
//
15+
// The temporary solution is for aspnet-webpack-react to add the following extra type information
16+
// that patches the compatibility issue. The longer-term solution will be for the templates to
17+
// pin versions of *every* package in the transitive closure, not just their direct dependencies.
18+
//
19+
// Note that for this workaround to work, the developer also needs 'aspnet-webpack-react' to be
20+
// present in the 'types' array in tsconfig.json. We automate the task of adding that in the
21+
// scripts/postinstall.js file in this package. Later, that action can be removed.
22+
23+
import * as React from 'react';
24+
declare module 'react' {
25+
interface Component<P, S={}> {}
26+
}

src/Microsoft.AspNetCore.SpaServices/npm/aspnet-webpack-react/tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"target": "es5",
66
"declaration": true,
77
"outDir": ".",
8-
"lib": ["es2015"]
8+
"lib": ["dom", "es2015"]
99
},
1010
"files": [
1111
"src/index.ts"

0 commit comments

Comments
 (0)