Skip to content
This repository has been archived by the owner on Apr 9, 2020. It is now read-only.

Using babel plugin to dynamically require a resource #5

Closed
gajus opened this issue Sep 3, 2015 · 1 comment
Closed

Using babel plugin to dynamically require a resource #5

gajus opened this issue Sep 3, 2015 · 1 comment

Comments

@gajus
Copy link

gajus commented Sep 3, 2015

Since this is done at the compilation time, I expect the following to work:

import path from 'path';

export default (options) => {
    let dirname,
        info,
        target;

    dirname = path.dirname(options.filename);
    target = path.resolve(dirname, 'index.json');

    info = require(target);

    return (ReactClass) => {
        return ReactClass;
    };
};

But that produces an error in webpack:

WARNING in ../react-transform-css-modules/dist/index.js
Critical dependencies:
23:11-26 the request of a dependency is an expression
 @ ../react-transform-css-modules/dist/index.js 23:11-26

WARNING in Loader /Users/gajus/Documents/dev/gajus/react-transform-css-modules/node_modules/babel/index.js didn't return a function
 @ ../react-transform-css-modules/dist ^\.\/.*$

and an error in the browser:

Uncaught Error: Cannot find module '/Users/gajus/Documents/dev/gajus/babel-react-css-modules-test/src/index.json'.

Is there a way to do it?

@gaearon
Copy link
Owner

gaearon commented Sep 3, 2015

Transform itself executes at module definition time.
At compile time, we only generate the call to it.

That said, you can put some path in imports:

{
  "stage": 0,
  "plugins": [
    "react-transform"
  ],
  "extra": {
    "react-transform": [{
      "target": "react-transform-css-modules",
      "imports": ["./src/index.json"]
    }]
  }
}

This module will be available as options.imports[0] in the transform.

@gajus gajus closed this as completed Sep 3, 2015
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants