gulp-webpack使用中遇到的疑问?

发布于 2022-09-04 05:40:38 字数 4194 浏览 15 评论 0

小弟今天打算搭一个gulp+webpack的环境,遇到一个小问题,请各位大神给分析一下。
首先安装了gulp,gulp-webpack,webpack等等。版本如下:

"devDependencies": {
    "gulp": "^3.9.1",
    "gulp-changed": "^1.3.2",
    "gulp-concat": "^2.6.1",
    "gulp-debug": "^3.0.0",
    "gulp-minify-css": "^1.2.4",
    "gulp-minify-html": "^1.0.6",
    "gulp-rename": "^1.2.2",
    "gulp-sass": "^2.3.2",
    "gulp-uglify": "^2.0.0",
    "gulp-webpack": "^1.5.0",
    "webpack": "^1.13.3"
  }

然后是gulpfile.js:

var gulp = require('gulp'),
    sass = require('gulp-sass'),
    rename = require('gulp-rename'),
    minifyCss = require('gulp-minify-css'),
    changed = require('gulp-changed'),
    debug = require('gulp-debug'),
    minifyHtml = require('gulp-minify-html'),
    webpack = require("webpack"),
    gulpWebpack = require('gulp-webpack');


var distCss = 'webContent/css',
    distJs = 'webContent/js',
    distBase = 'webContent';


gulp.task('compile-sass',function(){
    return gulp.src('src/scss/*.scss')
    .pipe(changed(distCss,{extension:'.css'}))
    .pipe(debug({title:'debuging:'}))
    .pipe(sass.sync().on('error', sass.logError))
    .pipe(minifyCss())
    .pipe(rename(function(path){
        path.basename=path.basename;
        path.extname = ".css";
    }))
    .pipe(gulp.dest(distCss));
});

gulp.task('compile-js',function(){
    return gulp.src('src/js/*.js')
    .pipe(changed(distJs))
    .pipe(debug({title:'debuging js:'}))
    .pipe(gulpWebpack(require('./webpack.config.js')))
    .pipe(gulp.dest(distJs));
});

gulp.task('compile-html',function(){
    return gulp.src('src/**/*.html')
    .pipe(changed(distBase,{extension:'.html'}))
    .pipe(debug({title:'debuging:'}))
    .pipe(minifyHtml())
    .pipe(rename(function(path){
        path.basename=path.basename;
        path.extname = ".html";
    }))
    .pipe(gulp.dest(distBase));
});

gulp.task('watch',function(){
    gulp.watch('src/sass/*.*',['compile-sass']);
    gulp.watch('src/js/*.*',['compile-js']);
    gulp.watch('src/**/*.*',['compile-html']);
});

gulp.task('default',['compile-sass','compile-js','compile-html']);

最后是webpack.config.js:

var webpack = require("webpack");
module.exports = {
    debug: true,
    watch: true,
    // this is the "entry" file of your app. Think of it as your Angular "app.js" file.
    entry: "./src/js/index.js",
    // this is the will-be-outputted file you'll reference in your index.html file
    output: {
        path: __dirname+'/webContent/js',
        filename: "bundle.js"
    },
    module: {
        loaders: [
           // nothing here yet! We'll add more stuff in Part 2
        ]
    },
    resolve: {
        // for illustration purposes, let's use lodash. 
        // this tells Webpack where actually to find lodash because you'll need it in the ProvidePlugin
        // alias: {
        //   lodash: path.resolve( __dirname, './node_modules/lodash-node/modern'),
        // }
    },
    plugins: [
        // this tells Webpack to provide the "_" variable globally in all your app files as lodash.
        // new webpack.ProvidePlugin({     
        //   _: "lodash",
        // })
    ]
}

单独运行webpack是可以的,会在js文件夹里输出bundle.js文件。
但是我用gulp去执行,不会产出bundle.js,控制台信息如下:

clipboard.png

并没有什么有用的信息输出。。。

我分析就是说gulp是执行了的,但是到了webpack里边可能有问题了。

然后我就从网上看到了gulp-webpack的别的一些参数:
https://www.npmjs.com/package...
我就试了试:

gulp.task('compile-js',function(){
    return gulp.src('src/js/*.js')
    .pipe(changed(distJs))
    .pipe(debug({title:'debuging js:'}))
    .pipe(gulpWebpack(require('./webpack.config.js'),null,function(){}))
    .pipe(gulp.dest(distJs));
});

竟然就成功了:
clipboard.png

我也多试了几次,发现gulpWebpack这个函数,如果不写第三个参数,也就是那个function(){},就会失败,写上就成功了,这是为啥??
这是我配置有问题,还是说这个版本的有bug?请大神帮忙分析一下,我的环境是win7,node v4.4.6

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夜未央樱花落 2022-09-11 05:40:38

这个问题没想到这么快就自问自答了。。。
我的nodejs版本是4.4.6,我从官网下载了一个稳定版的6.9.1
https://nodejs.org/en/
在原目录安装以后,直接运行,提示binding了一次node sass,然后问题就解决了,无论哪种写法都能顺利通过了。
原来是因为我的nodejs版本太陈旧了。
最开始我安装nodejs是从node中文网下载的稳定版,直到后来我去了node的官网才知道稳定版已经是6.9了,话说node中文网是不是该更新一下了。

E:\Projects\MAMProj\MAMWeb>gulp
E:\Projects\MAMProj\MAMWeb\node_modules\node-sass\lib\binding.js:15
      throw new Error(errors.missingBinary());
      ^

Error: Missing binding E:\Projects\MAMProj\MAMWeb\node_modules\node-sass\vendor\
win32-x64-48\binding.node
Node Sass could not find a binding for your current environment: Windows 64-bit
with Node.js 6.x

Found bindings for the following environments:
  - Windows 64-bit with Node.js 4.x

This usually happens because your environment has changed since running `npm ins
tall`.
Run `npm rebuild node-sass` to build the binding for your current environment.
    at module.exports (E:\Projects\MAMProj\MAMWeb\node_modules\node-sass\lib\bin
ding.js:15:13)
    at Object.<anonymous> (E:\Projects\MAMProj\MAMWeb\node_modules\node-sass\lib
\index.js:14:35)
    at Module._compile (module.js:570:32)
    at Object.Module._extensions..js (module.js:579:10)
    at Module.load (module.js:487:32)
    at tryModuleLoad (module.js:446:12)
    at Function.Module._load (module.js:438:3)
    at Module.require (module.js:497:17)
    at require (internal/module.js:20:19)
    at Object.<anonymous> (E:\Projects\MAMProj\MAMWeb\node_modules\gulp-sass\ind
ex.js:187:21)

E:\Projects\MAMProj\MAMWeb>npm rebuild node-sass

> node-sass@3.13.0 install E:\Projects\MAMProj\MAMWeb\node_modules\node-sass
> node scripts/install.js

Downloading binary from https://github.com/sass/node-sass/releases/download/v3.1
3.0/win32-x64-48_binding.node
Download complete  ] - :
Binary saved to E:\Projects\MAMProj\MAMWeb\node_modules\node-sass\vendor\win32-x
64-48\binding.node
Caching binary to C:\Program Files\nodejs\node_cache\node-sass\3.13.0\win32-x64-
48_binding.node

> node-sass@3.13.0 postinstall E:\Projects\MAMProj\MAMWeb\node_modules\node-sass

> node scripts/build.js

Binary found at E:\Projects\MAMProj\MAMWeb\node_modules\node-sass\vendor\win32-x
64-48\binding.node
Testing binary
Binary is fine
node-sass@3.13.0 E:\Projects\MAMProj\MAMWeb\node_modules\node-sass

E:\Projects\MAMProj\MAMWeb>gulp
[12:29:51] Using gulpfile E:\Projects\MAMProj\MAMWeb\gulpfile.js
[12:29:51] Starting 'compile-sass'...
[12:29:51] Starting 'compile-js'...
[12:29:51] Starting 'compile-html'...
[12:29:51] debuging: 0 items
[12:29:51] Finished 'compile-sass' after 91 ms
[12:29:51] debuging js: src\js\index.js
[12:29:51] debuging js: 1 item
[12:29:51] debuging: 0 items
[12:29:51] Finished 'compile-html' after 41 ms
[12:29:51] Version: webpack 1.13.3
    Asset     Size  Chunks             Chunk Names
bundle.js  1.41 kB       0  [emitted]  main
[12:29:51] webpack is watching for changes
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文