Code Monkey home page Code Monkey logo

rollup-plugin-root-import's Introduction

rollup-plugin-root-import

Add the ability to import modules by the root path, like babel-root-slash-import.

Build Status

// import a module relative to a specified root directory
import UserModel from '/models/user';

// illustrative - if we were in /views/user, we can reference a model's module
// using an absolute path from the root of the project, rather than relative to
// the current module
class UserView {
  // ...
}

Install

$ yarn add rollup-plugin-root-import

or

$ npm install --save-dev rollup-plugin-root-import

Usage

import { rollup } from 'rollup';
import rootImport from 'rollup-plugin-root-import';

rollup({
  entry: 'client/src/main.js',
  plugins: [
    rootImport({
      // Will first look in `client/src/*` and then `common/src/*`.
      root: `${__dirname}/common/src`,
      useInput: 'prepend',

      // If we don't find the file verbatim, try adding these extensions
      extensions: '.js',
    }),
  ],
});

API

rootImport([options])

Creates a rollup plugin to resolve absolute-pathed imports relative to the project's entry or specified root directory/ies.

Options:

  • root an optional string or ordered array of strings, which represent the roots from which to try and resolve imports. You should specify absolute paths (i.e. using __dirname) in order for downstream Rollup plugins to match module IDs.
  • useInput if provided, should be either 'prepend' or 'append', where each signify that the directory containing the entry should be prepended or appended to the array of roots, respectively. By default, if root is provided and useInput is not provided, the directory of the entry module will not be used to resolve absolute-pathed imports.
  • extensions, if provided, specifies a string or ordered array of strings, each of which represents an extension to append to the resolved file if the import isn't found verbatim.

License

The MIT License (MIT)

Copyright © 2016 Mixmax, Inc (mixmax.com)

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in allcopies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

rollup-plugin-root-import's People

Contributors

bradvogel avatar dependabot[bot] avatar dnechay avatar guilhermemj avatar jsalvata avatar renovate[bot] avatar richard-riverford avatar semantic-release-bot avatar skeggse avatar spencer-brown avatar ttacon avatar wearhere avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

rollup-plugin-root-import's Issues

Action Required: Fix Renovate Configuration

There is an error with this repository's Renovate configuration that needs to be fixed. As a precaution, Renovate will stop PRs until it is resolved.

Error type: Cannot find preset's package (local>mixmaxhq/renovate-config)

Node resolve

Is there a way to make it work with rollup-plugin-node-resolve?

Getting Error: Could not resolve

Usage with Bublé and it's JSX feature

In my project I'm using React and it's JSX format and Bublé trough rollup-plugin-buble with JSX support for compilation.
For some reason using rollup-plugin-root-import to resolve paths breaks Bublé transpilation functionality and Rollup throws an error (In configuration I had to add extensions: ['.js','.jsx']):

Error parsing xxx.jsx: Unexpected token (16:6) in xxx.jsx (this is where JSX markup is used).

There is no error when importing .js files, only the .jsx ones.

I've tried to debug this plugin and it seems that paths are correctly resolved however error pops up only when trying to resolve paths fro jsx files.

It looks like the JSX compilation is omitted for file paths resolved from root.

Tried with:

  • rollup-plugin-root-import: 0.2.1
  • rollup: 0.36.4, 0.37.0
  • buble: 0.14.3, 0.15.1
  • rollup-plugin-buble: 0.14.0, 0.15.0

Default settings do not resolve imports absolutely

When the entry is specified relative to the rollup configuration, subsequent imports will be resolved relative to the unresolved entry, rather than being resolved in absolute terms.

// :: rollup config
import {rollup} from 'rollup';
import rootImport from 'rollup-plugin-root-import';

rollup.rollup({
  entry: 'src/index.js',
  plugins: [rootImport()]
}).then((bundle) => {
  // ...
});

// :: src/index.js
// We'd like this to resolve to /full/path/to/src/utils/other.js, but it
// resolves to src/utils/other.js instead.
import {trivial} from '/utils/other.js';

export default trivial(1, 2);

// :: src/utils/other.js
export function trivial(a, b) {
  return a + b;
}

Yarn not supported

Right now engine entry in package.json prevents all users using yarn to install this package as dependency. Forces me to use npm. Is there any possibility to remove this restriction?

How to make it work with TypeScript?

If I place this plugin before typescript, typescript turns paths into something like '../../../../../../index', if I put it after, it does not recognize syntax like

import type { foo } from 'bar'

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.