2.0.1 • Published 2 years ago

@aternus/babel-plugin-include v2.0.1

Weekly downloads
3
License
MIT
Repository
github
Last release
2 years ago

@aternus/babel-plugin-include

Adds support for the include() function to the Babel compiler.

Why use this plugin?

A simple way to organize and reuse code.

The goal of this plugin is to allow a "native" way of including JavaScript code inline - at compilation time.

Installation

npm install -D @aternus/babel-plugin-include @babel/core @babel/cli

.babelrc.json / babel.config.json (Recommended)

{
  "plugins": ["@aternus/babel-plugin-include"]
}

Usage

The include() function takes an argument, a file path, e.g. file.js.

Code

main.js

include('welcome.js');
include('stateManager.js');

welcome.js

console.log('Welcome code');

stateManager.js

console.log('State manager code');

Result after compilation with Babel

console.log('Welcome code');
console.log('State manager code');

FAQ

  • The include() function takes a single string as an argument. Following arguments are ignored.
  • The included file must be a valid JavaScript file. If there are errors the compiler will throw an error.
  • The default encoding is assumed to be utf8.
  • You can use relative and absolute filenames, and change the root directory in plugin options.

Programmatic Invocation

Node API

const transformedCode = require('@babel/core').transform(code, {
  plugins: ['@aternus/babel-plugin-include'],
});

CLI

npx babel --plugins @aternus/babel-plugin-include main.js

Integrations

IDEs / TypeScript

The plugin ships with TypeScript types which the IDEs and TypeScript can use to understand the new include syntax capability without you having to take any action.

⚠ Keep in mind that you'll still need to use Babel as the transpiler (this is a babel plugin after all šŸ˜…)

ESLint

The plugin ships with an ESLint config you can extend to avoid getting errors about include being undefined.

In your .eslintrc.js:

module.exports = {
  extends: [
    // ...
    './node_modules/@aternus/babel-plugin-include/eslint',
  ],
  // ...
};

⚠ ./node_modules is required to ensure that ESLint won't add "eslint-config" to the package name, resulting in a wrong path.

Options

You can provide an options object to modify the default behavior of the plugin.

{
  plugins: [['@aternus/babel-plugin-include', options]];
}

The following options are available:

Option NameTypeDefaultNotes
rootstringcwd() of the fileThe root directory from which all files will be included.
encodingBufferEncodingutf-8The encoding option specifies which encoding to use when including files.

Credits

vihanb for the original package (no longer maintained).

License

Released under the MIT License - see LICENSE.md for details.

2.0.1

2 years ago

2.0.0

2 years ago

1.0.26

2 years ago

1.0.25

2 years ago

1.0.24

2 years ago

1.0.22

3 years ago

1.0.23

3 years ago

1.0.21

3 years ago

1.0.20

4 years ago

1.0.19

4 years ago

1.0.18

5 years ago

1.0.17

5 years ago

1.0.16

6 years ago

1.0.15

6 years ago

1.0.14

6 years ago

1.0.13

6 years ago

1.0.12

6 years ago

1.0.11

7 years ago

1.0.10

7 years ago

1.0.9

7 years ago

1.0.8

7 years ago

1.0.7

7 years ago

1.0.6

7 years ago

1.0.5

7 years ago

1.0.3

7 years ago

1.0.2

7 years ago

1.0.1

7 years ago

1.0.0

7 years ago