0

My goal is to create a simple app and output one js standalone file and one css standalone file without using create-react-app.

I have two functional components like

const Card = () => {

  return(
    <div>test</div>
  )
}

export default Card;

My app.js file

import ReactDOM from 'react-dom';
import Card from './Card';

ReactDOM.render(<Card />, document.getElementById('root'));

It looks like I need web pack so this is my web pack config file

const path = require("path");

module.exports = {
  entry:  "./src/app.js",
  output: {
    path: path.resolve(__dirname, "dist"),
    filename: "app.js"
  },
  resolve: {
    extensions: [".js"]
  },
  module: {
    rules: [
      { test: /\.js$/, exclude: /node_modules/, loader: "babel-loader" }
    ]
  }
};

I installed the following packages

"dependencies": {
    "react": "^16.13.1",
    "react-dom": "^16.13.1"
  },
  "devDependencies": {
    "@babel/cli": "^7.8.4",
    "@babel/core": "^7.9.6",
    "@babel/preset-react": "^7.9.4",
    "babel-loader": "^8.1.0",
    "eslint": "^7.0.0",
    "eslint-config-google": "^0.14.0",
    "webpack": "^4.43.0",
    "webpack-cli": "^3.3.11"
  }

Running nix babel src/app.js throws an error

Unexpected token 

   | 
> 7 |   ReactDOM.render(<Card />, document.getElementById('root'))
    |                   ^
  8 | }

The goal here to generate one JS file and one CSS file and that is why I didn't use the create-react-app.

Searching on the inter web shows a lot of results like using react and babel from a CDN which I do not want to do.

how do I compile JSK to pure JS into one JS minify file?

Edit

I did follow the other questions and this is the error I get

Error: Plugin/Preset files are not allowed to export objects, only functions.

.babelrc file

{
  "presets": ["es2015", "stage-1", "react"]
 }

web pack.config.js

const path = require("path");

module.exports = {
  entry:  "./src/app.js",
  output: {
    path: path.resolve(__dirname, "dist"),
    filename: "app.js"
  },
  module: {
    loaders: [{
        test: /\.jsx?$/,
        loader: 'babel',
        query:
        {
            presets:['es2015', 'react']
        }
    }]
},
};
iJK
  • 4,655
  • 12
  • 63
  • 95
  • It doesn't know how to accept JSX. Check out the babel react preset: https://babeljs.io/docs/en/babel-preset-react – rfestag May 17 '20 at 01:46
  • Does this answer your question? [babel-loader jsx SyntaxError: Unexpected token](https://stackoverflow.com/questions/33460420/babel-loader-jsx-syntaxerror-unexpected-token) – Dan O May 17 '20 at 01:47
  • Or this? https://stackoverflow.com/questions/48075620/webpack-build-failed-throws-unexpected-token-error-jsx-syntax – Dan O May 17 '20 at 01:48
  • there are MANY Stack Overflow questions regarding this exact error message in this exact scenario. which of those questions did you research and why specifically did they not solve your issue? – Dan O May 17 '20 at 01:49
  • I did and updated the question with new errors – iJK May 17 '20 at 02:06
  • Check this tutorial for React v17 setup without create-react-app: https://frontendguruji.com/blog/how-to-setup-a-react-js-project-from-scratch-without-create-react-app/ – Mandeep Pasbola Jan 31 '22 at 06:21

1 Answers1

0

Here's a stripped-down working example that's not using CRA (but has CRA-like functionality) and outputs single file bundled assets: Single Webpack Bundle Kit


Includes:

  • Bundles and minifies all .js into a single static/js/bundle.min.js
  • Bundles and minifies all .(s)css into a single static/css/bundle.min.css
  • Hot Module Replacement
  • Lint (eslint) on save
  • CSS and SCSS global or local stylesheets (similar to CRA, .module.(s)css is local otherwise without .module it's global)
  • Image and font imports (woff2|ttf|woff|eot|svg|png|jpg|jpeg)
  • Automatically aliased directories within src (using ~ will alias directories within src, such as import Card from "~components/Card"; this eliminates relative imports like: import Card from "../../../components/Card")
  • Includes notes as what each file in the config directory is and how it's supplementing the webpack.config.js

Installation:

1 - Clone the boilerplate repository:

git clone git@github.com:mattcarlotta/react-starter-kit.git

2 - Run yarn or npm install to install dependencies.

3 - Run yarn dev or npm run dev to start a development server.

4 - Run yarn build or npm run build to create a compiled application to dist, then run yarn start or npm start to start a local production server.


Alternatively, if you're looking for the CRA that outputs single file assets, then take a look at this answer (includes repo).

Matt Carlotta
  • 18,972
  • 4
  • 39
  • 51
  • Thank you so much Matt! JS tooling has become quite tricky. Any suggestions on how I can start learning from scratch? – iJK May 17 '20 at 16:23
  • 1
    When I first started building my own Webpack config several years ago, I reverse-engineered an ejected CRA (it also includes notes). Then, I branched out and started to refer to the [official Webpack docs](https://webpack.js.org/configuration/) and, over time, added additional plugins (and wrote some helper libraries) to aid my work-flow. – Matt Carlotta May 17 '20 at 16:45