Exemple #1
0
	function parseOptions(opts, bopts) {
		var configFile;
		if (fileExists(opts.project)) {
			configFile = opts.project;
		} else {
			var directory = opts.project || bopts.basedir || currentDirectory;
			directory = directory.replace(/\\/g, '/');
			configFile = ts.findConfigFile(directory, fileExists);
		}

		var config;
		if (configFile) {
			log('Using tsconfig file at ' + configFile);
			config = tsconfig.readFileSync(configFile);
			extend(config.compilerOptions, opts);
		} else {
			config = {
				files: [],
				compilerOptions: opts
			};
		}

		var parsed = parseJsonConfigFileContent(config, {
			useCaseSensitiveFileNames: true,
			readDirectory: ts.sys.readDirectory,
			fileExists: ts.sys.fileExists
		}, '');

		// Generate inline sourcemaps if Browserify's --debug option is set
		parsed.options.sourceMap = false;
		parsed.options.inlineSourceMap = bopts.debug;
		parsed.options.inlineSources = bopts.debug;

		// Default to CommonJS module mode
		parsed.options.module = parsed.options.module || ts.ModuleKind.CommonJS;

		// Blacklist --out/--outFile/--noEmit; these should definitely not be set, since we are doing
		// concatenation with Browserify instead
		delete parsed.options.out;
		delete parsed.options.outFile;
		delete parsed.options.noEmit;

		// Set rootDir and outDir so we know exactly where the TS compiler will be trying to
		// write files; the filenames will end up being the keys into our in-memory store.
		// The output directory needs to be distinct from the input directory to prevent the TS
		// compiler from thinking that it might accidentally overwrite source files, which would
		// prevent it from outputting e.g. the results of transpiling ES6 JS files with --allowJs.
		delete parsed.options.rootDir;
		parsed.options.outDir = '__tsify__';

		var result = {
			options: parsed.options,
			fileNames: parsed.fileNames
		};

		return result;
	}
Exemple #2
0
	function parseOptions(opts, bopts) {

		// Expand any short-name, command-line options
		var expanded = {};
		if (opts.m) { expanded.module = opts.m; }
		if (opts.p) { expanded.project = opts.p; }
		if (opts.t) { expanded.target = opts.t; }
		opts = assign(expanded, opts);

		var config;
		var configFile;
		if (typeof opts.project === "object"){
			log('Using inline tsconfig');
			config = JSON.parse(JSON.stringify(opts.project));
			config.compilerOptions = config.compilerOptions || {};
			extend(config.compilerOptions, opts);
		} else {
			if (fileExists(opts.project)) {
				configFile = opts.project;
			} else {
				configFile = ts.findConfigFile(
					ts.normalizeSlashes(opts.project || bopts.basedir || currentDirectory),
					fileExists
				);
			}
			if (configFile) {
				log('Using tsconfig file at %s', configFile);
				config = tsconfig.readFileSync(configFile);
				config.compilerOptions = config.compilerOptions || {};
				extend(config.compilerOptions, opts);
			} else {
				config = {
					files: [],
					compilerOptions: opts
				};
			}
		}

		// Note that subarg parses command line arrays in its own peculiar way:
		// https://github.com/substack/subarg

		if (opts.exclude) {
			config.exclude = opts.exclude._ || opts.exclude;
		}
		if (opts.files) {
			config.files = opts.files._ || opts.files;
		}
		if (opts.include) {
			config.include = opts.include._ || opts.include;
		}

		var parsed = parseJsonConfigFileContent(
			config,
			ts.sys,
			configFile ? ts.normalizeSlashes(path.resolve(path.dirname(configFile))) : currentDirectory,
			null,
			configFile ? ts.normalizeSlashes(path.resolve(configFile)) : undefined
		);

		// Generate inline sourcemaps if Browserify's --debug option is set
		parsed.options.sourceMap = false;
		parsed.options.inlineSourceMap = bopts.debug;
		parsed.options.inlineSources = bopts.debug;

		// Default to CommonJS module mode
		parsed.options.module = parsed.options.module || ts.ModuleKind.CommonJS;

		// Blacklist --out/--outFile/--noEmit; these should definitely not be set, since we are doing
		// concatenation with Browserify instead
		delete parsed.options.out;
		delete parsed.options.outFile;
		delete parsed.options.noEmit;

		// Set rootDir and outDir so we know exactly where the TS compiler will be trying to
		// write files; the filenames will end up being the keys into our in-memory store.
		// The output directory needs to be distinct from the input directory to prevent the TS
		// compiler from thinking that it might accidentally overwrite source files, which would
		// prevent it from outputting e.g. the results of transpiling ES6 JS files with --allowJs.
		parsed.options.rootDir = path.relative('.', '/');
		parsed.options.outDir = ts.normalizeSlashes(path.resolve('/__tsify__'));

		log('Files from tsconfig parse:');
		parsed.fileNames.forEach(function (filename) { log('  %s', filename); });

		var result = {
			options: parsed.options,
			fileNames: parsed.fileNames
		};

		return result;
	}
Exemple #3
0
function ensureInstance(webpack, options, instanceName) {
    ensureInstanceStore(webpack._compiler);
    var exInstance = resolveInstance(webpack._compiler, instanceName);
    if (exInstance) {
        return exInstance;
    }
    var tsFlow = Promise.resolve();
    var compilerName = options.compiler || 'typescript';
    var compilerPath = path.dirname(compilerName);
    if (compilerPath == '.') {
        compilerPath = compilerName;
    }
    var tsImpl = undefined;
    try {
        tsImpl = require(compilerPath);
    } catch (e) {
        console.error(e);
        console.error(COMPILER_ERROR);
        process.exit(1);
    }
    var libPath = path.join(compilerPath, 'lib', 'lib.d.ts');
    var lib6Path = path.join(compilerPath, 'lib', 'lib.es6.d.ts');
    try {
        require.resolve(libPath);
    } catch (e) {
        libPath = path.join(compilerPath, 'bin', 'lib.d.ts');
        lib6Path = path.join(compilerPath, 'bin', 'lib.es6.d.ts');
    }
    var compilerInfo = {
        compilerName: compilerName,
        compilerPath: compilerPath,
        tsImpl: tsImpl,
        lib5: helpers_1.loadLib(libPath),
        lib6: helpers_1.loadLib(lib6Path)
    };
    _.defaults(options, {
        resolveGlobs: true
    });
    var configFilePath = undefined;
    var configFile = undefined;
    if (options.tsconfig && options.tsconfig.match(/\.json$/)) {
        configFilePath = options.tsconfig;
    } else {
        configFilePath = tsconfig.resolveSync(options.tsconfig || process.cwd());
    }
    if (configFilePath) {
        var content = fs.readFileSync(configFilePath).toString();
        configFile = tsconfig_utils_1.parseContent(content, configFilePath);
        if (options.resolveGlobs) {
            tsconfig_utils_1.tsconfigSuggestions(configFile);
            configFile = tsconfig.readFileSync(configFilePath, { filterDefinitions: true });
        }
    }
    var tsFiles = [];
    if (configFile) {
        if (configFile.compilerOptions) {
            _.extend(options, configFile.compilerOptions);
            _.extend(options, configFile.awesomeTypescriptLoaderOptions);
            options.exclude = configFile.exclude || [];
            tsFiles = configFile.files;
        }
    }
    options = tsconfig_utils_1.rawToTsCompilerOptions(options, path.dirname(configFilePath), tsImpl);
    _.defaults(options, {
        externals: [],
        doTypeCheck: true,
        sourceMap: true,
        verbose: false,
        noLib: false,
        suppressOutputPathCheck: true,
        sourceRoot: process.cwd()
    });
    options = _.omit(options, 'outDir', 'files', 'out');
    options.externals.push.apply(options.externals, tsFiles);
    var babelImpl = undefined;
    if (options.useBabel) {
        try {
            babelImpl = require(path.join(process.cwd(), 'node_modules', 'babel-core'));
        } catch (e) {
            console.error(BABEL_ERROR);
            process.exit(1);
        }
    }
    var cacheIdentifier = null;
    if (options.useCache) {
        if (!options.cacheDirectory) {
            options.cacheDirectory = path.join(process.cwd(), '.awcache');
        }
        if (!fs.existsSync(options.cacheDirectory)) {
            fs.mkdirSync(options.cacheDirectory);
        }
        cacheIdentifier = {
            'typescript': tsImpl.version,
            'awesome-typescript-loader': pkg.version,
            'awesome-typescript-loader-query': webpack.query,
            'babel-core': babelImpl ? babelImpl.version : null
        };
    }
    var forkChecker = options.forkChecker && getRootCompiler(webpack._compiler)._tsFork;
    var resolver = resolver_1.default(webpack._compiler.options);
    var syncResolver = resolver.resolveSync.bind(resolver);
    var tsState = new host_1.State(options, webpack._compiler.inputFileSystem, compilerInfo, syncResolver);
    var compiler = webpack._compiler;
    setupWatchRun(compiler, instanceName);
    if (options.doTypeCheck) {
        setupAfterCompile(compiler, instanceName, forkChecker);
    }
    var webpackOptions = _.pick(webpack._compiler.options, 'resolve');
    var atlOptions = webpack.options.atl;
    var plugins = [];
    if (atlOptions && atlOptions.plugins) {
        plugins = atlOptions.plugins;
    }
    var initedPlugins = [];
    if (!forkChecker) {
        initedPlugins = plugins.map(function (plugin) {
            return require(plugin.file)(plugin.options);
        });
    }
    return getInstanceStore(webpack._compiler)[instanceName] = {
        id: ++id,
        tsFlow: tsFlow,
        tsState: tsState,
        babelImpl: babelImpl,
        compiledFiles: {},
        options: options,
        externalsInvoked: false,
        checker: forkChecker ? checker_1.createChecker(compilerInfo, options, webpackOptions, plugins) : null,
        cacheIdentifier: cacheIdentifier,
        plugins: plugins,
        initedPlugins: initedPlugins,
        externalsInvocation: null
    };
}