Skip to content
This repository was archived by the owner on Jan 16, 2025. It is now read-only.

Commit 880937f

Browse files
committed
fix(options): CLI option --ignore
1 parent 2471dae commit 880937f

File tree

21 files changed

+343
-306
lines changed

21 files changed

+343
-306
lines changed

Diff for: examples/spack-basic/spack.config.js

+9-10
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
1-
const { config } = require('@swc/core/spack')
2-
1+
const { config } = require("@swc/core/spack");
32

43
module.exports = config({
5-
entry: {
6-
'web': __dirname + '/src/index.ts',
7-
},
8-
output: {
9-
path: __dirname + '/lib'
10-
},
11-
module: {},
12-
});
4+
entry: {
5+
web: __dirname + "/src/index.ts",
6+
},
7+
output: {
8+
path: __dirname + "/lib",
9+
},
10+
module: {},
11+
});

Diff for: examples/spack-basic/src/common.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
export const A = 'foo';
2-
export const B = 'bar';
1+
export const A = "foo";
2+
export const B = "bar";

Diff for: examples/spack-basic/src/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
import { B } from "./common";
22

3-
console.log(B)
3+
console.log(B);

Diff for: examples/spack-multiple-entry/spack.config.js

+10-11
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
1-
const { config } = require('@swc/core/spack')
2-
1+
const { config } = require("@swc/core/spack");
32

43
module.exports = config({
5-
entry: {
6-
'web': __dirname + '/src/web.ts',
7-
'android': __dirname + '/src/android.ts',
8-
},
9-
output: {
10-
path: __dirname + '/lib'
11-
},
12-
module: {},
13-
});
4+
entry: {
5+
web: __dirname + "/src/web.ts",
6+
android: __dirname + "/src/android.ts",
7+
},
8+
output: {
9+
path: __dirname + "/lib",
10+
},
11+
module: {},
12+
});

Diff for: examples/spack-multiple-entry/src/android.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
import './common';
1+
import "./common";

Diff for: examples/spack-multiple-entry/src/common.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
console.log('initializing')
1+
console.log("initializing");

Diff for: examples/spack-multiple-entry/src/web.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
import './common';
1+
import "./common";

Diff for: examples/spack-node-modules/spack.config.js

+9-10
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
1-
const { config } = require('@swc/core/spack')
2-
1+
const { config } = require("@swc/core/spack");
32

43
module.exports = config({
5-
entry: {
6-
'web': __dirname + '/src/index.ts',
7-
},
8-
output: {
9-
path: __dirname + '/lib'
10-
},
11-
module: {},
12-
});
4+
entry: {
5+
web: __dirname + "/src/index.ts",
6+
},
7+
output: {
8+
path: __dirname + "/lib",
9+
},
10+
module: {},
11+
});

Diff for: examples/spack-node-modules/src/common.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
import { join } from 'path';
1+
import { join } from "path";
22

3-
export const A = join(__dirname, 'src');
3+
export const A = join(__dirname, "src");

Diff for: examples/spack-node-modules/src/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
import { A } from "./common";
22

3-
console.log(A)
3+
console.log(A);

Diff for: src/spack/index.ts

+56-52
Original file line numberDiff line numberDiff line change
@@ -1,70 +1,74 @@
1-
import { bundle } from '@swc/core';
2-
import { mkdir, writeFile } from 'fs';
3-
import { basename, dirname, extname, join, relative } from 'path'
4-
import { promisify } from 'util';
1+
import { bundle } from "@swc/core";
2+
import { mkdir, writeFile } from "fs";
3+
import { basename, dirname, extname, join, relative } from "path";
4+
import { promisify } from "util";
55

6-
import parseSpackArgs from './options';
6+
import parseSpackArgs from "./options";
77

88
const write = promisify(writeFile);
99
const makeDir = promisify(mkdir);
1010

11-
1211
(async () => {
13-
const { spackOptions } = await parseSpackArgs(process.argv);
12+
const { spackOptions } = await parseSpackArgs(process.argv);
1413

15-
function isUserDefinedEntry(name: string) {
16-
if (typeof spackOptions.entry === 'string') {
17-
return spackOptions.entry === name
18-
}
19-
if (Array.isArray(spackOptions.entry)) {
20-
for (const e of spackOptions.entry) {
21-
if (e === name) {
22-
return true;
23-
}
24-
}
25-
return false;
14+
function isUserDefinedEntry(name: string) {
15+
if (typeof spackOptions.entry === "string") {
16+
return spackOptions.entry === name;
17+
}
18+
if (Array.isArray(spackOptions.entry)) {
19+
for (const e of spackOptions.entry) {
20+
if (e === name) {
21+
return true;
2622
}
27-
28-
return name in spackOptions.entry;
23+
}
24+
return false;
2925
}
3026

27+
return name in spackOptions.entry;
28+
}
3129

32-
async function build() {
33-
const bundleStart = process.hrtime();
34-
const output = await bundle(spackOptions);
35-
const bundleEnd = process.hrtime(bundleStart);
36-
console.info(`Bundling done: ${bundleEnd[0]}s ${bundleEnd[1] / 1000000}ms`);
37-
38-
const emitStart = process.hrtime();
39-
if (spackOptions.output?.path) {
40-
await Object.keys(output).map(async (name) => {
41-
let fullPath = '';
42-
if (isUserDefinedEntry(name)) {
43-
fullPath = join(spackOptions.output.path, spackOptions.output.name.replace('[name]', name));
44-
} else {
45-
const ext = extname(name);
46-
const base = basename(name, ext);
47-
const filename = relative(process.cwd(), name);
48-
fullPath = join(spackOptions.output.path, dirname(filename), `${base}.js`)
49-
}
30+
async function build() {
31+
const bundleStart = process.hrtime();
32+
const output = await bundle(spackOptions);
33+
const bundleEnd = process.hrtime(bundleStart);
34+
console.info(`Bundling done: ${bundleEnd[0]}s ${bundleEnd[1] / 1000000}ms`);
5035

51-
await makeDir(dirname(fullPath), { recursive: true });
52-
await write(fullPath, output[name].code, 'utf-8');
53-
if (output[name].map) {
54-
await write(`${fullPath}.map`, output[name].map, 'utf-8')
55-
}
56-
});
36+
const emitStart = process.hrtime();
37+
if (spackOptions.output?.path) {
38+
await Object.keys(output).map(async name => {
39+
let fullPath = "";
40+
if (isUserDefinedEntry(name)) {
41+
fullPath = join(
42+
spackOptions.output.path,
43+
spackOptions.output.name.replace("[name]", name)
44+
);
5745
} else {
58-
throw new Error('Cannot print to stdout: not implemented yet')
46+
const ext = extname(name);
47+
const base = basename(name, ext);
48+
const filename = relative(process.cwd(), name);
49+
fullPath = join(
50+
spackOptions.output.path,
51+
dirname(filename),
52+
`${base}.js`
53+
);
5954
}
60-
const emitEnd = process.hrtime(emitStart);
61-
console.info(`Done: ${emitEnd[0]}s ${emitEnd[1] / 1000000}ms`);
6255

56+
await makeDir(dirname(fullPath), { recursive: true });
57+
await write(fullPath, output[name].code, "utf-8");
58+
if (output[name].map) {
59+
await write(`${fullPath}.map`, output[name].map, "utf-8");
60+
}
61+
});
62+
} else {
63+
throw new Error("Cannot print to stdout: not implemented yet");
6364
}
65+
const emitEnd = process.hrtime(emitStart);
66+
console.info(`Done: ${emitEnd[0]}s ${emitEnd[1] / 1000000}ms`);
67+
}
6468

65-
// if (cliOptions.watch) {
66-
// throw new Error('watch is not implemented yet')
67-
// }
69+
// if (cliOptions.watch) {
70+
// throw new Error('watch is not implemented yet')
71+
// }
6872

69-
await build();
70-
})()
73+
await build();
74+
})();

Diff for: src/spack/options.ts

+60-56
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import { version as swcCoreVersion } from "@swc/core";
22
import { BundleOptions, compileBundleOptions } from "@swc/core/spack";
33
import commander from "commander";
4-
import * as path from 'path';
4+
import * as path from "path";
55

66
const pkg = require("../../package.json");
77

88
export interface SpackCliOptions {
9-
debug: boolean
9+
debug: boolean;
1010
}
1111

1212
commander.option("--config [path]", "Path to a spack.config.js file to use.");
@@ -16,17 +16,17 @@ commander.option("--mode <development | production | none>", "Mode to use");
1616
commander.option("--target [browser | node]", "Target runtime environment");
1717

1818
commander.option(
19-
'--context [path]', `The base directory (absolute path!) for resolving the 'entry'`
20-
+ ` option. If 'output.pathinfo' is set, the included pathinfo is shortened to this directory`,
21-
'The current directory'
19+
"--context [path]",
20+
`The base directory (absolute path!) for resolving the 'entry'` +
21+
` option. If 'output.pathinfo' is set, the included pathinfo is shortened to this directory`,
22+
"The current directory"
2223
);
2324

24-
commander.option('--entry [list]', "List of entries", collect);
25+
commander.option("--entry [list]", "List of entries", collect);
2526

2627
// commander.option('-W --watch', `Enter watch mode, which rebuilds on file change.`)
2728

28-
29-
commander.option('--debug', `Switch loaders to debug mode`)
29+
commander.option("--debug", `Switch loaders to debug mode`);
3030
// commander.option('--devtool', `Select a developer tool to enhance debugging.`)
3131

3232
// -d shortcut for --debug --devtool eval-cheap-module-source-map
@@ -40,8 +40,11 @@ commander.option('--debug', `Switch loaders to debug mode`)
4040
// --module-bind-pre Bind an extension to a pre loader [문자열]
4141

4242
// Output options:
43-
commander.option('-o --output', `The output path and file for compilation assets`);
44-
commander.option('--output-path', `The output directory as **absolute path**`);
43+
commander.option(
44+
"-o --output",
45+
`The output path and file for compilation assets`
46+
);
47+
commander.option("--output-path", `The output directory as **absolute path**`);
4548
// --output-filename Specifies the name of each output file on disk.
4649
// You must **not** specify an absolute path here!
4750
// The `output.path` option determines the location
@@ -156,60 +159,61 @@ commander.option('--output-path', `The output directory as **absolute path**`);
156159
// --json, -j Prints the result as JSON. [boolean]
157160

158161
commander.version(
159-
`@swc/cli: ${pkg.version}
162+
`@swc/cli: ${pkg.version}
160163
@swc/core: ${swcCoreVersion}`
161164
);
162165

163166
export default async function parseSpackArgs(args: string[]): Promise<{
164-
cliOptions: SpackCliOptions,
165-
spackOptions: BundleOptions,
167+
cliOptions: SpackCliOptions;
168+
spackOptions: BundleOptions;
166169
}> {
167-
//
168-
const cmd = commander.parse(args);
169-
const opts = cmd.opts();
170-
171-
const cliOptions: SpackCliOptions = {
172-
// watch: !!opts.watch,
173-
debug: !!opts.debug,
174-
};
175-
176-
const configOpts: BundleOptions = await compileBundleOptions(opts.config ?? path.resolve('spack.config.js')) as any;
177-
if (opts.entry) {
178-
configOpts.entry = opts.entry;
179-
}
180-
if (opts.mode) {
181-
configOpts.mode = opts.mode;
182-
}
183-
if (opts.target) {
184-
configOpts.target = opts.target;
185-
}
186-
if (!configOpts.output) {
187-
configOpts.output = {} as any;
188-
}
189-
if (!configOpts.output.path) {
190-
configOpts.output.path = opts.outputPath ?? '[name].js';
191-
}
192-
if (!configOpts.output.name) {
193-
configOpts.output.name = opts.output ?? '[name].js';
194-
}
195-
// if (!configOpts.output.name) {
196-
// configOpts.output.path = opts.outputPath;
197-
// }
198-
199-
return {
200-
cliOptions,
201-
spackOptions: {
202-
...configOpts,
203-
},
204-
}
170+
//
171+
const cmd = commander.parse(args);
172+
const opts = cmd.opts();
173+
174+
const cliOptions: SpackCliOptions = {
175+
// watch: !!opts.watch,
176+
debug: !!opts.debug,
177+
};
178+
179+
const configOpts: BundleOptions = (await compileBundleOptions(
180+
opts.config ?? path.resolve("spack.config.js")
181+
)) as any;
182+
if (opts.entry) {
183+
configOpts.entry = opts.entry;
184+
}
185+
if (opts.mode) {
186+
configOpts.mode = opts.mode;
187+
}
188+
if (opts.target) {
189+
configOpts.target = opts.target;
190+
}
191+
if (!configOpts.output) {
192+
configOpts.output = {} as any;
193+
}
194+
if (!configOpts.output.path) {
195+
configOpts.output.path = opts.outputPath ?? "[name].js";
196+
}
197+
if (!configOpts.output.name) {
198+
configOpts.output.name = opts.output ?? "[name].js";
199+
}
200+
// if (!configOpts.output.name) {
201+
// configOpts.output.path = opts.outputPath;
202+
// }
203+
204+
return {
205+
cliOptions,
206+
spackOptions: {
207+
...configOpts,
208+
},
209+
};
205210
}
206211

207-
208212
function collect(value: any, previousValue: any): Array<string> {
209-
// If the user passed the option with no value, like "babel file.js --presets", do nothing.
210-
if (typeof value !== "string") return previousValue;
213+
// If the user passed the option with no value, like "babel file.js --presets", do nothing.
214+
if (typeof value !== "string") return previousValue;
211215

212-
const values = value.split(",");
216+
const values = value.split(",");
213217

214-
return previousValue ? previousValue.concat(values) : values;
218+
return previousValue ? previousValue.concat(values) : values;
215219
}

0 commit comments

Comments
 (0)