ivogabe / gulp-typescript

A TypeScript compiler for gulp with incremental compilation support.
MIT License
839 stars 130 forks source link

Fail on gulp.dest #529

Closed rafelsanso closed 6 years ago

rafelsanso commented 7 years ago

I have the simple following example:

gulp.task('compile-src', function() {

    var tsResult = tsProject.src()
        .pipe(sourcemaps.init())
        .pipe(tsProject());

    return merge([
        tsResult.js
            .pipe(sourcemaps.write('./', {
                sourceRoot: '.',
                sourceMappingURLPrefix: '.'
            })) // Now the sourcemaps are added to the .js file
            .pipe(gulp.dest(config.path.dist.js)),

        tsResult.dts
            .pipe(replace('reference path="', 'reference path="../'))
            .pipe(gulp.dest(config.path.dist.ts))
    ]);
});

Into versions before 3.2 works fine. In 3.2 and later, gulp.dest cannot create subfolders and copy the compiled files into the root folder. In the example, config.path.dist.ts is something looks like this: ./dist/js. But the compiled files are saved into ./dist/ with this latest version.

ivogabe commented 7 years ago

Can you share your project? Or at least some more information like the line were tsProject is created, your tsconfig file and directory structure.

rafelsanso commented 7 years ago

Sorry, it's a private project. But I can pass you the gulpfile.config.js and the gulpfile.js. There you can see the directory structure:

gulpfile.config.js

'use strict';
var GulpConfig = (function () {
    function gulpConfig() {

        /////////////////////////////////////////////////////////////////////////////
        // GULP PATHS
        this.path = {
            dist: {
                root: './dist',
                js: './dist/js',
                ts: './dist/ts',
                css: './dist/css',
                lib: './dist/lib',
                assets: './dist/assets',
                assets_img: './dist/assets/img',
                assets_partials: './dist/assets/partials',
                assets_sounds: './dist/assets/sounds',
                assets_animations: './dist/assets/animations',
                assets_animations_images: './dist/assets/animations/images'
            },
            src: {
                root: './src',
                stylus: './src/stylus',
                ts: './src/ts',
                lib: './src/lib',
                assets: './src/assets',
                assets_img: './src/assets/img',
                assets_partials: './src/assets/partials',
                assets_sounds: './src/assets/sounds',
                assets_animations: './src/assets/animations',
                assets_animations_images: './src/assets/animations/images'
            },
            example: {
                root: './example',
                ts: './example/ts',
                css: './example/stylus'
            },
            typings: {
                root: './typings'
            },
            out: {
                root: './out',
                js: './out/js',
                css: './out/css',
                lib: './out/lib',
                assets: './out/assets',
                assets_img: './out/assets/img',
                assets_partials: './out/assets/partials',
                assets_sounds: './out/assets/sounds',
                assets_animations: './out/assets/animations',
                assets_animations_images: './out/assets/animations/images'
            },
            node_modules: {
                phaserCe: './node_modules/phaser-ce',
                phaserSpine: './node_modules/@orange-games/phaser-spine',
                html5ServerClient: './node_modules/html5-server-client/dist',
                webCanvasHelpers: './node_modules/web-canvas-helpers'
            }
        };

        this.projectName = 'PSMiniGames';
        this.bundle = 'PSMiniGames';
        this.sourceMapRoot = '/' + this.projectName + '/';

        this.projectName = baseName(process.cwd());
        this.sourceMapRoot = '/' + this.projectName + '/';
    }

    function baseName(str) {

        var separator = '/';
        var idx = str.lastIndexOf(separator);

        if (idx < 0) {
            separator = '\\';
            idx = str.lastIndexOf(separator);

            if (idx < 0) {
                return str;
            }
        }

        if (idx == str.length) {
            str = str.substring(0, str.length - 1);
            idx = str.lastIndexOf(separator);
        }

        return idx < 0 ? str : str.substring(idx + 1);
    }

    return gulpConfig;
})();
module.exports = GulpConfig;

gulpfile.js

'use strict';

var Config = require('./gulpfile.config'),
    gulp = require("gulp"),
    gutil = require('gulp-util'),
    rename = require("gulp-rename"),
    concat = require('gulp-concat'),
    replace = require('gulp-replace'),
    clean = require('gulp-clean'),
    ts = require("gulp-typescript"),
    uglify = require('gulp-uglify'),
    sourcemaps = require('gulp-sourcemaps'),
    merge = require('merge2'),
    connect = require('gulp-connect'),
    runSequence = require('run-sequence'),
    stylus = require('gulp-stylus'),
    tslint = require('gulp-tslint');

var config = new Config();

gulp.task('connect', function() {
    connect.server({
        root: 'out',
        port: 7443,
        https: true,
        livereload: true
    });

    gutil.log('Open the url using: https://demo.playspace.com:8443/frameLoader.jsp')
});

var tsProject = ts.createProject('src/tsconfig.json', {
    declaration: true,
    outFile: config.bundle + '.js'
});

var tsProjectExample = ts.createProject('example/tsconfig.json', {
    declaration: true,
    outFile: 'example.js'
});

/////////////////////////////////////////////////////////////////////////////
// CLEAN PRODUCTION
gulp.task('clean', function () {

    return merge([
        gulp.src([
            config.path.dist.root,
            config.path.out.root
        ], {read: false})
            .pipe(clean())
    ]);
});

/////////////////////////////////////////////////////////////////////////////
// 1. COMPILE TS
gulp.task('compile', function(cb) {
    runSequence("scripts", "example", "css", cb);
});

gulp.task("example", function () {

    var tsResultExample = tsProjectExample.src()
        .pipe(sourcemaps.init())
        .pipe(tsProjectExample());

    return merge([
        gulp.src([config.path.example.root + '/*.html'])
            .pipe(gulp.dest(config.path.out.root)),

        tsResultExample.js
            .pipe(sourcemaps.write('./', {
                sourceRoot: '.',
                sourceMappingURLPrefix: '.'
            })) // Now the sourcemaps are added to the .js file
            .pipe(gulp.dest(config.path.out.js))
    ]);
});

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

    var tsResult = tsProject.src()
        .pipe(sourcemaps.init())
        .pipe(tsProject());

    return merge([
        tsResult.js
            .pipe(sourcemaps.write('./', {
                sourceRoot: '.',
                sourceMappingURLPrefix: '.'
            })) // Now the sourcemaps are added to the .js file
            .pipe(gulp.dest(config.path.dist.js)),

        tsResult.dts
            .pipe(replace(/reference path="src\/ts\//g, 'reference path="'))
            .pipe(gulp.dest(config.path.dist.ts))
    ]);
});

gulp.task('css', function() {
    return merge([
        gulp.src(config.path.example.css + '/example.styl')
            .pipe(stylus())
            //.pipe(rename({ basename: 'example' }))
            .pipe(gulp.dest(config.path.out.css)),

        gulp.src(config.path.src.stylus + '/style.styl')
            .pipe(stylus())
            .pipe(rename({ basename: config.bundle }))
            .pipe(gulp.dest(config.path.dist.css))
    ]);
});

/////////////////////////////////////////////////////////////////////////////
// 2. MOVE STATIC
gulp.task("move-static", function (cb) {
    runSequence("move-dist", "move-out", cb);
});

gulp.task("move-dist", function () {
    return merge([
        // dist/lib
        gulp.src([config.path.src.lib + '/**/*'])
            .pipe(gulp.dest(config.path.dist.lib)),

        // dist/ts
        gulp.src([config.path.typings + '/*.ts'])
            .pipe(gulp.dest(config.path.dist.ts)),

        // dist/assets
        gulp.src([config.path.src.assets + '/**/*'])
            .pipe(gulp.dest(config.path.dist.assets)),

        // Dependencies
        gulp.src([config.path.node_modules.html5ServerClient + '/js/*.js'])
            .pipe(gulp.dest(config.path.dist.lib)),

        gulp.src([config.path.node_modules.html5ServerClient + '/ts/*.d.ts'])
            .pipe(gulp.dest(config.path.dist.ts)),

        gulp.src([config.path.node_modules.phaserSpine + '/build/*.js'])
            .pipe(gulp.dest(config.path.dist.lib)),

        gulp.src([config.path.node_modules.phaserSpine + '/spine-ts/*.js'])
            .pipe(gulp.dest(config.path.dist.lib)),

        gulp.src([config.path.node_modules.phaserCe + '/build/*.js'])
            .pipe(gulp.dest(config.path.dist.lib)),

        gulp.src([config.path.node_modules.webCanvasHelpers + '/dist/js/*'])
            .pipe(gulp.dest(config.path.dist.lib)),

        gulp.src([config.path.node_modules.webCanvasHelpers + '/dist/lib/*'])
            .pipe(gulp.dest(config.path.dist.lib))
    ]);
});

gulp.task("move-out", function () {
    return merge([
        gulp.src([config.path.example.root + '/*.html'])
            .pipe(gulp.dest(config.path.out.root)),

        gulp.src([config.path.example.root + '/js/*.js'])
            .pipe(gulp.dest(config.path.out.lib)),

        // out/js
        gulp.src([config.path.dist.js + '/*.js'])
            .pipe(gulp.dest(config.path.out.js)),

        // out/stylus
        gulp.src([config.path.dist.css + '/*.css'])
            .pipe(gulp.dest(config.path.out.css)),

        // out/lib
        gulp.src([config.path.dist.lib + '/**/*'])
            .pipe(gulp.dest(config.path.out.lib)),

        // out/assets
        // out/assets/partials
        gulp.src([config.path.dist.assets + '/**/*'])
            .pipe(gulp.dest(config.path.out.assets))
    ]);
});

/////////////////////////////////////////////////////////////////////////////
// 3. DEFAULT & WATCH
gulp.task('default', function(cb) {
    runSequence('clean', 'compile', 'move-static', cb);
});

gulp.task('basic', function(cb) {
    runSequence('compile', cb);
});

gulp.task('watch', ['compile'], function() {
    gulp.watch(config.path.src.ts + '/*.ts', []);
    gulp.watch(config.path.src.css + '/*', []);
    gulp.watch(config.path.src.root + '/*.html', []);
});
ivogabe commented 6 years ago

Looks similar to #525, as your tsconfig file is in a subdirectory and you're using outFile. That issue is fixed in master, and I'll release a new version soon.