Last active
March 7, 2022 20:31
-
-
Save azarus/f369ee2ab0283ba0793b0ccf0e9ec590 to your computer and use it in GitHub Desktop.
Gulp Task to transform Typescript path imports into relative paths using the tsconfig
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Gulp Task | |
gulp.task("compile:source", function(done) { | |
gutil.log(gutil.colors.yellow("[Typescript]"), gutil.colors.magenta('Transpiling Source')); | |
var tsProject = ts.createProject('tsconfig.json', { | |
typescript: require('typescript'), | |
}); | |
gulp.src("source/**/*") | |
.pipe(tsProject()) | |
.pipe(tsimport(tsProject.config.compilerOptions)) | |
.on('error', function(error, callback) | |
{ | |
gutil.log(gutil.colors.red("[Typescript] Server Error:"), error.stack); | |
this.emit("end"); | |
}) | |
.pipe(gulp.dest("build/")) | |
.on("end", done); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var through = require('through2'); | |
var replacePath = require("./replace-path.js"); | |
module.exports = function(importOptions) | |
{ | |
return function(file) | |
{ | |
return through(function (buffer, enc, cb) | |
{ | |
var code = buffer.toString("utf8"); | |
code = replacePath(code, file, importOptions.baseUrl, importOptions.paths); | |
buffer = new Buffer(code); | |
this.push(buffer); | |
cb(); | |
}); | |
} | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var fs = require("fs"); | |
var path = require("path"); | |
module.exports = function(code, filePath, rootPath, targetPaths) | |
{ | |
var tscpaths = Object.keys(targetPaths); | |
var lines = code.split("\n"); | |
return lines.map((line) => | |
{ | |
var matches = []; | |
var require_matches = line.match(/require\(('|")(.*)('|")\)/g); | |
// var import_matches = line.match(/import ('|")(.*)('|")/g); | |
Array.prototype.push.apply(matches, require_matches); | |
if(!matches) | |
{ | |
return line; | |
} | |
// Go through each require statement | |
for(var match of matches) | |
{ | |
// Find each paths | |
for(var tscpath of tscpaths) | |
{ | |
// Find required module & check if its path matching what is described in the paths config. | |
var requiredModules = match.match(new RegExp(tscpath, "g")); | |
if(requiredModules && requiredModules.length > 0) | |
{ | |
for(var requiredModule of requiredModules) | |
{ | |
// Skip if it resolves to the node_modules folder | |
var modulePath = path.resolve('./node_modules/' + tscpath); | |
if (fs.existsSync(modulePath)) | |
{ | |
continue; | |
} | |
// Get relative path and replace | |
var sourcePath = path.dirname(filePath); | |
var targetPath = path.dirname(path.resolve(rootPath + "/" + targetPaths[tscpath])); | |
var relativePath = path.relative(sourcePath, targetPath) | |
line = line.replace(new RegExp(tscpath, "g"), "./" + relativePath + "/"); | |
} | |
} | |
} | |
} | |
return line; | |
}).join("\n"); | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var through = require('through2'); | |
var replacePath = require("./replace-path.js"); | |
module.exports = function(importOptions) | |
{ | |
return through.obj(function (file, enc, cb) | |
{ | |
var code = file.contents.toString('utf8'); | |
code = replacePath(code, file.history.toString(), importOptions.baseUrl, importOptions.paths); | |
file.contents = new Buffer(code); | |
this.push(file); | |
cb(); | |
}); | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var tsify = require('tsify'); | |
var importify = require("./typescript/importify.js"); | |
var tsconfig = require("../tsconfig.json"); | |
function CompileSources(entryFile) | |
{ | |
var bundler = watchify( browserify({ | |
entries: entryFile, | |
debug: false, | |
// defining transforms here will avoid crashing your stream | |
}) | |
.plugin(tsify, tsconfig.compilerOptions)) | |
.transform(importify(tsconfig.compilerOptions)) | |
// .. etc | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I have two issues with your script:
.."@backend-TP*": ["./platform/TP/*"]..
import { CategoryTraceListener } from '@backend-TP/shared/diagnostics';
const diagnostics_1 = require("./platform/TP//shared/diagnostics");
line = line.replace(new RegExp(tscpath+"/", "g"), "./" + relativePath + "/");