Skip to content

Commit

Permalink
ensure local ts is always a project-scoped lookup
Browse files Browse the repository at this point in the history
  • Loading branch information
guybedford committed Jan 29, 2019
1 parent 3136042 commit 02b10f5
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 13 deletions.
6 changes: 4 additions & 2 deletions scripts/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,16 +99,18 @@ async function main() {
writeFileSync(__dirname + "/../dist/ncc/cli.js", cli);
writeFileSync(__dirname + "/../dist/ncc/index.js", index);
writeFileSync(__dirname + "/../dist/ncc/typescript.js", `
const { Module } = require('module');
const m = new Module('', null);
m.paths = Module._nodeModulePaths(process.env.TYPESCRIPT_LOOKUP_PATH || path);
let typescript;
try {
typescript = require('typescript');
typescript = m.require('typescript');
console.log("ncc: Using typescript@" + typescript.version + " (local user-provided)");
}
catch (e) {
typescript = require('./loaders/ts-loader.js').typescript;
console.log("ncc: Using typescript@" + typescript.version + " (ncc built-in)");
}
module.exports = typescript;
`);
writeFileSync(__dirname + "/../dist/ncc/sourcemap-register.js", sourcemapSupport);
Expand Down
3 changes: 2 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ module.exports = (
} = {}
) => {
const resolvedEntry = resolve.sync(entry);
process.env.TYPESCRIPT_LOOKUP_PATH = resolvedEntry;
const shebangMatch = fs.readFileSync(resolvedEntry).toString().match(shebangRegEx);
const mfs = new MemoryFS();
const assetNames = Object.create(null);
Expand Down Expand Up @@ -154,7 +155,7 @@ module.exports = (
{
loader: eval('__dirname + "/loaders/ts-loader.js"'),
options: {
compiler: eval('__dirname + "/typescript"'),
compiler: eval('__dirname + "/typescript.js"'),
compilerOptions: {
outDir: '//'
}
Expand Down
13 changes: 4 additions & 9 deletions src/loaders/ts-loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,9 @@ logger.makeLogger = function (loaderOptions, colors) {
return instance;
};

const { getOptions } = require("loader-utils");
const loader = require("ts-loader");
module.exports = function () {
const options = getOptions(this);
if (!options.compiler)
options.compiler = eval('__dirname + "/../typescript"');

return loader.apply(this, arguments);
};
module.exports = require("ts-loader");

// ts-loader internally has a require("typescript") that applies
// regardless of "compiler".
// We could remap this too, as soon as ncc supports aliased externals
module.exports.typescript = require("typescript");
1 change: 1 addition & 0 deletions src/typescript.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require("typescript");
1 change: 0 additions & 1 deletion src/typescript/index.js

This file was deleted.

0 comments on commit 02b10f5

Please sign in to comment.