Skip to content
This repository has been archived by the owner on Jan 5, 2024. It is now read-only.

Latest commit

 

History

History
97 lines (88 loc) · 1.84 KB

Recipes.md

File metadata and controls

97 lines (88 loc) · 1.84 KB

Recipes

Typescript

You will need to install ts-loader for Haul to work with TypeScript.

yarn add -D ts-loader

This is a webpack.haul.js that works with TypeScript.

module.exports = ({ platform }, { module, resolve }) => ({
  entry: `./src/index.${platform}.tsx`,
  module: {
    ...module,
    rules: [
      {
        test: /\.tsx?$/,
        loader: 'ts-loader'
      },
      ...module.rules
    ]
  },
  resolve: {
    ...resolve,
    extensions: [
      '.ts',
      '.tsx',
      `.${platform}.ts`,
      '.native.ts',
      `.${platform}.tsx`,
      '.native.tsx',
      ...resolve.extensions
    ]
  }
});

And a corresponding (example) tsconfig.json

{
  "compilerOptions": {
    "jsx": "react",
    "target": "es2015",
    "moduleResolution": "node",
    "sourceMap": true
  },
  "exclude": [
    "node_modules"
  ]
}

However, if you want to use synthetic defaults imports, aka things like import React from 'react' instead of import * as React from 'react' you will need a little more. Your .tsconfig.json must have "allowSyntheticDefaultImports": true, and you will need to pass the code through babel. You will need babel-loader for this.

yarn add -D babel-loader

Revised webpack.haul.js

module.exports = ({ platform }, { module, resolve }) => ({
  entry: `./src/index.${platform}.tsx`,
  module: {
    ...module,
    rules: [
      {
        test: /\.tsx?$/,
        exclude: '/node_modules/',
        use: [
          {
            loader: 'babel-loader',
          },
          {
            loader: 'ts-loader'
          },
        ],
      },
      ...module.rules
    ]
  },
  resolve: {
    ...resolve,
    extensions: [
      '.ts',
      '.tsx',
      `.${platform}.ts`,
      '.native.ts',
      `.${platform}.tsx`,
      '.native.tsx',
      ...resolve.extensions],
  },
});