项目作者: henry-young

项目描述 :
A React-Static plugin that adds support for importing files from external directories
高级语言: JavaScript
项目地址: git://github.com/henry-young/react-static-plugin-include-external-path.git
创建时间: 2019-10-14T02:45:38Z
项目社区:https://github.com/henry-young/react-static-plugin-include-external-path

开源协议:MIT License

下载


react-static-plugin-include-external-path

A React-Static plugin that adds external path support for webpack imports.

This exposes a simple way to share components and functionality between sites in a monorepo without requiring extra compilation, private npm packages or symbolic links.

Installation

In an existing react-static site run:

  1. $ yarn add react-static-plugin-include-external-path

Then add the plugin to your static.config.js with a valid includePath directory and alias in the options:

  1. ...
  2. import path from 'path'
  3. ...
  4. export default {
  5. plugins: [
  6. [
  7. 'react-static-plugin-include-external-path',
  8. {
  9. includePath: path.resolve('../shared'),
  10. alias: "@shared"
  11. },
  12. ],
  13. ],
  14. }

Usage

You will now be able to import files from the ../shared folder in your project using the @shared alias.

Example:

  1. // '../shared/SharedComponent.jsx'
  2. import React from 'react';
  3. export default () => <div>Shared Component</div>

Then import using relative path or alias

  1. // Any component in your "./src" folder
  2. ...
  3. import SharedComponent from '@shared/SharedComponent';
  4. ...
  5. export default () => <SharedComponent></SharedComponent>

Typescript usage

When using this plugin with react-static-plugin-typescript, you must add the includePath and alias to compilerOptions.paths in your sites tsconfig.json

Example:

  1. {
  2. ...
  3. "compilerOptions": {
  4. ...
  5. "paths": {
  6. ...
  7. "@shared/*": ["../shared/*"]
  8. }
  9. }
  10. }