Skip to content
This repository has been archived by the owner on Nov 30, 2023. It is now read-only.

Commit

Permalink
Fixing error "Unexpected token =>" (#28)
Browse files Browse the repository at this point in the history
  • Loading branch information
zech authored and TrySound committed Feb 5, 2019
1 parent c225dbf commit fff1c73
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 27 deletions.
8 changes: 2 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
const { codeFrameColumns } = require("@babel/code-frame");
const Worker = require("jest-worker").default;
const { generate } = require("escodegen");
const lave = require("lave");
const serialize = require("serialize-javascript");

function terser(userOptions = {}) {
if (userOptions.sourceMap != null) {
Expand Down Expand Up @@ -33,10 +32,7 @@ function terser(userOptions = {}) {
}
}

const serializedOptions = lave(normalizedOptions, {
generate,
format: "expression"
});
const serializedOptions = serialize(normalizedOptions);

const result = this.worker
.transform(code, serializedOptions)
Expand Down
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,8 @@
"license": "MIT",
"dependencies": {
"@babel/code-frame": "^7.0.0",
"escodegen": "^1.11.0",
"jest-worker": "^24.0.0",
"lave": "^1.1.10",
"serialize-javascript": "^1.6.1",
"terser": "^3.14.1"
},
"peerDependencies": {
Expand Down
56 changes: 53 additions & 3 deletions test/test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
const assert = require("assert");
const { rollup } = require("rollup");
const readFile = require("fs").readFileSync;
const { terser } = require("../");

test("minify", async () => {
Expand Down Expand Up @@ -162,7 +160,33 @@ test("allow to pass not string values to worker", async () => {
);
});

test("allow to method shorthand definitions to worker", async () => {
test("allow classic function definitions passing to worker", async () => {
const bundle = await rollup({
input: "test/fixtures/unminified.js",
plugins: [
terser({
mangle: { properties: { regex: /^_/ } },
output: {
comments: function (node, comment) {
if (comment.type === "comment2") {
// multiline comment
return /@preserve|@license|@cc_on|^!/i.test(comment.value);
}
return false;
}
}
})
]
});
const result = await bundle.generate({ format: "cjs" });
expect(result.output).toHaveLength(1);
const [output] = result.output;
expect(output.code).toEqual(
'"use strict";window.a=5,window.a<3&&console.log(4);\n'
);
});

test("allow method shorthand definitions passing to worker", async () => {
const bundle = await rollup({
input: "test/fixtures/unminified.js",
plugins: [
Expand All @@ -187,3 +211,29 @@ test("allow to method shorthand definitions to worker", async () => {
'"use strict";window.a=5,window.a<3&&console.log(4);\n'
);
});

test("allow arrow function definitions passing to worker", async () => {
const bundle = await rollup({
input: "test/fixtures/unminified.js",
plugins: [
terser({
mangle: { properties: { regex: /^_/ } },
output: {
comments: (node, comment) => {
if (comment.type === "comment2") {
// multiline comment
return /@preserve|@license|@cc_on|^!/i.test(comment.value);
}
return false;
}
}
})
]
});
const result = await bundle.generate({ format: "cjs" });
expect(result.output).toHaveLength(1);
const [output] = result.output;
expect(output.code).toEqual(
'"use strict";window.a=5,window.a<3&&console.log(4);\n'
);
});
2 changes: 1 addition & 1 deletion transform.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const { minify } = require("terser");

const transform = (code, optionsString) => {
const options = eval(optionsString);
const options = eval(`(${optionsString})`);
const result = minify(code, options);
if (result.error) {
throw result.error;
Expand Down
20 changes: 5 additions & 15 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -765,17 +765,6 @@ escape-string-regexp@^1.0.5:
version "1.0.5"
resolved "https://registry.yarnpkg.com/escape-string-regexp/-/escape-string-regexp-1.0.5.tgz#1b61c0562190a8dff6ae3bb2cf0200ca130b86d4"

escodegen@^1.11.0:
version "1.11.0"
resolved "https://registry.yarnpkg.com/escodegen/-/escodegen-1.11.0.tgz#b27a9389481d5bfd5bec76f7bb1eb3f8f4556589"
dependencies:
esprima "^3.1.3"
estraverse "^4.2.0"
esutils "^2.0.2"
optionator "^0.8.1"
optionalDependencies:
source-map "~0.6.1"

escodegen@^1.9.0:
version "1.9.0"
resolved "https://registry.yarnpkg.com/escodegen/-/escodegen-1.9.0.tgz#9811a2f265dc1cd3894420ee3717064b632b8852"
Expand Down Expand Up @@ -1878,10 +1867,6 @@ kleur@^3.0.0:
version "3.0.1"
resolved "https://registry.yarnpkg.com/kleur/-/kleur-3.0.1.tgz#4f5b313f5fa315432a400f19a24db78d451ede62"

lave@^1.1.10:
version "1.1.10"
resolved "https://registry.yarnpkg.com/lave/-/lave-1.1.10.tgz#062207652c5502d7c6ff096c9de3995401f634d5"

lazy-cache@^1.0.3:
version "1.0.4"
resolved "https://registry.yarnpkg.com/lazy-cache/-/lazy-cache-1.0.4.tgz#a1d78fc3a50474cb80845d3b3b6e1da49a446e8e"
Expand Down Expand Up @@ -2688,6 +2673,11 @@ semver@^5.5.0:
version "5.5.1"
resolved "https://registry.yarnpkg.com/semver/-/semver-5.5.1.tgz#7dfdd8814bdb7cabc7be0fb1d734cfb66c940477"

serialize-javascript@^1.6.1:
version "1.6.1"
resolved "https://registry.yarnpkg.com/serialize-javascript/-/serialize-javascript-1.6.1.tgz#4d1f697ec49429a847ca6f442a2a755126c4d879"
integrity sha512-A5MOagrPFga4YaKQSWHryl7AXvbQkEqpw4NNYMTNYUNV51bA8ABHgYFpqKx+YFFrw59xMV1qGH1R4AgoNIVgCw==

set-blocking@^2.0.0, set-blocking@~2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/set-blocking/-/set-blocking-2.0.0.tgz#045f9782d011ae9a6803ddd382b24392b3d890f7"
Expand Down

0 comments on commit fff1c73

Please sign in to comment.