question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

plugin generates multiple cache folders for same TS project -- different Rollup input/output

See original GitHub issue

What happens and why it is wrong

TS plugin use entire Rollup options to generate hash code as a part of cache folder name. It means always generate new cache folder even input file is in the same TypeScript project.

image

Versions

  • typescript: 3.6.2
  • rollup: 1.20.3
  • rollup-plugin-typescript2: 0.24.0

rollup.config.js

page1

{
        input: 'src/js/page1.ts',
        output: {
            name: 'Page1',
            file: 'src/build/page1.js',
        },
        treeshake: true,
        output: {
            format: 'iife'
        },
        onwarn(warning) {
            if (warning.code === 'EVAL') return;

            console.warn(warning.code + ': ' + warning.message);
        },
        plugins: [
            resolve({
                modulesOnly: false,
                extensions: ['.js', '.json', '.css']
            }),
            sourcePlugin,
            json({
                include: 'src/**/*.json',
                preferConst: true
            }),
            html({
                include: ['src/**/*.html', 'src/**/*.svg'],
                htmlMinifierOptions: {
                    caseSensitive: true,
                    collapseWhitespace: true,
                    collapseBooleanAttributes: true,
                    removeComments: true
                }
            }),
            url({
                limit: 10 * 1024, // inline files < 10k, copy files > 10k
                include: ["src/**/*.png"],
                emitFiles: false
            }),
            less({
                // Generate CSS separately
                output: options.output.file.replace('.js', '.css'),
                exclude: [],
                option: {
                    plugins: [
                        cleanCSSPlugin
                    ]
                }
            }),
            terser({
                // https://github.com/terser-js/terser#minify-options
                compress: {},
                output: {},
                // https://github.com/mishoo/UglifyJS2/issues/1753
                mangle: {
                    safari10: true
                }
            }),
        ]
    }

page2

{
        input: 'src/js/page2.ts',
        output: {
            name: 'Page2',
            file: 'src/build/page2.js',
        },
        ...
}

page3

{
        input: 'src/js/page3.ts',
        output: {
            name: 'Page3',
            file: 'src/build/page3.js',
        },
        ...
}

page4

{
        input: 'src/js/page4.ts',
        output: {
            name: 'Page4',
            file: 'src/build/page4.js',
        },
        ...
}

page5

{
        input: 'src/js/page5.ts',
        output: {
            name: 'Page5',
            file: 'src/build/page5.js',
        },
        ...
}

tsconfig.json in src folder

{
  "extends": "../configs/tsconfig.base",
  "compilerOptions": {
    "composite": true,
    "module": "amd",
    "outDir": "./build",
    "baseUrl": ".",
    "declaration": true,
    "declarationMap": true,
    "sourceMap": false,
    "types": []
  },
  "include": [ "**/*.ts", "**/*.json" ],
  "exclude": [ "build", "lib" ]
}

tsconfig.base in config folder

{
  "compileOnSave": true,
  "compilerOptions": {
    "target": "es2016",
    "moduleResolution": "node",
    "noImplicitAny": true,
    "noImplicitReturns": true,
    "noImplicitThis": true,
    "noUnusedLocals": true,
    "noEmitHelpers": true,
    "strictFunctionTypes": true,
    "alwaysStrict": true,
    "experimentalDecorators": true,
    "keyofStringsOnly": true,
    "forceConsistentCasingInFileNames": true,
    "resolveJsonModule": true,
    // Common ES2016 - Proxy
    "lib": [
      "dom",
      "dom.iterable",
      "es5",
      "es2015.core",
      "es2015.collection",
      "es2015.iterable",
      "es2015.promise",
      "es2016.array.include",
      "es2017.object"
    ],
    "plugins": [
      {
        "name": "typescript-tslint-plugin",
        "ignoreDefinitionFiles": true
      }
    ]
  }
}

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:9 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
Soul-Mastercommented, May 8, 2022

Thanks again for your advise. I compile TypeScript project as ES6 module to cache folder and use the following Rollup options to resolve TypeScript files.

{
   resolveId(id, importer) {
       let absPath = path.resolve(path.dirname(importer), id);
       const fileName = path.basename(absPath);        

       if (!fileName.includes('.')) {
           return absPath + '.ts';
       }

       return null;
   },
   load(id) {
       if (id.slice(-3) !== '.ts') return null;

       const es6File = cacheFolderPath + id.substring(srcFolderPath.length, id.length - 2) + 'js';

       return { code: fs.readFileSync(es6File, 'utf-8') };
   }
}

It reduces compile time for 12 pages around 60%. image

1reaction
ezolenkocommented, Oct 10, 2019

So you are using same input typescript and get exactly the same output bundle from rollup, just in several different names (so page1.js === page2.js === etc)? Or are the bundles different?

RE: fixed cache, you can get the same effect if you precompile ts files into js (either as a separate rollup run, or by using tsc directly), and then use js directly in your many following rollup calls.

Read more comments on GitHub >

github_iconTop Results From Across the Web

@rollup/plugin-typescript - npm
Start using @rollup/plugin-typescript in your project by running `npm i @rollup/plugin-typescript`. There are 754 other projects in the npm ...
Read more >
rollup.js
Rollup can import existing CommonJS modules through a plugin. ... you can create multiple outputs from the same input to generate e.g. //...
Read more >
cardano-addresses - npm Package Health Analysis | Snyk
TSDX uses Rollup as a bundler and generates multiple rollup configs for various module formats and build settings. See Optimizations for details. TypeScript....
Read more >
@wessberg/di-compiler - NPM Package Overview - Socket
Description. This library enables you to use the DI library by providing several ways to transform your source code into a representation that...
Read more >
Upgrading from AngularJS to Angular
Using a Module Loaderlink. When you break application code down into one component per file, you often end up with a project structure...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found