Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

babel-plugin-inline-import

Quadric155.1kMIT3.0.0

Babel plugin to make raw files importable

babel-plugin, graphql, raw, extension, wrap, import, inline

readme

Babel Inline Import Build Status

Babel plugin to add the opportunity to use import with raw/literal content
It is good e.g. for importing *.graphql files into your code.

Examples

Before (without Babel-Inline-Import):

// server.js

// bad syntax highlighting, no syntax checking
const typeDefinitions = `
type Query {
  testString: String
}
schema {
  query: Query
}
`;

graphQLServer({
  schema: [typeDefinitions],
  ...
});

Now (with Babel-Inline-Import):

// /some/schema.graphql
type Query {
  testString: String
}
schema {
  query: Query
}
// server.js
import schema from '/some/schema.graphql';

graphQLServer({
  schema: [schema],
  ...
});

Note: both cases are equivalent and will result in similar code after Babel transpile them. Check How it works section for details.

Install

npm install babel-plugin-inline-import --save-dev

Use

Add a .babelrc file and write:

{
  "plugins": [
    "babel-plugin-inline-import"
  ]
}

or pass the plugin with the plugins-flag on CLI

babel-node myfile.js --plugins babel-plugin-inline-import

By default, Babel-Inline-Import is compatible with the following file extensions:

  • .raw
  • .text
  • .graphql

Customize

If you want to enable different file extensions, you can define them in your .babelrc file

{
  "plugins": [
    ["babel-plugin-inline-import", {
      "extensions": [
        ".json",
        ".sql"
      ]
    }]
  ]
}

How it works

It inserts the content of the imported file directly into the importing file, assigning it to a variable with the same identifier of the import statement, thus replacing the import statement and the file path by its resulting raw content (no parsing occurs).

Caveats

Babel does not track dependency between imported and importing files after the transformation is made. Therefore, you need to change the importing file in order to see your changes in the imported file spread. To overcome this:

Also make sure that your task runner is watching for changes in the imported file as well. You can see it working here.

Motivate

If you like this project just give it a star :) I like stars.

changelog

ChangeLog

v3.0.0

Removed specific Meteor handling that is no longer necessary as of Meteor 1.7.

BREAKING CHANGES: Do not update to this version if you are using this with a Meteor app that is on a version < 1.7. For non-Meteor uses, there are no breaking changes in the 3.0.0 release.

v2.0.6

Add leading comment indicating original file path

v2.0.5

Remove forgotten debugger statement in the built files #5

v2.0.4

Add support for Meteor projects.

v2.0.3

Visit node on exit instead of enter. It allows other plugins like babel-root-slash-import to modify the import path before we load it.

v2.0.2

Improve error for not found files.

v2.0.1

Add forgotten build files to npm package.

v2.0.0

Change approach. Replace import statements by inline content, instead of trying to export raw content according to JS standards.

Forks babel-root-slash-import.

v1.0.0

Try to wrap raw content and export it according to JS standards. Didn't work well. Couldn't manage to stop Babel parsing raw content.

It can be accessed at v1 branch. Forks babel-plugin-iife-wrap.