Gulp concat并需要路径 [英] Gulp concat and require path

查看:137
本文介绍了Gulp concat并需要路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的gulp-concat有问题。我试图将所有的js文件拼接在一个文件中,比如dist / app.js。

I have a problem with gulp-concat. I'm trying to concate all my js files in a single file, let's say, dist/app.js.

但有些东西我不明白。在那个文件中,我所需的文件路径仍然与以前相同...

But there is something that I don't understand. In that file, my required files path are still the same than before...

这是我的gulpfile.js:

Here is my gulpfile.js :

var gulp       = require("gulp");
var sourcemaps = require("gulp-sourcemaps");
var babel      = require("gulp-babel");
var concat     = require("gulp-concat");
var resolveDependencies      = require("gulp-resolve-dependencies");

gulp.task("default", function () {
  return gulp.src("client/**/*.js")
    .pipe(sourcemaps.init())
    .pipe(babel())
    .pipe(concat("app.js"))
    .pipe(sourcemaps.write("."))
    .pipe(gulp.dest("dist/js"));
});

正如您所看到的,我想要连接嵌套文件夹。

As you can see, I want to concat nested folders.

在我的/client/components/app.js顶部查看示例:

Take a look for example at the top of my /client/components/app.js :

import React, {PropTypes} from 'react';
import AppLeftNav from './AppLeftNav';
import {AppCanvas, AppBar, Styles} from 'material-ui';

//Code here

所以在我的应用app.js生成的我可以看到:

So in my app app.js generated by gulp, i can see :

var _AppLeftNav = require('./AppLeftNav');

这是一个相对路径,它不能工作。

It's a relative path and it can't work.

那么,用相对路径处理这些所需文件的技巧是什么?

So, what's the tricks to handle theses required files with relative path ?

非常感谢。

编辑:看到我得到的错误:

EDIT : See the error I get :

Error: Cannot find module './components/App.js'
    at Function.Module._resolveFilename (module.js:336:15)
    at Function.Module._load (module.js:278:25)
    at Module.require (module.js:365:17)
    at require (module.js:384:17)
    at Object.<anonymous> (MyFolder\dist\js\app.js:34:24)
    at Module._compile (module.js:460:26)
    at Object.Module._extensions..js (module.js:478:10)
    at Module.load (module.js:355:32)
    at Function.Module._load (module.js:310:12)
    at Module.require (module.js:365:17)


推荐答案

任何人可能有同样的问题。 Babel目前不支持导入内联#1681 ,也不支持gulp-concat,因为它是只是连接文件。

To anyone who might have the same problem. Babel currently doesn't support imports inlining #1681, neither do gulp-concat, as it's just concatenating files.

我选择使用 Rollup 来解决问题

I opted to use Rollup to propertly resolve dependencies and only then transpile output:

var gulp = require('gulp');
var gutil = require('gulp-util');
var babel = require('gulp-babel');
var concat = require('gulp-concat');
var rollup = require('gulp-rollup');

gulp.task('build', function () {
  return gulp.src('src/parser-factory.js', { read: false })
    .pipe(rollup({ external: ['request', 'cheerio'] }))
    .on('error', gutil.log)
    .pipe(babel({ stage: 0 }))
    .pipe(concat('realty-parser.js'))
    .pipe(gulp.dest('lib'));
});

这篇关于Gulp concat并需要路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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