简体   繁体   English

Gulp不使用webpack,找不到错误

[英]Gulp not working with webpack, can't find the error

currently I'm trying to implement gulp.js into my project. 目前我正在尝试将gulp.js实现到我的项目中。 I'm relativly new to this and have no experience in gulp. 我对此很陌生,没有经验。 I've used JavaScript before but I'm far away from beeing an expert. 我之前使用过JavaScript但是我远没有专家。

I've used this example to write my gulpfile but somethings seems to be wrong with my scripts (I'm guessing the error is in my webpack.config.js?). 我已经用这个例子来编写我的gulpfile但是我的脚本似乎有些错误(我猜错误是在我的webpack.config.js中?)。 SCSS compiling and browsersync works fine. SCSS编译和browsersync工作正常。

So this is what I'm trying to do: 所以这就是我想要做的:

The first thing I want the scripts block to do is import jQuery and bootstrap from my node modules, than import every file inside my scripts directory and then write one minified file from all of that. 我希望scripts块做的第一件事是从我的节点模块导入jQuery和bootstrap,而不是导入我脚本目录中的每个文件,然后从所有这些文件中写入一个缩小的文件。

My folder structure looks like this: 我的文件夹结构如下所示:

- Resources
  - css
  - js
    - bundle.js
  - sass
  - scripts
    - init.js
- index.html
- gulpfile.js
- webpack.config.js

I don't know if I'm using the right libraries for this task, feel free to correct me where every I might be wrong. 我不知道我是否正在使用正确的库来完成这项任务,请随时纠正我可能出错的地方。

So here is the gulpfile I'm using right now: 所以这是我正在使用的gulpfile:

'use strict';

const browsersync = require("browser-sync").create();
const eslint = require("gulp-eslint");
const gulp = require ('gulp');
const sass = require ('gulp-sass');
const plumber = require("gulp-plumber");
const webpack = require("webpack");
const webpackconfig = require("./webpack.config.js");
const webpackstream = require("webpack-stream");
const sourcemaps = require('gulp-sourcemaps');

function browserSync(done) {
    browsersync.init({
        proxy: "gh-induction.ddev.local"
    });
    done();
}
function browserSyncReload(done) {
    browsersync.reload();
    done();
}

function css() {
    return gulp
        .src("./Resources/sass/**/*.scss")
        .pipe(sourcemaps.init())
        .pipe(plumber())
        .pipe(sass({ outputStyle: "compressed" }))
        .pipe(sourcemaps.write({includeContent: false}))
        .pipe(sourcemaps.init({loadMaps: true}))
        .pipe(gulp.dest("./Resources/css/"))
        // .pipe(rename({ suffix: ".min" }))
        // .pipe(postcss([autoprefixer(), cssnano()]))
        .pipe(sourcemaps.write('.'))
        .pipe(gulp.dest("./Resources/css/"))
        .pipe(browsersync.stream());
}

function scriptsLint() {
    return gulp
        .src(["./Resources/js/**/*", "./gulpfile.js"])
        .pipe(plumber())
        .pipe(eslint())
        .pipe(eslint.format())
        .pipe(eslint.failAfterError());
}

function scripts() {
    return (
        gulp
            .src(["./Resources/scripts/**/*"])
            .pipe(plumber())
            .pipe(webpackstream(webpackconfig, webpack))
            // folder only, filename is specified in webpack config
            .pipe(gulp.dest("./Resources/js/"))
            .pipe(browsersync.stream())
    );
}

function watchFiles() {
    gulp.watch("./Resources/sass/**/*", css);
    gulp.watch("./Resources/js/**/*", gulp.series(scriptsLint, scripts));
    gulp.watch(
        [
            "./Resources/**/*",
            "*.html"
        ],
        gulp.series(browserSyncReload)
    );
}

const js = gulp.series(scriptsLint, scripts);
const build = gulp.series(gulp.parallel(css, js));
const watch = gulp.parallel(watchFiles, browserSync);

exports.css = css;
exports.js = js;
exports.build = build;
exports.watch = watch;
exports.default = build;

Sorry for the wall of code but since idk where the error is I might as well post the whole file. 对不起代码的墙,但由于idk的错误,我可能会发布整个文件。

Here's the webpack.config.js 这是webpack.config.js

const path = require('path');

module.exports = {
    entry: './Resources/scripts/init.js',

    output: {
        path: path.resolve('./Resources/js/'),
        filename: 'bundle.js'
    },

    module: {
        rules: [{
            test: /\.js$/,
            use: 'babel-loader'
        },

            {
                test: /\.scss$/,
                use: ['style-loader', 'css-loader', 'sass-loader']
            }
        ]
    }
};

I didn't write this file, I've used this generator to create it since I didn't know the syntax. 我没有写这个文件,我用这个生成器来创建它,因为我不知道语法。 Does anyone know where my error is? 有谁知道我的错误在哪里?

Okay It seems like I've found the problem by myself. 好吧好像我自己发现了问题。 The was the issue the eslint-part in my gulpfile. 问题是我的gulpfile中的eslint-part。 After I disabled it the file was created correct. 禁用后,文件创建正确。

I can now import jQuery and bootstrap into one file together with my own code but how can I import local files into the same one? 我现在可以将jQuery和bootstrap与我自己的代码一起导入到一个文件中,但是如何将本地文件导入到同一个文件中呢? The files I want to import are at the same level as the init.js file I mentioned above. 我要导入的文件与我上面提到的init.js文件处于同一级别。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM