Skip to content

Commit

Permalink
- commonjs hack documentation and build
Browse files Browse the repository at this point in the history
  • Loading branch information
ezolenko committed Mar 10, 2017
1 parent 014431b commit 664788a
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 25 deletions.
3 changes: 3 additions & 0 deletions README.md
Expand Up @@ -63,6 +63,9 @@ Plugin takes following options:

Bail out on first syntactic error. In most cases setting this to false will result in exception in rollup itself.

* `rollupCommonJSResolveHack`: false

On windows typescript resolver favors POSIX path, while commonjs plugin (and maybe others?) uses native path as module id. This can result in namedExports being ignored if rollup happened to use typescript's resolution. Set to true to pass resolved module path through `resolve()` to match up with commonjs.

### TypeScript version

Expand Down
29 changes: 17 additions & 12 deletions dist/rollup-plugin-typescript2.cjs.js
Expand Up @@ -6,8 +6,8 @@ var ts = require('typescript');
var _ = require('lodash');
var graph = require('graphlib');
var hash = require('object-hash');
var path = require('path');
var colors = require('colors/safe');
var path = require('path');
var resolve = require('resolve');

/*! *****************************************************************************
Expand Down Expand Up @@ -111,10 +111,12 @@ var LanguageServiceHost = (function () {
this.parsedConfig = parsedConfig;
this.cwd = process.cwd();
this.snapshots = {};
this.versions = {};
}
LanguageServiceHost.prototype.setSnapshot = function (fileName, data) {
var snapshot = ts.ScriptSnapshot.fromString(data);
this.snapshots[fileName] = snapshot;
this.versions[fileName] = (this.versions[fileName] || 0) + 1;
return snapshot;
};
LanguageServiceHost.prototype.getScriptSnapshot = function (fileName) {
Expand All @@ -130,7 +132,7 @@ var LanguageServiceHost = (function () {
return this.cwd;
};
LanguageServiceHost.prototype.getScriptVersion = function (_fileName) {
return "0";
return (this.versions[_fileName] || 0).toString();
};
LanguageServiceHost.prototype.getScriptFileNames = function () {
return this.parsedConfig.fileNames;
Expand Down Expand Up @@ -251,7 +253,7 @@ var Cache = (function () {
this.init();
}
Cache.prototype.clean = function () {
this.context.info("cleaning cache: " + this.cacheDir);
this.context.info(colors.blue("cleaning cache: " + this.cacheDir));
fs.emptyDirSync(this.cacheDir);
this.init();
};
Expand All @@ -261,7 +263,7 @@ var Cache = (function () {
_.each(graph.alg.topsort(this.dependencyTree), function (id) { return cb(id); });
return;
}
this.context.info("import tree has cycles");
this.context.info(colors.yellow("import tree has cycles"));
_.each(this.dependencyTree.nodes(), function (id) { return cb(id); });
};
Cache.prototype.setDependency = function (importee, importer) {
Expand All @@ -272,7 +274,7 @@ var Cache = (function () {
};
Cache.prototype.compileDone = function () {
var _this = this;
this.context.debug("Ambient types:");
this.context.debug(colors.blue("Ambient types:"));
var typeNames = _.filter(this.ambientTypes, function (snapshot) { return snapshot.snapshot !== undefined; })
.map(function (snapshot) {
_this.context.debug(" " + snapshot.id);
Expand All @@ -281,7 +283,7 @@ var Cache = (function () {
// types dirty if any d.ts changed, added or removed
this.ambientTypesDirty = !this.typesCache.match(typeNames);
if (this.ambientTypesDirty)
this.context.info("ambient types changed, redoing all diagnostics");
this.context.info(colors.yellow("ambient types changed, redoing all diagnostics"));
_.each(typeNames, function (name) { return _this.typesCache.touch(name); });
};
Cache.prototype.diagnosticsDone = function () {
Expand All @@ -292,16 +294,16 @@ var Cache = (function () {
};
Cache.prototype.getCompiled = function (id, snapshot, transform) {
var name = this.makeName(id, snapshot);
this.context.debug("transpiling '" + id + "'");
this.context.debug(colors.blue("transpiling") + " '" + id + "'");
this.context.debug(" cache: '" + this.codeCache.path(name) + "'");
if (!this.codeCache.exists(name) || this.isDirty(id, snapshot, false)) {
this.context.debug(" cache miss");
this.context.debug(colors.yellow(" cache miss"));
var data_1 = transform();
this.codeCache.write(name, data_1);
this.markAsDirty(id, snapshot);
return data_1;
}
this.context.debug(" cache hit");
this.context.debug(colors.green(" cache hit"));
var data = this.codeCache.read(name);
this.codeCache.write(name, data);
return data;
Expand All @@ -317,13 +319,13 @@ var Cache = (function () {
this.context.debug("diagnostics for '" + id + "'");
this.context.debug(" cache: '" + cache.path(name) + "'");
if (!cache.exists(name) || this.isDirty(id, snapshot, true)) {
this.context.debug(" cache miss");
this.context.debug(colors.yellow(" cache miss"));
var data_2 = convertDiagnostic(check());
cache.write(name, data_2);
this.markAsDirty(id, snapshot);
return data_2;
}
this.context.debug(" cache hit");
this.context.debug(colors.green(" cache hit"));
var data = cache.read(name);
cache.write(name, data);
return data;
Expand Down Expand Up @@ -354,7 +356,7 @@ var Cache = (function () {
var l = _this.dependencyTree.node(node);
var dirty = l === undefined ? true : l.dirty;
if (dirty)
_this.context.debug("import changed: " + id + " -> " + node);
_this.context.debug(" import changed: " + node);
return dirty;
});
};
Expand Down Expand Up @@ -437,8 +439,11 @@ function typescript(options) {
include: ["*.ts+(|x)", "**/*.ts+(|x)"],
exclude: ["*.d.ts", "**/*.d.ts"],
abortOnError: true,
rollupCommonJSResolveHack: false,
});
var context = new ConsoleContext(options.verbosity, "rpt2: ");
context.debug("Typescript version: " + ts.version);
context.debug("Options: " + JSON.stringify(options, undefined, 4));
var filter$$1 = createFilter(options.include, options.exclude);
var parsedConfig = parseTsConfig(context);
var servicesHost = new LanguageServiceHost(parsedConfig);
Expand Down
31 changes: 18 additions & 13 deletions dist/rollup-plugin-typescript2.es.js
Expand Up @@ -9,10 +9,10 @@ import { Graph, alg } from 'graphlib';
import * as graph from 'graphlib';
import { sha1 } from 'object-hash';
import * as hash from 'object-hash';
import { blue, green, red, white, yellow } from 'colors/safe';
import * as colors from 'colors/safe';
import { dirname } from 'path';
import * as path from 'path';
import { red, white, yellow } from 'colors/safe';
import * as colors from 'colors/safe';
import { sync } from 'resolve';
import * as resolve from 'resolve';

Expand Down Expand Up @@ -117,10 +117,12 @@ var LanguageServiceHost = (function () {
this.parsedConfig = parsedConfig;
this.cwd = process.cwd();
this.snapshots = {};
this.versions = {};
}
LanguageServiceHost.prototype.setSnapshot = function (fileName, data) {
var snapshot = ScriptSnapshot.fromString(data);
this.snapshots[fileName] = snapshot;
this.versions[fileName] = (this.versions[fileName] || 0) + 1;
return snapshot;
};
LanguageServiceHost.prototype.getScriptSnapshot = function (fileName) {
Expand All @@ -136,7 +138,7 @@ var LanguageServiceHost = (function () {
return this.cwd;
};
LanguageServiceHost.prototype.getScriptVersion = function (_fileName) {
return "0";
return (this.versions[_fileName] || 0).toString();
};
LanguageServiceHost.prototype.getScriptFileNames = function () {
return this.parsedConfig.fileNames;
Expand Down Expand Up @@ -257,7 +259,7 @@ var Cache = (function () {
this.init();
}
Cache.prototype.clean = function () {
this.context.info("cleaning cache: " + this.cacheDir);
this.context.info(blue("cleaning cache: " + this.cacheDir));
emptyDirSync(this.cacheDir);
this.init();
};
Expand All @@ -267,7 +269,7 @@ var Cache = (function () {
each(alg.topsort(this.dependencyTree), function (id) { return cb(id); });
return;
}
this.context.info("import tree has cycles");
this.context.info(yellow("import tree has cycles"));
each(this.dependencyTree.nodes(), function (id) { return cb(id); });
};
Cache.prototype.setDependency = function (importee, importer) {
Expand All @@ -278,7 +280,7 @@ var Cache = (function () {
};
Cache.prototype.compileDone = function () {
var _this = this;
this.context.debug("Ambient types:");
this.context.debug(blue("Ambient types:"));
var typeNames = filter(this.ambientTypes, function (snapshot) { return snapshot.snapshot !== undefined; })
.map(function (snapshot) {
_this.context.debug(" " + snapshot.id);
Expand All @@ -287,7 +289,7 @@ var Cache = (function () {
// types dirty if any d.ts changed, added or removed
this.ambientTypesDirty = !this.typesCache.match(typeNames);
if (this.ambientTypesDirty)
this.context.info("ambient types changed, redoing all diagnostics");
this.context.info(yellow("ambient types changed, redoing all diagnostics"));
each(typeNames, function (name) { return _this.typesCache.touch(name); });
};
Cache.prototype.diagnosticsDone = function () {
Expand All @@ -298,16 +300,16 @@ var Cache = (function () {
};
Cache.prototype.getCompiled = function (id, snapshot, transform) {
var name = this.makeName(id, snapshot);
this.context.debug("transpiling '" + id + "'");
this.context.debug(blue("transpiling") + " '" + id + "'");
this.context.debug(" cache: '" + this.codeCache.path(name) + "'");
if (!this.codeCache.exists(name) || this.isDirty(id, snapshot, false)) {
this.context.debug(" cache miss");
this.context.debug(yellow(" cache miss"));
var data_1 = transform();
this.codeCache.write(name, data_1);
this.markAsDirty(id, snapshot);
return data_1;
}
this.context.debug(" cache hit");
this.context.debug(green(" cache hit"));
var data = this.codeCache.read(name);
this.codeCache.write(name, data);
return data;
Expand All @@ -323,13 +325,13 @@ var Cache = (function () {
this.context.debug("diagnostics for '" + id + "'");
this.context.debug(" cache: '" + cache.path(name) + "'");
if (!cache.exists(name) || this.isDirty(id, snapshot, true)) {
this.context.debug(" cache miss");
this.context.debug(yellow(" cache miss"));
var data_2 = convertDiagnostic(check());
cache.write(name, data_2);
this.markAsDirty(id, snapshot);
return data_2;
}
this.context.debug(" cache hit");
this.context.debug(green(" cache hit"));
var data = cache.read(name);
cache.write(name, data);
return data;
Expand Down Expand Up @@ -360,7 +362,7 @@ var Cache = (function () {
var l = _this.dependencyTree.node(node);
var dirty = l === undefined ? true : l.dirty;
if (dirty)
_this.context.debug("import changed: " + id + " -> " + node);
_this.context.debug(" import changed: " + node);
return dirty;
});
};
Expand Down Expand Up @@ -443,8 +445,11 @@ function typescript(options) {
include: ["*.ts+(|x)", "**/*.ts+(|x)"],
exclude: ["*.d.ts", "**/*.d.ts"],
abortOnError: true,
rollupCommonJSResolveHack: false,
});
var context = new ConsoleContext(options.verbosity, "rpt2: ");
context.debug("Typescript version: " + version);
context.debug("Options: " + JSON.stringify(options, undefined, 4));
var filter$$1 = createFilter(options.include, options.exclude);
var parsedConfig = parseTsConfig(context);
var servicesHost = new LanguageServiceHost(parsedConfig);
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Expand Up @@ -113,6 +113,7 @@ export default function typescript (options: IOptions)
include: [ "*.ts+(|x)", "**/*.ts+(|x)" ],
exclude: [ "*.d.ts", "**/*.d.ts" ],
abortOnError: true,
rollupCommonJSResolveHack: false,
});

const context = new ConsoleContext(options.verbosity, "rpt2: ");
Expand Down

0 comments on commit 664788a

Please sign in to comment.