Skip to content

Commit 268ed27

Browse files
authored
feat(gatsby): enable babel-loader for all dependencies (#14111)
1 parent 0fa5955 commit 268ed27

File tree

4 files changed

+60
-34
lines changed

4 files changed

+60
-34
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`webpack utils js returns default values without any options 1`] = `
4+
Object {
5+
"exclude": /core-js\\|event-source-polyfill\\|webpack-hot-middleware\\\\/client/,
6+
"test": /\\\\\\.\\(js\\|mjs\\|jsx\\)\\$/,
7+
"type": "javascript/auto",
8+
"use": Array [
9+
Object {
10+
"loader": "<PROJECT_ROOT>/packages/gatsby/src/utils/babel-loader.js",
11+
"options": Object {},
12+
},
13+
],
14+
}
15+
`;
16+
17+
exports[`webpack utils js returns the correct exclude paths 1`] = `
18+
Object {
19+
"exclude": /core-js\\|event-source-polyfill\\|webpack-hot-middleware\\\\/client\\|node_modules/,
20+
"test": /\\\\\\.\\(js\\|mjs\\|jsx\\)\\$/,
21+
"type": "javascript/auto",
22+
"use": Array [
23+
Object {
24+
"loader": "<PROJECT_ROOT>/packages/gatsby/src/utils/babel-loader.js",
25+
"options": Object {},
26+
},
27+
],
28+
}
29+
`;

packages/gatsby/src/utils/__tests__/webpack-utils.js

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,23 @@ beforeAll(async () => {
1111
})
1212

1313
describe(`webpack utils`, () => {
14-
describe(`mjs`, () => {
15-
it(`adds .mjs rule`, () => {
16-
expect(config.rules.mjs).toEqual(expect.any(Function))
14+
describe(`js`, () => {
15+
it(`adds .js rule`, () => {
16+
expect(config.rules.js).toEqual(expect.any(Function))
1717
})
1818

19-
it(`returns correct rule`, () => {
20-
const rule = config.rules.mjs()
19+
it(`returns default values without any options`, () => {
20+
const rule = config.rules.js()
2121

22-
expect(rule).toEqual({
23-
include: /node_modules/,
24-
test: /\.mjs$/,
25-
type: `javascript/auto`,
22+
expect(rule).toMatchSnapshot()
23+
})
24+
25+
it(`returns the correct exclude paths`, () => {
26+
const rule = config.rules.js({
27+
exclude: [`node_modules`],
2628
})
29+
30+
expect(rule).toMatchSnapshot()
2731
})
2832
})
2933
})

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

Lines changed: 8 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -293,34 +293,20 @@ module.exports = async ({
293293
* JavaScript loader via babel, excludes node_modules
294294
*/
295295
{
296-
let js = (options = {}) => {
297-
return {
298-
test: /\.jsx?$/,
299-
exclude: vendorRegex,
300-
use: [loaders.js(options)],
301-
}
302-
}
303-
304-
rules.js = js
305-
}
296+
let js = ({ exclude = [], ...options } = {}) => {
297+
const excludeRegex = [
298+
`core-js|event-source-polyfill|webpack-hot-middleware/client`,
299+
].concat(exclude)
306300

307-
/**
308-
* mjs loader:
309-
* webpack 4 has issues automatically dealing with
310-
* the .mjs extension, thus we need to explicitly
311-
* add this rule to use the default webpack js loader
312-
*/
313-
{
314-
let mjs = (options = {}) => {
315301
return {
316-
test: /\.mjs$/,
317-
include: /node_modules/,
302+
test: /\.(js|mjs|jsx)$/,
303+
exclude: new RegExp(excludeRegex.join(`|`)),
318304
type: `javascript/auto`,
319-
...options,
305+
use: [loaders.js(options)],
320306
}
321307
}
322308

323-
rules.mjs = mjs
309+
rules.js = js
324310
}
325311

326312
{

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

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -245,12 +245,19 @@ module.exports = async (program, directory, suppliedStage) => {
245245
}
246246
}
247247

248-
function getModule(config) {
248+
function getModule() {
249+
const jsOptions = {}
250+
251+
// Speedup 🏎️💨 the build! We only include transpilation of node_modules on production builds
252+
// TODO create gatsby plugin to enable this behaviour on develop (only when people are requesting this feature)
253+
if (stage === `develop`) {
254+
jsOptions.exclude = [`node_modules`]
255+
}
256+
249257
// Common config for every env.
250258
// prettier-ignore
251259
let configRules = [
252-
rules.mjs(),
253-
rules.js(),
260+
rules.js(jsOptions),
254261
rules.yaml(),
255262
rules.fonts(),
256263
rules.images(),

0 commit comments

Comments
 (0)