awesome-typescript-loader
Version:
Awesome TS loader for webpack
212 lines (211 loc) • 6.42 kB
JavaScript
;
var path = require('path');
var fs = require('fs');
var _ = require('lodash');
var temp = require('temp').track();
require('babel-polyfill');
require('source-map-support').install();
var chai_1 = require('chai');
exports.expect = chai_1.expect;
var webpack = require('webpack');
var BPromise = require('bluebird');
var mkdirp = BPromise.promisify(require('mkdirp'));
var rimraf = BPromise.promisify(require('rimraf'));
var readFile = BPromise.promisify(fs.readFile);
var writeFile = BPromise.promisify(fs.writeFile);
var loaderDir = path.join(process.cwd(), 'dist.babel');
var ForkCheckerPlugin = require(loaderDir).ForkCheckerPlugin;
exports.defaultOutputDir = path.join(process.cwd(), 'src', 'test', 'output');
exports.defaultFixturesDir = path.join(process.cwd(), 'src', 'test', 'fixtures');
var defaultOptions = {
watch: false,
forkChecker: false
};
function createConfig(conf, _options) {
if (_options === void 0) {
_options = defaultOptions;
}
var options = _.merge({}, defaultOptions, _options);
var defaultConfig = {
watch: false,
output: {
path: exports.defaultOutputDir,
filename: '[name].js'
},
resolve: {
extensions: ['', '.ts', '.tsx', '.js', '.jsx']
},
module: {
loaders: [{
test: /\.(tsx?|jsx?)/,
loader: loaderDir,
query: Object.assign({
target: 'es6'
}, {
tsconfigContent: {
exclude: ["*"]
}
}, options.loaderQuery)
}]
},
plugins: []
};
var loader = defaultConfig.module.loaders[0];
if (options.include) {
loader.include = (loader.include || []).concat(options.include);
}
if (options.exclude) {
loader.exclude = (loader.exclude || []).concat(options.exclude);
}
if (options.watch) {
defaultConfig.watch = true;
}
if (options.forkChecker) {
defaultConfig.plugins.push(new ForkCheckerPlugin());
}
return _.merge(defaultConfig, conf);
}
exports.createConfig = createConfig;
function expectSource(source, fragment) {
chai_1.expect(source.replace(/\s/g, '')).include(fragment.replace(/\s/g, ''));
}
exports.expectSource = expectSource;
function fixturePath(fileName, fixturesDir) {
if (fixturesDir === void 0) {
fixturesDir = exports.defaultFixturesDir;
}
return path.join.apply(path, [fixturesDir].concat(fileName));
}
exports.fixturePath = fixturePath;
function readFixture(fileName, fixturesDir) {
if (fixturesDir === void 0) {
fixturesDir = exports.defaultFixturesDir;
}
var filePath = fixturePath(fileName, fixturesDir);
return readFile(filePath).then(function (buf) {
return buf.toString();
});
}
exports.readFixture = readFixture;
function writeFixture(fileName, text, fixturesDir) {
if (fixturesDir === void 0) {
fixturesDir = exports.defaultFixturesDir;
}
var filePath = fixturePath(fileName, fixturesDir);
return writeFile(filePath, text);
}
exports.writeFixture = writeFixture;
function touchFile(fileName) {
return readFile(fileName).then(function (buf) {
return buf.toString();
}).then(function (source) {
return writeFile(fileName, source);
});
}
exports.touchFile = touchFile;
function outputFileName(fileName, outputDir) {
if (outputDir === void 0) {
outputDir = exports.defaultOutputDir;
}
return path.join(exports.defaultOutputDir, fileName);
}
exports.outputFileName = outputFileName;
function readOutputFile(fileName, outputDir) {
if (outputDir === void 0) {
outputDir = exports.defaultOutputDir;
}
return readFile(outputFileName(fileName || 'main.js', outputDir)).then(function (buf) {
return buf.toString();
});
}
exports.readOutputFile = readOutputFile;
function cleanOutputDir(outputDir) {
if (outputDir === void 0) {
outputDir = exports.defaultOutputDir;
}
return rimraf(outputDir).then(function () {
return mkdirp(outputDir);
});
}
exports.cleanOutputDir = cleanOutputDir;
function cleanAndCompile(config, outputDir) {
if (outputDir === void 0) {
outputDir = exports.defaultOutputDir;
}
return cleanOutputDir(outputDir).then(function () {
return compile(config);
});
}
exports.cleanAndCompile = cleanAndCompile;
function compile(config) {
return new Promise(function (resolve, reject) {
var compiler = webpack(config);
compiler.run(function (err, stats) {
if (err) {
reject(err);
} else {
resolve(stats);
}
});
});
}
exports.compile = compile;
function watch(config, cb) {
var compiler = webpack(config);
var watch = new Watch();
var webpackWatcher = compiler.watch({}, function (err, stats) {
watch.call(err, stats);
if (cb) {
cb(err, stats);
}
});
watch.close = webpackWatcher.close;
return watch;
}
exports.watch = watch;
var Watch = function () {
function Watch() {
this.resolves = [];
}
Watch.prototype.call = function (err, stats) {
this.resolves.forEach(function (resolver) {
resolver([err, stats]);
});
this.resolves = [];
};
Watch.prototype.wait = function () {
var _this = this;
return new Promise(function (resolve, reject) {
_this.resolves.push(resolve);
});
};
return Watch;
}();
var Fixture = function () {
function Fixture(text, ext) {
if (ext === void 0) {
ext = '.tsx';
}
this.text = text;
var tmpobj = temp.openSync({
prefix: 'atl-',
suffix: '.tsx'
});
this.fileName = tmpobj.path;
fs.writeFileSync(this.fileName, text);
}
Fixture.prototype.path = function () {
return this.fileName;
};
Fixture.prototype.touch = function () {
touchFile(this.fileName);
};
Fixture.prototype.update = function (updater) {
var newText = updater(this.text);
this.text = newText;
fs.writeFileSync(this.fileName, newText);
};
return Fixture;
}();
exports.Fixture = Fixture;
//# sourceMappingURL=utils.js.map