Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add "add polyfills" script #21

Open
jamcry opened this issue Jul 20, 2023 · 0 comments
Open

Add "add polyfills" script #21

jamcry opened this issue Jul 20, 2023 · 0 comments
Assignees

Comments

@jamcry
Copy link
Collaborator

jamcry commented Jul 20, 2023

Usually we need to add polyfills to be able to use 3rd party libraries such as algosdk.
I have created a script to easily do this, we can include this in the template:

// add-polyfills.mjs
/* eslint-disable consistent-return */
import util from "util";
import {exec} from "child_process";
import fs from "fs";

// promisify `exec` so we can "await" it
const execAsync = util.promisify(exec);

// content of config-overrides.js file to be added
const CONFIG_OVERRIDES_CONTENT = `
const webpack = require("webpack");

module.exports = function override(config) {
  const fallback = config.resolve.fallback || {};

  Object.assign(fallback, {
    crypto: require.resolve("crypto-browserify"),
    stream: require.resolve("stream-browserify"),
    buffer: require.resolve("buffer/"),
  });
  config.resolve.fallback = fallback;

  config.ignoreWarnings = [/Failed to parse source map/];
  config.plugins = (config.plugins || []).concat([
    new webpack.ProvidePlugin({
      process: "process/browser",
      Buffer: ["buffer", "Buffer"],
    }),
  ]);

  return config;
};
`;

async function main() {
  await installReactAppRewired();
  createConfigOverrides();
  updateNpmScripts();
}

function installReactAppRewired() {
  return execAsync(
    "npm install react-app-rewired --save-dev",
    (error, stdout, stderr) => {
      if (error) {
        console.log(`ERROR while installing react-app-rewired: ${error.message}`);
        return;
      }

      if (stderr) {
        console.log(`stderr: ${stderr}`);
        return;
      }

      console.log(`stdout: ${stdout}`);
    }
  );
}

function createConfigOverrides() {
  fs.writeFile("config-overrides.js", CONFIG_OVERRIDES_CONTENT, (err) => {
    if (err) {
      throw err;
    }

    console.log("config-overrides.js was created successfully!");
  });
}

function updateNpmScripts() {
  // next line opens package json, updates start and other scripts to use react-app-rewired
  // and saves the file
  fs.readFile("package.json", "utf8", (err, data) => {
    if (err) {
      return console.log(err);
    }

    console.log(data);
    const result = data
      .replace(/"start": "react-scripts start"/g, '"start": "react-app-rewired start"')
      .replace(/"build": "react-scripts build"/g, '"build": "react-app-rewired build"')
      .replace(/"test": "react-scripts test"/g, '"test": "react-app-rewired test"')
      .replace(/"eject": "react-scripts eject"/g, '"eject": "react-app-rewired eject"');

    fs.writeFile(
      "package.json",
      result,
      {
        encoding: "utf8",
        // "w" so it will overwrite
        flag: "w"
      },
      (writeError) => {
        if (writeError) {
          return console.log(writeError);
        }
      }
    );
  });
}

main();

run:

node add-polyfills.mjs
@jamcry jamcry self-assigned this Jul 20, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant