├── .eslintignore ├── .eslintrc.json ├── .github └── workflows │ ├── deploy.yml │ └── test.yml ├── .gitignore ├── .npmignore ├── .tool-versions ├── CODE_OF_CONDUCT.md ├── LICENSE ├── README.md ├── config ├── env.js ├── jest │ ├── cssTransform.js │ └── fileTransform.js ├── paths.js ├── polyfills.js ├── webpack.config.demo.js ├── webpack.config.dev.js ├── webpack.config.prod.js └── webpackDevServer.config.js ├── package.json ├── public ├── favicon.ico ├── index.html └── manifest.json ├── scripts ├── build.js ├── demo.js ├── start.js └── test.js ├── src ├── demo │ ├── App.js │ ├── App.test.js │ ├── I18n.js │ ├── __snapshots__ │ │ └── App.test.js.snap │ ├── index.js │ └── registerServiceWorker.js └── lib │ ├── components │ └── I18n │ │ ├── __snapshots__ │ │ ├── createI18n.test.js.snap │ │ └── withLocale.test.js.snap │ │ ├── createI18n.js │ │ ├── createI18n.test.js │ │ ├── index.js │ │ ├── withLocale.js │ │ └── withLocale.test.js │ └── index.js └── yarn.lock /.eslintignore: -------------------------------------------------------------------------------- 1 | /build/ 2 | /demo/ 3 | /config/ 4 | /scripts/ 5 | /src/demo/index.js 6 | /src/demo/registerServiceWorker.js 7 | -------------------------------------------------------------------------------- /.eslintrc.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "airbnb", 3 | "parser": "babel-eslint", 4 | "rules": { 5 | "react/jsx-filename-extension": [1, { "extensions": [".js"] }], 6 | "react/jsx-props-no-spreading": 0 7 | }, 8 | "overrides": [ 9 | { 10 | "files": [ 11 | "**/*.test.js" 12 | ], 13 | "env": { 14 | "jest": true 15 | }, 16 | "plugins": ["jest"], 17 | "rules": { 18 | "jest/no-disabled-tests": "warn", 19 | "jest/no-focused-tests": "error", 20 | "jest/no-identical-title": "error", 21 | "jest/prefer-to-have-length": "warn", 22 | "jest/valid-expect": "error", 23 | "react/prop-types": [0] 24 | }, 25 | "globals": { 26 | "window": true 27 | } 28 | } 29 | ] 30 | } 31 | -------------------------------------------------------------------------------- /.github/workflows/deploy.yml: -------------------------------------------------------------------------------- 1 | name: deploy 2 | 3 | on: 4 | push: 5 | branches: [master] 6 | 7 | jobs: 8 | deploy: 9 | runs-on: ubuntu-latest 10 | 11 | steps: 12 | - uses: actions/checkout@v2 13 | 14 | - uses: actions/setup-node@v2-beta 15 | with: 16 | node-version: '14' 17 | 18 | - name: Install dependencies 19 | run: yarn install 20 | 21 | - name: Run tests 22 | run: yarn test --coverage 23 | env: 24 | CI: true 25 | 26 | - name: Build 27 | run: yarn demo 28 | env: 29 | PUBLIC_URL: /react-router-i18n 30 | 31 | - name: Deploy GitHub 32 | uses: JamesIves/github-pages-deploy-action@releases/v3 33 | with: 34 | GITHUB_TOKEN: ${{ github.token }} 35 | BRANCH: gh-pages 36 | FOLDER: demo 37 | CLEAN: true 38 | -------------------------------------------------------------------------------- /.github/workflows/test.yml: -------------------------------------------------------------------------------- 1 | name: Run tests 2 | 3 | on: [push, pull_request] 4 | 5 | jobs: 6 | test: 7 | runs-on: ubuntu-latest 8 | strategy: 9 | matrix: 10 | node: ['10', '12', '14'] 11 | name: Node ${{ matrix.node }} test 12 | steps: 13 | - uses: actions/checkout@v2 14 | 15 | - uses: actions/setup-node@v1 16 | with: 17 | node-version: ${{ matrix.node }} 18 | 19 | - name: Install dependencies 20 | run: yarn install 21 | 22 | - name: Lint code 23 | run: yarn lint 24 | 25 | # Ideally yarn audit would be part of the CI flow 26 | # but currently there is too much noise 27 | # See e.g. https://github.com/facebook/create-react-app/issues/8529 28 | # - name: Audit dependencies 29 | # - run: yarn audit 30 | 31 | - name: Run tests 32 | run: yarn test --coverage 33 | env: 34 | CI: true 35 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # See https://help.github.com/ignore-files/ for more about ignoring files. 2 | 3 | # dependencies 4 | /node_modules 5 | 6 | # production 7 | /build 8 | /demo 9 | 10 | # testing 11 | /coverage 12 | 13 | # misc 14 | .DS_Store 15 | .env.local 16 | .env.development.local 17 | .env.test.local 18 | .env.production.local 19 | 20 | npm-debug.log* 21 | package-lock.json 22 | yarn-debug.log* 23 | yarn-error.log* 24 | -------------------------------------------------------------------------------- /.npmignore: -------------------------------------------------------------------------------- 1 | # dependencies 2 | /node_modules 3 | 4 | # testing 5 | /coverage 6 | 7 | # misc 8 | .DS_Store 9 | .env.local 10 | .env.development.local 11 | .env.test.local 12 | .env.production.local 13 | 14 | npm-debug.log* 15 | yarn-debug.log* 16 | yarn-error.log* 17 | 18 | # Development folders and files 19 | public 20 | src 21 | scripts 22 | config 23 | demo 24 | .travis.yml 25 | .eslintignore 26 | .eslintrc.json 27 | .tool-versions 28 | yarn.lock 29 | /.github 30 | -------------------------------------------------------------------------------- /.tool-versions: -------------------------------------------------------------------------------- 1 | nodejs 12.6.0 2 | -------------------------------------------------------------------------------- /CODE_OF_CONDUCT.md: -------------------------------------------------------------------------------- 1 | # Contributor Covenant Code of Conduct 2 | 3 | ## Our Pledge 4 | 5 | In the interest of fostering an open and welcoming environment, we as 6 | contributors and maintainers pledge to make participation in react-router-i18n and 7 | its associated community a harassment-free experience for everyone, regardless of age, 8 | body size, disability, ethnicity, sex characteristics, gender identity and expression, 9 | level of experience, education, socio-economic status, nationality, personal 10 | appearance, race, religion, or sexual identity and orientation. 11 | 12 | ## Our Standards 13 | 14 | Examples of behavior that contributes to creating a positive environment 15 | include: 16 | 17 | * Using welcoming and inclusive language 18 | * Being respectful of differing viewpoints and experiences 19 | * Gracefully accepting constructive criticism 20 | * Focusing on what is best for the community 21 | * Showing empathy towards other community members 22 | 23 | Examples of unacceptable behavior by participants include: 24 | 25 | * The use of sexualized language or imagery and unwelcome sexual attention or 26 | advances 27 | * Trolling, insulting/derogatory comments, and personal or political attacks 28 | * Public or private harassment 29 | * Publishing others' private information, such as a physical or electronic 30 | address, without explicit permission 31 | * Other conduct which could reasonably be considered inappropriate in a 32 | professional setting 33 | 34 | ## Our Responsibilities 35 | 36 | Project maintainers are responsible for clarifying the standards of acceptable 37 | behavior and are expected to take appropriate and fair corrective action in 38 | response to any instances of unacceptable behavior. 39 | 40 | Project maintainers have the right and responsibility to remove, edit, or 41 | reject comments, commits, code, wiki edits, issues, and other contributions 42 | that are not aligned to this Code of Conduct, or to ban temporarily or 43 | permanently any contributor for other behaviors that they deem inappropriate, 44 | threatening, offensive, or harmful. 45 | 46 | ## Scope 47 | 48 | This Code of Conduct applies within project spaces and when 49 | an individual is officially representing the project or its community. 50 | Examples of representing a project or community include using an official 51 | project e-mail address, posting via an official social media account, or acting 52 | as an appointed representative at an online or offline event. Representation of 53 | a project may be further defined and clarified by project maintainers. 54 | 55 | ## Enforcement 56 | 57 | Instances of abusive, harassing, or otherwise unacceptable behavior may be 58 | reported by contacting the project team at zf.node [at] gmail [dot] com. All 59 | complaints will be reviewed and investigated and will result in a response that 60 | is deemed necessary and appropriate to the circumstances. The project team is 61 | obligated to maintain confidentiality with regard to the reporter of an incident. 62 | Further details of specific enforcement policies may be posted separately. 63 | 64 | Project maintainers who do not follow or enforce the Code of Conduct in good 65 | faith may face temporary or permanent repercussions as determined by other 66 | members of the project's leadership. 67 | 68 | ## Attribution 69 | 70 | This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4, 71 | available at https://www.contributor-covenant.org/version/1/4/code-of-conduct.html 72 | 73 | [homepage]: https://www.contributor-covenant.org 74 | 75 | For answers to common questions about this code of conduct, see 76 | https://www.contributor-covenant.org/faq 77 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2021 The Perseids Project, zfletch 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # React Router I18n 2 | 3 | This is a small opinionated library for I18n (internationalization) using React Router. 4 | 5 | ## Demo 6 | 7 | [https://zfletch.github.io/react-router-i18n/](https://zfletch.github.io/react-router-i18n/) 8 | 9 | ## Installation 10 | 11 | `yarn add react-router-i18n` 12 | 13 | Note that this package has the following peer dependencies: 14 | 15 | ```json 16 | { 17 | "react": "^16.8.4", 18 | "react-dom": "^16.8.1", 19 | "react-router-dom": "^4.3.0 || ^5.0.0" 20 | } 21 | ``` 22 | 23 | (See project on [npm](https://www.npmjs.com/package/react-router-i18n)) 24 | 25 | ## How to use 26 | 27 | ### Demo 28 | 29 | See the demo [App.js](/src/demo/App.js) and [I18n.js](/src/demo/I18n.js). 30 | 31 | ### Setup 32 | 33 | First, create a component called `I18n`. It should look like this: 34 | 35 | ```jsx 36 | import { createI18n } from 'react-router-i18n'; 37 | 38 | // Array of supported locales 39 | // The first in the array is treated as the default locale 40 | const locales = ['en', 'fr']; 41 | 42 | // Dictionary of translations 43 | const translations = { 44 | en: { 45 | hello: 'Hello', 46 | }, 47 | fr: { 48 | hello: 'Bonjour', 49 | } 50 | } 51 | 52 | const I18n = createI18n( 53 | locales, 54 | translations, 55 | ); 56 | 57 | export default I18n; 58 | ``` 59 | 60 | Then make the following changes to your routes: 61 | 62 | ```jsx 63 | import React from 'react'; 64 | import { BrowserRouter as Router, Route } from 'react-router-dom'; 65 | 66 | // Match locales with regular expression containing each locale separated by `|` 67 | const base = '/:locale(en|fr)?'; 68 | 69 | const App = () => ( 70 | 71 | <> 72 | {/* */} 73 | 74 | 75 | {/* */} 76 | 77 | 78 | 79 | ); 80 | 81 | export default App; 82 | ``` 83 | 84 | ### Usage 85 | 86 | Once you have everything set up, you can use the `I18n` component for localization: 87 | 88 | ```jsx 89 | import React from 'react'; 90 | 91 | import I18n from './I18n'; 92 | 93 | const Hello = () => ( 94 |
95 | 96 |
97 | ) 98 | 99 | export default Hello; 100 | ``` 101 | 102 | Since we defined `"hello"` in the `translations` object above, 103 | this component will display "Hello" when the user visits `/hello`, or `/en/hello`. 104 | It will display "Bonjour" when the user visits `/fr/hello`. 105 | 106 | #### Links 107 | 108 | To preserve the locale in links, the library provides `Link`, `NavLink`, and `Redirect` components. 109 | These can be used anywhere the React Router component with the same name is used: 110 | 111 | ```jsx 112 | import React from 'react'; 113 | import { Link, NavLink, Redirect } from 'react-router-i18n'; 114 | 115 | import I18n from './I18n'; 116 | 117 | const Hello = () => ( 118 |
119 | 120 | 121 | 122 | 123 | 124 |
125 | ); 126 | 127 | export default Hello; 128 | ``` 129 | 130 | To create a link that does not preserve the locale, use `ignoreLocale`: 131 | 132 | ```jsx 133 | import React from 'react'; 134 | import { Link, NavLink, Redirect } from 'react-router-i18n'; 135 | 136 | import I18n from './I18n'; 137 | 138 | const Hello = () => ( 139 |
140 | 141 | 142 | 143 | English 144 | 145 | 146 | French 147 | 148 |
149 | ); 150 | 151 | export default Hello; 152 | ``` 153 | 154 | ##### Note 155 | 156 | A `Link` must be used inside of a route that receives the `locale` param. For example: 157 | 158 | ```jsx 159 | import React from 'react'; 160 | import { BrowserRouter as Router, Route } from 'react-router-dom'; 161 | import { Link } from 'react-router-i18n'; 162 | 163 | const base = '/:locale(en|fr)?'; 164 | 165 | const App = () => ( 166 | 167 | <> 168 | {/* This will not work because it does not receive `locale` */} 169 | {/* Home */} 170 | 171 | {/* Use code like this instead */} 172 | Home} /> 173 | 174 | 175 | 176 | 177 | 178 | ); 179 | 180 | export default App; 181 | ``` 182 | 183 | #### Other functionality 184 | 185 | ##### Nested translations 186 | 187 | The `translations` object can have objects inside of it. For example: 188 | 189 | ```javascript 190 | const translations = { 191 | en: { 192 | home: { 193 | title: 'Home', 194 | } 195 | }, 196 | fr: { 197 | home: { 198 | title: 'Accueil', 199 | } 200 | }, 201 | }; 202 | ``` 203 | 204 | These can be used by giving the `I18n` component `t="home.title"`: 205 | 206 | ```jsx 207 | 208 | ``` 209 | 210 | ##### Function translations 211 | 212 | The `translations` object can have a function as the translation. For example: 213 | 214 | ```javascript 215 | const translations = { 216 | en: { 217 | time: ({ hour, minute }) => `${hour}:${minute}`, 218 | }, 219 | fr: { 220 | time: ({ hour, minute }) => `${hour}h${minute}`, 221 | }, 222 | }; 223 | ``` 224 | 225 | An argument can be passed to the function by giving the `I18n` component `args`: 226 | 227 | ```jsx 228 | 229 | ``` 230 | 231 | ##### Translations outside of <I18n> 232 | 233 | Translation text can also be retrieved outside of an `I18n` component using the `getTranslation` function. For example: 234 | 235 | ```javascript 236 | const translations = { 237 | en: { 238 | search: 'Search...' 239 | }, 240 | fr: { 241 | search: 'Rechercher...', 242 | }, 243 | } 244 | ``` 245 | 246 | ```jsx 247 | const { location } = this.props; 248 | 249 | 250 | ``` 251 | 252 | Note that 253 | [location](https://github.com/ReactTraining/react-router/blob/0f5d701648568cf95bef66c9be0798c15eef6d50/packages/react-router/docs/api/location.md) 254 | needs to be passed in as the first argument. 255 | The `location` prop is passed to a component from React Router. 256 | 257 | The `getTranslation` function takes an optional third argument that corresponds to the `args` attribute of the I18n component: 258 | 259 | ```jsx 260 | const { location } = this.props; 261 | 262 | 263 | ``` 264 | 265 | ##### Default translation 266 | 267 | If there is no translation found for the given locale, the library will look in the following places: 268 | 269 | 1. The same `t` in the default locale (i.e. the first locale in the locales array). 270 | 2. The children of the `` component. 271 | 3. The `missingText` passed to the `createI18n` function. By default this is `false`, so if no translation text is found, the `` component will simply not render. 272 | 273 | For example, given the following `I18n` component: 274 | 275 | ```jsx 276 | import { createI18n } from 'react-router-i18n'; 277 | 278 | // Array of supported locales 279 | // The first in the array is treated as the default locale 280 | const locales = ['en', 'fr']; 281 | 282 | // Dictionary of translations 283 | const translations = { 284 | en: { 285 | hello: 'Hello', 286 | }, 287 | fr: { 288 | goodbye: 'Au revoir', 289 | } 290 | }; 291 | 292 | const I18n = createI18n( 293 | locales, 294 | translations, 295 | 'Unknown text', 296 | ); 297 | 298 | export default I18n; 299 | ``` 300 | 301 | Used in this way: 302 | 303 | ```jsx 304 | <> 305 | 306 | 307 | Goodbye 308 | 309 | 310 | 311 | ``` 312 | 313 | The page when viewed with `/fr` will show: 314 | 315 | ``` 316 | Hello 317 | Au revoir 318 | Unknown text 319 | ``` 320 | 321 | With `/en` it will show: 322 | 323 | ``` 324 | Hello 325 | Goodbye 326 | Unknown text 327 | ``` 328 | 329 | ## Development 330 | 331 | ### Installation 332 | 333 | `yarn install` 334 | 335 | ### Running tests 336 | 337 | `yarn test` 338 | 339 | ### Running demo application 340 | 341 | `yarn start` 342 | 343 | ### Deploying demo application 344 | 345 | `yarn deploy` 346 | 347 | ### Building 348 | 349 | `yarn build` 350 | 351 | ### Publishing 352 | 353 | ``` 354 | yarn build 355 | npm publish 356 | ``` 357 | 358 | (Make sure to update the `version` in `package.json` before publishing a new release.) 359 | 360 | ## Upgrading Notes 361 | 362 | This library is build on top of [DimiMikadze/create-react-library](https://github.com/DimiMikadze/create-react-library). 363 | To upgrade to the latest version of `create-react-library`: 364 | 365 | * In `package.json`, everything above `devDependencies` should not be updated, 366 | but everything below it should be replaced by the new versions in `create-react-library`. 367 | * Add back the dependencies for the project 368 | * All of the files in `./scripts` should be replaced with new versions in `create-react-library`. 369 | * All of the files in `./config` should be replaced with new versions in `create-react-library`. 370 | * Test to make sure that building and deploying demo application still work 371 | -------------------------------------------------------------------------------- /config/env.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const fs = require('fs'); 4 | const path = require('path'); 5 | const paths = require('./paths'); 6 | 7 | // Make sure that including paths.js after env.js will read .env variables. 8 | delete require.cache[require.resolve('./paths')]; 9 | 10 | const NODE_ENV = process.env.NODE_ENV; 11 | if (!NODE_ENV) { 12 | throw new Error( 13 | 'The NODE_ENV environment variable is required but was not specified.' 14 | ); 15 | } 16 | 17 | // https://github.com/bkeepers/dotenv#what-other-env-files-can-i-use 18 | var dotenvFiles = [ 19 | `${paths.dotenv}.${NODE_ENV}.local`, 20 | `${paths.dotenv}.${NODE_ENV}`, 21 | // Don't include `.env.local` for `test` environment 22 | // since normally you expect tests to produce the same 23 | // results for everyone 24 | NODE_ENV !== 'test' && `${paths.dotenv}.local`, 25 | paths.dotenv, 26 | ].filter(Boolean); 27 | 28 | // Load environment variables from .env* files. Suppress warnings using silent 29 | // if this file is missing. dotenv will never modify any environment variables 30 | // that have already been set. 31 | // https://github.com/motdotla/dotenv 32 | dotenvFiles.forEach(dotenvFile => { 33 | if (fs.existsSync(dotenvFile)) { 34 | require('dotenv').config({ 35 | path: dotenvFile, 36 | }); 37 | } 38 | }); 39 | 40 | // We support resolving modules according to `NODE_PATH`. 41 | // This lets you use absolute paths in imports inside large monorepos: 42 | // https://github.com/facebookincubator/create-react-app/issues/253. 43 | // It works similar to `NODE_PATH` in Node itself: 44 | // https://nodejs.org/api/modules.html#modules_loading_from_the_global_folders 45 | // Note that unlike in Node, only *relative* paths from `NODE_PATH` are honored. 46 | // Otherwise, we risk importing Node.js core modules into an app instead of Webpack shims. 47 | // https://github.com/facebookincubator/create-react-app/issues/1023#issuecomment-265344421 48 | // We also resolve them to make sure all tools using them work consistently. 49 | const appDirectory = fs.realpathSync(process.cwd()); 50 | process.env.NODE_PATH = (process.env.NODE_PATH || '') 51 | .split(path.delimiter) 52 | .filter(folder => folder && !path.isAbsolute(folder)) 53 | .map(folder => path.resolve(appDirectory, folder)) 54 | .join(path.delimiter); 55 | 56 | // Grab NODE_ENV and REACT_APP_* environment variables and prepare them to be 57 | // injected into the application via DefinePlugin in Webpack configuration. 58 | const REACT_APP = /^REACT_APP_/i; 59 | 60 | function getClientEnvironment(publicUrl) { 61 | const raw = Object.keys(process.env) 62 | .filter(key => REACT_APP.test(key)) 63 | .reduce( 64 | (env, key) => { 65 | env[key] = process.env[key]; 66 | return env; 67 | }, 68 | { 69 | // Useful for determining whether we’re running in production mode. 70 | // Most importantly, it switches React into the correct mode. 71 | NODE_ENV: process.env.NODE_ENV || 'development', 72 | // Useful for resolving the correct path to static assets in `public`. 73 | // For example, . 74 | // This should only be used as an escape hatch. Normally you would put 75 | // images into the `src` and `import` them in code to get their paths. 76 | PUBLIC_URL: publicUrl, 77 | } 78 | ); 79 | // Stringify all values so we can feed into Webpack DefinePlugin 80 | const stringified = { 81 | 'process.env': Object.keys(raw).reduce((env, key) => { 82 | env[key] = JSON.stringify(raw[key]); 83 | return env; 84 | }, {}), 85 | }; 86 | 87 | return { raw, stringified }; 88 | } 89 | 90 | module.exports = getClientEnvironment; 91 | -------------------------------------------------------------------------------- /config/jest/cssTransform.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | // This is a custom Jest transformer turning style imports into empty objects. 4 | // http://facebook.github.io/jest/docs/tutorial-webpack.html 5 | 6 | module.exports = { 7 | process() { 8 | return 'module.exports = {};'; 9 | }, 10 | getCacheKey() { 11 | // The output is always the same. 12 | return 'cssTransform'; 13 | }, 14 | }; 15 | -------------------------------------------------------------------------------- /config/jest/fileTransform.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const path = require('path'); 4 | 5 | // This is a custom Jest transformer turning file imports into filenames. 6 | // http://facebook.github.io/jest/docs/tutorial-webpack.html 7 | 8 | module.exports = { 9 | process(src, filename) { 10 | return `module.exports = ${JSON.stringify(path.basename(filename))};`; 11 | }, 12 | }; 13 | -------------------------------------------------------------------------------- /config/paths.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const path = require('path'); 4 | const fs = require('fs'); 5 | const url = require('url'); 6 | 7 | // Make sure any symlinks in the project folder are resolved: 8 | // https://github.com/facebook/create-react-app/issues/637 9 | const appDirectory = fs.realpathSync(process.cwd()); 10 | const resolveApp = relativePath => path.resolve(appDirectory, relativePath); 11 | 12 | const envPublicUrl = process.env.PUBLIC_URL; 13 | 14 | function ensureSlash(inputPath, needsSlash) { 15 | const hasSlash = inputPath.endsWith('/'); 16 | if (hasSlash && !needsSlash) { 17 | return inputPath.substr(0, inputPath.length - 1); 18 | } else if (!hasSlash && needsSlash) { 19 | return `${inputPath}/`; 20 | } else { 21 | return inputPath; 22 | } 23 | } 24 | 25 | const getPublicUrl = appPackageJson => 26 | envPublicUrl || require(appPackageJson).homepage; 27 | 28 | // We use `PUBLIC_URL` environment variable or "homepage" field to infer 29 | // "public path" at which the app is served. 30 | // Webpack needs to know it to put the right