-
Notifications
You must be signed in to change notification settings - Fork 10.3k
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
[v2] Typescript plugin not working #5520
Comments
Hmm not sure — you can check out the source here to see if something looks awry https://github.com/gatsbyjs/gatsby/blob/v2/packages/gatsby-plugin-typescript/src/gatsby-node.js#L14 |
Why are you adding the js loaders here? https://github.com/gatsbyjs/gatsby/blob/v2/packages/gatsby-plugin-typescript/src/gatsby-node.js#L39 |
Not sure — @jquense wrote this so I'll you off to him ;-) |
Hmm are any of the functions being called? |
confirmed that resolvableExtensions is def getting called |
confirmed that preprocessSource is too. |
Odd — this is where the API is called — perhaps there's a subtle typo or something?
Is there any console errors? |
oh well v2 of typescript plugin is using |
|
So #5522 fixes this issue? |
#5522 and the TypeScript plugin needs a newer version of ts-loader that's compat with Webpack 4 |
I've never felt like that was a good choice tho...especially now that babel has a TS parser |
Indeed NextJS 6 uses babel parser for Typescript which integrates better with the build toolchain (you still get the TS experience in the editor) |
I'm new to the Gatsby ecosystem and I have a Typescript app in which I would really benefit from webpack 4 tree-shaking. So I'm trying the following configuration in exports.resolvableExtensions = () => ['.ts', '.tsx']
exports.onCreateBabelConfig = ({ actions }, options) => {
actions.setBabelPreset({
name: '@babel/preset-typescript',
options,
})
}
exports.onCreateWebpackConfig = ({ actions, loaders }) => {
actions.setWebpackConfig({
module: {
rules: [
{
test: /\.tsx?$/,
use: [loaders.js()],
},
],
},
})
} Unfortunately this isn't working. Any pointer to what could go wrong here? |
Yeah, switching to a native babel parser sounds great (from my limited, haven't used typescript perspective) |
We should pull in someone who is knowlegable about TS tooling, my understanding is that as a compiler babel is incomplete in terms of which features it supports (if any?). From what i can understand the original problem was that the TS plugin was only compiling to es6, so that Gatsby tools could read out |
@mquandalle gatsby loads general babel configs which contain preset @jquense from what I get from playing with it, @babel/preset-typescript handles build perfectly. And it's faster than TS loader. The issue is, that the only thing it does is stripping TS markup - you still want to fire ts-loader (with noEmit in config (possibly in spawned process)) to get friendly errors and static type analysis. What's the point in using TS without compile time type check? |
@vadistic on large applications ts-loader becomes a bottleneck typechecking. In addition to all of our apps at TPG, I know that the Outlook team also opts for fork-ts-webpack-checker-plugin https://github.com/Realytics/fork-ts-checker-webpack-plugin which allows you to typecheck with multiple workers and pipe errors into terminal logs. You just keep ts-loader in happyPack / transpileOnly mode. You can get a serious speed boost if you use the also turn on experimentalWatchApi which enables incremental builds |
@vadistic One possibility is to have type errors being reported in the code editor and not handled by the build toolchain (since Babel is only stripping TS type annotations). That's already the case with Gatsby v1 as your app compiles even if the TS type check fails. Thanks for the pointer about |
In case someone is looking for a temporary fix before
const { transpileModule } = require(`typescript`)
const test = /\.tsx?$/
const compilerDefaults = {
target: `esnext`,
experimentalDecorators: true,
jsx: `react`,
module: `es6`,
}
exports.resolvableExtensions = () => {
return [`.ts`, `.tsx`]
}
exports.onCreateWebpackConfig = (
{ actions, loaders },
{ compilerOptions, ...options }
) => {
const typescriptOptions = {
transpileOnly: false,
compilerOptions: {
...compilerDefaults,
...compilerOptions,
},
}
actions.setWebpackConfig({
module: {
rules: [
{
test,
use: [
loaders.js(),
{
loader: require.resolve(`ts-loader`),
options: typescriptOptions,
},
],
},
],
},
})
}
exports.preprocessSource = ({ contents, filename }, { compilerOptions }) => {
const copts = { ...compilerDefaults, ...compilerOptions }
// return the transpiled source if it's TypeScript, otherwise null
return test.test(filename)
? transpileModule(contents, { compilerOptions: copts }).outputText
: null
} My {
"include": ["./src/**/*"],
"compilerOptions": {
"target": "esnext",
"module": "commonjs",
"lib": ["dom", "es2017"],
"jsx": "react",
"strict": true,
"esModuleInterop": true,
"experimentalDecorators": true,
"emitDecoratorMetadata": true
}
} |
Regarding the future of |
I'm able to get a v2 project working with ts-loader with the guidance above but no static queries are running in dev or build. Debugging a bit leads me to believe the issue is related to this function: The execution never seems to get to line 130. Anybody else running into this or have any ideas why typescript would affect static query execution? |
Looking at this commit, it seems that the two have been made compatible. Btw, why is |
My guess is that it was to reduce breakage in the babel7 migration, babel6's react preset included flow. Good question tho as to whether that is still a good idea.. |
Got const resolvableExtensions = () => {
return [`.ts`, `.tsx`]
}
const onCreateWebpackConfig = (
{ actions, loaders, stage },
{ compilerOptions, ...options }
) => {
actions.setWebpackConfig({
module: {
rules: [
{
test: /\.tsx?$/,
use: [
{
loader: require.resolve(`babel-loader`),
options: {
cacheDirectory: true,
babelrc: false,
sourceType: "unambiguous",
presets: [
`@babel/preset-env`,
`@babel/preset-react`,
`@babel/preset-typescript`,
].map(require.resolve),
plugins: [
require.resolve(`babel-plugin-remove-graphql-queries`),
],
},
},
],
},
],
},
})
}
exports.onCreateWebpackConfig = onCreateWebpackConfig
exports.resolvableExtensions = resolvableExtensions Here I'm not using This is only a quick fix though, since in a more general setup we should somehow be able to pass all the correct arguments to the presets and plugins. For comparison, here's a simplified version of the default rules for js/jsx parsing: {
rules: [
{
test: /\.jsx?$/,
exclude: /(node_modules|bower_components)/,
use: [
{
options: {
cacheDirectory: true,
babelrc: false,
sourceType: "unambiguous",
presets: [
[
"preset-env",
{
loose: true,
modules: false,
useBuiltIns: "usage",
targets: { browsers: [Array] },
},
],
[
"preset-react",
{
useBuiltIns: true,
pragma: "React.createElement",
development: false,
},
],
["preset-flow", {}],
],
plugins: [
"babel-plugin-remove-graphql-queries",
["plugin-proposal-class-properties", { loose: true }],
["plugin-syntax-dynamic-import", {}],
[
"plugin-transform-runtime",
{ helpers: true, regenerator: true, polyfill: false },
],
],
},
loader: "babel-loader",
},
],
},
]
} |
@dannywils , can you get the static query execution working with the above? |
It works but I had to force the preset to
Test repo at https://github.com/dannywils/gatsby-typescript-staticquery/tree/babel |
I used @dennari
I also experienced the same issue than @dannywils about the |
I believe #5709 should solve the mentioned problems. |
I'm using #5709 since a few days without issues. Also as noted before |
Due to the high volume of issues, we're closing out older ones without recent activity. Please open a new issue if you need help! |
Description
I tried to use the typescript plugin with v2 to no avail. I get the following error message.
Environment
npm list gatsby
): 2.0.0-alpha.40gatsby --version
):File contents (if changed)
gatsby-config.js
:package.json
: N/Agatsby-node.js
: N/Agatsby-browser.js
: N/Agatsby-ssr.js
: N/AThe text was updated successfully, but these errors were encountered: