0

I need to save the code of a es6 code module and dump it. So ideally what I would need is something like:

import React, {PropTypes} from 'react'; // this function get the source code of the // module I am running in function getMyModuleSourceCode = { ... } class DemoComponent extends React.Component { render() { const myCode = getMyModuleSourceCode(); processMySourceCode(myCode); } } 
4
  • I'm not entirely sure what your question is, can you be more precise? At which point do you want to save the code of an ES6 module? How does webpack come into play here? Commented May 10, 2016 at 12:41
  • I updated the text. My guess is that only by using webpack it is possibile in the render function to know the code of the module. Commented May 10, 2016 at 12:57
  • So you want to include the source code of another module (not the compiled javascript) into the DemoComponent? Commented May 10, 2016 at 13:04
  • I want the source code of the current module (not the compiled javascript) so that in the DemoComponent I can make stats / show on video dump on console / whatever. Commented May 10, 2016 at 13:07

2 Answers 2

2

If the module is in a different file, you can import twice, once normally to run code, once with the raw-loader to just pull in the file verbatim.

import module from './module'; //compiled code import moduleSource from '!!raw!./module'; //source as text 

The !! is necessary to override the existing loaders if you are just testing for .js extensions.

Sign up to request clarification or add additional context in comments.

1 Comment

Wao 😿, I wasted like 4 hours of my life searching for this, couldn't find any information about the override !! in the raw-loader documentation.
1

You could use the file-loader in webpack and then require whatever file you need in your function:

import React, {PropTypes} from 'react'; // this function get the source code of the // module I am running in function getMyModuleSourceCode = { return require('!!file!./path/to/module.js'); } class DemoComponent extends React.Component { render() { const myCode = getMyModuleSourceCode(); processMySourceCode(myCode); } } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.