@shopify/babel-plugin-convert-empty-typescript-file-to-es-module
Turns empty TypeScript files into ES modules for better webpack tree-shaking
Last updated 7 months ago by shopify-dep .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install @shopify/babel-plugin-convert-empty-typescript-file-to-es-module 
SYNC missed versions from official npm registry.

@shopify/babel-plugin-convert-empty-typescript-file-to-es-module

When compiling a project with type checking disabled, TypeScript files containing only type declarations can increase asset sizes. This is because:

  • tsc transpiles the types module down to empty JavaScript source
  • When processing files in isolation, tsc does not remove import/export references to the empty types file
    • Hence, building with type-checking enabled eliminates this problem
  • webpack interprets a module with no imports/exports as being a CommonJS module
  • ModuleConcatenationPlugin refuses to hoist non-ES6 modules

This forces webpack to include per-module boilerplate for siblings of the types module.

Fixing the problem

This plugin adds an unused ES export to empty .ts/.tsx files. This allows ModuleConcatenationPlugin to:

  • Safely discard the module as side effects free
  • Remove import/export references to the module from other files
  • Hoist sibling modules into a single scope, with minimal boilerplate

Rationale for building without type-checking

For larger applications, type-checking can add several minutes (and OutOfMemory exceptions) to builds.

Installation

yarn add --dev @shopify/babel-plugin-convert-empty-typescript-file-to-es-module

Usage

Add this as a Babel plugin that transforms the output of a TypeScript loader. e.g., a minimal configuration using ts-loader:

const path = require('path');

module.exports = {
  mode: 'production',
  module: {
    rules: [
      {
        test: /\.tsx?$/,
        use: [
          {
            loader: 'babel-loader',
            options: {
              plugins: [
                '@shopify/babel-plugin-convert-empty-typescript-file-to-es-module',
              ],
            },
          },
          {
            loader: 'ts-loader',
            options: {
              transpileOnly: true,
            },
          },
        ],
      },
    ],
  },
};

Current Tags

  • 0.1.3                                ...           latest (7 months ago)

3 Versions

  • 0.1.3                                ...           7 months ago
  • 0.1.1                                ...           9 months ago
  • 0.1.0                                ...           9 months ago
Downloads
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 2
Dependencies (0)
None
Dev Dependencies (1)

Copyright 2014 - 2016 © taobao.org |