-
Notifications
You must be signed in to change notification settings - Fork 10.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(gatsby-plugin-mdx): enable hmr when importing mdx (#31288)
* save MDXContent to different file * tmp: skip mdx loader unit tests * test(e2e-mdx): upgrade cypress, setup running dev * test(e2e-mdx): add hmr test case * only apply hmr workaround to develop stage * don't save mdx component to fs, use webpack tricks with query params * wait for hmr in mdx/develop * drop passthrough fs location * revert unneeded change * more reverts * revert devtool debugging change * adjust unit tests * add more e2e test - editing prop in markdown, editing component imported by mdx
- Loading branch information
Showing
16 changed files
with
1,057 additions
and
127 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"baseUrl": "http://localhost:8000", | ||
"env": { | ||
"GATSBY_COMMAND": "develop" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,6 @@ | ||
{ | ||
"baseUrl": "http://localhost:9000" | ||
"baseUrl": "http://localhost:9000", | ||
"env": { | ||
"GATSBY_COMMAND": "build" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
if (Cypress.env("GATSBY_COMMAND") === `develop`) { | ||
before(() => { | ||
cy.exec(`npm run reset`) | ||
}) | ||
|
||
after(() => { | ||
cy.exec(`npm run reset`) | ||
}) | ||
|
||
it(`Can hot-reload markdown content`, () => { | ||
cy.visit(`/hmr`, { | ||
onBeforeLoad: win => { | ||
cy.spy(win.console, "log").as(`hmrConsoleLog`) | ||
}, | ||
}).waitForRouteChange() | ||
cy.get(`h2`).invoke(`text`).should(`eq`, `Lorem`) | ||
|
||
cy.exec( | ||
`npm run update -- --file src/pages/hmr.mdx --exact --replacements "Lorem:Ipsum"` | ||
) | ||
|
||
cy.get(`@hmrConsoleLog`).should(`be.calledWithMatch`, `App is up to date`) | ||
cy.wait(1000) | ||
|
||
cy.get(`h2`).invoke(`text`).should(`eq`, `Ipsum`) | ||
}) | ||
|
||
it(`Can hot-reload react content (i.e. change prop in mdx content)`, () => { | ||
cy.visit(`/hmr`, { | ||
onBeforeLoad: win => { | ||
cy.spy(win.console, "log").as(`hmrConsoleLog`) | ||
}, | ||
}).waitForRouteChange() | ||
cy.get(`[data-testid="test-prop-edit"]`) | ||
.invoke(`text`) | ||
.should(`eq`, `prop-before`) | ||
|
||
cy.exec( | ||
`npm run update -- --file src/pages/hmr.mdx --exact --replacements "prop-before:prop-after"` | ||
) | ||
|
||
cy.get(`@hmrConsoleLog`).should(`be.calledWithMatch`, `App is up to date`) | ||
cy.wait(1000) | ||
|
||
cy.get(`[data-testid="test-prop-edit"]`) | ||
.invoke(`text`) | ||
.should(`eq`, `prop-after`) | ||
}) | ||
|
||
it(`Can hot-reload imported js components`, () => { | ||
cy.visit(`/hmr`, { | ||
onBeforeLoad: win => { | ||
cy.spy(win.console, "log").as(`hmrConsoleLog`) | ||
}, | ||
}).waitForRouteChange() | ||
cy.get(`[data-testid="test-imported-edit"]`) | ||
.invoke(`text`) | ||
.should(`eq`, `component-before`) | ||
|
||
cy.exec( | ||
`npm run update -- --file src/components/hmr-component-edit.js --exact --replacements "component-before:component-after"` | ||
) | ||
|
||
cy.get(`@hmrConsoleLog`).should(`be.calledWithMatch`, `App is up to date`) | ||
cy.wait(1000) | ||
|
||
cy.get(`[data-testid="test-imported-edit"]`) | ||
.invoke(`text`) | ||
.should(`eq`, `component-after`) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
const fs = require(`fs-extra`) | ||
|
||
const HISTORY_FILE = `__history__.json` | ||
|
||
exports.__HISTORY_FILE__ = HISTORY_FILE | ||
|
||
exports.getHistory = async (file = HISTORY_FILE) => { | ||
try { | ||
const contents = await fs | ||
.readFile(file, `utf8`) | ||
.then(contents => JSON.parse(contents)) | ||
|
||
return new Map(contents) | ||
} catch (e) { | ||
return new Map() | ||
} | ||
} | ||
|
||
exports.writeHistory = async (contents, file = HISTORY_FILE) => { | ||
try { | ||
await fs.writeFile(file, JSON.stringify([...contents]), `utf8`) | ||
} catch (e) { | ||
console.error(e) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
const fs = require(`fs-extra`) | ||
const path = require(`path`) | ||
|
||
const { __HISTORY_FILE__, getHistory } = require(`./history`) | ||
|
||
async function reset() { | ||
const history = await getHistory() | ||
|
||
await Promise.all( | ||
Array.from(history).map(([filePath, value]) => { | ||
if (typeof value === `string`) { | ||
return fs.writeFile(path.resolve(filePath), value, `utf8`) | ||
} | ||
return fs.remove(path.resolve(filePath)) | ||
}) | ||
) | ||
|
||
await fs.remove(__HISTORY_FILE__) | ||
} | ||
|
||
reset() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
const fs = require(`fs-extra`) | ||
const path = require(`path`) | ||
const yargs = require(`yargs`) | ||
|
||
const { getHistory, writeHistory } = require(`./history`) | ||
|
||
const args = yargs | ||
.option(`file`, { | ||
demand: true, | ||
type: `string`, | ||
}) | ||
.option(`replacements`, { | ||
default: [], | ||
type: `array`, | ||
}) | ||
.option(`exact`, { | ||
default: false, | ||
type: `boolean`, | ||
}) | ||
.option(`delete`, { | ||
default: false, | ||
type: `boolean`, | ||
}) | ||
.option(`fileContent`, { | ||
default: JSON.stringify( | ||
` | ||
import * as React from 'react'; | ||
import Layout from '../components/layout'; | ||
export default function SomeComponent() { | ||
return ( | ||
<Layout> | ||
<h1 data-testid="message">Hello %REPLACEMENT%</h1> | ||
</Layout> | ||
) | ||
} | ||
` | ||
).trim(), | ||
type: `string`, | ||
}) | ||
.option(`fileSource`, { | ||
type: `string`, | ||
}) | ||
.option(`restore`, { | ||
default: false, | ||
type: `boolean`, | ||
}).argv | ||
|
||
async function update() { | ||
const history = await getHistory() | ||
|
||
const { file: fileArg, replacements, restore } = args | ||
const filePath = path.resolve(fileArg) | ||
if (restore) { | ||
const original = history.get(filePath) | ||
if (original) { | ||
await fs.writeFile(filePath, original, `utf-8`) | ||
} else if (original === false) { | ||
await fs.remove(filePath) | ||
} else { | ||
console.log(`Didn't make changes to "${fileArg}". Nothing to restore.`) | ||
} | ||
history.delete(filePath) | ||
return | ||
} | ||
let exists = true | ||
if (!fs.existsSync(filePath)) { | ||
exists = false | ||
let fileContent | ||
if (args.fileSource) { | ||
fileContent = await fs.readFile(args.fileSource, `utf8`) | ||
} else if (args.fileContent) { | ||
fileContent = JSON.parse(args.fileContent).replace(/\+n/g, `\n`) | ||
} | ||
await fs.writeFile(filePath, fileContent, `utf8`) | ||
} | ||
const file = await fs.readFile(filePath, `utf8`) | ||
|
||
if (!history.has(filePath)) { | ||
history.set(filePath, exists ? file : false) | ||
} | ||
|
||
if (args.delete) { | ||
if (exists) { | ||
await fs.remove(filePath) | ||
} | ||
} else { | ||
const contents = replacements.reduce((replaced, pair) => { | ||
const [key, value] = pair.split(`:`) | ||
return replaced.replace( | ||
args.exact ? key : new RegExp(`%${key}%`, `g`), | ||
value | ||
) | ||
}, file) | ||
|
||
await fs.writeFile(filePath, contents, `utf8`) | ||
} | ||
|
||
await writeHistory(history) | ||
} | ||
|
||
update() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import React from "react" | ||
|
||
const HMRImportEditComponent = () => ( | ||
<div data-testid="test-imported-edit">component-before</div> | ||
) | ||
|
||
export default HMRImportEditComponent |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import React from "react" | ||
|
||
const HMRPropEditComponent = ({ test }) => ( | ||
<div data-testid="test-prop-edit">{test}</div> | ||
) | ||
|
||
export default HMRPropEditComponent |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import HMRImportEditComponent from "../components/hmr-component-edit" | ||
import HMRPropEditComponent from "../components/hmr-prop-edit" | ||
|
||
## Lorem | ||
|
||
<HMRImportEditComponent /> | ||
|
||
<HMRPropEditComponent test="prop-before" /> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.