2022-03-16 17:25:26 +03:00
|
|
|
import swc from "../..";
|
2021-07-31 09:30:06 +03:00
|
|
|
|
|
|
|
it("should compress", async () => {
|
|
|
|
const { code } = await swc.minify(`
|
|
|
|
import foo from '@src/app';
|
|
|
|
console.log(foo)
|
|
|
|
`);
|
|
|
|
|
2022-08-02 07:24:24 +03:00
|
|
|
expect(code).toMatchInlineSnapshot(
|
2022-08-30 07:01:04 +03:00
|
|
|
`"import o from\\"@src/app\\";console.log(o);"`
|
2022-08-02 07:24:24 +03:00
|
|
|
);
|
2022-03-16 17:25:26 +03:00
|
|
|
});
|
2021-07-31 09:30:06 +03:00
|
|
|
|
|
|
|
it("should accept object", async () => {
|
2022-03-16 17:25:26 +03:00
|
|
|
const { code } = await swc.minify(
|
|
|
|
`
|
2021-07-31 09:30:06 +03:00
|
|
|
import foo from '@src/app';
|
|
|
|
console.log(foo)
|
2022-03-16 17:25:26 +03:00
|
|
|
`,
|
|
|
|
{}
|
|
|
|
);
|
2021-07-31 09:30:06 +03:00
|
|
|
|
2022-08-02 07:24:24 +03:00
|
|
|
expect(code).toMatchInlineSnapshot(
|
2022-08-30 07:01:04 +03:00
|
|
|
`"import o from\\"@src/app\\";console.log(o);"`
|
2022-08-02 07:24:24 +03:00
|
|
|
);
|
2022-03-16 17:25:26 +03:00
|
|
|
});
|
2021-07-31 09:30:06 +03:00
|
|
|
|
2022-04-11 21:45:58 +03:00
|
|
|
it("should accept { mangle = true }", async () => {
|
2022-03-16 17:25:26 +03:00
|
|
|
const { code } = await swc.minify(
|
|
|
|
`
|
2021-07-31 09:30:06 +03:00
|
|
|
import foo from '@src/app';
|
|
|
|
console.log(foo)
|
2022-03-16 17:25:26 +03:00
|
|
|
`,
|
|
|
|
{
|
|
|
|
compress: false,
|
|
|
|
mangle: true,
|
|
|
|
}
|
|
|
|
);
|
2021-07-31 09:30:06 +03:00
|
|
|
|
2022-03-16 17:25:26 +03:00
|
|
|
expect(code).toMatchInlineSnapshot(
|
2022-08-30 07:01:04 +03:00
|
|
|
`"import o from\\"@src/app\\";console.log(o);"`
|
2022-03-16 17:25:26 +03:00
|
|
|
);
|
|
|
|
});
|
2021-07-31 09:30:06 +03:00
|
|
|
|
2022-04-11 21:45:58 +03:00
|
|
|
it("should accept { mangle = object }", async () => {
|
2022-03-16 17:25:26 +03:00
|
|
|
const { code } = await swc.minify(
|
|
|
|
`
|
2021-07-31 09:30:06 +03:00
|
|
|
import foo from '@src/app';
|
|
|
|
console.log(foo)
|
2022-03-16 17:25:26 +03:00
|
|
|
`,
|
|
|
|
{
|
|
|
|
compress: false,
|
|
|
|
mangle: {
|
|
|
|
topLevel: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
);
|
2021-07-31 09:30:06 +03:00
|
|
|
|
2022-03-16 17:25:26 +03:00
|
|
|
expect(code).toMatchInlineSnapshot(
|
2022-08-30 07:01:04 +03:00
|
|
|
`"import o from\\"@src/app\\";console.log(o);"`
|
2022-03-16 17:25:26 +03:00
|
|
|
);
|
|
|
|
});
|
2021-07-31 09:30:06 +03:00
|
|
|
|
|
|
|
it("should mangle locals", async () => {
|
2022-03-16 17:25:26 +03:00
|
|
|
const { code } = await swc.minify(
|
|
|
|
`
|
2021-07-31 09:30:06 +03:00
|
|
|
(function(){
|
|
|
|
const longName = Math.random() + '_' + Math.random();
|
|
|
|
console.log(longName);
|
|
|
|
console.log(longName);
|
|
|
|
console.log(longName);
|
|
|
|
console.log(longName);
|
|
|
|
console.log(longName);
|
|
|
|
console.log(longName);
|
|
|
|
})()
|
2022-03-16 17:25:26 +03:00
|
|
|
`,
|
|
|
|
{
|
|
|
|
compress: false,
|
|
|
|
mangle: {
|
|
|
|
topLevel: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
);
|
2021-07-31 09:30:06 +03:00
|
|
|
|
2022-03-16 17:25:26 +03:00
|
|
|
expect(code).toMatchInlineSnapshot(
|
2022-08-30 07:01:04 +03:00
|
|
|
`"(function(){const o=Math.random()+\\"_\\"+Math.random();console.log(o);console.log(o);console.log(o);console.log(o);console.log(o);console.log(o)})();"`
|
2022-03-16 17:25:26 +03:00
|
|
|
);
|
|
|
|
});
|
2021-08-03 18:52:47 +03:00
|
|
|
|
2022-04-11 21:45:58 +03:00
|
|
|
describe("source map", () => {
|
2021-08-26 13:44:38 +03:00
|
|
|
it("should have `names`", async () => {
|
2022-03-16 17:25:26 +03:00
|
|
|
const { map } = await swc.minify(
|
|
|
|
`
|
2021-08-26 13:44:38 +03:00
|
|
|
(function(){
|
|
|
|
const longName = Math.random() + '_' + Math.random();
|
|
|
|
console.log(longName);
|
|
|
|
console.log(longName);
|
|
|
|
})()
|
2022-03-16 17:25:26 +03:00
|
|
|
`,
|
|
|
|
{
|
|
|
|
sourceMap: true,
|
|
|
|
compress: false,
|
|
|
|
mangle: {
|
|
|
|
topLevel: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
);
|
2021-08-26 13:44:38 +03:00
|
|
|
|
|
|
|
expect(JSON.parse(map)).toHaveProperty("names");
|
2022-03-16 17:25:26 +03:00
|
|
|
expect(JSON.parse(map).names).not.toEqual([]);
|
2021-08-26 13:44:38 +03:00
|
|
|
});
|
|
|
|
|
2022-04-11 21:45:58 +03:00
|
|
|
it("should have `sources` if file name is specified", async () => {
|
2022-03-16 17:25:26 +03:00
|
|
|
const { map } = await swc.minify(
|
|
|
|
{
|
|
|
|
"foo.js": `(function(){
|
2021-08-26 13:44:38 +03:00
|
|
|
const longName = Math.random() + '_' + Math.random();
|
|
|
|
console.log(longName);
|
2022-03-16 17:25:26 +03:00
|
|
|
})()`,
|
2021-08-26 13:44:38 +03:00
|
|
|
},
|
2022-03-16 17:25:26 +03:00
|
|
|
{
|
|
|
|
sourceMap: true,
|
|
|
|
compress: false,
|
|
|
|
mangle: {
|
|
|
|
topLevel: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
);
|
2021-08-26 13:44:38 +03:00
|
|
|
|
|
|
|
const j = JSON.parse(map);
|
|
|
|
expect(j).toHaveProperty("sources");
|
|
|
|
expect(j.sources).not.toEqual([]);
|
|
|
|
});
|
2022-03-16 17:25:26 +03:00
|
|
|
});
|
2021-08-03 18:52:47 +03:00
|
|
|
|
2022-03-16 17:25:26 +03:00
|
|
|
describe("transform apis", () => {
|
2021-08-03 18:52:47 +03:00
|
|
|
it("handle jsc.minify", async () => {
|
2022-03-16 17:25:26 +03:00
|
|
|
const { code } = await swc.transform(
|
|
|
|
`
|
2021-08-03 18:52:47 +03:00
|
|
|
(function(){
|
|
|
|
const longName = Math.random() + '_' + Math.random();
|
|
|
|
console.log(longName);
|
|
|
|
console.log(longName);
|
|
|
|
console.log(longName);
|
|
|
|
console.log(longName);
|
|
|
|
console.log(longName);
|
|
|
|
console.log(longName);
|
|
|
|
})()
|
2022-03-16 17:25:26 +03:00
|
|
|
`,
|
|
|
|
{
|
|
|
|
jsc: {
|
|
|
|
minify: {
|
|
|
|
compress: false,
|
|
|
|
mangle: {
|
|
|
|
topLevel: true,
|
|
|
|
},
|
2021-08-03 18:52:47 +03:00
|
|
|
},
|
2022-03-16 17:25:26 +03:00
|
|
|
},
|
|
|
|
minify: true,
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
expect(code).toMatchInlineSnapshot(
|
2022-08-30 07:01:04 +03:00
|
|
|
`"(function(){var o=Math.random()+\\"_\\"+Math.random();console.log(o);console.log(o);console.log(o);console.log(o);console.log(o);console.log(o)})();"`
|
2022-03-16 17:25:26 +03:00
|
|
|
);
|
|
|
|
});
|
|
|
|
});
|
2022-08-02 07:24:24 +03:00
|
|
|
|
|
|
|
describe("should remove comments", () => {
|
|
|
|
it("should remove", async () => {
|
|
|
|
const { code } = await swc.minify(
|
|
|
|
`
|
|
|
|
(function(){
|
|
|
|
/**
|
|
|
|
* 1
|
|
|
|
*/
|
|
|
|
const longName = Math.random() + '_' + Math.random();
|
|
|
|
console.log(longName);
|
|
|
|
})()
|
|
|
|
`,
|
|
|
|
{
|
|
|
|
compress: false,
|
|
|
|
mangle: {
|
|
|
|
topLevel: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
expect(code).toMatchInlineSnapshot(
|
2022-09-08 10:17:11 +03:00
|
|
|
`"(function(){const o=Math.random()+\\"_\\"+Math.random();console.log(o)})();"`
|
2022-08-02 07:24:24 +03:00
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2022-11-20 13:03:26 +03:00
|
|
|
it("should preserve license", async () => {
|
2022-08-02 07:24:24 +03:00
|
|
|
const { code } = await swc.minify(
|
|
|
|
`
|
|
|
|
(function(){
|
|
|
|
/**
|
|
|
|
* @license
|
|
|
|
*/
|
|
|
|
const longName = Math.random() + '_' + Math.random();
|
|
|
|
console.log(longName);
|
|
|
|
})()
|
|
|
|
`,
|
|
|
|
{
|
|
|
|
compress: false,
|
|
|
|
mangle: {
|
|
|
|
topLevel: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
expect(code).toMatchInlineSnapshot(`
|
|
|
|
"(function(){/**
|
|
|
|
* @license
|
2022-11-20 13:03:26 +03:00
|
|
|
*/const o=Math.random()+\\"_\\"+Math.random();console.log(o)})();"
|
2022-08-02 07:24:24 +03:00
|
|
|
`);
|
|
|
|
});
|
2022-11-20 13:03:26 +03:00
|
|
|
it("should remove comment near to license", async () => {
|
2022-08-02 07:24:24 +03:00
|
|
|
const { code } = await swc.minify(
|
|
|
|
`
|
|
|
|
(function(){
|
|
|
|
/**
|
|
|
|
* @license
|
|
|
|
*/
|
|
|
|
/*
|
|
|
|
* 1
|
|
|
|
*/
|
|
|
|
const longName = Math.random() + '_' + Math.random();
|
|
|
|
console.log(longName);
|
|
|
|
})()
|
|
|
|
`,
|
|
|
|
{
|
|
|
|
compress: false,
|
|
|
|
mangle: {
|
|
|
|
topLevel: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
expect(code).toMatchInlineSnapshot(`
|
|
|
|
"(function(){/**
|
|
|
|
* @license
|
2022-11-20 13:03:26 +03:00
|
|
|
*/const o=Math.random()+\\"_\\"+Math.random();console.log(o)})();"
|
2022-08-02 07:24:24 +03:00
|
|
|
`);
|
|
|
|
});
|
|
|
|
});
|