1.1.1 • Published 4 years ago

babel-plugin-specify-imports v1.1.1

Weekly downloads
6
License
MIT
Repository
github
Last release
4 years ago

Specify Imports

A Babel plugin that allows you to specify your imports based off a config file.

A good use case for this plugin would be a project that will be built many different times, and each build requires a different set of components. Instead of inculding each component for every different build, you can use Specify Imports to import only the required modules for that build.

Installation

$ npm install babel-specify-imports

Usage

In your .babelrc, or wherever your plugins are defined, specify the imports by defining moduleListPath. This path is relative to process.cwd().

{
    plugins: [
        ["specify-imports", {
            "moduleListPath": "path/to/settings.js",
            "extensions" : 'js',
            }
        ]
    ]
}

settings.js should export the list of module names that you want to use

module.exports = {
    moduleList: [
        "myModule",
        "aCustomModule",
        "awesomeComponent"
    ]
}

However, a more useful way to export the module list would be something like this

const settings = {
    views: [
        {
            moduleName: "myModule",
            viewSettings: {}
        },
        {
            moduleName: "aCustomModule",
            viewSettings: {}
        },
        {
            moduleName: "awesomeComponent",
            viewSettings: {}
        }
    ]
}
module.exports = {
    settings: settings, // to be used elsewhere in the project
    moduleList: settings.views.map(view => view.moduleName) // used by specify-import
}

In the file that you want to import your modules into, indicate that you will be using Specify Imports by using the "[list]" identifier and a relative path. Specify Imports will not modify any other import statements.

import myModules from './modules/[list]'; 

This will cause the above import statement to transpile into:

const myModules = {};
import  myThirdModule2 from "./modules/myThirdModule.js";
myModules["myThirdModule"] =  myThirdModule2;
import  myOtherModule1 from "./modules/myOtherModule.js";
myModules["myOtherModule"] =  myOtherModule1;
import  myModule0 from "./modules/myModule.js";
myModules["myModule"] =  myModule0;

The modules can be referenced like any object member

myModules.myOtherModule1.doSomething();

But its typically most useful to iterate through the parent object with a for...in loop, or another method.

for(const myModule in myModules){
    // do something with each imported module
}

Other Features

Specify Imports checks for the existance of a file before including it.

Future Features

  • Allow for granular specification of file types, or, let users associate module names with file extensions
  • Support multiple list identifiers. eg: [list1], [list2]

References

ababacornacorn-globalsacorn-walkajvansi-escapesansi-regexansi-stylesanymatchargparsearr-diffarr-flattenarr-unionarray-equalarray-uniqueasn1assert-plusassign-symbolsastral-regexasynckitatobaws-sign2aws4babel-jestbabel-plugin-istanbulbabel-plugin-jest-hoistbabel-preset-jestbalanced-matchbasebcrypt-pbkdfbrace-expansionbracesbrowser-process-hrtimebrowser-resolvebserbuffer-fromcache-basecallsitescamelcasecapture-exitcaselesschalkci-infoclass-utilscliuicocollect-v8-coveragecollection-visitcolor-convertcolor-namecombined-streamcomponent-emitterconcat-mapconvert-source-mapcopy-descriptorcore-util-iscross-spawncssomcssstyledashdashdata-urlsdebugdecamelizedecode-uri-componentdeep-isdefine-propertiesdefine-propertydelayed-streamdetect-newlinediff-sequencesdomexceptionecc-jsbnemoji-regexend-of-streames-abstractes-to-primitiveescape-string-regexpescodegenesprimaestraverseesutilsexec-shexecaexitexpand-bracketsexpectextendextend-shallowextglobextsprintffast-deep-equalfast-json-stable-stringifyfast-levenshteinfb-watchmanfill-rangefind-upfor-inforever-agentform-datafragment-cachefs.realpathfseventsfunction-bindgensyncget-caller-fileget-streamget-valuegetpassglobglobalsgraceful-fsgrowlyhar-schemahar-validatorhashas-flaghas-symbolshas-valuehas-valueshtml-encoding-snifferhtml-escaperhttp-signaturehuman-signalsiconv-liteimport-localimurmurhashinflightinheritsip-regexis-accessor-descriptoris-bufferis-callableis-ciis-data-descriptoris-date-objectis-descriptoris-extendableis-fullwidth-code-pointis-generator-fnis-numberis-plain-objectis-regexis-streamis-symbolis-typedarrayis-windowsis-wslisarrayisexeisobjectisstreamistanbul-lib-coverageistanbul-lib-instrumentistanbul-lib-reportistanbul-lib-source-mapsistanbul-reportsjest-changed-filesjest-clijest-configjest-diffjest-docblockjest-eachjest-environment-jsdomjest-environment-nodejest-get-typejest-haste-mapjest-jasmine2jest-leak-detectorjest-matcher-utilsjest-message-utiljest-mockjest-pnp-resolverjest-regex-utiljest-resolvejest-resolve-dependenciesjest-runnerjest-runtimejest-serializerjest-snapshotjest-utiljest-validatejest-watcherjest-workerjs-tokensjs-yamljsbnjsdomjsescjson-schemajson-schema-traversejson-stringify-safejson5jsprimkind-ofkleurlevenlevnlocate-pathlodashlodash.mergewithlodash.sortbylolexmake-dirmakeerrormap-cachemap-visitmerge-streammicromatchmime-dbmime-typesmimic-fnmin-indentminimatchminimistmixin-deepmkdirpmsnanomatchnatural-comparenice-trynode-int64node-modules-regexpnode-notifiernormalize-pathnpm-run-pathnwsapioauth-signobject-copyobject-inspectobject-keysobject-visitobject.assignobject.getownpropertydescriptorsobject.pickonceonetimeoptionatorp-each-seriesp-finallyp-limitp-locatep-tryparse5pascalcasepath-existspath-is-absolutepath-keypath-parseperformance-nowpicomatchpiratespkg-dirpnposix-character-classesprelude-lspretty-formatpromptspslpumppunycodeqsreact-isrealpath-nativeregex-notremove-trailing-separatorrepeat-elementrepeat-stringrequestrequest-promise-corerequest-promise-nativerequire-directoryrequire-main-filenameresolveresolve-cwdresolve-fromresolve-urlretrimrafrsvpsafe-buffersafe-regexsafer-buffersanesaxessemverset-blockingset-valueshebang-commandshebang-regexshellwordssignal-exitsisteransislashsnapdragonsnapdragon-nodesnapdragon-utilsource-mapsource-map-resolvesource-map-supportsource-map-urlsplit-stringsprintf-jssshpkstack-utilsstatic-extendstealthy-requirestring-lengthstring-widthstring.prototype.trimleftstring.prototype.trimrightstrip-ansistrip-bomstrip-eofstrip-final-newlinestrip-indentsupports-colorsupports-hyperlinkssymbol-treeterminal-linktest-excludethroattmplto-fast-propertiesto-object-pathto-regexto-regex-rangetough-cookietr46tunnel-agenttweetnacltype-checktype-detecttype-festtypedarray-to-bufferunion-valueunset-valueuri-jsurixuseutil.promisifyuuidv8-to-istanbulverrorw3c-hr-timew3c-xmlserializerwalkerwebidl-conversionswhatwg-encodingwhatwg-mimetypewhatwg-urlwhichwhich-moduleword-wrapwrap-ansiwrappywrite-file-atomicwsxml-name-validatorxmlcharsy18nyargsyargs-parser
1.1.1

4 years ago

1.1.0

4 years ago

1.0.0

4 years ago