Skip to content

Instantly share code, notes, and snippets.

@timonweb
Created February 6, 2019 11:07

Revisions

  1. timonweb revised this gist Feb 6, 2019. 1 changed file with 15 additions and 13 deletions.
    28 changes: 15 additions & 13 deletions patch-cra-set-public-path.js
    Original file line number Diff line number Diff line change
    @@ -4,8 +4,11 @@ const pathClient = "./node_modules/react-dev-utils/webpackHotDevClient.js";

    fs.writeFileSync(
    pathClient,
    fs.readFileSync(pathClient).toString().replace(
    `var connection = new SockJS(
    fs
    .readFileSync(pathClient)
    .toString()
    .replace(
    `var connection = new SockJS(
    url.format({
    protocol: window.location.protocol,
    hostname: window.location.hostname,
    @@ -14,11 +17,10 @@ fs.writeFileSync(
    pathname: '/sockjs-node',
    })
    );`,
    `
    `
    var scriptUrl = document.currentScript
    ? url.parse(document.currentScript.src)
    : window.location;
    var connection = new SockJS(
    url.format({
    protocol: scriptUrl.protocol,
    @@ -28,17 +30,17 @@ var connection = new SockJS(
    pathname: '/sockjs-node',
    })
    );`
    )
    )
    );


    // NOTE: I am using the "react-script-ts" fork
    const pathConfig = "./node_modules/react-scripts-ts/config/webpack.config.dev.js";

    const pathConfig = "./node_modules/react-scripts/config/webpackDevServer.config.js";
    fs.writeFileSync(
    pathConfig,
    fs.readFileSync(pathConfig).toString().replace(
    `const publicPath = '/';`,
    `const publicPath = process.env.PUBLIC_PATH || '/';`
    )
    fs
    .readFileSync(pathConfig)
    .toString()
    .replace(
    `const publicPath = '/';`,
    `const publicPath = process.env.PUBLIC_PATH || '/';`
    )
    );
  2. @madebyherzblut madebyherzblut created this gist Aug 25, 2017.
    44 changes: 44 additions & 0 deletions patch-cra-set-public-path.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,44 @@
    const fs = require("fs");

    const pathClient = "./node_modules/react-dev-utils/webpackHotDevClient.js";

    fs.writeFileSync(
    pathClient,
    fs.readFileSync(pathClient).toString().replace(
    `var connection = new SockJS(
    url.format({
    protocol: window.location.protocol,
    hostname: window.location.hostname,
    port: window.location.port,
    // Hardcoded in WebpackDevServer
    pathname: '/sockjs-node',
    })
    );`,
    `
    var scriptUrl = document.currentScript
    ? url.parse(document.currentScript.src)
    : window.location;
    var connection = new SockJS(
    url.format({
    protocol: scriptUrl.protocol,
    hostname: scriptUrl.hostname,
    port: scriptUrl.port,
    // Hardcoded in WebpackDevServer
    pathname: '/sockjs-node',
    })
    );`
    )
    );


    // NOTE: I am using the "react-script-ts" fork
    const pathConfig = "./node_modules/react-scripts-ts/config/webpack.config.dev.js";

    fs.writeFileSync(
    pathConfig,
    fs.readFileSync(pathConfig).toString().replace(
    `const publicPath = '/';`,
    `const publicPath = process.env.PUBLIC_PATH || '/';`
    )
    );