5

Friends, I'm trying to keep an external file out of the Webpack bundle, but to remain as a dependency - a settings file in this case. I've tried several variations of the following -

externals: {
  'Settings': JSON.stringify(require('./settings.json'))
},

...but Webpack keeps including it in the bundle. The only examples I've found in the docs are of common presets like jQuery, nothing is mentioned of local but external files. Help? Thanks!

1
  • I don't actually understand the question. You either require a file or not. What do you expect to happen? How would be this supposed to work? Commented May 18, 2016 at 8:37

2 Answers 2

2

So after much research, the only way, as of writing this answer, to fetch an external file is to, in a way, ignore Webpack and simply add another script call to the index.html. This is what I ended up doing -

<body>
    <div id="whatever-app-id"></div>
    <script type="text/javascript" src="settings.js"></script>
    <script type="text/javascript" src="app.js" defer></script>
</body>

For good measure, you can also add the webpack externals as mentioned in @gmaliar 's answer with reference to the global object declared in the settings.js file. However that's somewhat redundant as the object is global anyway.

Hope it helped anyone.

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

Comments

1

How about?

// Outside of webpack context
var fs = require("fs");
var settings = JSON.parse(fs.readFileSync('./settings.json', 'utf8'));

// Within webpack context
externals: {
  'Settings': settings
}

2 Comments

What difference does this make?
None. It just embeds the file in the webpack context, completely negating the principle of fetching in separately in load-time. :/ Thanks for the answer though.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.