Last active
May 1, 2021 00:32
-
-
Save juanpasolano/f85b6dd3ddf8ae443e9f92f11797f125 to your computer and use it in GitHub Desktop.
Sage 9 Wordpress theme
This file contains hidden or 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
import React from "react"; | |
import ReactDOM from "react-dom"; | |
import "babel-polyfill"; | |
const App = ()=> { | |
return <h1>I'm an app</h1> | |
} | |
const init = () => { | |
ReactDOM.render(<App />, document.getElementById("reactApp")); | |
}; | |
export default init; |
This file contains hidden or 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
import App from "./../react/app.jsx"; | |
export default { | |
init() { | |
if (document.getElementById("reactApp")) { | |
App(); | |
} | |
}, | |
finalize() { | |
// JavaScript to be fired on all pages, after page specific JS is fired | |
}, | |
}; |
This file contains hidden or 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
yarn add react react-dom | |
yarn add @babel/core @babel/preset-env babel-polyfill babel-loader --dev | |
# I would suggest you use dynamic imports for the app | |
yarn add @babel/plugin-syntax-dynamic-import --dev | |
# Since Imports is not valid ES2015 Eslint will not like it so we need to add | |
yarn babel-eslint --dev | |
And change the es-lint config | |
'parser': 'babel-eslint', | |
'parserOptions': { | |
"allowImportExportEverywhere": true | |
}, | |
Add the plugin in the webpack, and now that we have babel we might want to parse js and jsx with babel so: | |
{ | |
test: /\.(jsx?)$/, | |
exclude: [ | |
/(node_modules|bower_components)(?)/, | |
], | |
use: [ | |
{loader: 'cache'}, | |
{loader: 'babel', options: { | |
presets: ["@babel/preset-env", "@babel/preset-react"], | |
plugins: ["@babel/plugin-syntax-dynamic-import"], | |
}} | |
] | |
}, | |
And now we can so we dont load react in every page | |
export default { | |
init() { | |
if (document.getElementById("mapApp")) { | |
import("./../react/map.jsx").then(module => module.default()); | |
} | |
}, | |
finalize() { | |
// JavaScript to be fired on all pages, after page specific JS is fired | |
}, | |
}; |
This file contains hidden or 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
let webpackConfig = { | |
// other configs... | |
module: { | |
rules: [ | |
// other rules... | |
// remove the js rule | |
// Add the following to process JSX files | |
{ | |
test: /\.(jsx?)$/, | |
exclude: [/(node_modules|bower_components)(?)/], | |
loader: 'babel', | |
query: { | |
presets: ['@babel/preset-env', '@babel/preset-react'], | |
}, | |
}, | |
], | |
}, | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment