Coder Social home page Coder Social logo

Comments (4)

jpuncle avatar jpuncle commented on August 23, 2024 2

最终方案:采用 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.

jpuncle avatar jpuncle commented on August 23, 2024 1

ProvidePlugin, 提供全局的变量,在模块中使用无需用require引入

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

from create-autofe-app.

jpuncle avatar jpuncle commented on August 23, 2024

在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.

jpuncle avatar jpuncle commented on August 23, 2024

将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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.