diff --git a/package.json b/package.json index 46d68d5..39e888a 100644 --- a/package.json +++ b/package.json @@ -60,10 +60,9 @@ "scripts": { "start": "node scripts/watch.js", "serve": "node scripts/watch.js", - "build": "react-app-rewired build", + "build": "node scripts/build.js", "test": "react-app-rewired test" }, - "homepage": "./", "config-overrides-path": "scripts/config-overrides", "browserslist": { "production": [ diff --git a/scripts/build.js b/scripts/build.js new file mode 100644 index 0000000..5d8ad0e --- /dev/null +++ b/scripts/build.js @@ -0,0 +1,5 @@ +// Running modifications that are shared across watch.js and build.js +require("./common"); + +// Running react-app-rewired to monkey-patch webpack-config +require("react-app-rewired/scripts/build"); diff --git a/scripts/common.js b/scripts/common.js new file mode 100644 index 0000000..cd627f4 --- /dev/null +++ b/scripts/common.js @@ -0,0 +1,11 @@ +/** + * Rewire publicPath generator + * + * .htaccess reroutes requests of any undefined `static` directory to the `static` + * folder found in project root. Thus, we can make `publicPath` a relative value. + * + * Preferably, the project should be written with subdirectory support in mind, as + * re-building the code because of a different directory placement is inconvenient. + */ +require("react-dev-utils/getPublicUrlOrPath"); +require.cache[require.resolve("react-dev-utils/getPublicUrlOrPath")].exports = () => "./"; diff --git a/scripts/watch.js b/scripts/watch.js index b1f8a81..ec609ef 100644 --- a/scripts/watch.js +++ b/scripts/watch.js @@ -15,31 +15,8 @@ process.on("unhandledRejection", err => { throw err; }); -/** - * Rewire publicPath generator - * - * .htaccess reroutes requests of any undefined `static` directory to the `static` - * folder found in project root. Thus, we can make `publicPath` a relative value. - */ -require("react-dev-utils/getPublicUrlOrPath"); -require.cache[require.resolve("react-dev-utils/getPublicUrlOrPath")].exports = ( - isEnvDevelopment, - homepage -) => { - const { URL } = require("url"); - const stubDomain = "https://create-react-app.dev"; - - if (homepage) { - // strip last slash if exists - homepage = homepage.endsWith("/") ? homepage : homepage + "/"; - - // validate if `homepage` is a URL or path like and use just pathname - const validHomepagePathname = new URL(homepage, stubDomain).pathname; - return homepage.startsWith(".") ? homepage : validHomepagePathname; - } - - return "/"; -}; +// Running modifications that are shared across watch.js and build.js +require("./common"); const fs = require("fs-extra"); const paths = require("react-scripts/config/paths");