Skip to content

Instantly share code, notes, and snippets.

@iamakulov
Last active August 14, 2021 06:14
Show Gist options
  • Save iamakulov/4217f4d846490519ee15fb7e7166312c to your computer and use it in GitHub Desktop.
Save iamakulov/4217f4d846490519ee15fb7e7166312c to your computer and use it in GitHub Desktop.
Webpack’s externals work with local paths too
// my-app/webpack.config.js
{
externals: {
'jquery': 'jQuery',
/* ↑ With this line, Webpack will replace every
* import $ from 'jquery';
*
* in your code with approximately
* const $ = window.jQuery;
*
* This is useful if you already have jQuery in your global environment
* and don’t want to download it several times.
* Most people use `externals` this way. */
'../foo/index.js': 'MyFoo',
/* ↑ Turns out, aliasing local paths works too!
* This:
* import F from '../foo/index.js';
*
* will be replaced approximatey with
* const F = window.MyFoo;
*
* Useful e. g. if you need to share your module between your old code in the global environment
* and your new code in the current app bundle. */
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment