Skip to content

Commit

Permalink
fix(gatsby-core-utils): add tree-shake (esm) to core-utils (#36020)
Browse files Browse the repository at this point in the history
Co-authored-by: Lennart <lekoarts@gmail.com>
  • Loading branch information
wardpeet and LekoArts committed Jul 5, 2022
1 parent d20a74b commit 6df573f
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 7 deletions.
9 changes: 8 additions & 1 deletion packages/gatsby-core-utils/.babelrc
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
{
"presets": [["babel-preset-gatsby-package"]]
"presets": [["babel-preset-gatsby-package"]],
"env": {
"modern": {
"presets": [["babel-preset-gatsby-package", {
"esm": true
}]]
}
}
}
31 changes: 26 additions & 5 deletions packages/gatsby-core-utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,21 @@
"gatsby-core-utils"
],
"exports": {
".": "./dist/index.js",
"./*": "./dist/*.js",
"./dist/*": "./dist/*.js"
".": {
"types": "./dist/index.d.ts",
"import": "./dist/index.mjs",
"require": "./dist/index.js"
},
"./*": {
"types": "./dist/*.d.ts",
"import": "./dist/*.mjs",
"require": "./dist/*.js"
},
"./dist/*": {
"types": "./dist/*.d.ts",
"import": "./dist/*.mjs",
"require": "./dist/*.js"
}
},
"typesVersions": {
"*": {
Expand All @@ -23,17 +35,24 @@
"homepage": "https://github.com/gatsbyjs/gatsby/tree/master/packages/gatsby-core-utils#readme",
"license": "MIT",
"main": "dist/index.js",
"module": "dist/index.mjs",
"types": "dist/index.d.ts",
"repository": {
"type": "git",
"url": "https://github.com/gatsbyjs/gatsby.git",
"directory": "packages/gatsby-core-utils"
},
"scripts": {
"build": "babel src --out-dir dist/ --ignore \"**/__tests__\" --ignore \"**/__mocks__\" --extensions \".ts\"",
"prebuild": "npm-run-all --npm-path npm clean",
"build": "npm-run-all --npm-path npm -s build:*",
"build:cjs": "babel src --out-dir dist/ --ignore \"**/__tests__\" --ignore \"**/__mocks__\" --extensions \".ts\"",
"build:mjs": "cross-env BABEL_ENV=modern babel src --out-dir dist/ --ignore \"**/__tests__\" --ignore \"**/__mocks__\" --extensions \".ts\" --out-file-extension .mjs",
"typegen": "tsc --emitDeclarationOnly --declaration --declarationDir dist/",
"prepare": "cross-env NODE_ENV=production npm run build && npm run typegen",
"watch": "babel -w src --out-dir dist/ --ignore \"**/__tests__\" --extensions \".ts\""
"watch": "npm-run-all --npm-path npm -p watch:*",
"watch:cjs": "babel -w src --out-dir dist/ --ignore \"**/__tests__\" --ignore \"**/__mocks__\" --extensions \".ts\"",
"watch:mjs": "cross-env BABEL_ENV=modern babel -w src --out-dir dist/ --ignore \"**/__tests__\" --ignore \"**/__mocks__\" --extensions \".ts\" --out-file-extension .mjs",
"clean": "del-cli dist/*"
},
"bugs": {
"url": "https://github.com/gatsbyjs/gatsby/issues"
Expand Down Expand Up @@ -64,8 +83,10 @@
"@types/ci-info": "2.0.0",
"babel-preset-gatsby-package": "^2.19.0-next.0",
"cross-env": "^7.0.3",
"del-cli": "^3.0.1",
"is-uuid": "^1.0.2",
"msw": "^0.38.2",
"npm-run-all": "^4.1.5",
"typescript": "^4.7.2"
},
"engines": {
Expand Down
4 changes: 3 additions & 1 deletion packages/gatsby-core-utils/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export { createRequireFromPath } from "./create-require-from-path"
export { getConfigStore } from "./get-config-store"
export { getGatsbyVersion } from "./get-gatsby-version"
export { getTermProgram } from "./get-term-program"
export { fetchRemoteFile, IFetchRemoteFileOptions } from "./fetch-remote-file"
export { fetchRemoteFile } from "./fetch-remote-file"
export { isTruthy } from "./is-truthy"
export * as uuid from "./uuid"
export { getMatchPath } from "./match-path"
Expand All @@ -19,3 +19,5 @@ export { listPlugins } from "./list-plugins"
export { createFilePath } from "./filename-utils"
export { readConfigFile, getConfigPath } from "./utils"
export { lock } from "./lock"

export type { IFetchRemoteFileOptions } from "./fetch-remote-file"
32 changes: 32 additions & 0 deletions packages/gatsby/src/internal-plugins/functions/gatsby-node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,38 @@ const createWebpackConfig = async ({
// watch: !isProductionEnv,
module: {
rules: [
// Webpack expects extensions when importing ESM modules as that's what the spec describes.
// Not all libraries have adapted so we don't enforce its behaviour
// @see https://github.com/webpack/webpack/issues/11467
{
test: /\.mjs$/i,
resolve: {
byDependency: {
esm: {
fullySpecified: false,
},
},
},
},
{
test: /\.js$/i,
descriptionData: {
type: `module`,
},
resolve: {
byDependency: {
esm: {
fullySpecified: false,
},
},
},
use: {
loader: `babel-loader`,
options: {
presets: [`@babel/typescript`],
},
},
},
{
test: [/.js$/, /.ts$/],
exclude: /node_modules/,
Expand Down

0 comments on commit 6df573f

Please sign in to comment.