Skip to content

Commit 4101294

Browse files
authored
feat: support --sourcemap (#23)
* 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`
1 parent 3664108 commit 4101294

File tree

7 files changed

+205
-209
lines changed

7 files changed

+205
-209
lines changed

.changeset/two-fans-repeat.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'farmup': patch
3+
---
4+
5+
support `--sourcemap`

README.md

+7
Original file line numberDiff line numberDiff line change
@@ -117,3 +117,10 @@ set external package or path
117117
- `default`: `true`
118118

119119
in your code, if not find `package` or `source`, set external
120+
121+
### sourcemap
122+
123+
- `default`: `undefined`
124+
- option: `boolean` | `'inline'` | `'all'` | `'all-inline'`
125+
126+
generate sourcemap

playground/package.json

+2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
"private": true,
55
"description": "",
66
"scripts": {
7+
"build": "farmup",
8+
"sourcemap": "farmup --sourcemap",
79
"dev": "farmup -w"
810
},
911
"keywords": [],

pnpm-lock.yaml

+171-205
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/config/normalize/index.ts

+6-2
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,9 @@ async function normalizedSimpleConfig(
179179
...(commonOptions.mode || config.compilation?.mode
180180
? { mode: commonOptions.mode || config.compilation?.mode }
181181
: {}),
182+
...(!isUndefined(commonOptions.sourcemap ?? config.compilation?.sourcemap)
183+
? { sourcemap: commonOptions.sourcemap ?? config.compilation?.sourcemap }
184+
: {}),
182185
...(commonOptions.format || config.compilation?.output?.format
183186
? { format: commonOptions.format || config.compilation?.output?.format }
184187
: {}),
@@ -233,9 +236,10 @@ export class NormalizeOption {
233236
noExecute: false,
234237
watchFiles: [],
235238
outputDir: './dist',
239+
sourcemap: undefined,
236240
};
237241

238-
constructor(private commonOption: CommonOptions, private logger: Logger) {}
242+
constructor(private commonOption: CommonOptions, private logger: Logger) { }
239243

240244
async config(config: UserConfig): Promise<UserConfig> {
241245
await normalizedSimpleConfig(config, this.commonOption, this.options, this.logger);
@@ -249,7 +253,7 @@ export class NormalizeOption {
249253
...(this.options.outputEntry ? { entryFilename: this.options.outputEntry.name } : {}),
250254
path: this.options.outputDir,
251255
},
252-
...pick(this.options, 'minify'),
256+
...pick(this.options, 'minify', 'sourcemap'),
253257
},
254258
},
255259
this.options

src/index.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ cli.option('-w, --watch [...files]', 'watch files', { default: false })
8787
.option('--format [format]', 'choose one from "cjs" or "esm"')
8888
.option('--external [...external]', 'external')
8989
.option('--no-auto-external', 'if not found module, auto as external', { default: true })
90+
.option('--sourcemap [sourcemap]', 'generate sourcemap or not')
9091
.option(
9192
'--target [target]',
9293
"target for output, default is node, support 'browser'、'node'、'node16'、'node-legacy'、'node-next'、'browser-legacy'、'browser-es2015'、'browser-es2017'、'browser-esnext'"
@@ -101,8 +102,8 @@ async function commonOptionsFromArgs(args: Record<string, any>): Promise<Partial
101102
? args.config
102103
: path.resolve(root, args.config)
103104
: args.config
104-
? await getConfigFilePath(root)
105-
: undefined;
105+
? await getConfigFilePath(root)
106+
: undefined;
106107
const execute = isString(args.exec) && !isBoolean(args.exec) ? args.exec : undefined;
107108

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

src/types/options.ts

+10
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,11 @@ export interface CommonOptions {
6363
* @default './dist'
6464
*/
6565
outputDir?: string;
66+
/**
67+
* generate sourcemap
68+
* @params boolean | 'inline' | 'all' | 'all-inline'
69+
*/
70+
sourcemap?: boolean | 'inline' | 'all' | 'all-inline'
6671
}
6772

6873
export interface ResolvedCommonOptions {
@@ -92,6 +97,11 @@ export interface ResolvedCommonOptions {
9297
* @default './dist'
9398
*/
9499
outputDir: string;
100+
/**
101+
* generate sourcemap
102+
* @params boolean | 'inline' | 'all' | 'all-inline'
103+
*/
104+
sourcemap?: boolean | 'inline' | 'all' | 'all-inline'
95105
}
96106

97107
export enum ExecuteMode {

0 commit comments

Comments
 (0)