在 webpack 中导入 jQuery 插件

丹尼尔·泰特

我搜索并发现了很多关于 angular 或 react 的文章。我的项目不包括这些。

我只是想使用 webpack 将旧的 jquery 模块导入到 js 文件中。

我使用这种方法包含了 jquery:

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

这有效。我可以在我需要的文件中访问 jquery。

但是,当我尝试导入脚本时,例如:

import coverflow from '../vendor/coverflow/dist/coverflow';

来源:https : //github.com/coverflowjs/coverflow

我收到浏览器错误

Cannot read property 'createElement' of undefined

当我查看此内容时,我看到“未定义”指的是文档。文档未定义怎么办?为什么这个导入不起作用。

使用方法:

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

我收到此错误:

[0] Invalid configuration object. Webpack has been initialised using a configuration object that does not match the API schema.
[0]  - configuration has an unknown property 'rules'. These properties are valid:
[0]    object { amd?, bail?, cache?, context?, dependencies?, devServer?, devtool?, entry, externals?, loader?, module?, name?, node
?, output?, performance?, plugins?, profile?, recordsInputPath?, recordsOutputPath?, recordsPath?, resolve?, resolveLoader?, stats?,
 target?, watch?, watchOptions? }
[0]    For typos: please correct them.
[0]    For loader options: webpack 2 no longer allows custom properties in configuration.
[0]      Loaders should be updated to allow passing options via loader options in module.rules.
[0]      Until loaders are updated one can use the LoaderOptionsPlugin to pass these options to the loader:
[0]      plugins: [
[0]        new webpack.LoaderOptionsPlugin({
[0]          // test: /\.xxx$/, // may apply this only for some modules
[0]          options: {
[0]            rules: ...
[0]          }
[0]        })
[0]      ]

奥洛尔

很可能你正在使用预计库设置为窗口,但它不是。我在使用Modernizr 时遇到了同样的问题,并使用import-loader解决了它,如下所示:

module: {
  rules: [
  {
    test: require.resolve('modernizr'),
    use: [
      'expose-loader?Modernizr',
      'imports-loader?this=>window!exports-loader?window.Modernizr'
    ]
  }
}

重要的部分是this=>window您可以在此处阅读有关我如何解决它的更多详细信息

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章