Skip to content

Commit

Permalink
Merge branch 'master' into feature/codeql
Browse files Browse the repository at this point in the history
  • Loading branch information
aleks-ivanov committed Dec 29, 2023
2 parents fef4bcc + 862226f commit 2042450
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 8 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -424,7 +424,7 @@ Browser loading is also supported. It exposes a global variable `Terser` contain
<script src="https://cdn.jsdelivr.net/npm/terser/dist/bundle.min.js"></script>
```

There is a single async high level function, **`async minify(code, options)`**,
There is an async high level function, **`async minify(code, options)`**,
which will perform all minification [phases](#minify-options) in a configurable
manner. By default `minify()` will enable [`compress`](#compress-options)
and [`mangle`](#mangle-options). Example:
Expand All @@ -435,6 +435,8 @@ console.log(result.code); // minified output: function add(n,d){return n+d}
console.log(result.map); // source map
```

There is also a `minify_sync()` alternative version of it, which returns instantly.

You can `minify` more than one JavaScript file at a time by using an object
for the first argument where the keys are file names and the values are source
code:
Expand Down
34 changes: 32 additions & 2 deletions lib/minify.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ function log_input(files, options, fs, debug_folder) {
fs.writeFileSync(log_path, "Options: \n" + options_str + "\n\nInput files:\n\n" + files_str(files) + "\n");
}

async function minify(files, options, _fs_module) {
function* minify_sync_or_async(files, options, _fs_module) {
if (
_fs_module
&& typeof process === "object"
Expand Down Expand Up @@ -314,7 +314,7 @@ async function minify(files, options, _fs_module) {
if (options.sourceMap.includeSources && files instanceof AST_Toplevel) {
throw new Error("original source content unavailable");
}
format_options.source_map = await SourceMap({
format_options.source_map = yield* SourceMap({
file: options.sourceMap.filename,
orig: options.sourceMap.content,
root: options.sourceMap.root,
Expand Down Expand Up @@ -376,7 +376,37 @@ async function minify(files, options, _fs_module) {
return result;
}

async function minify(files, options, _fs_module) {
const gen = minify_sync_or_async(files, options, _fs_module);

let yielded;
let val;
do {
val = gen.next(await yielded);
yielded = val.value;
} while (!val.done);

return val.value
}

function minify_sync(files, options, _fs_module) {
const gen = minify_sync_or_async(files, options, _fs_module);

let yielded;
let val;
do {
if (yielded && typeof yielded.then === "function") {
throw new Error("minify_sync cannot be used with the legacy source-map module");
}
val = gen.next(yielded);
yielded = val.value;
} while (!val.done);

return val.value
}

export {
minify,
minify_sync,
to_ascii,
};
4 changes: 2 additions & 2 deletions lib/sourcemap.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ import {SourceMapConsumer, SourceMapGenerator} from "@jridgewell/source-map";
import {defaults, HOP} from "./utils/index.js";

// a small wrapper around source-map and @jridgewell/source-map
async function SourceMap(options) {
function* SourceMap(options) {
options = defaults(options, {
file : null,
root : null,
Expand All @@ -70,7 +70,7 @@ async function SourceMap(options) {
// We support both @jridgewell/source-map (which has a sync
// SourceMapConsumer) and source-map (which has an async
// SourceMapConsumer).
orig_map = await new SourceMapConsumer(options.orig);
orig_map = yield new SourceMapConsumer(options.orig);
if (orig_map.sourcesContent) {
orig_map.sources.forEach(function(source, i) {
var content = orig_map.sourcesContent[i];
Expand Down
2 changes: 1 addition & 1 deletion main.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import "./lib/transform.js";
import "./lib/mozilla-ast.js";
import { minify } from "./lib/minify.js";

export { minify } from "./lib/minify.js";
export { minify, minify_sync } from "./lib/minify.js";
export { run_cli as _run_cli } from "./lib/cli.js";

export async function _default_options() {
Expand Down
18 changes: 17 additions & 1 deletion test/mocha/minify.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import assert from "assert";
import { readFileSync } from "fs";
import { run_code } from "../sandbox.js";
import { for_each_async } from "./utils.js";
import { minify } from "../../main.js";
import { minify, minify_sync } from "../../main.js";

function read(path) {
return readFileSync(path, "utf8");
Expand All @@ -15,6 +15,12 @@ describe("minify", function() {
assert.strictEqual(result.code, "function foo(n){return n?3:7}");
});

it("Should have a sync version", function() {
var js = "console.log(1 + 1);";
var result = minify_sync(js);
assert.strictEqual(result.code, "console.log(2);");
});

it("Should skip inherited keys from `files`", async function() {
var files = Object.create({ skip: this });
files[0] = "alert(1 + 1)";
Expand Down Expand Up @@ -313,6 +319,16 @@ describe("minify", function() {
})).code + "\n";
assert.strictEqual(code, readFileSync("test/input/issue-520/output.js", "utf8"));
});
it("Should process inline source map (minify_sync)", function() {
var code = minify_sync(read("./test/input/issue-520/input.js"), {
compress: { toplevel: true },
sourceMap: {
content: "inline",
url: "inline"
}
}).code + "\n";
assert.strictEqual(code, readFileSync("test/input/issue-520/output.js", "utf8"));
});
it("Should fail with multiple input and inline source map", async function() {
await assert.rejects(
() =>
Expand Down
18 changes: 17 additions & 1 deletion test/mocha/sourcemaps.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { readFileSync } from "fs";
import source_map_module from "source-map";
import { assertCodeWithInlineMapEquals } from "./utils.js";
import { to_ascii } from "../../lib/minify.js";
import { minify } from "../../main.js";
import { minify, minify_sync } from "../../main.js";

const { SourceMapConsumer } = source_map_module;

Expand Down Expand Up @@ -154,6 +154,22 @@ describe("sourcemaps", function() {
mappings: "AAAAA,QAAQC,IAAI",
});
});
it("Should return source map as object when asObject is given (minify_sync)", function() {
var code = "console.log(42);";
var result = minify_sync(code, {
sourceMap: {
asObject: true,
},
});
if (result.error) throw result.error;
assert.strictEqual(result.code, code);
assert.deepStrictEqual(result.map, {
version: 3,
sources: ["0"],
names: ["console","log"],
mappings: "AAAAA,QAAQC,IAAI",
});
});

it("Should grab names from methods and properties correctly", async () => {
const code = `class Foo {
Expand Down
1 change: 1 addition & 0 deletions tools/terser.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -213,3 +213,4 @@ export interface SourceMapOptions {
}

export function minify(files: string | string[] | { [file: string]: string }, options?: MinifyOptions): Promise<MinifyOutput>;
export function minify_sync(files: string | string[] | { [file: string]: string }, options?: MinifyOptions): MinifyOutput;

0 comments on commit 2042450

Please sign in to comment.