Skip to content

Plugin not respecting include / exclude when generating declarations #162

@aapoalas

Description

@aapoalas

What happens and why it is wrong

I have a directory within which various entry files are sought based on their file paths. These entry files are then one by one (not as an array of input files) built using Rollup. As some of these files may be TS files or may import TS files, I wanted to use rollup-plugin-typescript2 to type check and build these files.

However, after adding the plugin to the rollup config with only .ts and .tsx files included, build times jump massively. At the same time JavaScript files which import eg. lodash with the intention that Rollup will warn me of these modules being taken as external since I'm not using rollup-plugin-node-resolve (intentional, again) start appearing in the bundling and give errors for being CJS and thus not exporting anything. That is to say that clearly this plugin is resolving those imports even in normal JavaScript files and happily transpiling or at least analyzing these JavaScript files for types. This despite me explicitly only including TS files and explicitly excluding non-TS files.

Versions

  • typescript: 3.4.1
  • rollup: 1.15.1
  • rollup-plugin-typescript2: 0.21.2

rollup.config.js

const EXTERNALS = ["known-external-module", "another-known-external-module"];
const inputOptions = {
    external: moduleId =>
        moduleId.startsWith("custom-loader!") ||
        (isTestFile &&
            !(
                moduleId.startsWith("./") || moduleId.startsWith("../") // Test files are expected to contain external modules that will later be resolved by a webpack test build
            )) ||
        EXTERNALS.includes(moduleId),
    input, // input file path
    onwarn: (warning, warn) => {
        warning.message = `${input}: ${warning.message}`;
        warn(warning);
    },
    plugins: [
        rollupPluginReplace({
            delimiters: ["<%= ", " %>"],
            values, // Doing some string replacement
        }),
        rollupPluginTypescript2({
            include: ["*.ts", "**/*.ts", "*.tsx", "**/*.tsx"], // Only run the plugin on TS files
            exclude: ["!*.ts", "!**/*.ts", "!*.tsx", "!**/*.tsx"], // Seriously, don't run it for any other files
        }),
        rollupPluginBabel({
            include: ["*.jsx", "**/*.jsx", "*.tsx", "**/*.tsx"],
            plugins: [[babelPluginJSX, { useBuiltIns: true }]], // Just transpiling JSX and TSX
        }),
        customImportCSSPlugin(), // Include CSS files as text
        rollupPluginHTML(), // Include HTML files as text
        rollupPluginJSON(), // Include JSON files as JS objects
        PRODUCTION && // Minify if necessary
            terser({
                sourcemap: false,
            }),
    ],
};
const outputOptions = {
    amd: {
        id: dataObject[input],
    },
    esModule: false,
    file: output.replace(/\.(jsx|tsx?)$/, ".js"), // Write output as normal JS
    format: isTestFile ? "esm" : "amd", // Yes yes, I know, AMD *sigh*
    interop: false,
    preferConst: true,
};

tsconfig.json

{
    "compilerOptions": {
        "baseUrl": ".",
        "sourceMap": true, // allow sourcemap support
        "strictNullChecks": true, // enable strict null checks as a best practice
        "module": "esnext",
        "target": "es6", // specify ECMAScript target version
        "allowJs": true, // allow a partial TypeScript and JavaScript codebase
        "moduleResolution": "node",
        "traceResolution": false,
        "jsx": "react",
        "lib": ["dom", "es2017", "esnext"],
        "resolveJsonModule": true,
        "paths": {
            "*": ["./node_modules/@types/*", "*"]
        }
    },
    "exclude": ["bin", "build", "node_modules"]
}

plugin output with verbosity 3

log:
rpt2: built-in options overrides: {
    "noEmitHelpers": false,
    "importHelpers": true,
    "noResolve": false,
    "noEmit": false,
    "inlineSourceMap": false,
    "outDir": "/path/.rpt2_cache/placeholder",
    "moduleResolution": 2,
    "allowNonTsExtensions": true
}
rpt2: parsed tsconfig: {
    "options": {
        "baseUrl": "/path",
        "sourceMap": true,
        "strictNullChecks": true,
        "module": 6,
        "target": 2,
        "allowJs": true,
        "moduleResolution": 2,
        "traceResolution": false,
        "jsx": 2,
        "lib": [
            "lib.dom.d.ts",
            "lib.es2017.d.ts",
            "lib.esnext.d.ts"
        ],
        "resolveJsonModule": true,
        "paths": {
            "*": [
                "./node_modules/@types/*",
                "*"
            ]
        },
        "configFilePath": "/path/tsconfig.json",
        "noEmitHelpers": false,
        "importHelpers": true,
        "noResolve": false,
        "noEmit": false,
        "inlineSourceMap": false,
        "outDir": "/path/.rpt2_cache/placeholder",
        "allowNonTsExtensions": true
    },
    "fileNames": [
        ...
    ],
    "typeAcquisition": {
        "enable": false,
        "include": [],
        "exclude": []
    },
    "raw": {
        "compilerOptions": {
            "baseUrl": ".",
            "sourceMap": true,
            "strictNullChecks": true,
            "module": "esnext",
            "target": "es6",
            "allowJs": true,
            "moduleResolution": "node",
            "traceResolution": false,
            "jsx": "react",
            "lib": [
                "dom",
                "es2017",
                "esnext"
            ],
            "resolveJsonModule": true,
            "paths": {
                "*": [
                    "./node_modules/@types/*",
                    "*"
                ]
            }
        },
        "exclude": [
            "bin",
            "build",
            "node_modules"
        ],
        "compileOnSave": false
    },
    "errors": [],
    "wildcardDirectories": {
        "/path": 1
    },
    "compileOnSave": false,
    "configFileSpecs": {
        "includeSpecs": [
            "**/*"
        ],
        "excludeSpecs": [
            "bin",
            "build",
            "node_modules"
        ],
        "validatedIncludeSpecs": [
            "**/*"
        ],
        "validatedExcludeSpecs": [
            "bin",
            "build",
            "node_modules"
        ],
        "wildcardDirectories": {
            "/path": 1
        }
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    problem: staleIssue has not been responded to in some time

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions