html导入es6,如何使用ES6语法导入jquery?

The accepted answer did not work for me

note : using rollup js dont know if this answer belongs here

after

npm i --save jquery

in custom.js

import {$, jQuery} from 'jquery';

or

import {jQuery as $} from 'jquery';

i was getting error :

Module ...node_modules/jquery/dist/jquery.js does not export jQuery

or

Module ...node_modules/jquery/dist/jquery.js does not export $

rollup.config.js

export default {

entry: 'source/custom',

dest: 'dist/custom.min.js',

plugins: [

inject({

include: '**/*.js',

exclude: 'node_modules/**',

jQuery: 'jquery',

// $: 'jquery'

}),

nodeResolve({

jsnext: true,

}),

babel(),

// uglify({}, minify),

],

external: [],

format: 'iife', //'cjs'

moduleName: 'mycustom',

};

instead of rollup inject, tried

commonjs({

namedExports: {

// left-hand side can be an absolute path, a path

// relative to the current directory, or the name

// of a module in node_modules

// 'node_modules/jquery/dist/jquery.js': [ '$' ]

// 'node_modules/jquery/dist/jquery.js': [ 'jQuery' ]

'jQuery': [ '$' ]

},

format: 'cjs' //'iife'

};

package.json

"devDependencies": {

"babel-cli": "^6.10.1",

"babel-core": "^6.10.4",

"babel-eslint": "6.1.0",

"babel-loader": "^6.2.4",

"babel-plugin-external-helpers": "6.18.0",

"babel-preset-es2015": "^6.9.0",

"babel-register": "6.9.0",

"eslint": "2.12.0",

"eslint-config-airbnb-base": "3.0.1",

"eslint-plugin-import": "1.8.1",

"rollup": "0.33.0",

"rollup-plugin-babel": "2.6.1",

"rollup-plugin-commonjs": "3.1.0",

"rollup-plugin-inject": "^2.0.0",

"rollup-plugin-node-resolve": "2.0.0",

"rollup-plugin-uglify": "1.0.1",

"uglify-js": "2.7.0"

},

"scripts": {

"build": "rollup -c",

},

This worked :

removed the rollup inject and commonjs plugins

import * as jQuery from 'jquery';

then in custom.js

$(function () {

console.log('Hello jQuery');

});

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值