-
-
Notifications
You must be signed in to change notification settings - Fork 58
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Output typescript declarations during build. Resolves #289. #316
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
--- | ||
"generator-single-spa": major | ||
"ts-config-single-spa": major | ||
--- | ||
|
||
For typescript projects, automatically emit types during `build`. | ||
|
||
# Migrating | ||
|
||
The create-single-spa api for generating new typescript projects did not change. However, to upgrade existing projects, do the following: | ||
|
||
1. Update your package.json. Make sure to replace `<%= packageManager %>` with either `npm`, `yarn`, or `pnpm` | ||
|
||
```diff | ||
{ | ||
"scripts": { | ||
- "build": "webpack --mode=production", | ||
+ "build": "concurrently <%= packageManager %>:build:*", | ||
+ "build:webpack": "webpack --mode=production", | ||
+ "build:types": "tsc" | ||
} | ||
} | ||
``` | ||
|
||
2. Update your tsconfig.json. Make sure to replace `<%= mainFile %>` with the proper value. This is in the format `org-project.ts`. React projects should have the `.tsx` file extension | ||
|
||
```diff | ||
{ | ||
"compilerOptions": { | ||
+ "declarationDir": "dist" | ||
}, | ||
+ "files": ["src/<%= mainFile %>"] | ||
- "include": ["src/**/*", "node_modules/@types"], | ||
+ "include": ["src/**/*"] | ||
} | ||
``` | ||
|
||
3. Add the `"types"` property to your package.json: | ||
|
||
```diff | ||
{ | ||
+ "types": "dist/<%= mainFile %>.d.ts" | ||
} | ||
``` | ||
|
||
4. Upgrade `ts-config-single-spa` to the latest 3.x release, which has new configuration for emitting types. | ||
|
||
```sh | ||
npm install --save-dev ts-config-single-spa@^3.0.0 | ||
|
||
pnpm install --save-dev ts-config-single-spa@^3.0.0 | ||
|
||
yarn add --dev ts-config-single-spa@^3.0.0 | ||
``` | ||
|
||
5. Now run `npm run build` or `npm run build:types` and verify that a typescript declaration file is outputted to your `dist` directory. Verify that the output file name is the same as the `"types"` property in your package.json. |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -58,6 +58,9 @@ module.exports = class SingleSpaReactGenerator extends PnpmGenerator { | |
Object.assign(this.options, answers, { framework: "react" }); | ||
} | ||
async createPackageJson() { | ||
this.srcFileExtension = this.options.typescript ? "tsx" : "js"; | ||
this.mainFile = `src/${this.options.orgName}-${this.options.projectName}.${this.srcFileExtension}`; | ||
|
||
const packageJsonTemplate = await fs.readFile( | ||
this.templatePath("react.package.json"), | ||
{ encoding: "utf-8" } | ||
|
@@ -66,6 +69,7 @@ module.exports = class SingleSpaReactGenerator extends PnpmGenerator { | |
name: `@${this.options.orgName}/${this.options.projectName}`, | ||
packageManager: this.options.packageManager, | ||
typescript: this.options.typescript, | ||
mainFile: this.mainFile, | ||
}); | ||
|
||
const packageJson = JSON.parse(packageJsonStr); | ||
|
@@ -79,6 +83,8 @@ module.exports = class SingleSpaReactGenerator extends PnpmGenerator { | |
delete packageJson.devDependencies["webpack-config-single-spa"]; | ||
// Will be replaced by webpack-config-single-spa-react-ts | ||
delete packageJson.devDependencies["webpack-config-single-spa-ts"]; | ||
|
||
packageJson.types = `dist/${this.options.orgName}-${this.options.projectName}.d.ts`; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In case people publish the packages to npm, the |
||
} | ||
|
||
this.fs.extendJSON(this.destinationPath("package.json"), packageJson); | ||
|
@@ -119,8 +125,6 @@ module.exports = class SingleSpaReactGenerator extends PnpmGenerator { | |
} | ||
} | ||
async copyOtherFiles() { | ||
const srcFileExtension = this.options.typescript ? "tsx" : "js"; | ||
|
||
this.fs.copyTpl( | ||
this.templatePath("jest.config.js"), | ||
this.destinationPath("jest.config.js"), | ||
|
@@ -168,26 +172,27 @@ module.exports = class SingleSpaReactGenerator extends PnpmGenerator { | |
); | ||
this.fs.copyTpl( | ||
this.templatePath("src/root.component.js"), | ||
this.destinationPath(`src/root.component.${srcFileExtension}`), | ||
this.destinationPath(`src/root.component.${this.srcFileExtension}`), | ||
this.options | ||
); | ||
this.fs.copyTpl( | ||
this.templatePath("src/root.component.test.js"), | ||
this.destinationPath(`src/root.component.test.${srcFileExtension}`), | ||
this.destinationPath(`src/root.component.test.${this.srcFileExtension}`), | ||
this.options | ||
); | ||
this.fs.copyTpl( | ||
this.templatePath("src/main.js"), | ||
this.destinationPath( | ||
`src/${this.options.orgName}-${this.options.projectName}.${srcFileExtension}` | ||
), | ||
this.destinationPath(this.mainFile), | ||
this.options | ||
); | ||
if (this.options.typescript) { | ||
this.fs.copyTpl( | ||
this.templatePath("tsconfig.json"), | ||
this.destinationPath("tsconfig.json"), | ||
this.options | ||
{ | ||
...this.options, | ||
mainFile: this.mainFile, | ||
} | ||
); | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,10 @@ | ||
{ | ||
"extends": "ts-config-single-spa", | ||
"compilerOptions": { | ||
"jsx": "react-jsx" | ||
"jsx": "react-jsx", | ||
"declarationDir": "dist" | ||
}, | ||
"include": ["src/**/*", "node_modules/@types"], | ||
"files": ["<%- mainFile %>"], | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The |
||
"include": ["src/**/*"], | ||
"exclude": ["src/**/*.test*"] | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,9 @@ | ||
{ | ||
"extends": "ts-config-single-spa", | ||
"include": ["src/**/*", "node_modules/@types"], | ||
"files": ["<%- mainFile %>"], | ||
"compilerOptions": { | ||
"declarationDir": "dist" | ||
}, | ||
"include": ["src/**/*"], | ||
"exclude": ["src/**/*.test*"] | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,9 @@ | ||
{ | ||
"extends": "ts-config-single-spa", | ||
"include": ["src/**/*", "node_modules/@types"], | ||
"files": ["<%= mainFile %>"], | ||
"compilerOptions": { | ||
"declarationDir": "dist" | ||
}, | ||
"include": ["src/**/*"], | ||
"exclude": ["src/**/*.test*"] | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,6 +14,7 @@ | |
"es2016.array.include", | ||
"es2018" | ||
], | ||
"noEmit": true | ||
"declaration": true, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These changes are what makes this a breaking change - previously no types emitted, now types are emitted |
||
"emitDeclarationOnly": true | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This single-spa-react types rely on the single-spa types, so I added this dependency. This file is used by more than just react applications, but I figure having the single-spa library and its types available is not a bad idea for util modules / root configs, as well