简体   繁体   English

Webpack模块源代码

[英]Webpack module source code

I need to save the code of a es6 code module and dump it. 我需要保存es6代码模块的代码并将其转储。 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);
    }
}

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. 如果模块位于其他文件中,则可以导入两次,一次可以正常运行代码,一次可以通过raw-loader逐字导入文件。

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. 如果您仅测试.js扩展名,则必须覆盖现有的加载程序。

You could use the file-loader in webpack and then require whatever file you need in your function: 您可以在webpack中使用file-loader ,然后需要函数中需要的任何文件:

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);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM