Skip to content

Commit

Permalink
chore(gatsby): upgrade latest-version (#36434)
Browse files Browse the repository at this point in the history
  • Loading branch information
pieh committed Oct 18, 2022
1 parent ab51cd4 commit eb9a330
Show file tree
Hide file tree
Showing 13 changed files with 263 additions and 15 deletions.
Expand Up @@ -3,7 +3,7 @@ const path = require("path")
module.exports = {
sourceType: "module",
plugins: [
[path.resolve(__dirname, "../../babel-transform-compiler-flags.js"),
[path.resolve(__dirname, "../../../babel-transform-compiler-flags.js"),
{
availableFlags: ['MAJOR'],
flags: {
Expand Down
@@ -0,0 +1,3 @@
export async function someFunction() {
return await import(`path`);
}
@@ -0,0 +1,12 @@
"use strict";

exports.__esModule = true;
exports.someFunction = someFunction;

function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }

function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }

async function someFunction() {
return await Promise.resolve().then(() => _interopRequireWildcard(require(`path`)));
}
@@ -0,0 +1,17 @@
const path = require("path")

// "babel-plugin-dynamic-import-node" is used in tests, so we force not a test environment
process.env.BABEL_ENV = `not-a-test`

module.exports = {
sourceType: "module",
presets: [
[path.resolve(__dirname, "../../../index.js"),
{
keepDynamicImports: [`./packages/babel-preset-gatsby-package/lib/__tests__/fixtures/keep-dynamic-import/with-override/input.js`]
}
],
],
babelrc: false,
configFile: false,
}
@@ -0,0 +1,3 @@
export async function someFunction2() {
return await import(`path`);
}
@@ -0,0 +1,8 @@
"use strict";

exports.__esModule = true;
exports.someFunction2 = someFunction2;

async function someFunction2() {
return await import(`path`);
}
@@ -1,3 +1,3 @@
const runner = require(`@babel/helper-plugin-test-runner`).default

runner(__dirname)
runner(__dirname )
@@ -0,0 +1,54 @@
const path = require(`path`)

/**
* @typedef {import('@babel/core').NodePath} NodePath
* @typedef {import('@babel/core').PluginObj} PluginObj
* @typedef {import('@babel/core').PluginPass} PluginPass
* @typedef {import('@babel/core').types} BabelTypes
* @typedef {import('@babel/core').types.Identifier} Identifier
* @typedef {import('@babel/core').types.MemberExpression} MemberExpression
*/

/**
* @typedef {Object} IPluginOptions
* @property {Array<string>} keepDynamicImports
*/

/**
*
* @param {{ types: BabelTypes }} _unused
* @param {Partial<IPluginOptions>} opts
* @returns {PluginObj}
*/
module.exports = function keepDynamicImports(
_unused,
opts
) {
if (!opts.keepDynamicImports) {
throw new Error(`keepDynamicImports option needs to be set`)
} else if (!Array.isArray(opts.keepDynamicImports)) {
throw new Error(`keepDynamicImports option needs to be an array`)
}

const absolutePaths = opts.keepDynamicImports.map(p => path.resolve(p))

return {
name: `babel-transform-mark-to-keep-dynamic-import`,
visitor: {
Program() {
const filename = this.file?.opts?.filename
if (!filename) {
return
}

if (absolutePaths.includes(filename)) {
// this is big hack - it relies on some babel plugins internal to basically
// do early return ( https://github.com/babel/babel/blob/3526b79c87863052f1c61ec0c49c0fc287ba32e6/packages/babel-plugin-transform-modules-commonjs/src/index.ts#L174 )
// on top of that `BabelFile` doesn't expose delete for the metadata,
// so we reach into internal `_map` to delete it
this.file._map.delete("@babel/plugin-proposal-dynamic-import")
}
}
},
}
}
7 changes: 7 additions & 0 deletions packages/babel-preset-gatsby-package/lib/index.js
Expand Up @@ -7,6 +7,7 @@ function preset(context, options = {}) {
nodeVersion = `18.0.0`,
esm = false,
availableCompilerFlags = [`GATSBY_MAJOR`],
keepDynamicImports = null
} = options
const {
NODE_ENV,
Expand Down Expand Up @@ -86,6 +87,12 @@ function preset(context, options = {}) {
},
],
r(`babel-plugin-lodash`),
Array.isArray(keepDynamicImports) && keepDynamicImports.length > 0 && [
r(`./babel-transform-mark-to-keep-dynamic-import`),
{
keepDynamicImports,
},
]
].filter(Boolean),
overrides: [
{
Expand Down
4 changes: 3 additions & 1 deletion packages/gatsby/babel.config.js
Expand Up @@ -3,5 +3,7 @@
// Ref: https://github.com/babel/babel/pull/7358
module.exports = {
sourceMaps: true,
presets: [["babel-preset-gatsby-package"]],
presets: [["babel-preset-gatsby-package", {
keepDynamicImports: [`./src/utils/feedback.ts`]
}]],
}
2 changes: 1 addition & 1 deletion packages/gatsby/package.json
Expand Up @@ -117,7 +117,7 @@
"is-relative-url": "^3.0.0",
"joi": "^17.4.2",
"json-loader": "^0.5.7",
"latest-version": "5.1.0",
"latest-version": "^7.0.0",
"lmdb": "2.5.3",
"lodash": "^4.17.21",
"md5-file": "^5.0.0",
Expand Down
2 changes: 1 addition & 1 deletion packages/gatsby/src/utils/feedback.ts
@@ -1,7 +1,6 @@
import report from "gatsby-cli/lib/reporter"
import { getConfigStore, getGatsbyVersion, isCI } from "gatsby-core-utils"
import { trackCli } from "gatsby-telemetry"
import latestVersion from "latest-version"
import getDayOfYear from "date-fns/getDayOfYear"

const feedbackKey = `feedback.disabled`
Expand Down Expand Up @@ -115,6 +114,7 @@ export async function userPassesFeedbackRequestHeuristic(): Promise<boolean> {
const versionPoints = getGatsbyVersion().split(`.`)
let latestVersionPoints: Array<string> = []
try {
const { default: latestVersion } = await import(`latest-version`)
latestVersionPoints = (await latestVersion(`gatsby`)).split(`.`)
} catch (e) {
// do nothing.
Expand Down

0 comments on commit eb9a330

Please sign in to comment.