Skip to content

Commit c0199cf

Browse files
farcallerKyleAMathews
authored andcommitted
Run typescript sources through full babel transpiling pipeline (#3369)
* Run typescript sources through full babel transpiling pipeline * Pass the babel config around to simplify the pipelines that need to use babel * Bubble up errors from webpack run
1 parent bad90b6 commit c0199cf

File tree

5 files changed

+27
-19
lines changed

5 files changed

+27
-19
lines changed

packages/gatsby-plugin-typescript/src/__tests__/gatsby-node.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,24 +15,26 @@ describe(`gatsby-plugin-typescript`, () => {
1515
})
1616

1717
it(`modifies webpack config`, () => {
18+
const babelConfig = { "plugins":[``] }
1819
const config = {
1920
loader: jest.fn(),
2021
}
2122

22-
modifyWebpackConfig({ config }, { compilerOptions: {} })
23+
modifyWebpackConfig({ config, babelConfig }, { compilerOptions: {} })
2324

2425
expect(config.loader).toHaveBeenCalledTimes(1)
2526
const lastCall = config.loader.mock.calls.pop()
2627
expect(lastCall).toMatchSnapshot()
2728
})
2829

2930
it(`passes the configuration to the ts-loader plugin`, () => {
31+
const babelConfig = { "plugins":[``] }
3032
const config = {
3133
loader: jest.fn(),
3234
}
3335
const options = { compilerOptions: { foo: `bar` }, transpileOnly: false }
3436

35-
modifyWebpackConfig({ config }, options)
37+
modifyWebpackConfig({ config, babelConfig }, options)
3638

3739
const expectedOptions = {
3840
compilerOptions: {
@@ -55,10 +57,11 @@ describe(`gatsby-plugin-typescript`, () => {
5557
})
5658

5759
it(`uses default configuration for the ts-loader plugin when no config is provided`, () => {
60+
const babelConfig = { "plugins":[``] }
5861
const config = {
5962
loader: jest.fn(),
6063
}
61-
modifyWebpackConfig({ config }, { compilerOptions: {} })
64+
modifyWebpackConfig({ config, babelConfig }, { compilerOptions: {} })
6265

6366
const expectedOptions = {
6467
compilerOptions: {

packages/gatsby-plugin-typescript/src/gatsby-node.js

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
const { transpileModule } = require(`typescript`)
2-
const path = require(`path`)
32

43
const test = /\.tsx?$/
54
const compilerDefaults = {
@@ -11,7 +10,7 @@ const compilerDefaults = {
1110
module.exports.resolvableExtensions = () => [`.ts`, `.tsx`]
1211

1312
module.exports.modifyWebpackConfig = (
14-
{ config },
13+
{ config, babelConfig },
1514
{ compilerOptions, transpileOnly = true }
1615
) => {
1716
// CommonJS to keep Webpack happy.
@@ -23,16 +22,10 @@ module.exports.modifyWebpackConfig = (
2322
// error (i.e., not build) at something or other.
2423
const opts = { compilerOptions: copts, transpileOnly }
2524

26-
// Load gatsby babel plugin to extract graphql query
27-
const extractQueryPlugin = path.resolve(
28-
__dirname,
29-
`../gatsby/dist/utils/babel-plugin-extract-graphql.js`
30-
)
31-
3225
config.loader(`typescript`, {
3326
test,
3427
loaders: [
35-
`babel?${JSON.stringify({ plugins: [extractQueryPlugin] })}`,
28+
`babel?${JSON.stringify(babelConfig)}`,
3629
`ts-loader?${JSON.stringify(opts)}`,
3730
],
3831
})

packages/gatsby/src/commands/build-javascript.js

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,20 @@ module.exports = async program => {
1111
`build-javascript`
1212
)
1313

14-
return new Promise(resolve => {
15-
webpack(compilerConfig.resolve()).run(() => resolve())
14+
return new Promise((resolve, reject) => {
15+
webpack(compilerConfig.resolve()).run((err, stats) => {
16+
if (err) {
17+
reject(err)
18+
return
19+
}
20+
21+
const jsonStats = stats.toJson()
22+
if (jsonStats.errors && jsonStats.errors.length > 0) {
23+
reject(jsonStats.errors[0])
24+
return
25+
}
26+
27+
resolve()
28+
})
1629
})
1730
}

packages/gatsby/src/utils/webpack-modify-validate.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ const validationWhitelist = Joi.object({
1717
responsiveLoader: Joi.any(),
1818
})
1919

20-
export default (async function ValidateWebpackConfig(config, stage) {
20+
export default (async function ValidateWebpackConfig(program, config, babelConfig, stage) {
2121
// We don't care about the return as plugins just mutate the config directly.
22-
await apiRunnerNode(`modifyWebpackConfig`, { config, stage })
22+
await apiRunnerNode(`modifyWebpackConfig`, { program, config, babelConfig, stage })
2323

2424
// console.log(JSON.stringify(config, null, 4))
2525

packages/gatsby/src/utils/webpack.config.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,12 @@ module.exports = async (
4949
webpackPort = 1500,
5050
pages = []
5151
) => {
52-
const babelStage = suppliedStage
5352
const directoryPath = withBasePath(directory)
5453

5554
// We combine develop & develop-html stages for purposes of generating the
5655
// webpack config.
5756
const stage = suppliedStage
58-
const babelConfig = await genBabelConfig(program, babelStage)
57+
const babelConfig = await genBabelConfig(program, suppliedStage)
5958

6059
function processEnv(stage, defaultNodeEnv) {
6160
debug(`Building env for "${stage}"`)
@@ -568,7 +567,7 @@ module.exports = async (
568567

569568
// Use the suppliedStage again to let plugins distinguish between
570569
// server rendering the html.js and the frontend development config.
571-
const validatedConfig = await webpackModifyValidate(config, suppliedStage)
570+
const validatedConfig = await webpackModifyValidate(program, config, babelConfig, suppliedStage)
572571

573572
return validatedConfig
574573
}

0 commit comments

Comments
 (0)