kenanpengyou / express-webpack-full-live-reload-example

A workflow with full live reload for webpack&express application.
279 stars 88 forks source link

`npm install jquery --save`安装的jquery如何引入到项目中呢? #3

Closed simplecooler closed 8 years ago

simplecooler commented 8 years ago

commom.jsrequire("node_modules/jquery/dist.jquery.js");会报错

kenanpengyou commented 8 years ago

@simplecooler 直接用 var $ = require("jquery");

simplecooler commented 8 years ago

这样引入之后,在各页面中的js文件里使用$会报ReferenceError: Can't find variable: $,感觉没有全局引入,而且这样引入jquery之后跑npm run production会有两个warning

kenanpengyou commented 8 years ago

@simplecooler 常规的CommonJs风格的用法是任何时候需要用到jquery,都require()一次。 不过,全局准备好jquery可以更方便。推荐的做法是在webpack的配置文件的plugins增加以下内容:

new webpack.ProvidePlugin({
    $: 'jquery',
    jQuery: 'jquery'
}),

这是webpack的插件,详情可以参照官方文档。这里表示把require('jquery')的内容作为所有bundle module共用的内容,输出为$jQuery两个变量。

simplecooler commented 8 years ago

谢谢帮助,现在终于都理顺了,开发流很不错。