Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Webpack in Action (4) #15

Open
cheungseol opened this issue Oct 27, 2017 · 0 comments
Open

Webpack in Action (4) #15

cheungseol opened this issue Oct 27, 2017 · 0 comments
Labels

Comments

@cheungseol
Copy link
Owner

webpack 配置中的外部扩展 Externals

通过配置 externals, 可以防止把某些 import 的包(package)打包到 bundle 中,而是在运行时(runtime)再去从外部获取这些扩展依赖(external dependencies)。

就是说 webpack 可以不处理某些依赖库,使用externals配置后,仍然可以在代码中通过CMD、AMD或者window/global全局的方式访问依赖。

比如在项目中是通过 script 引入 jquery

index.html

<script src="https://code.jquery.com/jquery-3.1.0.js"
  integrity="sha256-slogkvB1K3VOkzAI8QITxV3VzpOnkeNVsKvtkYLMjfk="
  crossorigin="anonymous"></script>

此时,就是运行时加载 jquery ,可以配置 webpack 不把 jquery 打包。

webpack.config.js

output: {
    ...
    libraryTarget: "umd"
}

externals: {
   jquery: 'jQuery'
}

既剥离了不需要改动的依赖模块,同时也不影响下面的书写方式:
使用方式 1 :

import $ from 'jquery';
$('.my-element').animate(...);

使用方式 2 :
const $ = window.jQuery

使用方式 3:

const $ = require("jquery")
$("#content").html("<h1>hello world</h1>")

参考

外部扩展(Externals)

zhengweikeng/blog#10

创建 Library

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant