首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >jQuery + Rollup.js导入Babel

jQuery + Rollup.js导入Babel
EN

Stack Overflow用户
提问于 2017-05-01 03:38:06
回答 1查看 1.6K关注 0票数 3

我试图用Babel和Rollup将jQuery加载到我捆绑的js中,但由于某些原因,它一直抛出这个错误消息:

代码语言:javascript
复制
src/js/components/jquery/dist/jquery.js (40:46) The 'this' keyword is equivalent to 'undefined' at the top level of an ES module, and has been rewritten

events.js:163
      throw er; // Unhandled 'error' event
      ^
Error: 'default' is not exported by src/js/components/jquery/dist/jquery.js
    at error (/var/www/node_modules/rollup/dist/rollup.js:170:12)
    at Module.error$1 [as error] (/var/www/node_modules/rollup/dist/rollup.js:8007:2)
    at Module.trace (/var/www/node_modules/rollup/dist/rollup.js:8106:9)
    at ModuleScope.findDeclaration (/var/www/node_modules/rollup/dist/rollup.js:7691:22)
    at Node.bind (/var/www/node_modules/rollup/dist/rollup.js:6743:29)
    at /var/www/node_modules/rollup/dist/rollup.js:5291:50
    at Node.eachChild (/var/www/node_modules/rollup/dist/rollup.js:5308:5)
    at Node.bind (/var/www/node_modules/rollup/dist/rollup.js:5291:7)
    at /var/www/node_modules/rollup/dist/rollup.js:5291:50
    at Node.eachChild (/var/www/node_modules/rollup/dist/rollup.js:5305:19)

这是我的gulp文件。

代码语言:javascript
复制
var gulp         = require('gulp');
var gutil        = require('gulp-util');
var sourcemaps   = require('gulp-sourcemaps');
var sass         = require('gulp-sass');
var rollup       = require('gulp-better-rollup')
var autoprefixer = require('gulp-autoprefixer');
var server       = require('gulp-express');
var babel        = require('rollup-plugin-babel');
var eslint       = require('rollup-plugin-eslint');
//var commonjs     = require('rollup-plugin-commonjs');
//var nodeResolve  = require('rollup-plugin-node-resolve');
var inject       = require('rollup-plugin-inject');


gulp.task('bundle', function() {
  gulp.src('./src/js/**/*.js')
  .pipe(sourcemaps.init())
    // transform the files here.
    .pipe(rollup({
      // any option supported by Rollup can be set here.
      entry: './src/js/app.js',
      plugins: [
        babel({
          exclude: 'node_modules/**',
        }),
        eslint({
          exclude: 'node_modules/**',
        }),
        inject({
          include: '**/*.js',
          exclude: 'node_modules/**',
          jQuery: 'jquery',
          $: 'jquery',
        })
      ],
      format: 'cjs',
    }))
    .pipe(sourcemaps.write())
    .pipe(gulp.dest('./dist'));
});

当然,我让Bower将jQuery导入到我的“components”文件夹中,其中存放着Babel代码……

这是我的app.js

代码语言:javascript
复制
import jQuery from 'jquery';
var $ = jQuery;

$(function(){
  console.log("Hello, jQuery!");
});

我是否错误地导入了jQuery?我需要使用一个更好的插件来完成这个任务吗?我在网上找到的所有教程都让我绕了一圈。(这就是为什么我在gulpfile中注释掉了几个模块。)

EN

回答 1

Stack Overflow用户

发布于 2019-12-17 18:57:51

代码语言:javascript
复制
Had the same issue! Quite annoying one. Took me some time to figure out.

1. npm i jquery --save-dev (for development)
2. import $ from 'jquery' where is needed
or: use import inject from '@rollup/plugin-inject';
Ex: const injectJQueryInModule = inject({
    $: 'jquery',
    fs: ['fs', '*'],
    include: ['**/*.mjs', '**/*.js'],
    exclude: 'node_modules/**',
    'Module_name_to_assign': path.resolve(`${__dirname}/libs/Module_name_to_assign.js/.mjs/.ts`),
    modules: { }
})
Checkout: https://github.com/rollup/rollup-plugin-inject
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43710556

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档