How to use ts-loader - 10 common examples

To help you get started, we’ve selected a few ts-loader examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github vuejs / vue-cli / packages / @vue / cli-plugin-typescript / index.js View on Github external
.prepend('.tsx')

    const tsRule = config.module.rule('ts').test(/\.ts$/)
    const tsxRule = config.module.rule('tsx').test(/\.tsx$/)

    // add a loader to both *.ts & vue
    const addLoader = ({ name, loader, options }) => {
      tsRule.use(name).loader(loader).options(options)
      tsxRule.use(name).loader(loader).options(options)
    }

    addLoader({
      name: 'cache-loader',
      loader: require.resolve('cache-loader'),
      options: api.genCacheConfig('ts-loader', {
        'ts-loader': require('ts-loader/package.json').version,
        'typescript': require('typescript/package.json').version,
        modern: !!process.env.VUE_CLI_MODERN_BUILD
      }, 'tsconfig.json')
    })

    if (useThreads) {
      addLoader({
        name: 'thread-loader',
        loader: require.resolve('thread-loader'),
        options:
          typeof projectOptions.parallel === 'number'
            ? { workers: projectOptions.parallel }
            : {}
      })
    }
github zeit / ncc / scripts / build.js View on Github external
checkUnknownAssets('source-map-support/register', Object.keys(sourcemapAssets));

  // detect unexpected asset emissions from core build
  function checkUnknownAssets (buildName, assets) {
    assets = assets.filter(name => !name.endsWith('.cache') && !name.endsWith('.cache.js'));
    if (!assets.length) return;
    console.error(`New assets are being emitted by the ${buildName} build`);
    console.log(assets);
  }

  writeFileSync(__dirname + "/../dist/ncc/cli.js.cache", cliAssets["cli.js.cache"].source);
  writeFileSync(__dirname + "/../dist/ncc/index.js.cache", indexAssets["index.js.cache"].source);
  writeFileSync(__dirname + "/../dist/ncc/sourcemap-register.js.cache", sourcemapAssets["sourcemap-register.js.cache"].source);
  writeFileSync(__dirname + "/../dist/ncc/loaders/relocate-loader.js.cache", relocateLoaderAssets["relocate-loader.js.cache"].source);
  writeFileSync(__dirname + "/../dist/ncc/loaders/shebang-loader.js.cache", shebangLoaderAssets["shebang-loader.js.cache"].source);
  writeFileSync(__dirname + "/../dist/ncc/loaders/ts-loader.js.cache", tsLoaderAssets["ts-loader.js.cache"].source);

  writeFileSync(__dirname + "/../dist/ncc/cli.js.cache.js", cliAssets["cli.js.cache.js"].source);
  writeFileSync(__dirname + "/../dist/ncc/index.js.cache.js", indexAssets["index.js.cache.js"].source);
  writeFileSync(__dirname + "/../dist/ncc/sourcemap-register.js.cache.js", sourcemapAssets["sourcemap-register.js.cache.js"].source);
  writeFileSync(__dirname + "/../dist/ncc/loaders/relocate-loader.js.cache.js", relocateLoaderAssets["relocate-loader.js.cache.js"].source);
  writeFileSync(__dirname + "/../dist/ncc/loaders/shebang-loader.js.cache.js", shebangLoaderAssets["shebang-loader.js.cache.js"].source);
  writeFileSync(__dirname + "/../dist/ncc/loaders/ts-loader.js.cache.js", tsLoaderAssets["ts-loader.js.cache.js"].source);

  writeFileSync(__dirname + "/../dist/ncc/cli.js", cli, { mode: 0o777 });
  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 || (process.cwd() + '/'));
let typescript;
try {
github zeit / ncc / src / loaders / ts-loader.js View on Github external
// we re-export so that we generate a unique
// optional bundle for the ts-loader, that
// doesn't get loaded unless the user is
// compiling typescript
const logger = require("ts-loader/dist/logger");
const makeLogger = logger.makeLogger;
logger.makeLogger = function (loaderOptions, colors) {
  const instance = makeLogger(loaderOptions, colors);
  const logWarning = instance.logWarning;
  instance.logWarning = function (message) {
    // Disable TS Loader TypeScript compatibility warning
    if (message.indexOf('This version may or may not be compatible with ts-loader') !== -1)
      return;
    return logWarning(message);
  };
  return instance;
};

module.exports = require("ts-loader");

// ts-loader internally has a require("typescript") that applies
// regardless of "compiler".
github zeit / ncc / src / loaders / ts-loader.js View on Github external
// we re-export so that we generate a unique
// optional bundle for the ts-loader, that
// doesn't get loaded unless the user is
// compiling typescript
const logger = require("ts-loader/dist/logger");
const makeLogger = logger.makeLogger;
logger.makeLogger = function (loaderOptions, colors) {
  const instance = makeLogger(loaderOptions, colors);
  const logWarning = instance.logWarning;
  instance.logWarning = function (message) {
    // Disable TS Loader TypeScript compatibility warning
    if (message.indexOf('This version may or may not be compatible with ts-loader') !== -1)
      return;
    return logWarning(message);
  };
  return instance;
};

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
github google / neuroglancer / config / ts_loader_patch.js View on Github external
* The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

const tsLoaderConfigModule = require('ts-loader/dist/config.js');
const tsLoaderUtilsModule = require('ts-loader/dist/utils.js');

tsLoaderConfigModule.getConfigFile = function getConfigFile(
    compiler, loader, loaderOptions, compilerCompatible, log, compilerDetailsLogMessage) {
  let configFilePath = loaderOptions.configFileName;
  var configFileError;
  log.logInfo(('ts-loader: Using config file at ' + configFilePath).green);
  let configFile = compiler.readConfigFile(configFilePath, compiler.sys.readFile);
  if (configFile.error) {
    configFileError = tsLoaderUtilsModule.formatErrors(
        [configFile.error], loaderOptions, compiler, {file: configFilePath})[0];
  }
  if (!configFileError) {
    configFile.config.compilerOptions =
        Object.assign({}, configFile.config.compilerOptions, loaderOptions.compilerOptions);
    // do any necessary config massaging
    if (loaderOptions.transpileOnly) {
      configFile.config.compilerOptions.isolatedModules = true;
    }
github google / neuroglancer / config / ts_loader_patch.js View on Github external
tsLoaderConfigModule.getConfigFile = function getConfigFile(
    compiler, loader, loaderOptions, compilerCompatible, log, compilerDetailsLogMessage) {
  let configFilePath = loaderOptions.configFileName;
  var configFileError;
  log.logInfo(('ts-loader: Using config file at ' + configFilePath).green);
  let configFile = compiler.readConfigFile(configFilePath, compiler.sys.readFile);
  if (configFile.error) {
    configFileError = tsLoaderUtilsModule.formatErrors(
        [configFile.error], loaderOptions, compiler, {file: configFilePath})[0];
  }
  if (!configFileError) {
    configFile.config.compilerOptions =
        Object.assign({}, configFile.config.compilerOptions, loaderOptions.compilerOptions);
    // do any necessary config massaging
    if (loaderOptions.transpileOnly) {
      configFile.config.compilerOptions.isolatedModules = true;
    }
  }
  return {configFilePath: configFilePath, configFile: configFile, configFileError: configFileError};
};
github Akryum / nodepack / packages / @nodepack / plugin-typescript / src / index.js View on Github external
config.resolveLoader.modules.prepend(path.join(__dirname, 'node_modules'))

    config.resolve
      .extensions
        .merge(['.ts'])

    const tsRule = config.module.rule('ts').test(/\.ts$/)

    const addLoader = ({ loader, options = {}}) => {
      tsRule.use(loader).loader(loader).options(options)
    }

    addLoader({
      loader: 'cache-loader',
      options: api.genCacheConfig('ts-loader', {
        'ts-loader': require('ts-loader/package.json').version,
        'typescript': require('typescript/package.json').version,
      }, 'tsconfig.json'),
    })

    if (useThreads) {
      addLoader({
        loader: 'thread-loader',
      })
    }

    if (api.hasPlugin('babel')) {
      addLoader({
        loader: 'babel-loader',
      })
    }
    addLoader({
github Akryum / nodepack / packages / @nodepack / plugin-typescript / src / index.js View on Github external
config.resolveLoader.modules.prepend(path.join(__dirname, 'node_modules'))

    config.resolve
      .extensions
      .merge(['.ts'])

    const tsRule = config.module.rule('ts').test(/\.ts$/)

    const addLoader = ({ loader, options = {} }) => {
      tsRule.use(loader).loader(loader).options(options)
    }

    addLoader({
      loader: 'cache-loader',
      options: api.genCacheConfig('ts-loader', {
        'ts-loader': require('ts-loader/package.json').version,
        'typescript': require('typescript/package.json').version,
      }, 'tsconfig.json'),
    })

    if (useThreads) {
      addLoader({
        loader: 'thread-loader',
      })
    }

    if (api.hasPlugin('babel')) {
      addLoader({
        loader: 'babel-loader',
      })
    }
    addLoader({
github zeit / ncc / scripts / build.js View on Github external
console.log(assets);
  }

  writeFileSync(__dirname + "/../dist/ncc/cli.js.cache", cliAssets["cli.js.cache"].source);
  writeFileSync(__dirname + "/../dist/ncc/index.js.cache", indexAssets["index.js.cache"].source);
  writeFileSync(__dirname + "/../dist/ncc/sourcemap-register.js.cache", sourcemapAssets["sourcemap-register.js.cache"].source);
  writeFileSync(__dirname + "/../dist/ncc/loaders/relocate-loader.js.cache", relocateLoaderAssets["relocate-loader.js.cache"].source);
  writeFileSync(__dirname + "/../dist/ncc/loaders/shebang-loader.js.cache", shebangLoaderAssets["shebang-loader.js.cache"].source);
  writeFileSync(__dirname + "/../dist/ncc/loaders/ts-loader.js.cache", tsLoaderAssets["ts-loader.js.cache"].source);

  writeFileSync(__dirname + "/../dist/ncc/cli.js.cache.js", cliAssets["cli.js.cache.js"].source);
  writeFileSync(__dirname + "/../dist/ncc/index.js.cache.js", indexAssets["index.js.cache.js"].source);
  writeFileSync(__dirname + "/../dist/ncc/sourcemap-register.js.cache.js", sourcemapAssets["sourcemap-register.js.cache.js"].source);
  writeFileSync(__dirname + "/../dist/ncc/loaders/relocate-loader.js.cache.js", relocateLoaderAssets["relocate-loader.js.cache.js"].source);
  writeFileSync(__dirname + "/../dist/ncc/loaders/shebang-loader.js.cache.js", shebangLoaderAssets["shebang-loader.js.cache.js"].source);
  writeFileSync(__dirname + "/../dist/ncc/loaders/ts-loader.js.cache.js", tsLoaderAssets["ts-loader.js.cache.js"].source);

  writeFileSync(__dirname + "/../dist/ncc/cli.js", cli, { mode: 0o777 });
  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 || (process.cwd() + '/'));
let typescript;
try {
  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)");
}
github egoist / poi / plugins / typescript / index.js View on Github external
api.webpackUtils.addCacheSupport(rule, () =>
      api.getCacheConfig(
        'ts-loader',
        {
          typescript: api.localRequire('typescript/package').version,
          'ts-loader': require('ts-loader/package').version
        },
        configFile
      )
    )