— webpack, babel, eslint, reactjs, react, front-end, coding project, troubleshooting — 1 min read
I tried created the front end with create-react-app
, but the webpack config was not configurable, and I had to follow the existing folder structure, of which I was not keen. So I decided to go the challenging route of setting it up on my own. :)
1|-- my_app2 |-- .babelrc3 |-- .eslintrc.js4 |-- .gitignore5 |-- package-lock.json6 |-- package.json7 |-- webpack.config.js8 |-- src9 |-- App.js10 |-- index.html11 |-- index.js12 |-- api13 | |-- apisauce.js14 |-- components15 | |-- Error16 | | |-- index.js17 | |-- Layout18 | |-- index.js19 | |-- styles.js20 |-- config21 |-- pages22 | |-- dynamic23 | | |-- index.js24 | | |-- test25 | | |-- index.js26 | |-- home27 | |-- index.js28 |-- services29 |-- util
resolve
and alias
objects to the config:1const alias = {2 '~': path.resolve(__dirname, './src'),3 react: path.resolve(__dirname, './node_modules/react'),4}56webpackConfig.resolve = {7 alias,8}910module.exports = webpackConfig
1npm install eslint-import-resolver-webpack --save-dev
alias
object to the import/resolver
setting. Note that .
is relative to where the .eslintrc.js
file lives. I wanted the relative path (represented by the tilde, ~
) to be pointing to my src
since I'll be importing within that scope anyway.1'import/resolver': {2 node: true, // default for node modules3 webpack: {4 config: path.resolve(__dirname, 'webpack.config.js'), //5 },6 alias: [7 ['~', path.resolve(__dirname, './src')],8 ],9 },
output
filename
in webpack.config.js
.1output: {2 path: path.join(__dirname, '/dist'),3 filename: '[name].js',4 },
publicPath
to output
in webpack.config.js
:1output: {2 publicPath: '/',3 },
historyApiFallback: true,
in devServer
:1devServer: {2 historyApiFallback: true,3 },
plugins
, add hash
and inject
to HtmlWebpackPlugin
options:1plugins: [2 new HtmlWebpackPlugin({3 template: './src/index.html',4 hash: true,5 inject: true,6 }),7 ],
1> ERROR in ./src/index.js 6:162> Module parse failed: Unexpected token (6:16)3> You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file. See https://webpack.js.org/concepts#loaders45| import App from './App';6|7| ReactDom.render(<App />, document.getElementById('app'));8|
.babelrc
. Previously was babel.config.js
.Basically can't use async/await. SO Answer
1npm i regenerator-runtime core-js
.babelrc
file.1{2 "presets": [3 ["@babel/preset-env",4 {5 "useBuiltIns": "usage", // alternative mode: "entry"6 "corejs": 3, // default would be 27 "targets": "> 0.25%, not dead"8 // set your own target environment here (see Browserslist)9 }10 ]11 ]12}
Tried to load my server data, and got this error when I checked the Network tab.
Fixed it server-side by allowing a more flexible origin. Obviously a temporary fix, will come back to it later.