-
Notifications
You must be signed in to change notification settings - Fork 916
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a1a333c
commit 31d0a1a
Showing
11 changed files
with
138 additions
and
51 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
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
9 changes: 9 additions & 0 deletions
9
packages/create-snowpack-app/packages/app-scripts-react/babel.config.json
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,9 @@ | ||
{ | ||
"presets": [["@babel/preset-react"], "@babel/preset-typescript"], | ||
"plugins": ["@babel/plugin-syntax-import-meta"], | ||
"env": { | ||
"development": { | ||
"plugins": ["react-refresh/babel"] | ||
} | ||
} | ||
} |
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
13 changes: 13 additions & 0 deletions
13
packages/create-snowpack-app/packages/plugin-react-refresh/package.json
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,13 @@ | ||
{ | ||
"name": "@snowpack/plugin-react-refresh", | ||
"version": "0.6.2", | ||
"main": "plugin.js", | ||
"license": "MIT", | ||
"publishConfig": { | ||
"access": "public" | ||
}, | ||
"dependencies": { | ||
"react-refresh": "^0.8.0" | ||
}, | ||
"gitHead": "795f4311d79a70cc9f19f21b512b7f8675d73f17" | ||
} |
74 changes: 74 additions & 0 deletions
74
packages/create-snowpack-app/packages/plugin-react-refresh/plugin.js
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,74 @@ | ||
/** | ||
* @snowpack/plugin-react-refresh (Fast Refresh) | ||
* Based on details provided by: | ||
* - https://github.com/facebook/react/issues/16604#issuecomment-528663101 | ||
* - https://github.com/vitejs/vite-plugin-react (see LICENSE) | ||
*/ | ||
|
||
const fs = require("fs"); | ||
|
||
const reactRefreshLoc = require.resolve( | ||
"react-refresh/cjs/react-refresh-runtime.development.js" | ||
); | ||
const reactRefreshCode = fs | ||
.readFileSync(reactRefreshLoc, { encoding: "utf-8" }) | ||
.replace(`process.env.NODE_ENV`, JSON.stringify("development")); | ||
|
||
function transformHtml(contents, urlPath) { | ||
return contents.replace( | ||
/<body.*?>/, | ||
`$& | ||
<script> | ||
function debounce(e,t){let u;return()=>{clearTimeout(u),u=setTimeout(e,t)}} | ||
const exports = {}; | ||
${reactRefreshCode} | ||
exports.performReactRefresh = debounce(exports.performReactRefresh, 30); | ||
window.$RefreshRuntime$ = exports; | ||
window.$RefreshRuntime$.injectIntoGlobalHook(window); | ||
window.$RefreshReg$ = () => {}; | ||
window.$RefreshSig$ = () => (type) => type; | ||
</script>` | ||
); | ||
} | ||
|
||
function transformJs(contents, urlPath) { | ||
return ` | ||
/** React Refresh: Setup **/ | ||
if (import.meta.hot) { | ||
var prevRefreshReg = window.$RefreshReg$; | ||
var prevRefreshSig = window.$RefreshSig$; | ||
window.$RefreshReg$ = (type, id) => { | ||
window.$RefreshRuntime$.register(type, ${JSON.stringify( | ||
urlPath | ||
)} + " " + id); | ||
} | ||
window.$RefreshSig$ = window.$RefreshRuntime$.createSignatureFunctionForTransform; | ||
} | ||
${contents} | ||
/** React Refresh: Connect **/ | ||
if (import.meta.hot) { | ||
window.$RefreshReg$ = prevRefreshReg | ||
window.$RefreshSig$ = prevRefreshSig | ||
import.meta.hot.accept(() => { | ||
window.$RefreshRuntime$.performReactRefresh() | ||
}); | ||
}`; | ||
} | ||
|
||
module.exports = function reactRefreshTransform() { | ||
return { | ||
transform({ contents, urlPath, isDev }) { | ||
if (!isDev) { | ||
return null; | ||
} | ||
if (urlPath.endsWith(".js") && /\$RefreshReg\$\(/.test(contents)) { | ||
return { result: transformJs(contents, urlPath) }; | ||
} | ||
if (urlPath.endsWith("/") || urlPath.endsWith(".html")) { | ||
return { result: transformHtml(contents, urlPath) }; | ||
} | ||
}, | ||
}; | ||
}; |
3 changes: 3 additions & 0 deletions
3
packages/create-snowpack-app/templates/app-template-react-typescript/babel.config.json
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,3 @@ | ||
{ | ||
"extends": "@snowpack/app-scripts-react/babel.config.json" | ||
} |
3 changes: 3 additions & 0 deletions
3
packages/create-snowpack-app/templates/app-template-react/babel.config.json
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,3 @@ | ||
{ | ||
"extends": "@snowpack/app-scripts-react/babel.config.json" | ||
} |
7 changes: 5 additions & 2 deletions
7
packages/create-snowpack-app/templates/app-template-react/src/App.jsx
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