GVKun编程网logo

使用Webpack将jQuery公开到真实的Window对象(webpack jquery)

18

以上就是给各位分享使用Webpack将jQuery公开到真实的Window对象,其中也会对webpackjquery进行解释,同时本文还将给你拓展javascript–jquery.mockjax使用

以上就是给各位分享使用Webpack将jQuery公开到真实的Window对象,其中也会对webpack jquery进行解释,同时本文还将给你拓展javascript – jquery.mockjax使用Webpack需要垫片、javascript – webpack jquery插件加载自己的jquery实例、javascript – 使用Webpack将jQuery暴露给真正的Window对象、javascript – 关于新Window对象的Jquery?等相关知识,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!

本文目录一览:

使用Webpack将jQuery公开到真实的Window对象(webpack jquery)

使用Webpack将jQuery公开到真实的Window对象(webpack jquery)

我不会将jQuery对象公开给浏览器中开发人员控制台内部可访问的全局窗口对象。现在在我的webpack配置中,有以下几行:

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

这些行将jQuery定义添加到我的webpack模块中的每个文件中。但是,当我构建项目并尝试在开发者控制台中访问jQuery时,如下所示:

window.$;window.jQuery;

它说这些属性是不确定的…

有没有办法来解决这个问题?

答案1

小编典典

您需要使用expose-loader.。

npm install expose-loader --save-dev

您可以在需要时执行以下操作:

require("expose?$!jquery");

或者您可以在配置中执行以下操作:

loaders: [    { test: require.resolve(''jquery''), loader: ''expose?jQuery!expose?$'' }]

更新 :从webpack 2开始,您需要使用 expose-loader 而不是 expose

module: {    rules: [{        test: require.resolve(''jquery''),        use: [{            loader: ''expose-loader'',            options: ''$''        }]    }]}

javascript – jquery.mockjax使用Webpack需要垫片

javascript – jquery.mockjax使用Webpack需要垫片

有没有人使用mockjax进行webpack的茉莉花单元测试?我一直得到$.mockjax是未定义的.它与某些事情有关

//注意:要使用Mockjax作为Node模块,你必须为工厂提供//有效版本的jQuery和一个window对象(全局作用域):// var mockjax = require(‘jquery.mockjax’)(jQuery,窗口);

但我不能在AMD模块中使用require.我试图使用垫片和webpack的import-loader,但我可能做错了,因为我不完全理解这个问题.

解决方法

这是相当古老的,但我错过了它,因为缺乏mockjax标签!无论如何,这应该在 PR #284中得到解决.有 an issue about this with some suggestions,具体来说:

{
    test: require.resolve('jquery-mockjax/dist/jquery.mockjax.js'),use: ['imports-loader?this=>window,exports=>""']
}

javascript – webpack jquery插件加载自己的jquery实例

javascript – webpack jquery插件加载自己的jquery实例

我一直在尝试通过webpack加载一个jquery插件.
此插件打包为npm模块,其依赖项仅包含jquery.
我认为webpack加载了jquery的实例,而不是使用我提供的全局提供的ProvidePlugin.
我尝试了另一个stackoverflow帖子(Managing jQuery plugin dependency in webpack)中提供的所有解决方案,但他们没有成功;结果总是一样的:“terminal()不是函数”.
如果我手动修改node_modules文件夹中的包,删除package.json中的jquery依赖项,并在node_modules插件文件夹中下载的依赖项webpack成功地将该插件与jquery的全局实例绑定.
我知道,我可以简单地创建该包的一个分支并使用私有的npm存储库,但我想使用官方包.

这是我的webpack配置:

var webpack = require('webpack');
var HtmlWebpackPlugin = require('html-webpack-plugin');
var CleanWebpackPlugin = require('clean-webpack-plugin');
var copyWebpackPlugin = require('copy-webpack-plugin');
var ExtractTextPlugin = require('extract-text-webpack-plugin');
var moment = require('moment');
var path = require('path');
var environment = process.env.APP_ENVIRONMENT || 'dev';

module.exports = {

  entry: {
    'app': './src/main.ts',
    'polyfills': './src/polyfills.ts',
    'vendor': './src/vendor.ts'
  },
  /*devtool: 'source-map',*/
  output: {
    path: './dist',
    filename: '[name].browser.' + moment().format('DDMMYYYYHHmm') + '.js'
  },
  module: {
    loaders: [
      { test: /\.component.ts$/, loader: 'ts!angular2-template' },
      { test: /\.ts$/, exclude: /\.component.ts$/, loader: 'ts' },
      { test: /\.html$/, loader: 'raw-loader' },
      { test: /\.css$/, include: path.resolve('src/app'), loader: 'raw-loader' },
      {
        test: /\.css$/, exclude: path.resolve('src/app'), loader: ExtractTextPlugin.extract('style', 'css', {
          fallbackLoader: "style-loader",
          loader: "css-loader"
        })
      },
      { test: /\.(png|jpe?g|gif|ico)$/, loader: 'file?name=fonts/[name].[ext]' },
      { test: /\.woff(\?v=\d+\.\d+\.\d+)?$/, loader: "url?limit=10000&mimetype=application/font-woff&name=fonts/[name].[ext]" },
      { test: /\.woff2(\?v=\d+\.\d+\.\d+)?$/, loader: "url?limit=10000&mimetype=application/font-woff&name=fonts/[name].[ext]" },
      { test: /\.ttf(\?v=\d+\.\d+\.\d+)?$/, loader: "url?limit=10000&mimetype=application/octet-stream&name=fonts/[name].[ext]" },
      { test: /\.eot(\?v=\d+\.\d+\.\d+)?$/, loader: "file?name=fonts/[name].[ext]" },
      { test: /\.svg(\?v=\d+\.\d+\.\d+)?$/, loader: "url?limit=10000&mimetype=image/svg+xml&name=fonts/[name].[ext]" },
    ]
  },
  resolve: {
    extensions: ['', '.js', '.ts', '.html', '.css']
  },
  plugins: [
    new webpack.optimize.CommonsChunkPlugin({
      name: ['vendor', 'polyfills']
    }),
    new HtmlWebpackPlugin({
      template: './src/index.html'
    }),
    new webpack.DefinePlugin({
      app: {
        environment: JSON.stringify(environment),
        config: JSON.stringify(require('./profile/' + environment + ".profile.js"))
      }
    }),
    new CleanWebpackPlugin(
      ['dist']
    ),
    new copyWebpackPlugin([
      { from: './src/images', to: 'images' }
    ]),
    new ExtractTextPlugin('[name].browser.css'),
    new webpack.optimize.UglifyJsPlugin({ minimize: true }),
    new webpack.ProvidePlugin({
      $: "jquery",
      jQuery: "jquery",
      "window.jQuery": "jquery"
    })
  ]
};

这里是jquery.terminal package.json:

{
  "_args": [
    [
      {
        "raw": "jquery.terminal@0.11.11",
        "scope": null,
        "escapedname": "jquery.terminal",
        "name": "jquery.terminal",
        "rawSpec": "0.11.11",
        "spec": "0.11.11",
        "type": "version"
      },
      "/home/giovanni/Projects/Private/site"
    ]
  ],
  "_from": "jquery.terminal@0.11.11",
  "_id": "jquery.terminal@0.11.11",
  "_inCache": true,
  "_installable": true,
  "_location": "/jquery.terminal",
  "_nodeVersion": "4.2.6",
  "_npmOperationalInternal": {
    "host": "packages-16-east.internal.npmjs.com",
    "tmp": "tmp/jquery.terminal-0.11.11.tgz_1475868856610_0.3736777463927865"
  },
  "_npmUser": {
    "name": "jcubic",
    "email": "jcubic@onet.pl"
  },
  "_npmVersion": "3.5.2",
  "_phantomChildren": {},
  "_requested": {
    "raw": "jquery.terminal@0.11.11",
    "scope": null,
    "escapedname": "jquery.terminal",
    "name": "jquery.terminal",
    "rawSpec": "0.11.11",
    "spec": "0.11.11",
    "type": "version"
  },
  "_requiredBy": [
    "#USER",
    "/"
  ],
  "_resolved": "https://registry.npmjs.org/jquery.terminal/-/jquery.terminal-0.11.11.tgz",
  "_shasum": "eaeed2f8f305ac0477d71ef492e7d98d6064d812",
  "_shrinkwrap": null,
  "_spec": "jquery.terminal@0.11.11",
  "_where": "/home/giovanni/Projects/Private/site",
  "author": {
    "name": "Jakub Jankiewicz",
    "email": "jcubic@onet.pl",
    "url": "http://jakub.jankiewi.cz"
  },
  "bugs": {
    "url": "https://github.com/jcubic/jquery.terminal/issues"
  },
  "dependencies": {
    "jquery": "^2.1.4"
  },
  "description": "jQuery Terminal Emulator is a plugin for creating command line interpreters in your applications.",
  "devDependencies": {
    "istanbul": "^0.4.3",
    "jasmine": "^2.4.1",
    "jasmine-node": "^1.14.5",
    "jsdom": "^3.1.2"
  },
  "directories": {},
  "dist": {
    "shasum": "eaeed2f8f305ac0477d71ef492e7d98d6064d812",
    "tarball": "https://registry.npmjs.org/jquery.terminal/-/jquery.terminal-0.11.11.tgz"
  },
  "gitHead": "0f2e55a6501d96aa17d42e4fcc071fab906309d8",
  "homepage": "http://terminal.jcubic.pl",
  "keywords": [
    "terminal",
    "emulator",
    "prompt",
    "console",
    "keyboard",
    "type",
    "rpc",
    "input",
    "ui"
  ],
  "license": "MIT",
  "main": "js/jquery.terminal-0.11.11.js",
  "maintainers": [
    {
      "name": "jcubic",
      "email": "jcubic@onet.pl"
    }
  ],
  "name": "jquery.terminal",
  "optionalDependencies": {},
  "readme": "ERROR: No README data found!",
  "repository": {
    "type": "git",
    "url": "git+https://github.com/jcubic/jquery.terminal.git"
  },
  "scripts": {},
  "version": "0.11.11"
}

解决方法:

我通过稍微修改我的Webpack配置解决了我的问题.
正如这里所建议的(Managing jQuery plugin dependency in webpack),我添加了一个jquery别名.
在提供的示例中,别名是静态指定的,它对我不起作用.
我切换到动态值,这解决了问题.

这是片段:

alias: {
      'jquery': path.resolve(path.join(__dirname, 'node_modules', 'jquery'))
     },

javascript – 使用Webpack将jQuery暴露给真正的Window对象

javascript – 使用Webpack将jQuery暴露给真正的Window对象

我不想将jQuery对象公开给可在浏览器中的开发人员控制台中访问的全局窗口对象.现在在我的webpack配置中,我有以下几行:

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

这些行将jQuery定义添加到我的webpack模块中的每个文件中.
但是,当我构建项目并尝试在开发人员控制台中访问jQuery时,如下所示:

window.$;
window.jQuery;

它说这些属性是未定义的……

有没有办法来解决这个问题?

解决方法:

你需要使用expose-loader.

npm install expose-loader --save-dev

您可以在需要时执行此操作:

require("expose?$!jquery");

或者您可以在配置中执行此操作:

loaders: [
    { test: require.resolve('jquery'), loader: 'expose?jQuery!expose?$' }
]

更新:从webpack 2开始,您需要使用expose-loader而不是公开:

module: {
    rules: [{
        test: require.resolve('jquery'),
        use: [{
            loader: 'expose-loader',
            options: '$'
        }]
    }]
}

javascript – 关于新Window对象的Jquery?

javascript – 关于新Window对象的Jquery?

是否可以在新的Window javascript对象上使用jQuery?

例:

win = new Window('mywindow','width= 400', 'height=400');

win.getContent().innerHTML = xmlFindNodeContent(XmlHttp.responseXML, "windowHtml");

jQuery(win).ready(function(){
  do jQuery stuff on the new window here??
}); 

这样的事情可能吗?

注意:新的Window()函数在正常工作之前需要一些参数.像这样的东西:

window.open( ‘mywindow的’, ‘宽度= 400,高度= 200’)

解决方法:

我认为最好在新窗口上插入jquery.min.js并执行$(function(){// jquery stuff});

今天关于使用Webpack将jQuery公开到真实的Window对象webpack jquery的讲解已经结束,谢谢您的阅读,如果想了解更多关于javascript – jquery.mockjax使用Webpack需要垫片、javascript – webpack jquery插件加载自己的jquery实例、javascript – 使用Webpack将jQuery暴露给真正的Window对象、javascript – 关于新Window对象的Jquery?的相关知识,请在本站搜索。

本文标签: