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

Commit

Permalink
feat(compiler/js): add support for babel preset-stage-3
Browse files Browse the repository at this point in the history
  • Loading branch information
zanona committed Nov 19, 2017
1 parent 019f74c commit 6167ee8
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 21 deletions.
42 changes: 34 additions & 8 deletions min/js.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,22 @@ module.exports = function (files) {
browserify = require('browserify'),
babel = require('babel-core'),
babelTransform = require('babelify'),
esPresets = require('babel-preset-env'),
uglifyjs = require('uglify-js'),
babelEnv = require('babel-preset-env'),
babelStage3 = require('babel-preset-stage-3'),
uglifyjs = require('uglify-js'),
regenerator = require('regenerator'),
log = require('../utils').log;

function getOffsetContent(file) {
// adjust inline script contents with the line number it was located
if (!file.inline) return file.contents;
// using zero-base index to match with source map spec
const fmtLine = file.line ? file.line - 1 : 0,
fmtCol = file.column ? file.column - 1 : 0;
return Array(fmtLine).fill('\n').join('')
+ Array(fmtCol).fill(' ').join('')
+ file.contents;
}
function replaceNodeEnvVars(file) {
const nodePattern = /process.env(?:\.(.+?)\b|\[(["'])(.+?)\2\])/g;
file.contents = file.contents.replace(nodePattern, (m,v1,_,v3) => {
Expand Down Expand Up @@ -54,12 +65,23 @@ module.exports = function (files) {
output: {
inline_script: true,
beautify: false
},
sourceMap: {
content: file.sourceMap
/*
* need to set the `url` param based on cmd option
* such as --source-map-expose=true, which will then
* print //# sourceMapURL at the end of scripts
*/
//,url: `sourcemaps/${file.name}.map`
}
};

return new Promise((resolve, reject) => {
try {
file.contents = uglifyjs.minify(file.contents, options).code;
const minified = uglifyjs.minify(file.contents, options);
file.sourceMap = minified.map;
file.contents = minified.code;
resolve(file);
} catch (e) {
reject(e);
Expand All @@ -77,7 +99,7 @@ module.exports = function (files) {
.transform(regenerator, {global: true})
.transform(babelTransform, {
filename: file.name,
presets: [esPresets],
presets: [babelEnv, babelStage3],
global: true
})
.bundle(function (error, buffer) {
Expand All @@ -88,14 +110,18 @@ module.exports = function (files) {
});
});
}

function babelify(file) {
return new Promise((resolve, reject) => {
try {
file.contents = babel
.transform(file.contents, {
const transpiled = babel
.transform(getOffsetContent(file), {
filename: file.name,
presets: [esPresets]
}).code;
presets: [babelEnv, babelStage3],
sourceMaps: true
});
file.sourceMap = transpiled.map;
file.contents = transpiled.code;
resolve(file);
} catch (e) {
reject(e);
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"autoprefixer": "^6.7.7",
"babel-core": "^6.26.0",
"babel-preset-env": "^1.6.0",
"babel-preset-stage-3": "^6.24.1",
"babelify": "^7.3.0",
"browserify": "^11.2.0",
"clean-css": "^3.4.28",
Expand Down
51 changes: 38 additions & 13 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -323,15 +323,31 @@ babel-plugin-syntax-async-functions@^6.8.0:
version "6.13.0"
resolved "https://registry.yarnpkg.com/babel-plugin-syntax-async-functions/-/babel-plugin-syntax-async-functions-6.13.0.tgz#cad9cad1191b5ad634bf30ae0872391e0647be95"

babel-plugin-syntax-async-generators@^6.5.0:
version "6.13.0"
resolved "https://registry.yarnpkg.com/babel-plugin-syntax-async-generators/-/babel-plugin-syntax-async-generators-6.13.0.tgz#6bc963ebb16eccbae6b92b596eb7f35c342a8b9a"

babel-plugin-syntax-exponentiation-operator@^6.8.0:
version "6.13.0"
resolved "https://registry.yarnpkg.com/babel-plugin-syntax-exponentiation-operator/-/babel-plugin-syntax-exponentiation-operator-6.13.0.tgz#9ee7e8337290da95288201a6a57f4170317830de"

babel-plugin-syntax-object-rest-spread@^6.8.0:
version "6.13.0"
resolved "https://registry.yarnpkg.com/babel-plugin-syntax-object-rest-spread/-/babel-plugin-syntax-object-rest-spread-6.13.0.tgz#fd6536f2bce13836ffa3a5458c4903a597bb3bf5"

babel-plugin-syntax-trailing-function-commas@^6.22.0:
version "6.22.0"
resolved "https://registry.yarnpkg.com/babel-plugin-syntax-trailing-function-commas/-/babel-plugin-syntax-trailing-function-commas-6.22.0.tgz#ba0360937f8d06e40180a43fe0d5616fff532cf3"

babel-plugin-transform-async-to-generator@^6.22.0:
babel-plugin-transform-async-generator-functions@^6.24.1:
version "6.24.1"
resolved "https://registry.yarnpkg.com/babel-plugin-transform-async-generator-functions/-/babel-plugin-transform-async-generator-functions-6.24.1.tgz#f058900145fd3e9907a6ddf28da59f215258a5db"
dependencies:
babel-helper-remap-async-to-generator "^6.24.1"
babel-plugin-syntax-async-generators "^6.5.0"
babel-runtime "^6.22.0"

babel-plugin-transform-async-to-generator@^6.22.0, babel-plugin-transform-async-to-generator@^6.24.1:
version "6.24.1"
resolved "https://registry.yarnpkg.com/babel-plugin-transform-async-to-generator/-/babel-plugin-transform-async-to-generator-6.24.1.tgz#6536e378aff6cb1d5517ac0e40eb3e9fc8d08761"
dependencies:
Expand Down Expand Up @@ -507,14 +523,21 @@ babel-plugin-transform-es2015-unicode-regex@^6.22.0:
babel-runtime "^6.22.0"
regexpu-core "^2.0.0"

babel-plugin-transform-exponentiation-operator@^6.22.0:
babel-plugin-transform-exponentiation-operator@^6.22.0, babel-plugin-transform-exponentiation-operator@^6.24.1:
version "6.24.1"
resolved "https://registry.yarnpkg.com/babel-plugin-transform-exponentiation-operator/-/babel-plugin-transform-exponentiation-operator-6.24.1.tgz#2ab0c9c7f3098fa48907772bb813fe41e8de3a0e"
dependencies:
babel-helper-builder-binary-assignment-operator-visitor "^6.24.1"
babel-plugin-syntax-exponentiation-operator "^6.8.0"
babel-runtime "^6.22.0"

babel-plugin-transform-object-rest-spread@^6.22.0:
version "6.26.0"
resolved "https://registry.yarnpkg.com/babel-plugin-transform-object-rest-spread/-/babel-plugin-transform-object-rest-spread-6.26.0.tgz#0f36692d50fef6b7e2d4b3ac1478137a963b7b06"
dependencies:
babel-plugin-syntax-object-rest-spread "^6.8.0"
babel-runtime "^6.26.0"

babel-plugin-transform-regenerator@^6.22.0:
version "6.26.0"
resolved "https://registry.yarnpkg.com/babel-plugin-transform-regenerator/-/babel-plugin-transform-regenerator-6.26.0.tgz#e0703696fbde27f0a3efcacf8b4dca2f7b3a8f2f"
Expand Down Expand Up @@ -563,6 +586,16 @@ babel-preset-env@^1.6.0:
invariant "^2.2.2"
semver "^5.3.0"

babel-preset-stage-3@^6.24.1:
version "6.24.1"
resolved "https://registry.yarnpkg.com/babel-preset-stage-3/-/babel-preset-stage-3-6.24.1.tgz#836ada0a9e7a7fa37cb138fb9326f87934a48395"
dependencies:
babel-plugin-syntax-trailing-function-commas "^6.22.0"
babel-plugin-transform-async-generator-functions "^6.24.1"
babel-plugin-transform-async-to-generator "^6.24.1"
babel-plugin-transform-exponentiation-operator "^6.24.1"
babel-plugin-transform-object-rest-spread "^6.22.0"

babel-register@^6.26.0:
version "6.26.0"
resolved "https://registry.yarnpkg.com/babel-register/-/babel-register-6.26.0.tgz#6ed021173e2fcb486d7acb45c6009a856f647071"
Expand Down Expand Up @@ -1104,15 +1137,7 @@ [email protected]:
version "0.0.1"
resolved "https://registry.yarnpkg.com/concat-map/-/concat-map-0.0.1.tgz#d8a96bd77fd68df7793a73036a3ba0d5405d477b"

concat-stream@^1.4.6, concat-stream@^1.4.7:
version "1.6.0"
resolved "https://registry.yarnpkg.com/concat-stream/-/concat-stream-1.6.0.tgz#0aac662fd52be78964d5532f694784e70110acf7"
dependencies:
inherits "^2.0.3"
readable-stream "^2.2.2"
typedarray "^0.0.6"

concat-stream@~1.4.1, concat-stream@~1.4.5:
concat-stream@^1.4.6, concat-stream@^1.4.7, concat-stream@~1.4.1, concat-stream@~1.4.5:
version "1.4.10"
resolved "https://registry.yarnpkg.com/concat-stream/-/concat-stream-1.4.10.tgz#acc3bbf5602cb8cc980c6ac840fa7d8603e3ef36"
dependencies:
Expand Down Expand Up @@ -3194,7 +3219,7 @@ read-pkg@^1.0.0:
isarray "0.0.1"
string_decoder "~0.10.x"

readable-stream@^2.0.0, readable-stream@^2.0.1, readable-stream@^2.0.2, readable-stream@^2.0.4, readable-stream@^2.0.5, readable-stream@^2.1.5, readable-stream@^2.2.2:
readable-stream@^2.0.0, readable-stream@^2.0.1, readable-stream@^2.0.2, readable-stream@^2.0.4, readable-stream@^2.0.5, readable-stream@^2.1.5:
version "2.3.3"
resolved "https://registry.yarnpkg.com/readable-stream/-/readable-stream-2.3.3.tgz#368f2512d79f9d46fdfc71349ae7878bbc1eb95c"
dependencies:
Expand Down Expand Up @@ -3833,7 +3858,7 @@ tweetnacl@^0.14.3, tweetnacl@~0.14.0:
version "0.14.5"
resolved "https://registry.yarnpkg.com/tweetnacl/-/tweetnacl-0.14.5.tgz#5ae68177f192d4456269d108afa93ff8743f4f64"

typedarray@^0.0.6, typedarray@~0.0.5:
typedarray@~0.0.5:
version "0.0.6"
resolved "https://registry.yarnpkg.com/typedarray/-/typedarray-0.0.6.tgz#867ac74e3864187b1d3d47d996a78ec5c8830777"

Expand Down

0 comments on commit 6167ee8

Please sign in to comment.