-
Notifications
You must be signed in to change notification settings - Fork 368
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
feat: automatic create tsconfig for editor support on functions #6036
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
@@ -0,0 +1,62 @@ | ||||
// @ts-check | ||||
import { existsSync } from 'fs' | ||||
import { writeFile } from 'fs/promises' | ||||
import { createRequire } from 'module' | ||||
import { join } from 'path' | ||||
|
||||
import { NETLIFYDEVLOG, NETLIFYDEVWARN, chalk, log } from '../../utils/command-helpers.mjs' | ||||
|
||||
/** | ||||
* The `tsconfig.json` we are going to write to the functions directory. | ||||
* We use a template string instead of JSON.stringify to be able to add comments to the JSON. | ||||
* Comments inside the JSON are accepted by TypeScript and tsconfig. | ||||
*/ | ||||
const TSCONFIG_TMPL = `{ | ||||
// "extends": "../tsconfig.json", /** If you want to share configuration enable the extends property (like strict: true) */ | ||||
"compilerOptions": { | ||||
"noEmit": true /** This tsconfig.json is only used for type checking and editor support */, | ||||
"module": "ESNext", | ||||
"moduleResolution": "Bundler" /** This is needed to use .ts file extensions as we bundle it */, | ||||
"allowImportingTsExtensions": true /** This allows using .ts file extension instead of the standard .js extension. We allow this for better compatibility with Deno Edge Functions */, | ||||
"checkJs": true /** Enable type checking in JavaScript files as well */, | ||||
"allowJs": true /** Make JavaScript files part of the program as well */ | ||||
} | ||||
} | ||||
` | ||||
|
||||
/** | ||||
* Function that is responsible for validating the typescript configuration for serverless functions. | ||||
* It validates the `tsconfig.json` settings and if they don't comply it will throw an error. | ||||
* @param {object} config | ||||
* @param {string|undefined} config.functionsDir An absolute path to the functions directory | ||||
*/ | ||||
export async function checkTsconfigForV2Api(config) { | ||||
// if no functionsDir is specified or the dir does not exist just return | ||||
if (!config.functionsDir || !existsSync(config.functionsDir)) { | ||||
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. Have you considered moving this logic into the cli/src/lib/functions/registry.mjs Line 181 in f8a9703
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. sure but then we can't run it on the create command. |
||||
return | ||||
} | ||||
|
||||
try { | ||||
const require = createRequire(config.functionsDir) | ||||
require.resolve('@netlify/functions') | ||||
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. I stole this from you and it's already been shipped π¬ cli/src/lib/functions/registry.mjs Line 181 in f8a9703
|
||||
} catch { | ||||
log( | ||||
`${NETLIFYDEVWARN} Please install the ${chalk.dim( | ||||
'@netlify/functions', | ||||
)} package to get a better typed experience!`, | ||||
) | ||||
} | ||||
const tsconfig = join(config.functionsDir, 'tsconfig.json') | ||||
|
||||
if (existsSync(tsconfig)) { | ||||
return | ||||
} | ||||
|
||||
await writeFile(tsconfig, TSCONFIG_TMPL, 'utf-8') | ||||
|
||||
log( | ||||
`${NETLIFYDEVLOG} Successfully created a ${chalk.dim( | ||||
'tsconfig.json', | ||||
)} file in your functions folder for better Editor support!`, | ||||
) | ||||
} |
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.
I don't think we need to set this or
moduleResolution
anymore? I think all we need isallowImportingTsExtensions
+noEmit
for the.ts
extensions, and thencheckJs
+allowJs
for the JavaScript files?