Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

output.entryFileNames - accept a function? #2585

Closed
PatNeedham opened this issue Dec 6, 2018 · 7 comments · Fixed by #3658
Closed

output.entryFileNames - accept a function? #2585

PatNeedham opened this issue Dec 6, 2018 · 7 comments · Fixed by #3658

Comments

@PatNeedham
Copy link

Feature Use Case

Is it possible for entryFileNames option to accept a function in addition to a string?

For background context, this is what my rollup.config.js looks like:

import commonjs from 'rollup-plugin-commonjs';
import nodeResolve from 'rollup-plugin-node-resolve';
import globals from 'rollup-plugin-node-globals';
import builtins from 'rollup-plugin-node-builtins';
import babel from 'rollup-plugin-babel';
import json from 'rollup-plugin-json';

module.exports = {
  experimentalCodeSplitting: true,
  optimizeChunks: true,
  external: […],
  input: [
    'src/components/Alert.jsx',
    'src/components/Anchor.jsx',
    'src/components/Button.jsx’,
    'src/components/Dropdown/index.jsx', // inside own folder
    ‘src/components/SomethingElse/index.jsx’,  // inside own folder
    …
  ],
  output: {
    dir: 'dist',
    format: 'esm'
  },
  plugins: [
    json(),
    nodeResolve({
      jsnext: true,
      preferBuiltins: false,
      extensions: ['.js', '.jsx', '.json'],
    }),
    commonjs({
      include: [
        'node_modules/**'
      ],
      exclude: [
        'node_modules/process-es6/**'
      ],
      namedExports: {
        'node_modules/downshift/node_modules/react-is/index.js': ['isForwardRef'],
        'node_modules/prop-types/index.js': [ ... ],
        'node_modules/react/index.js': [ ... ],
        'node_modules/react-dom/index.js': ['createPortal', 'findDOMNode', 'render']
      }
    }),
    babel({
      exclude: 'node_modules/**',
      presets: ['@babel/preset-env', 'react-app'],
      plugins: [
        ['syntax-trailing-function-commas', { loose: true }],
        ['@babel/plugin-proposal-class-properties', { loose: true }],
      ],
      runtimeHelpers: true,
    }),
    globals(),
    builtins(),
  ],
}

The default [name].js value for output.entryFileNames field this leads to a dist folder which contains the majority of the components necessary, except for those like Dropdown, SomethingElse, etc. the ones inside their own folders. However, they do appear inside files named like index.js, index1.js, index2.js, etc.

What can be done to make Rollup create output files like Dropdown.js and SomethingElse.js instead of index.js and index.1.js?

Feature Proposal

If entryFileNames accepted a function, that would address this issue because I could then write something like

output: {
    dir: 'dist',
    entryFileNames: (filePath) => endsWithIndexJSX(filePath) ? `${getLastFolderName(filePath)}.js` : `[name].js`
    format: 'esm'
  },
@lukastaegert
Copy link
Member

lukastaegert commented Dec 7, 2018

This is definitely doable but might take some time. Note that you can already achieve the same (though maybe not as elegantly) today by using the object form of input:

input: {
  Alert: 'src/components/Alert.jsx',
  Dropdown: 'src/components/Dropdown/index.jsx', 
  // if you actually do want it in a special sub-folder
  'separate/SomethingElse': 'src/components/SomethingElse.jsx', 
  …
},

@jakearchibald
Copy link
Contributor

The same feature would solve a slightly different use-case:

Most names should feature a [hash], but some resources (like a service worker, or in some cases a favicon) should not, so a single rule doesn't quite work.

@shellscape
Copy link
Contributor

Hey folks. This is a saved-form message, but rest assured we mean every word. The Rollup team is attempting to clean up the Issues backlog in the hopes that the active and still-needed, still-relevant issues bubble up to the surface. With that, we're closing issues that have been open for an eon or two, and have gone stale like pirate hard-tack without activity.

We really appreciate the folks have taken the time to open and comment on this issue. Please don't confuse this closure with us not caring or dismissing your issue, feature request, discussion, or report. The issue will still be here, just in a closed state. If the issue pertains to a bug, please re-test for the bug on the latest version of Rollup and if present, please tag @shellscape and request a re-open, and we'll be happy to oblige.

@lukastaegert
Copy link
Member

For me, making the file name output properties functions is definitely a desirable feature which could also solve some other issues. Reopening it to track this.

@shellscape
Copy link
Contributor

shellscape commented Aug 12, 2019

@lukastaegert I'd like to tackle this one but I'll need some direction on when and where the function should be called. I've tracked down where the options are fetched from config, but unsure on the best place to call the fn. Is renderNamePattern in renderNamePattern.ts the right spot for that?

We're also going to get requests for async functions I'm sure, so I'll have to take that into consideration.

@lukastaegert
Copy link
Member

I would not go for async functions unless there is actual demand. Also there are hardly any places in the user facing options where you can do something async.

Also I think this should make all three XYfileNames options replaceable with functions.

As for the right place, yes, renderNamePattern could be a good choice. I assume the idea is that if pattern is a function, it is called to get the actual pattern? Sounds good!

@frank-dspeed
Copy link
Contributor

I did implement it in #3658 and added a test it works also with assets and all other filds that support a pattern.

lukastaegert added a commit that referenced this issue Jul 6, 2020
…aces where you could use a pattern string before (#3658)

* # Das ist eine Kombination aus 4 Commits.
# Das ist die erste Commit-Beschreibung:

#2585

# Das ist Commit-Beschreibung #2:

Better Implementation now with a test

# Das ist Commit-Beschreibung #3:

Add: Tests

# Das ist Commit-Beschreibung #4:

Fix: License

* #2585

Better Implementation now with a test

Add: Tests

Fix: License

Now passing a object { replacements, meta() }

Add: docs and renamed property

* Corrected types

* add: test

* Upgraded test to include a chunk via dynamic import

* Fix: solo test

* Refine interfaces, types, docs and test

* Further refine docs

Co-authored-by: Lukas Taegert-Atkinson <lukas.taegert-atkinson@tngtech.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
5 participants