尝试使用 webpack 连接 js 文件(如 gulp 脚本) [英] Trying to concatenate js files with webpack (like a gulp script)

查看:18
本文介绍了尝试使用 webpack 连接 js 文件(如 gulp 脚本)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用 webpack 重现这个 Gulp 脚本,只让一个进程监听文件,但我无法让它工作.这是我的 Gulp 任务

I want to reproduce this Gulp script with webpack to have only one process who listen on files but i can't make it work. This is my Gulp task

gulp.task('scripts', function(){

return gulp.src( WATCH_SCRIPTS.SCRIPTS )
  .pipe(plumber())
  .pipe(sourcemaps.init())
  .pipe(concat('plugins.min.js'))
  .pipe(babel({ compact: false, babelrc: false, presets: ['es2015-allow-top-level-this'] }))
  .pipe(ifElse(PROD_ENV,
    () => {return uglify({
      hoist_vars: true,
      global_defs: {
        jQuery: false,
        $: false,
      }
    })},
    () => {return sourcemaps.write('./')}
  ))
  .pipe(plumber.stop())
  .pipe(gulp.dest(BUILD_PATH + '/js'));
});

我尝试过使用暴露加载器,但它总是遗漏一些依赖项.

I tried with the expose-loader, but it always misses some dependancies.

{
  name: 'scripts',
  target: 'web',
  entry: glob.sync('./src/assets/js/**/*.js'),
  devtool: 'cheap-module-eval-source-map',
  output: {
    path: path.resolve(__dirname, 'public/js/'),
    filename: 'plugins.min.js',
    sourceMapFilename: 'plugins.min.js.map',
  },
  module: {
    rules: [
      {
        test: /[\/]global\.js$/,
        use: [{
          loader: 'expose-loader',
          options: 'Materialize'
        }]
      },
      {
        test: /[\/]jquery\.js$/,
        use: [{
          loader: 'expose-loader',
          options: 'jQuery'
        },{
          loader: 'expose-loader',
          options: '$'
        }]
      }
    ]
  }
}

我尝试使用带有预设的 babel-loader(就像在 gulp 任务中一样)

I tried with the babel-loader with the preset (like in the gulp task)

presets: ['es2015-allow-top-level-this']

未在此文件中定义 Materialize https://github.com/Dogfalo/materialize/blob/master/js/global.js.

Materialize was undefined from this file https://github.com/Dogfalo/materialize/blob/master/js/global.js.

我可以只听文件更改并启动我的 gulp 脚本 或者有没有办法直接连接文件?我对 webpack 2 很陌生.

Can i just listen to files changes and start my gulp scripts or is there a way to concatenate files directly? I'm very new to webpack 2.

谢谢

推荐答案

尝试 'script-loader' 不串联,而是在全局上下文中执行脚本,这也是我原来找简单串联"时需要的.

Try 'script-loader' It does not concatenate, but it executes the script in the global context, which is what I originally needed when looking for "simple concatenation".

这篇关于尝试使用 webpack 连接 js 文件(如 gulp 脚本)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆