Skip to content

Commit

Permalink
Correctly serialize YAML to JS (#682)
Browse files Browse the repository at this point in the history
  • Loading branch information
lbguilherme authored and devongovett committed Feb 5, 2018
1 parent e2728fc commit 58f8900
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 6 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
"posthtml": "^0.10.1",
"resolve": "^1.4.0",
"sanitize-filename": "^1.6.1",
"serialize-to-js": "^1.1.1",
"serve-static": "^1.12.4",
"source-map": "0.6.1",
"toml": "^2.3.3",
Expand Down
3 changes: 2 additions & 1 deletion src/assets/YAMLAsset.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const Asset = require('../Asset');
const yaml = require('js-yaml');
const serializeObject = require('../utils/serializeObject');

class YAMLAsset extends Asset {
constructor(name, pkg, options) {
Expand All @@ -13,7 +14,7 @@ class YAMLAsset extends Asset {

generate() {
return {
js: `module.exports=${JSON.stringify(this.ast, false, 2)};`
js: serializeObject(this.ast, this.options.minify)
};
}
}
Expand Down
19 changes: 19 additions & 0 deletions src/utils/serializeObject.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
const {minify} = require('uglify-es');
const {serialize} = require('serialize-to-js');

function serializeObject(obj, shouldMinify = false) {
let code = `module.exports = ${serialize(obj)};`;

if (shouldMinify) {
let minified = minify(code);
if (minified.error) {
throw minified.error;
}

code = minified.code;
}

return code;
}

module.exports = serializeObject;
9 changes: 9 additions & 0 deletions test/javascript.js
Original file line number Diff line number Diff line change
Expand Up @@ -567,4 +567,13 @@ describe('javascript', function() {
let json = fs.readFileSync(__dirname + '/dist/index.js', 'utf8');
assert(json.includes('{test:"test"}'));
});

it('should minify YAML for production', async function() {
await bundle(__dirname + '/integration/yaml/index.js', {
production: true
});

let json = fs.readFileSync(__dirname + '/dist/index.js', 'utf8');
assert(json.includes('{a:1,b:{c:2}}'));
});
});
71 changes: 66 additions & 5 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -792,6 +792,10 @@ block-stream@*:
dependencies:
inherits "~2.0.0"

bluebird@^3.0.5:
version "3.5.1"
resolved "https://npm.cubos.io/bluebird/-/bluebird-3.5.1.tgz#d9551f9de98f1fcda1e683d17ee91a0602ee2eb9"

bn.js@^4.0.0, bn.js@^4.1.0, bn.js@^4.1.1, bn.js@^4.4.0:
version "4.11.8"
resolved "https://registry.yarnpkg.com/bn.js/-/bn.js-4.11.8.tgz#2cde09eb5ee341f484746bb0309b3253b1b1442f"
Expand Down Expand Up @@ -1158,6 +1162,10 @@ clone@^1.0.2:
version "1.0.3"
resolved "https://registry.yarnpkg.com/clone/-/clone-1.0.3.tgz#298d7e2231660f40c003c2ed3140decf3f53085f"

clones@^1.1.0:
version "1.1.0"
resolved "https://npm.cubos.io/clones/-/clones-1.1.0.tgz#87e904132d6140c5c0b72006c08c0d05bd7b63b3"

co@^4.6.0:
version "4.6.0"
resolved "https://registry.yarnpkg.com/co/-/co-4.6.0.tgz#6ea6bdf3d853ae54ccb8e47bfa0bf3f9031fb184"
Expand Down Expand Up @@ -1267,6 +1275,13 @@ concat-stream@^1.6.0:
readable-stream "^2.2.2"
typedarray "^0.0.6"

config-chain@~1.1.5:
version "1.1.11"
resolved "https://npm.cubos.io/config-chain/-/config-chain-1.1.11.tgz#aba09747dfbe4c3e70e766a6e41586e1859fc6f2"
dependencies:
ini "^1.3.4"
proto-list "~1.2.1"

console-browserify@^1.1.0:
version "1.1.0"
resolved "https://registry.yarnpkg.com/console-browserify/-/console-browserify-1.1.0.tgz#f0241c45730a9fc6323b206dbf38edc741d0bb10"
Expand Down Expand Up @@ -1658,6 +1673,16 @@ ecc-jsbn@~0.1.1:
dependencies:
jsbn "~0.1.0"

editorconfig@^0.13.2:
version "0.13.3"
resolved "https://npm.cubos.io/editorconfig/-/editorconfig-0.13.3.tgz#e5219e587951d60958fd94ea9a9a008cdeff1b34"
dependencies:
bluebird "^3.0.5"
commander "^2.9.0"
lru-cache "^3.2.0"
semver "^5.1.0"
sigmund "^1.0.1"

[email protected]:
version "1.1.1"
resolved "https://registry.yarnpkg.com/ee-first/-/ee-first-1.1.1.tgz#590c61156b0ae2f4f0255732a158b266bc56b21d"
Expand Down Expand Up @@ -2561,7 +2586,7 @@ [email protected]:
version "2.0.1"
resolved "https://registry.yarnpkg.com/inherits/-/inherits-2.0.1.tgz#b17d08d326b4423e568eff719f91b0b1cbdf69f1"

ini@~1.3.0:
ini@^1.3.4, ini@~1.3.0:
version "1.3.5"
resolved "https://registry.yarnpkg.com/ini/-/ini-1.3.5.tgz#eee25f56db1c9ec6085e0c22778083f596abf927"

Expand Down Expand Up @@ -2931,6 +2956,15 @@ js-base64@^2.1.8, js-base64@^2.1.9:
version "2.4.1"
resolved "https://registry.yarnpkg.com/js-base64/-/js-base64-2.4.1.tgz#e02813181cd53002888e918935467acb2910e596"

js-beautify@~1.6.8:
version "1.6.14"
resolved "https://npm.cubos.io/js-beautify/-/js-beautify-1.6.14.tgz#d3b8f7322d02b9277d58bd238264c327e58044cd"
dependencies:
config-chain "~1.1.5"
editorconfig "^0.13.2"
mkdirp "~0.5.0"
nopt "~3.0.1"

js-tokens@^3.0.0, js-tokens@^3.0.2:
version "3.0.2"
resolved "https://registry.yarnpkg.com/js-tokens/-/js-tokens-3.0.2.tgz#9866df395102130e38f7f996bceb65443209c25b"
Expand Down Expand Up @@ -3469,6 +3503,12 @@ loud-rejection@^1.0.0:
currently-unhandled "^0.4.1"
signal-exit "^3.0.0"

lru-cache@^3.2.0:
version "3.2.0"
resolved "https://npm.cubos.io/lru-cache/-/lru-cache-3.2.0.tgz#71789b3b7f5399bec8565dda38aa30d2a097efee"
dependencies:
pseudomap "^1.0.1"

lru-cache@^4.0.1:
version "4.1.1"
resolved "https://registry.yarnpkg.com/lru-cache/-/lru-cache-4.1.1.tgz#622e32e82488b49279114a4f9ecf45e7cd6bba55"
Expand Down Expand Up @@ -3640,7 +3680,7 @@ mixin-deep@^1.2.0:
for-in "^1.0.2"
is-extendable "^1.0.1"

[email protected], [email protected], "mkdirp@>=0.5 0", mkdirp@^0.5.0, mkdirp@^0.5.1, mkdirp@~0.5.1:
[email protected], [email protected], "mkdirp@>=0.5 0", mkdirp@^0.5.0, mkdirp@^0.5.1, mkdirp@~0.5.0, mkdirp@~0.5.1:
version "0.5.1"
resolved "https://registry.yarnpkg.com/mkdirp/-/mkdirp-0.5.1.tgz#30057438eac6cf7f8c4767f38648d6697d75c903"
dependencies:
Expand Down Expand Up @@ -3795,7 +3835,7 @@ node-sass@^4.5.3:
stdout-stream "^1.4.0"
"true-case-path" "^1.0.2"

"nopt@2 || 3":
"nopt@2 || 3", nopt@~3.0.1:
version "3.0.6"
resolved "https://registry.yarnpkg.com/nopt/-/nopt-3.0.6.tgz#c6465dbf08abcd4db359317f79ac68a646b28ff9"
dependencies:
Expand Down Expand Up @@ -4545,11 +4585,15 @@ promise@^7.1.1:
dependencies:
asap "~2.0.3"

proto-list@~1.2.1:
version "1.2.4"
resolved "https://npm.cubos.io/proto-list/-/proto-list-1.2.4.tgz#212d5bfe1318306a420f6402b8e26ff39647a849"

prr@~1.0.1:
version "1.0.1"
resolved "https://registry.yarnpkg.com/prr/-/prr-1.0.1.tgz#d3fc114ba06995a45ec6893f484ceb1d78f5f476"

pseudomap@^1.0.2:
pseudomap@^1.0.1, pseudomap@^1.0.2:
version "1.0.2"
resolved "https://registry.yarnpkg.com/pseudomap/-/pseudomap-1.0.2.tgz#f052a28da70e618917ef0a8ac34c1ae5a68286b3"

Expand Down Expand Up @@ -4947,6 +4991,12 @@ safe-buffer@^5.0.1, safe-buffer@^5.1.0, safe-buffer@^5.1.1, safe-buffer@~5.1.0,
version "5.1.1"
resolved "https://registry.yarnpkg.com/safe-buffer/-/safe-buffer-5.1.1.tgz#893312af69b2123def71f57889001671eeb2c853"

safer-eval@^1.2.1:
version "1.2.3"
resolved "https://npm.cubos.io/safer-eval/-/safer-eval-1.2.3.tgz#73ba74a34bc8a07d6a44135c815fd18a8eebe7a0"
dependencies:
clones "^1.1.0"

sanitize-filename@^1.6.1:
version "1.6.1"
resolved "https://registry.yarnpkg.com/sanitize-filename/-/sanitize-filename-1.6.1.tgz#612da1c96473fa02dccda92dcd5b4ab164a6772a"
Expand Down Expand Up @@ -4977,7 +5027,7 @@ scss-tokenizer@^0.2.3:
js-base64 "^2.1.8"
source-map "^0.4.2"

"semver@2 || 3 || 4 || 5", semver@^5.3.0:
"semver@2 || 3 || 4 || 5", semver@^5.1.0, semver@^5.3.0:
version "5.5.0"
resolved "https://registry.yarnpkg.com/semver/-/semver-5.5.0.tgz#dc4bbc7a6ca9d916dee5d43516f0092b58f7b8ab"

Expand All @@ -5003,6 +5053,13 @@ [email protected]:
range-parser "~1.2.0"
statuses "~1.3.1"

serialize-to-js@^1.1.1:
version "1.1.1"
resolved "https://npm.cubos.io/serialize-to-js/-/serialize-to-js-1.1.1.tgz#4cd3a40a3d5f2d014625cb9283cdf990800228f5"
dependencies:
js-beautify "~1.6.8"
safer-eval "^1.2.1"

serve-static@^1.12.4:
version "1.13.1"
resolved "https://registry.yarnpkg.com/serve-static/-/serve-static-1.13.1.tgz#4c57d53404a761d8f2e7c1e8a18a47dbf278a719"
Expand Down Expand Up @@ -5069,6 +5126,10 @@ shebang-regex@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/shebang-regex/-/shebang-regex-1.0.0.tgz#da42f49740c0b42db2ca9728571cb190c98efea3"

sigmund@^1.0.1:
version "1.0.1"
resolved "https://npm.cubos.io/sigmund/-/sigmund-1.0.1.tgz#3ff21f198cad2175f9f3b781853fd94d0d19b590"

signal-exit@^3.0.0, signal-exit@^3.0.1, signal-exit@^3.0.2:
version "3.0.2"
resolved "https://registry.yarnpkg.com/signal-exit/-/signal-exit-3.0.2.tgz#b5fdc08f1287ea1178628e415e25132b73646c6d"
Expand Down

0 comments on commit 58f8900

Please sign in to comment.