2017-08-25 58 views
1

我想我最好用代碼解釋它。我有一個像下面的的WebPack文件:如何從外部導入webpack條目文件的默認導出?

import ReactDOMServer from 'react-dom/server'; 

import Server from './server'; 

import templateFn from './template'; 

export default (req, res) => { 
    const reactString = ReactDOMServer.renderToString(<Server />); 
    const template = templateFn(html); 
    res.send(template); 
}; 

我也有一個明確的應用程序,我想有機會獲得默認的導出函數。如果它有什麼區別,這個文件就是webpack條目文件。以下是我在我的快遞應用嘗試:

const handleRequest = require(path.resolve(webpackConfig.output.path, webpackConfig.output.filename)); 

app.get('*', (req, res) => { 
    console.log(handleRequest); 
}); 

我試圖導入的WebPack產生,希望我將能夠訪問入口文件的默認導出文件。那麼,我錯了,因爲進口的產量是{}

是否有一個webpack插件或某種技術來做我想要構建的東西?我不希望快速應用程序成爲webpack構建的一部分。這是我以這種方式分離代碼的主要原因。

回答

0

我能夠訪問使用庫參數(webpack.config.js)的WebPack的內容:

output: { 
    path: ..., 
    filename: ..., 
    library: 'myapp', 
    libraryTarget: 'commonjs' 
} 

然後訪問代碼:

const output = require(path.resolve(webpackConfig.output.path, webpackConfig.output.filename)); 

const defaultExportFunction = output.myapp.default;