2

thanks for taking the time to look at this. Here's the issue at hand:

I am building a desktop application using ElectronJS and React. But every time that I load the project, I get these annoying warnings about sourcemaps in the console:

Sourcemap warning screenshot

I know that the manual solution is to go into the dev tool settings every time I run the project and uncheck these boxes:

enter image description here

However, is there a way to configure this so I don't have to do this each time I load the project? It's very cumbersome and annoying.

In case you need it, here are my package.json scripts (I use "app" to run it)

"scripts": {
    "app": "concurrently \"cross-env BROWSER=none npm start\" \"wait-on http://localhost:3000 && electron .\" \"npm run watch-css\"",
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "build-css": "node-sass ./src/css/sass_css/ -o ./src/css/vanilla_css/",
    "watch-css": "nodemon -e scss -x \"npm run build-css\"",
    "eject": "react-scripts eject"
  },

Yes I used create-react-app. Added my webpack config:

#!/usr/bin/env node

/**
 * @param {string} command process to run
 * @param {string[]} args command line arguments
 * @returns {Promise<void>} promise
 */
const runCommand = (command, args) => {
    const cp = require("child_process");
    return new Promise((resolve, reject) => {
        const executedCommand = cp.spawn(command, args, {
            stdio: "inherit",
            shell: true
        });

        executedCommand.on("error", error => {
            reject(error);
        });

        executedCommand.on("exit", code => {
            if (code === 0) {
                resolve();
            } else {
                reject();
            }
        });
    });
};

/**
 * @param {string} packageName name of the package
 * @returns {boolean} is the package installed?
 */
const isInstalled = packageName => {
    if (process.versions.pnp) {
        return true;
    }

    const path = require("path");
    const fs = require("graceful-fs");

    let dir = __dirname;

    do {
        try {
            if (
                fs.statSync(path.join(dir, "node_modules", packageName)).isDirectory()
            ) {
                return true;
            }
        } catch (_error) {
            // Nothing
        }
    } while (dir !== (dir = path.dirname(dir)));

    return false;
};

/**
 * @param {CliOption} cli options
 * @returns {void}
 */
const runCli = cli => {
    const path = require("path");
    const pkgPath = require.resolve(`${cli.package}/package.json`);
    // eslint-disable-next-line node/no-missing-require
    const pkg = require(pkgPath);
    // eslint-disable-next-line node/no-missing-require
    require(path.resolve(path.dirname(pkgPath), pkg.bin[cli.binName]));
};

/**
 * @typedef {Object} CliOption
 * @property {string} name display name
 * @property {string} package npm package name
 * @property {string} binName name of the executable file
 * @property {boolean} installed currently installed?
 * @property {string} url homepage
 */

/** @type {CliOption} */
const cli = {
    name: "webpack-cli",
    package: "webpack-cli",
    binName: "webpack-cli",
    installed: isInstalled("webpack-cli"),
    url: "https://github.com/webpack/webpack-cli"
};

if (!cli.installed) {
    const path = require("path");
    const fs = require("graceful-fs");
    const readLine = require("readline");

    const notify =
        "CLI for webpack must be installed.\n" + `  ${cli.name} (${cli.url})\n`;

    console.error(notify);

    let packageManager;

    if (fs.existsSync(path.resolve(process.cwd(), "yarn.lock"))) {
        packageManager = "yarn";
    } else if (fs.existsSync(path.resolve(process.cwd(), "pnpm-lock.yaml"))) {
        packageManager = "pnpm";
    } else {
        packageManager = "npm";
    }

    const installOptions = [packageManager === "yarn" ? "add" : "install", "-D"];

    console.error(
        `We will use "${packageManager}" to install the CLI via "${packageManager} ${installOptions.join(
            " "
        )} ${cli.package}".`
    );

    const question = `Do you want to install 'webpack-cli' (yes/no): `;

    const questionInterface = readLine.createInterface({
        input: process.stdin,
        output: process.stderr
    });

    // In certain scenarios (e.g. when STDIN is not in terminal mode), the callback function will not be
    // executed. Setting the exit code here to ensure the script exits correctly in those cases. The callback
    // function is responsible for clearing the exit code if the user wishes to install webpack-cli.
    process.exitCode = 1;
    questionInterface.question(question, answer => {
        questionInterface.close();

        const normalizedAnswer = answer.toLowerCase().startsWith("y");

        if (!normalizedAnswer) {
            console.error(
                "You need to install 'webpack-cli' to use webpack via CLI.\n" +
                    "You can also install the CLI manually."
            );

            return;
        }
        process.exitCode = 0;

        console.log(
            `Installing '${
                cli.package
            }' (running '${packageManager} ${installOptions.join(" ")} ${
                cli.package
            }')...`
        );

        runCommand(packageManager, installOptions.concat(cli.package))
            .then(() => {
                runCli(cli);
            })
            .catch(error => {
                console.error(error);
                process.exitCode = 1;
            });
    });
} else {
    runCli(cli);
}
Frank Edwards
  • 76
  • 1
  • 9
  • Can you show your webpack conf file? Also, are using the Chrome "React Developer Tools" extension? – midnight-coding Jun 21 '22 at 13:11
  • As @midnight-coding mentioned, this is only going to be answerable with knowledge of your Webpack config, or what build system you're using instead of Webpack. I'm guessing you're using `create-react-app`, in which case you can try this: https://marmelab.com/blog/2021/07/22/cra-webpack-no-eject.html The option to generate sourcemaps in Webpack is called [`devtool`](https://webpack.js.org/configuration/devtool/), try setting it to 'none'. – Slbox Jun 22 '22 at 00:05
  • I added my webpack.js file in a revision to the question. Yes I did use create-react-app, and I use React Developer Tools Extension. @Slbox – Frank Edwards Jun 22 '22 at 06:38
  • @FrankEdwards that doesn't appear to be a Webpack configuration file. Most likely you'll need to do what that link mentions to allow you to override a part of the default configuration. – Slbox Jun 22 '22 at 20:02
  • Your webpack config file is usually named `webpack.config.js`. It should be located within the root (top level) directory of your project. See [Webpack Configuration](https://webpack.js.org/configuration/) for more information. As @Slbox mentioned, the `devtool:` option is of particular interest to us helping you with your question. See [Devtool](https://webpack.js.org/configuration/devtool/) for more information. – midnight-coding Jun 23 '22 at 22:53
  • @midnight-coding I don't think there is an exposed `webpack.config.js` or equivalent when you're using `create-react-app`. – Slbox Jun 23 '22 at 23:04
  • @Slbox Oh, your right. Looks like it is in `node_modules/create-react-app/packages/react-scripts/config/webpack.config.js`. Is that right? That's nuts. What happens when you `npm install` or `npm update`? Bye bye customisations... @FrankEdwards [`eject`](https://create-react-app.dev/docs/available-scripts/#npm-run-eject) is an option but only use it after thoroughly understanding the consequences. IE: You get full control of webpack, but you forgo `create-react-app` updates and there will be more code to maintain. **Only do it if you understand it**. An alternative could be react-app-rewired? – midnight-coding Jun 24 '22 at 07:56
  • Please take a look at: [Disable source maps in Chrome DevTools](https://stackoverflow.com/questions/35002087/disable-source-maps-in-chrome-devtools) – Eskandar Abedini Jun 27 '22 at 12:31

1 Answers1

2

You can use create a file .env and add

GENERATE_SOURCEMAP=false

This will remove any .map files from your build/static/js folder the next time you run build.

wick3d
  • 1,164
  • 14
  • 41