Skip to content

Commit

Permalink
feat: support --sourcemap (#23)
Browse files Browse the repository at this point in the history
* feat: support `--sourcemap`

close #22

* change the default value of sourcemap to `undefined`

* maintain consistent type with farm

* modify merge option

* handle string Boolean values

* add `changeset` msg to support `--sourcemap`

* optimized code

* remove redundant functions

* update `pnpm-lock`
  • Loading branch information
RSS1102 committed Jun 3, 2024
1 parent 3664108 commit 4101294
Show file tree
Hide file tree
Showing 7 changed files with 205 additions and 209 deletions.
5 changes: 5 additions & 0 deletions .changeset/two-fans-repeat.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'farmup': patch
---

support `--sourcemap`
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -117,3 +117,10 @@ set external package or path
- `default`: `true`

in your code, if not find `package` or `source`, set external

### sourcemap

- `default`: `undefined`
- option: `boolean` | `'inline'` | `'all'` | `'all-inline'`

generate sourcemap
2 changes: 2 additions & 0 deletions playground/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
"private": true,
"description": "",
"scripts": {
"build": "farmup",
"sourcemap": "farmup --sourcemap",
"dev": "farmup -w"
},
"keywords": [],
Expand Down
376 changes: 171 additions & 205 deletions pnpm-lock.yaml

Large diffs are not rendered by default.

8 changes: 6 additions & 2 deletions src/config/normalize/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,9 @@ async function normalizedSimpleConfig(
...(commonOptions.mode || config.compilation?.mode
? { mode: commonOptions.mode || config.compilation?.mode }
: {}),
...(!isUndefined(commonOptions.sourcemap ?? config.compilation?.sourcemap)
? { sourcemap: commonOptions.sourcemap ?? config.compilation?.sourcemap }
: {}),
...(commonOptions.format || config.compilation?.output?.format
? { format: commonOptions.format || config.compilation?.output?.format }
: {}),
Expand Down Expand Up @@ -233,9 +236,10 @@ export class NormalizeOption {
noExecute: false,
watchFiles: [],
outputDir: './dist',
sourcemap: undefined,
};

constructor(private commonOption: CommonOptions, private logger: Logger) {}
constructor(private commonOption: CommonOptions, private logger: Logger) { }

async config(config: UserConfig): Promise<UserConfig> {
await normalizedSimpleConfig(config, this.commonOption, this.options, this.logger);
Expand All @@ -249,7 +253,7 @@ export class NormalizeOption {
...(this.options.outputEntry ? { entryFilename: this.options.outputEntry.name } : {}),
path: this.options.outputDir,
},
...pick(this.options, 'minify'),
...pick(this.options, 'minify', 'sourcemap'),
},
},
this.options
Expand Down
6 changes: 4 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ cli.option('-w, --watch [...files]', 'watch files', { default: false })
.option('--format [format]', 'choose one from "cjs" or "esm"')
.option('--external [...external]', 'external')
.option('--no-auto-external', 'if not found module, auto as external', { default: true })
.option('--sourcemap [sourcemap]', 'generate sourcemap or not')
.option(
'--target [target]',
"target for output, default is node, support 'browser'、'node'、'node16'、'node-legacy'、'node-next'、'browser-legacy'、'browser-es2015'、'browser-es2017'、'browser-esnext'"
Expand All @@ -101,8 +102,8 @@ async function commonOptionsFromArgs(args: Record<string, any>): Promise<Partial
? args.config
: path.resolve(root, args.config)
: args.config
? await getConfigFilePath(root)
: undefined;
? await getConfigFilePath(root)
: undefined;
const execute = isString(args.exec) && !isBoolean(args.exec) ? args.exec : undefined;

return {
Expand All @@ -122,6 +123,7 @@ async function commonOptionsFromArgs(args: Record<string, any>): Promise<Partial
.map((item) => (item === true ? undefined : item))
.filter(Boolean),
outputDir: args.output || './dist',
sourcemap: args.sourcemap === 'false' ? false : args.sourcemap === 'true' ? true : args.sourcemap,
};
}

Expand Down
10 changes: 10 additions & 0 deletions src/types/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,11 @@ export interface CommonOptions {
* @default './dist'
*/
outputDir?: string;
/**
* generate sourcemap
* @params boolean | 'inline' | 'all' | 'all-inline'
*/
sourcemap?: boolean | 'inline' | 'all' | 'all-inline'
}

export interface ResolvedCommonOptions {
Expand Down Expand Up @@ -92,6 +97,11 @@ export interface ResolvedCommonOptions {
* @default './dist'
*/
outputDir: string;
/**
* generate sourcemap
* @params boolean | 'inline' | 'all' | 'all-inline'
*/
sourcemap?: boolean | 'inline' | 'all' | 'all-inline'
}

export enum ExecuteMode {
Expand Down

0 comments on commit 4101294

Please sign in to comment.