Skip to content

Commit 6df573f

Browse files
wardpeetLekoArts
andauthored
fix(gatsby-core-utils): add tree-shake (esm) to core-utils (#36020)
Co-authored-by: Lennart <[email protected]>
1 parent d20a74b commit 6df573f

File tree

4 files changed

+69
-7
lines changed

4 files changed

+69
-7
lines changed

packages/gatsby-core-utils/.babelrc

+8-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
11
{
2-
"presets": [["babel-preset-gatsby-package"]]
2+
"presets": [["babel-preset-gatsby-package"]],
3+
"env": {
4+
"modern": {
5+
"presets": [["babel-preset-gatsby-package", {
6+
"esm": true
7+
}]]
8+
}
9+
}
310
}

packages/gatsby-core-utils/package.json

+26-5
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,21 @@
77
"gatsby-core-utils"
88
],
99
"exports": {
10-
".": "./dist/index.js",
11-
"./*": "./dist/*.js",
12-
"./dist/*": "./dist/*.js"
10+
".": {
11+
"types": "./dist/index.d.ts",
12+
"import": "./dist/index.mjs",
13+
"require": "./dist/index.js"
14+
},
15+
"./*": {
16+
"types": "./dist/*.d.ts",
17+
"import": "./dist/*.mjs",
18+
"require": "./dist/*.js"
19+
},
20+
"./dist/*": {
21+
"types": "./dist/*.d.ts",
22+
"import": "./dist/*.mjs",
23+
"require": "./dist/*.js"
24+
}
1325
},
1426
"typesVersions": {
1527
"*": {
@@ -23,17 +35,24 @@
2335
"homepage": "https://github.com/gatsbyjs/gatsby/tree/master/packages/gatsby-core-utils#readme",
2436
"license": "MIT",
2537
"main": "dist/index.js",
38+
"module": "dist/index.mjs",
2639
"types": "dist/index.d.ts",
2740
"repository": {
2841
"type": "git",
2942
"url": "https://github.com/gatsbyjs/gatsby.git",
3043
"directory": "packages/gatsby-core-utils"
3144
},
3245
"scripts": {
33-
"build": "babel src --out-dir dist/ --ignore \"**/__tests__\" --ignore \"**/__mocks__\" --extensions \".ts\"",
46+
"prebuild": "npm-run-all --npm-path npm clean",
47+
"build": "npm-run-all --npm-path npm -s build:*",
48+
"build:cjs": "babel src --out-dir dist/ --ignore \"**/__tests__\" --ignore \"**/__mocks__\" --extensions \".ts\"",
49+
"build:mjs": "cross-env BABEL_ENV=modern babel src --out-dir dist/ --ignore \"**/__tests__\" --ignore \"**/__mocks__\" --extensions \".ts\" --out-file-extension .mjs",
3450
"typegen": "tsc --emitDeclarationOnly --declaration --declarationDir dist/",
3551
"prepare": "cross-env NODE_ENV=production npm run build && npm run typegen",
36-
"watch": "babel -w src --out-dir dist/ --ignore \"**/__tests__\" --extensions \".ts\""
52+
"watch": "npm-run-all --npm-path npm -p watch:*",
53+
"watch:cjs": "babel -w src --out-dir dist/ --ignore \"**/__tests__\" --ignore \"**/__mocks__\" --extensions \".ts\"",
54+
"watch:mjs": "cross-env BABEL_ENV=modern babel -w src --out-dir dist/ --ignore \"**/__tests__\" --ignore \"**/__mocks__\" --extensions \".ts\" --out-file-extension .mjs",
55+
"clean": "del-cli dist/*"
3756
},
3857
"bugs": {
3958
"url": "https://github.com/gatsbyjs/gatsby/issues"
@@ -64,8 +83,10 @@
6483
"@types/ci-info": "2.0.0",
6584
"babel-preset-gatsby-package": "^2.19.0-next.0",
6685
"cross-env": "^7.0.3",
86+
"del-cli": "^3.0.1",
6787
"is-uuid": "^1.0.2",
6888
"msw": "^0.38.2",
89+
"npm-run-all": "^4.1.5",
6990
"typescript": "^4.7.2"
7091
},
7192
"engines": {

packages/gatsby-core-utils/src/index.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ export { createRequireFromPath } from "./create-require-from-path"
77
export { getConfigStore } from "./get-config-store"
88
export { getGatsbyVersion } from "./get-gatsby-version"
99
export { getTermProgram } from "./get-term-program"
10-
export { fetchRemoteFile, IFetchRemoteFileOptions } from "./fetch-remote-file"
10+
export { fetchRemoteFile } from "./fetch-remote-file"
1111
export { isTruthy } from "./is-truthy"
1212
export * as uuid from "./uuid"
1313
export { getMatchPath } from "./match-path"
@@ -19,3 +19,5 @@ export { listPlugins } from "./list-plugins"
1919
export { createFilePath } from "./filename-utils"
2020
export { readConfigFile, getConfigPath } from "./utils"
2121
export { lock } from "./lock"
22+
23+
export type { IFetchRemoteFileOptions } from "./fetch-remote-file"

packages/gatsby/src/internal-plugins/functions/gatsby-node.ts

+32
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,38 @@ const createWebpackConfig = async ({
313313
// watch: !isProductionEnv,
314314
module: {
315315
rules: [
316+
// Webpack expects extensions when importing ESM modules as that's what the spec describes.
317+
// Not all libraries have adapted so we don't enforce its behaviour
318+
// @see https://github.com/webpack/webpack/issues/11467
319+
{
320+
test: /\.mjs$/i,
321+
resolve: {
322+
byDependency: {
323+
esm: {
324+
fullySpecified: false,
325+
},
326+
},
327+
},
328+
},
329+
{
330+
test: /\.js$/i,
331+
descriptionData: {
332+
type: `module`,
333+
},
334+
resolve: {
335+
byDependency: {
336+
esm: {
337+
fullySpecified: false,
338+
},
339+
},
340+
},
341+
use: {
342+
loader: `babel-loader`,
343+
options: {
344+
presets: [`@babel/typescript`],
345+
},
346+
},
347+
},
316348
{
317349
test: [/.js$/, /.ts$/],
318350
exclude: /node_modules/,

0 commit comments

Comments
 (0)