Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Non-default imports #1

Closed
TylorS opened this issue Oct 16, 2015 · 11 comments
Closed

Non-default imports #1

TylorS opened this issue Oct 16, 2015 · 11 comments

Comments

@TylorS
Copy link

TylorS commented Oct 16, 2015

Hello, currently I'm using webpack to provide this kind of functionality. I'm investigating the possibility of removing webpack from my builds, and I came along this project. First I'd like to say thank you for your work on this package.

I was wondering though, would it be possible to do the following.

{
 "extras": {
    "provide-modules": {
        "{Rx}": "@cycle/core"
    }
 }
}

Where it would insert

import {Rx} from '@cycle/core'

This would be an awesome capability, at least in my use case.

Thank you for your time!

@tomatau
Copy link
Owner

tomatau commented Oct 16, 2015

Hi Tylor!

I started this only to experiment with the process of building babel plugins to solve the same global provides functionality of webpack.

However my knowledge of writing babel plugins is very limited! I'm actually not using this anywhere either -- happy to accept PRs though! :)

NB. Does this package actually work for you?

@TylorS
Copy link
Author

TylorS commented Oct 16, 2015

Yes my knowledge is very limited on babel plugins as well, did try to create one once but I didn't find it possible currently.

Thank you for your very quick response, if I do decide to move away from webpack I'll likely see if I'm able to create the above and submit a PR if successful 😄

@vyorkin
Copy link

vyorkin commented Nov 23, 2015

@tomatau @TylorS its not that hard, here is an example (for babel v5.x):

        var reactImportDeclaration = t.importDeclaration([
          t.importDefaultSpecifier(t.identifier('React')),
          t.importSpecifier(t.identifier('Component'), t.identifier('Component')),
          t.importSpecifier(t.identifier('PropTypes'), t.identifier('PropTypes'))
        ], t.literal('react'))

take a look at the babel source, here and here

@tomatau
Copy link
Owner

tomatau commented Nov 23, 2015

Feel free to make a PR.

I'll be looking to hack on this again in around 2 weeks from now (between jobs) but am waiting on gaearon/babel-plugin-react-transform#46 before moving to babel6.

@vyorkin are you using this plugin currently?

@vyorkin
Copy link

vyorkin commented Nov 23, 2015

No, I'm just playing around with babel, nothing serious

@tomat
Copy link

tomat commented Feb 7, 2016

I'll be looking to hack on this again in around 2 weeks from now (between jobs) but am waiting on gaearon/babel-plugin-react-transform#46 before moving to babel6.

@tomatau any chance of updating this for Babel 6? or do you know of an existing solution?

(nice username btw ;-))

Edit: for anyone else ending up here; found this, seems to work: https://www.npmjs.com/package/babel-plugin-module-provider

@TylorS
Copy link
Author

TylorS commented Feb 7, 2016

Hey @tomat, glad that you found my experiment to be useful. My apologies for not sharing it myself, I almost forgot about this thread.

@tomatau
Copy link
Owner

tomatau commented Feb 7, 2016

Oh nice! Thanks @tomat and @TylorS -- I was just sitting down to learn how to do babel 6 plugins with this functionality :D Will take a look!

@TylorS
Copy link
Author

TylorS commented Feb 7, 2016

Only thing I couldn't figure out was how to do import {x as y} from 'wherever' so that would be a great addition I think

@tomatau
Copy link
Owner

tomatau commented Feb 7, 2016

I've just published version 1 for babel6 with the following setup:

{
  "plugins": [
    ["provide-modules", {
      "debug": "debug", // default
      "lodash": ["get", "assign"], // de-structured
      "react-dom": [{"findDOMNode" : "find"}], // de-structured with alias
      // both default and de-structured imports with alias
      "react": {
        "default": "React",
        "destructured": ["PropTypes", {"cloneElement": "clone" }],
      }
    }]
  ]
}

The above will inject the following into every file:

import debug from 'debug';
import {get, assign} from 'lodash';
import {findDOMNode as find} from 'react-dom';
import React, {PropTypes, cloneElement as clone} from 'react';

Thanks @TylorS for your version of it, stole your API as it felt clean -- but didn't want to impose on your code too much and just keen to practice and get this up to date :)

@TylorS
Copy link
Author

TylorS commented Feb 7, 2016

Looks like you have it all figured out 👍

I think it's safe to close this one now, great work!

@TylorS TylorS closed this as completed Feb 7, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants