Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for custom mangle.json paths #432

Merged
merged 6 commits into from
Jun 26, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -439,7 +439,12 @@ function createConfig(options, entry, format, writeMeta) {
let nameCache = {};
const bareNameCache = nameCache;
// Support "minify" field and legacy "mangle" field via package.json:
let minifyOptions = options.pkg.minify || options.pkg.mangle || {};
const rawMinifyValue = options.pkg.minify || options.pkg.mangle || {};
let minifyOptions = typeof rawMinifyValue === 'string' ? {} : rawMinifyValue;
const getNameCachePath =
typeof rawMinifyValue === 'string'
? () => resolve(options.cwd, rawMinifyValue)
: () => resolve(options.cwd, 'mangle.json');

const useTypescript = extname(entry) === '.ts' || extname(entry) === '.tsx';

Expand All @@ -449,9 +454,7 @@ function createConfig(options, entry, format, writeMeta) {

function loadNameCache() {
try {
nameCache = JSON.parse(
fs.readFileSync(resolve(options.cwd, 'mangle.json'), 'utf8'),
);
nameCache = JSON.parse(fs.readFileSync(getNameCachePath(), 'utf8'));
// mangle.json can contain a "minify" field, same format as the pkg.mangle:
if (nameCache.minify) {
minifyOptions = Object.assign(
Expand Down Expand Up @@ -593,7 +596,7 @@ function createConfig(options, entry, format, writeMeta) {
writeBundle() {
if (writeMeta && nameCache) {
fs.writeFile(
resolve(options.cwd, 'mangle.json'),
getNameCachePath(),
JSON.stringify(nameCache, null, 2),
);
}
Expand Down
195 changes: 194 additions & 1 deletion test/__snapshots__/index.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -423,7 +423,7 @@ Build \\"basicCss\\" to dist:

exports[`fixtures build basic-css with microbundle 2`] = `7`;

exports[`fixtures build basic-css with microbundle 3`] = `".testing{display:flex;color:red;background:#00f}"`;
exports[`fixtures build basic-css with microbundle 3`] = `".testing{display:-webkit-box;display:flex;color:red;background:#00f}"`;

exports[`fixtures build basic-css with microbundle 4`] = `
"module.exports=function(){var e=document.createElement(\\"div\\");return e.className=\\"testing\\",e};
Expand Down Expand Up @@ -1374,6 +1374,199 @@ exports[`fixtures build macro with microbundle 5`] = `
"
`;

exports[`fixtures build mangle-json-file with microbundle 1`] = `
"Used script: microbundle

Directory tree:

mangle-json-file
dist
mangle-json-file.js
mangle-json-file.js.map
mangle-json-file.mjs
mangle-json-file.mjs.map
mangle-json-file.umd.js
mangle-json-file.umd.js.map
mangle.json
package.json
src
index.js
two.js


Build \\"mangleJsonFile\\" to dist:
103 B: mangle-json-file.js.gz
89 B: mangle-json-file.js.br
103 B: mangle-json-file.mjs.gz
95 B: mangle-json-file.mjs.br
198 B: mangle-json-file.umd.js.gz
164 B: mangle-json-file.umd.js.br"
`;

exports[`fixtures build mangle-json-file with microbundle 2`] = `6`;

exports[`fixtures build mangle-json-file with microbundle 3`] = `
"var o={prop1:1,__p2:2};module.exports=function(){return console.log(o.prop1),console.log(o.__p2),o};
//# sourceMappingURL=mangle-json-file.js.map
"
`;

exports[`fixtures build mangle-json-file with microbundle 4`] = `
"var o={prop1:1,__p2:2};export default function(){return console.log(o.prop1),console.log(o.__p2),o}
//# sourceMappingURL=mangle-json-file.mjs.map
"
`;

exports[`fixtures build mangle-json-file with microbundle 5`] = `
"!function(e,n){\\"object\\"==typeof exports&&\\"undefined\\"!=typeof module?module.exports=n():\\"function\\"==typeof define&&define.amd?define(n):(e=e||self).mangleJsonFile=n()}(this,function(){var e={prop1:1,__p2:2};return function(){return console.log(e.prop1),console.log(e.__p2),e}});
//# sourceMappingURL=mangle-json-file.umd.js.map
"
`;

exports[`fixtures build minify-config with microbundle 1`] = `
"Used script: microbundle

Directory tree:

minify-config
dist
minify-config.js
minify-config.js.map
minify-config.mjs
minify-config.mjs.map
minify-config.umd.js
minify-config.umd.js.map
package.json
src
index.js
two.js


Build \\"minifyConfig\\" to dist:
99 B: minify-config.js.gz
85 B: minify-config.js.br
99 B: minify-config.mjs.gz
88 B: minify-config.mjs.br
190 B: minify-config.umd.js.gz
144 B: minify-config.umd.js.br"
`;

exports[`fixtures build minify-config with microbundle 2`] = `6`;

exports[`fixtures build minify-config with microbundle 3`] = `
"var o={prop1:1,o:2};module.exports=function(){return console.log(o.prop1),console.log(o.o),o};
//# sourceMappingURL=minify-config.js.map
"
`;

exports[`fixtures build minify-config with microbundle 4`] = `
"var o={prop1:1,o:2};export default function(){return console.log(o.prop1),console.log(o.o),o}
//# sourceMappingURL=minify-config.mjs.map
"
`;

exports[`fixtures build minify-config with microbundle 5`] = `
"!function(e,n){\\"object\\"==typeof exports&&\\"undefined\\"!=typeof module?module.exports=n():\\"function\\"==typeof define&&define.amd?define(n):(e=e||self).minifyConfig=n()}(this,function(){var e={prop1:1,o:2};return function(){return console.log(e.prop1),console.log(e.o),e}});
//# sourceMappingURL=minify-config.umd.js.map
"
`;

exports[`fixtures build minify-path-config with microbundle 1`] = `
"Used script: microbundle

Directory tree:

minify-path-config
dist
minify-path-config.js
minify-path-config.js.map
minify-path-config.mjs
minify-path-config.mjs.map
minify-path-config.umd.js
minify-path-config.umd.js.map
minify.json
package.json
src
index.js
two.js


Build \\"minifyPathConfig\\" to dist:
103 B: minify-path-config.js.gz
89 B: minify-path-config.js.br
103 B: minify-path-config.mjs.gz
95 B: minify-path-config.mjs.br
200 B: minify-path-config.umd.js.gz
155 B: minify-path-config.umd.js.br"
`;

exports[`fixtures build minify-path-config with microbundle 2`] = `6`;

exports[`fixtures build minify-path-config with microbundle 3`] = `
"var o={prop1:1,__p2:2};module.exports=function(){return console.log(o.prop1),console.log(o.__p2),o};
//# sourceMappingURL=minify-path-config.js.map
"
`;

exports[`fixtures build minify-path-config with microbundle 4`] = `
"var o={prop1:1,__p2:2};export default function(){return console.log(o.prop1),console.log(o.__p2),o}
//# sourceMappingURL=minify-path-config.mjs.map
"
`;

exports[`fixtures build minify-path-config with microbundle 5`] = `
"!function(e,n){\\"object\\"==typeof exports&&\\"undefined\\"!=typeof module?module.exports=n():\\"function\\"==typeof define&&define.amd?define(n):(e=e||self).minifyPathConfig=n()}(this,function(){var e={prop1:1,__p2:2};return function(){return console.log(e.prop1),console.log(e.__p2),e}});
//# sourceMappingURL=minify-path-config.umd.js.map
"
`;

exports[`fixtures build minify-path-parent-dir-with-cwd with microbundle 1`] = `
"Used script: microbundle

Directory tree:

minify-path-parent-dir
dist
minify-path-parent-dir.js
minify-path-parent-dir.js.map
minify-path-parent-dir.mjs
minify-path-parent-dir.mjs.map
minify-path-parent-dir.umd.js
minify-path-parent-dir.umd.js.map
index.js
package.json
two.js


Build \\"minifyPathParentDir\\" to dist:
103 B: minify-path-parent-dir.js.gz
89 B: minify-path-parent-dir.js.br
103 B: minify-path-parent-dir.mjs.gz
95 B: minify-path-parent-dir.mjs.br
202 B: minify-path-parent-dir.umd.js.gz
169 B: minify-path-parent-dir.umd.js.br"
`;

exports[`fixtures build minify-path-parent-dir-with-cwd with microbundle 2`] = `6`;

exports[`fixtures build minify-path-parent-dir-with-cwd with microbundle 3`] = `
"var o={prop1:1,__p2:2};module.exports=function(){return console.log(o.prop1),console.log(o.__p2),o};
//# sourceMappingURL=minify-path-parent-dir.js.map
"
`;

exports[`fixtures build minify-path-parent-dir-with-cwd with microbundle 4`] = `
"var o={prop1:1,__p2:2};export default function(){return console.log(o.prop1),console.log(o.__p2),o}
//# sourceMappingURL=minify-path-parent-dir.mjs.map
"
`;

exports[`fixtures build minify-path-parent-dir-with-cwd with microbundle 5`] = `
"!function(e,n){\\"object\\"==typeof exports&&\\"undefined\\"!=typeof module?module.exports=n():\\"function\\"==typeof define&&define.amd?define(n):(e=e||self).minifyPathParentDir=n()}(this,function(){var e={prop1:1,__p2:2};return function(){return console.log(e.prop1),console.log(e.__p2),e}});
//# sourceMappingURL=minify-path-parent-dir.umd.js.map
"
`;

exports[`fixtures build modern with microbundle 1`] = `
"Used script: microbundle -f modern

Expand Down
15 changes: 15 additions & 0 deletions test/fixtures/mangle-json-file/mangle.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"minify": {
"mangle": {
"properties": {
"regex": "^_"
}
}
},
"props": {
"cname": 6,
"props": {
"$_prop2": "__p2"
}
}
}
3 changes: 3 additions & 0 deletions test/fixtures/mangle-json-file/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"name": "mangle-json-file"
}
7 changes: 7 additions & 0 deletions test/fixtures/mangle-json-file/src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { two } from './two';

export default function() {
console.log(two.prop1);
console.log(two._prop2);
return two;
}
4 changes: 4 additions & 0 deletions test/fixtures/mangle-json-file/src/two.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export const two = {
prop1: 1,
_prop2: 2,
};
10 changes: 10 additions & 0 deletions test/fixtures/minify-config/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"name": "minify-config",
"minify": {
"mangle": {
"properties": {
"regex": "^_"
}
}
}
}
7 changes: 7 additions & 0 deletions test/fixtures/minify-config/src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { two } from './two';

export default function() {
console.log(two.prop1);
console.log(two._prop2);
return two;
}
4 changes: 4 additions & 0 deletions test/fixtures/minify-config/src/two.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export const two = {
prop1: 1,
_prop2: 2,
};
15 changes: 15 additions & 0 deletions test/fixtures/minify-path-config/minify.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"minify": {
"mangle": {
"properties": {
"regex": "^_"
}
}
},
"props": {
"cname": 6,
"props": {
"$_prop2": "__p2"
}
}
}
4 changes: 4 additions & 0 deletions test/fixtures/minify-path-config/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"name": "minify-path-config",
"minify": "./minify.json"
}
7 changes: 7 additions & 0 deletions test/fixtures/minify-path-config/src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { two } from './two';

export default function() {
console.log(two.prop1);
console.log(two._prop2);
return two;
}
4 changes: 4 additions & 0 deletions test/fixtures/minify-path-config/src/two.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export const two = {
prop1: 1,
_prop2: 2,
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { two } from './two';

export default function() {
console.log(two.prop1);
console.log(two._prop2);
return two;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"name": "minify-path-parent-dir",
"minify": "../minify.json"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export const two = {
prop1: 1,
_prop2: 2,
};
15 changes: 15 additions & 0 deletions test/fixtures/minify-path-parent-dir-with-cwd/minify.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"minify": {
"mangle": {
"properties": {
"regex": "^_"
}
}
},
"props": {
"cname": 6,
"props": {
"$_prop2": "__p2"
}
}
}
6 changes: 6 additions & 0 deletions test/fixtures/minify-path-parent-dir-with-cwd/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"name": "minify-path-parent-dir-with-cwd",
"scripts": {
"build": "microbundle --cwd ./minify-path-parent-dir"
}
}