Skip to content

Commit 7e58839

Browse files
committedFeb 22, 2019
feat: allow output.fileName to be a function
1 parent 1047e76 commit 7e58839

File tree

3 files changed

+34
-16
lines changed

3 files changed

+34
-16
lines changed
 

‎src/index.ts

+21-14
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ import {
3232
Format,
3333
ConfigEntryObject,
3434
Env,
35-
ConfigOutput
35+
ConfigOutput,
36+
FileNameContext
3637
} from './types'
3738

3839
// Make rollup-plugin-vue use basename in component.__file instead of absolute path
@@ -197,7 +198,9 @@ export class Bundler {
197198

198199
// UMD format should always bundle node_modules
199200
const bundleNodeModules =
200-
format === 'umd' || format === 'iife' || config.bundleNodeModules
201+
rollupFormat === 'umd' ||
202+
rollupFormat === 'iife' ||
203+
config.bundleNodeModules
201204

202205
plugins.push(
203206
nodeResolvePlugin({
@@ -384,15 +387,14 @@ export class Bundler {
384387
}
385388
})
386389

387-
const isESM = /^esm?$/.test(format)
388-
389-
const fileName =
390-
config.output.fileName ||
391-
(format === 'cjs' || isESM
392-
? `[name][min][ext]`
393-
: `[name].[format][min][ext]`)
394-
395-
const extPlaceholder = isESM ? '.mjs' : '.js'
390+
const getFileName = config.output.fileName || defaultGetFileName
391+
const fileNameTemplate =
392+
typeof getFileName === 'function'
393+
? getFileName({ format: rollupFormat, minify })
394+
: getFileName
395+
const fileName = fileNameTemplate
396+
.replace(/\[min\]/, minPlaceholder)
397+
.replace(/\[ext\]/, /^esm?$/.test(rollupFormat) ? '.mjs' : '.js')
396398

397399
return {
398400
inputConfig: {
@@ -424,9 +426,7 @@ export class Bundler {
424426
globals: config.globals,
425427
format: rollupFormat,
426428
dir: path.resolve(config.output.dir || 'dist'),
427-
entryFileNames: fileName
428-
.replace(/\[min\]/, minPlaceholder)
429-
.replace(/\[ext\]/, extPlaceholder),
429+
entryFileNames: fileName,
430430
name: config.output.moduleName,
431431
banner,
432432
sourcemap:
@@ -654,4 +654,11 @@ async function printAssets(assets: Assets, title: string) {
654654
)
655655
}
656656

657+
function defaultGetFileName({ format }: FileNameContext) {
658+
const isESM = /^esm?$/.test(format)
659+
return format === 'cjs' || isESM
660+
? `[name][min][ext]`
661+
: `[name].[format][min][ext]`
662+
}
663+
657664
export { Config, NormalizedConfig, Options, ConfigOutput }

‎src/types.ts

+12-1
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,13 @@ export type ExtendConfig = (
3535
{ format, input }: { format: Format; input: string[] | ConfigEntryObject }
3636
) => NormalizedConfig
3737

38+
export interface FileNameContext {
39+
format: RollupFormat
40+
minify: boolean
41+
}
42+
43+
export type GetFileName = (context: FileNameContext) => string
44+
3845
export interface BabelPresetOptions {
3946
/**
4047
* Transform `async/await` to `Promise`.
@@ -92,9 +99,13 @@ export interface ConfigOutput {
9299
* - `[format]`: The output format. (without `-min` suffix)
93100
* - `[ext]`: The extension. It's `.mjs` for `esm` format, `.js` otherwise
94101
* - `[min]`: It will replaced by `.min` when the format ends with `-min`, otherwise it's an empty string.
102+
*
103+
* The value can also be a function which returns the fileName template,
104+
* The placeholders are also available in the return value.
105+
*
95106
* @cli `--file-name <fileName>`
96107
*/
97-
fileName?: string
108+
fileName?: string | GetFileName
98109
/**
99110
* Module name for umd bundle
100111
*/

‎test/__snapshots__/index.test.ts.snap

+1-1
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,7 @@ module.exports = index;
256256
"
257257
`;
258258

259-
exports[`uglify: uglify dist/index.cjs.min.js 1`] = `
259+
exports[`uglify: uglify dist/index.min.js 1`] = `
260260
"\\"use strict\\";Object.defineProperty(exports,\\"__esModule\\",{value:!0});const a=1;exports.a=1;
261261
"
262262
`;

0 commit comments

Comments
 (0)
Please sign in to comment.