0.0.10 • Published 3 years ago

auto-import-global-components v0.0.10

Weekly downloads
-
License
MIT
Repository
-
Last release
3 years ago

auto-import-global-components

Requirements

  • Typescript
  • React
  • Components must be exported as any of these:

    • export default class
    • export default function
    • export default const
    • export class
    • export function
    • export const
    • export default
  • The component's file name cannot start with a _ (underscore), all other ts/tsx files will be scanned

    • ./folder/Banner/Banner.tsx or ./folder/Banner/Index.tsx are two examples of files which will be scanned

Usage

Automatically searches for exported components in folders you specify through plugin-registration in webpack.config.

  • Each component found is added as an import statement in each entry file at the very top, if the import statement do not already exists
  • Each component found is added to globalThis object making it available for server side rendering
  • Each component can have multiple exports, such as export class car, export class user... will be imported as:
    • import { car, user, ... } from './folder/file'
  • Each component is then available for server-side rendering through globalThis - Tested via .NET package: React.Web.Mvc4

Latest version

  • Supporting the --watch true to avoid infinite looping
  • Added some caching in case the same import/exports are to be used to save some looping and reading of files
  • Updated readme and docs

Install

  • npm i auto-import-global-components --save-dev

Setup

Navigate to webpack.config.js:

const autoImportGlobalComponentsPlugin = require('auto-import-global-components');
...
new autoImportGlobalComponentsPlugin({
	debug: true/false, //enable/disable some logging output
	clean: true/false, //enable/disable cleanup of "globalThis" added to the entry files, the "import statements" cannot be undone
    rules: [{
        folders: ['./src/Components/'], //find all components in 'Components' folder and its subfolders
        entries: ['src/index.tsx']      //all found components gets imported in these entries, currently supporting only 1 entry file per rule
    }],
})

Example:

Sample structure

Project:
node_modules/...
dist/...
src/Modules/HeroBanner/Index.tsx //contains: export default class HeroBanner...
src/Components/Banner/Banner.tsx //contains: export default const Banner...
src/index.tsx
tsconfig.json
webpack.config.js

webpack.config.js

Navigate to webpack.config.js and add:

1

const autoImportGlobalComponentsPlugin = require('auto-import-global-components');

2

plugins: [
    ...
    new autoImportGlobalComponentsPlugin({
		clean: false,	//false, does not remove the imports/global declarations after build
		debug: true,	//true, outputs more logging info that can be turned off
		rules: [
			{
			  folders: ['./src/Components/'],
			  entries: ['./src/index.tsx']
			},
			{
			  folders: ['./src/Modules/'],
			  entries: ['./src/index.tsx']
			}
		]
    })
    ...
]

3

Before compilation the src\index.tsx file will get new import statements at the top of the file, and at the bottom of the file globalThis initialization per component found

  • The globalThis initialization can be removed if 'clean' is true

Future

Lisence

  • Free forever, copy paste as you'd like
0.0.10

3 years ago

0.0.9

3 years ago

0.0.8

3 years ago

0.0.7

3 years ago

0.0.6

3 years ago

0.0.5

3 years ago

0.0.4

3 years ago

0.0.3

3 years ago

0.0.2

3 years ago

0.0.1

3 years ago