Comments (4)
最终方案:采用 externals
,支持在外部引入 jQuery 。
index.html
<script src="https://s.autoimg.cn/as/jquery/1.12.4/jquery.js"></script>
<script src="js/index.js"></script>
index.entry.js
import $ from 'jquery';
$(function () {
console.log('jQuery document.ready');
});
creator.config.js
module.exports = {
externals: {
jquery: 'jQuery'
}
}
from create-autofe-app.
ProvidePlugin, 提供全局的变量,在模块中使用无需用require引入
new webpack.ProvidePlugin({
$: 'jquery',
jQuery: 'jquery'
})
from create-autofe-app.
在html中用script标签引入js文件,如
<script src="https://code.jquery.com/jquery-git2.min.js"></script>
然后再配置文件中添加externals
externals: { jquery: 'jQuery' }
该字段的作用是将加jQuery全局变量变为模块可引入。然后在各个模块中,就可以如下使用:
const $ = require('jquery');
from create-autofe-app.
将jQuery代码保存到本地,在配置文件中添加:
resolve: { alias: { jquery: '/path/to/jquery-git2.min.js' } }
即为jquery添加了别名,然后在模块中也是这样使用:
const $ = require('jquery');
还可以配合使用ProvidePlugin,这样就不需要在模块中通过require引入,例如:
使用前:
var $ = require('jquery');
$(...);
使用后:
plugins: [
new webpack.ProvidePlugin({
$: 'jquery'
})
]
// If you use "$", jquery is automatically required
$.(...)
from create-autofe-app.
Related Issues (20)
- nunjucks render 模板不支持数据填充 HOT 2
- 解决node-sass安装失败问题 HOT 1
- import() 需要 webpack 的 publicPath 配置 HOT 2
- ES特性以及Babel支持情况和方案梳理 HOT 1
- 是否支持全局安装,或者提供桌面 App HOT 3
- 统一规划自定义配置需求 HOT 2
- 压缩 css 时遇到将 null 吞掉的情况 HOT 1
- 打包时代码错误定位不到具体问题
- 打包时Eslint检测适当放宽
- 请加入自动 ESLint 验证 HOT 3
- 2018 RoadMap HOT 1
- Autoprefixer 是否是时候更新一波了 HOT 2
- 不支持es6语法 HOT 1
- 加入 webp 和 srcset 的后处理
- 关于 js 打包策略 HOT 2
- 图片更新不及时,其他类型文件,比如mp3打包时没有在build中
- 解决node-sass安装失败问题
- 错误信息返回到页面 HOT 1
- 3000端口号可以配置吗? HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from create-autofe-app.