2016-09-06 80 views
0

我目前有一個js文件中的幾個變量,並希望將它放在我的主app.bundle.js之外的它自己的包中。如何排除捆綁中的js文件並將其用於不同的塊/捆綁包中?

config.js

export const url1= 'testing1'; 
export const url2= 'test2'; 

我有這樣一個非常基本的WebPack並添加新條目(這只是一個例子)

module.exports = { 
{ 
entry: { 
    app: [path.resolve(__dirname, './src/index.js')], 
    config: [path.resolve(__dirname, './config.js')] <---here 
}, 
output: { 
    filename: '[name].js', 
    path: __dirname + '/built' 
    } 
} 

module.loaders: [ 
{ 
    // "test" is commonly used to match the file extension 
    test: /\.jsx$/, 

    // "include" is commonly used to match the directories 
    include: [ 
    path.resolve(__dirname, "app/src"), 
    path.resolve(__dirname, "app/test") 
], 

    // "exclude" should be used to exclude exceptions 
    // try to prefer "include" when possible 

    // the "loader" 
    loader: "babel-loader" 
    }] 

最後我有一個src/file.js想要使用config.js(我目前正在這樣做,但我不知道這是否正確)

import { url1, url2} from '../../config.js'; 

我的應用程序包w病毒捆綁從我的index.js(這是所有my/src .js文件)擴展的所有js文件。 我的config.js位於src文件夾之外,但是我希望我的一個來自src的js文件能夠使用我在config.js中設置的那些變量。

我的結果確實產生了一個帶變量的conf.bundle.js,但它似乎也被捆綁到了app.bundle.js中。

所以現在我的問題是如何將我能夠用我的app.bundle.js變量config.bundle.js沒有在app.bundle.js有config.js的副本

回答

0

添加CommonChunksPlugin解決了這個問題。

+0

這將是很好的解釋爲什麼它解決了這個問題。鏈接只有答案很麻煩。 – ceebreenk