2017-08-13 69 views
2

我想在加載器和運行時代碼之間進行通信。具體而言,我想編寫一個加載器,該加載器存儲已加載到可在運行時讀取的變量中的所有CSS。下面是一些僞代碼來說明:將函數注入到Webpack中,如DefinePlugin

myLoader.js

module.exports = function(content) { 
    // This should store the content accessible to the runtime code 
    storeCss(content); 
    return content; 
}; 

app.js

// This should load the CSS as stored by the loader 
const css = getStoredCss(); 

例如與webpack.DefinePlugin我可以這樣做:

new webpack.DefinePlugin({ 
    SOME_GLOBALLY_AVAILABLE_CONST: JSON.stringify('my value'), 
}), 

現在在我的裝載程序和我的運行時代碼中,我可以訪問SOME_GLOBALLY_AVAILABLE_CONST並獲得'my value'。是否可以編寫一個插件來執行相同的操作,但是實現storeCssgetStoredCss,這樣我就可以在我的加載程序和運行時代碼中訪問它們了?

回答

0
new webpack.DefinePlugin({ 
    SOME_GLOBALLY_AVAILABLE_FUNCTION_THAT_PROBABLY_SHOULDNT_BE_ALL_CAPS: require('./myLoader.js').toString(), 
}), 
相關問題