From ad2909c4e564b7a6d1ff7adcd24e3a8326bdd484 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=98yvind=20Saltvik?= Date: Wed, 9 Dec 2020 00:32:01 +0100 Subject: [PATCH] docs: document absolute imports --- website/pages/docs/customization.md | 34 +++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/website/pages/docs/customization.md b/website/pages/docs/customization.md index 81e55394a..6b403aeb8 100644 --- a/website/pages/docs/customization.md +++ b/website/pages/docs/customization.md @@ -336,3 +336,37 @@ Next, place one of the following lines at the very top of `src/client.js:` import 'react-app-polyfill/ie9'; // For IE 9-11 support import 'react-app-polyfill/ie11'; // For IE 11 support ``` + +## Absolute Imports + +You can configure your application to support importing modules using absolute paths. This can be done by configuring a jsconfig.json or tsconfig.json file in the root of your project. If you're using TypeScript in your project, you will already have a tsconfig.json file. + +Below is an example jsconfig.json file for a JavaScript project (or tsconfig.json if you're using TypeScript). You can create the file if it doesn't already exist: + +```json +{ + "compilerOptions": { + "baseUrl": "src" + }, + "include": ["src"] +} +``` + +To make the Jest test runner work with absolute imports, you'll need to add a `jest` configuration option to your package.json: + +```jsonc +{ + "name": "my-razzle-app", + "version": "1.0.0", + "license": "MIT", + /* ...dependencies, etc... */ + "jest": { + "moduleDirectories": ["node_modules", "src"] + } +} +``` + +Now that you've configured your project to support absolute imports, if you want to import a module located at src/components/Button.js, you can import the module like so: +```js +import Button from 'components/Button'; +```