crtxdmp.

A collection of ideas, snippets and other things.


Export create-react-app app into single file

Install react-app-rewired via yarn add react-app-rewired --dev

File: package.json
{"scripts": {
    "start": "react-app-rewired start",
    "build": "react-app-rewired build"
  }}
File: config-overrides.js
module.exports = function override(config, env) {

  config.output.filename = 'widget.js';

  config.optimization.splitChunks  = {
    cacheGroups: {
      default: false,
    },
  };
  config.optimization.runtimeChunk = false;

  return config;
};
File: ./src/index.tsx
import React from 'react';
import ReactDOM from 'react-dom';
import App from 'src/app/index';

//@ts-ignore
window.myWidget = (containerElement, config) => {
  ReactDOM.render(
    <React.StrictMode>
        <App config={config} />
    </React.StrictMode>,
    containerElement
  );
};

// this should be disabled if you don't want to mount the app automatically.
// anyway this is very helpful while doing dev work
if (document.getElementById("root") !== null) {
//@ts-ignore
  window.myWidget(
    document.getElementById("root"),
    {}
  );
}

— Apr 13, 2021