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

Commit

Permalink
fix(options): CLI option --ignore
Browse files Browse the repository at this point in the history
  • Loading branch information
3axap4eHko committed Mar 20, 2023
1 parent 2471dae commit 880937f
Show file tree
Hide file tree
Showing 21 changed files with 343 additions and 306 deletions.
19 changes: 9 additions & 10 deletions examples/spack-basic/spack.config.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
const { config } = require('@swc/core/spack')

const { config } = require("@swc/core/spack");

module.exports = config({
entry: {
'web': __dirname + '/src/index.ts',
},
output: {
path: __dirname + '/lib'
},
module: {},
});
entry: {
web: __dirname + "/src/index.ts",
},
output: {
path: __dirname + "/lib",
},
module: {},
});
4 changes: 2 additions & 2 deletions examples/spack-basic/src/common.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export const A = 'foo';
export const B = 'bar';
export const A = "foo";
export const B = "bar";
2 changes: 1 addition & 1 deletion examples/spack-basic/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import { B } from "./common";

console.log(B)
console.log(B);
21 changes: 10 additions & 11 deletions examples/spack-multiple-entry/spack.config.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
const { config } = require('@swc/core/spack')

const { config } = require("@swc/core/spack");

module.exports = config({
entry: {
'web': __dirname + '/src/web.ts',
'android': __dirname + '/src/android.ts',
},
output: {
path: __dirname + '/lib'
},
module: {},
});
entry: {
web: __dirname + "/src/web.ts",
android: __dirname + "/src/android.ts",
},
output: {
path: __dirname + "/lib",
},
module: {},
});
2 changes: 1 addition & 1 deletion examples/spack-multiple-entry/src/android.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
import './common';
import "./common";
2 changes: 1 addition & 1 deletion examples/spack-multiple-entry/src/common.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
console.log('initializing')
console.log("initializing");
2 changes: 1 addition & 1 deletion examples/spack-multiple-entry/src/web.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
import './common';
import "./common";
19 changes: 9 additions & 10 deletions examples/spack-node-modules/spack.config.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
const { config } = require('@swc/core/spack')

const { config } = require("@swc/core/spack");

module.exports = config({
entry: {
'web': __dirname + '/src/index.ts',
},
output: {
path: __dirname + '/lib'
},
module: {},
});
entry: {
web: __dirname + "/src/index.ts",
},
output: {
path: __dirname + "/lib",
},
module: {},
});
4 changes: 2 additions & 2 deletions examples/spack-node-modules/src/common.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import { join } from 'path';
import { join } from "path";

export const A = join(__dirname, 'src');
export const A = join(__dirname, "src");
2 changes: 1 addition & 1 deletion examples/spack-node-modules/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import { A } from "./common";

console.log(A)
console.log(A);
108 changes: 56 additions & 52 deletions src/spack/index.ts
Original file line number Diff line number Diff line change
@@ -1,70 +1,74 @@
import { bundle } from '@swc/core';
import { mkdir, writeFile } from 'fs';
import { basename, dirname, extname, join, relative } from 'path'
import { promisify } from 'util';
import { bundle } from "@swc/core";
import { mkdir, writeFile } from "fs";
import { basename, dirname, extname, join, relative } from "path";
import { promisify } from "util";

import parseSpackArgs from './options';
import parseSpackArgs from "./options";

const write = promisify(writeFile);
const makeDir = promisify(mkdir);


(async () => {
const { spackOptions } = await parseSpackArgs(process.argv);
const { spackOptions } = await parseSpackArgs(process.argv);

function isUserDefinedEntry(name: string) {
if (typeof spackOptions.entry === 'string') {
return spackOptions.entry === name
}
if (Array.isArray(spackOptions.entry)) {
for (const e of spackOptions.entry) {
if (e === name) {
return true;
}
}
return false;
function isUserDefinedEntry(name: string) {
if (typeof spackOptions.entry === "string") {
return spackOptions.entry === name;
}
if (Array.isArray(spackOptions.entry)) {
for (const e of spackOptions.entry) {
if (e === name) {
return true;
}

return name in spackOptions.entry;
}
return false;
}

return name in spackOptions.entry;
}

async function build() {
const bundleStart = process.hrtime();
const output = await bundle(spackOptions);
const bundleEnd = process.hrtime(bundleStart);
console.info(`Bundling done: ${bundleEnd[0]}s ${bundleEnd[1] / 1000000}ms`);

const emitStart = process.hrtime();
if (spackOptions.output?.path) {
await Object.keys(output).map(async (name) => {
let fullPath = '';
if (isUserDefinedEntry(name)) {
fullPath = join(spackOptions.output.path, spackOptions.output.name.replace('[name]', name));
} else {
const ext = extname(name);
const base = basename(name, ext);
const filename = relative(process.cwd(), name);
fullPath = join(spackOptions.output.path, dirname(filename), `${base}.js`)
}
async function build() {
const bundleStart = process.hrtime();
const output = await bundle(spackOptions);
const bundleEnd = process.hrtime(bundleStart);
console.info(`Bundling done: ${bundleEnd[0]}s ${bundleEnd[1] / 1000000}ms`);

await makeDir(dirname(fullPath), { recursive: true });
await write(fullPath, output[name].code, 'utf-8');
if (output[name].map) {
await write(`${fullPath}.map`, output[name].map, 'utf-8')
}
});
const emitStart = process.hrtime();
if (spackOptions.output?.path) {
await Object.keys(output).map(async name => {
let fullPath = "";
if (isUserDefinedEntry(name)) {
fullPath = join(
spackOptions.output.path,
spackOptions.output.name.replace("[name]", name)
);
} else {
throw new Error('Cannot print to stdout: not implemented yet')
const ext = extname(name);
const base = basename(name, ext);
const filename = relative(process.cwd(), name);
fullPath = join(
spackOptions.output.path,
dirname(filename),
`${base}.js`
);
}
const emitEnd = process.hrtime(emitStart);
console.info(`Done: ${emitEnd[0]}s ${emitEnd[1] / 1000000}ms`);

await makeDir(dirname(fullPath), { recursive: true });
await write(fullPath, output[name].code, "utf-8");
if (output[name].map) {
await write(`${fullPath}.map`, output[name].map, "utf-8");
}
});
} else {
throw new Error("Cannot print to stdout: not implemented yet");
}
const emitEnd = process.hrtime(emitStart);
console.info(`Done: ${emitEnd[0]}s ${emitEnd[1] / 1000000}ms`);
}

// if (cliOptions.watch) {
// throw new Error('watch is not implemented yet')
// }
// if (cliOptions.watch) {
// throw new Error('watch is not implemented yet')
// }

await build();
})()
await build();
})();
116 changes: 60 additions & 56 deletions src/spack/options.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { version as swcCoreVersion } from "@swc/core";
import { BundleOptions, compileBundleOptions } from "@swc/core/spack";
import commander from "commander";
import * as path from 'path';
import * as path from "path";

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

export interface SpackCliOptions {
debug: boolean
debug: boolean;
}

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

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

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

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


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

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

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

commander.version(
`@swc/cli: ${pkg.version}
`@swc/cli: ${pkg.version}
@swc/core: ${swcCoreVersion}`
);

export default async function parseSpackArgs(args: string[]): Promise<{
cliOptions: SpackCliOptions,
spackOptions: BundleOptions,
cliOptions: SpackCliOptions;
spackOptions: BundleOptions;
}> {
//
const cmd = commander.parse(args);
const opts = cmd.opts();

const cliOptions: SpackCliOptions = {
// watch: !!opts.watch,
debug: !!opts.debug,
};

const configOpts: BundleOptions = await compileBundleOptions(opts.config ?? path.resolve('spack.config.js')) as any;
if (opts.entry) {
configOpts.entry = opts.entry;
}
if (opts.mode) {
configOpts.mode = opts.mode;
}
if (opts.target) {
configOpts.target = opts.target;
}
if (!configOpts.output) {
configOpts.output = {} as any;
}
if (!configOpts.output.path) {
configOpts.output.path = opts.outputPath ?? '[name].js';
}
if (!configOpts.output.name) {
configOpts.output.name = opts.output ?? '[name].js';
}
// if (!configOpts.output.name) {
// configOpts.output.path = opts.outputPath;
// }

return {
cliOptions,
spackOptions: {
...configOpts,
},
}
//
const cmd = commander.parse(args);
const opts = cmd.opts();

const cliOptions: SpackCliOptions = {
// watch: !!opts.watch,
debug: !!opts.debug,
};

const configOpts: BundleOptions = (await compileBundleOptions(
opts.config ?? path.resolve("spack.config.js")
)) as any;
if (opts.entry) {
configOpts.entry = opts.entry;
}
if (opts.mode) {
configOpts.mode = opts.mode;
}
if (opts.target) {
configOpts.target = opts.target;
}
if (!configOpts.output) {
configOpts.output = {} as any;
}
if (!configOpts.output.path) {
configOpts.output.path = opts.outputPath ?? "[name].js";
}
if (!configOpts.output.name) {
configOpts.output.name = opts.output ?? "[name].js";
}
// if (!configOpts.output.name) {
// configOpts.output.path = opts.outputPath;
// }

return {
cliOptions,
spackOptions: {
...configOpts,
},
};
}


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

const values = value.split(",");
const values = value.split(",");

return previousValue ? previousValue.concat(values) : values;
return previousValue ? previousValue.concat(values) : values;
}
Loading

0 comments on commit 880937f

Please sign in to comment.