forked from babel/babel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulp-tasks.js
160 lines (150 loc) · 4.49 KB
/
gulp-tasks.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
"use strict";
/**
* This file contains the Gulp tasks for babel-standalone. Note that
* babel-standalone is compiled using Webpack, and performs its own Babel
* compilation of all the JavaScript files. This is because it targets web
* browsers, so more transforms are needed than the regular Babel builds that
* only target Node.js.
*
* The tasks in this file are designed to be reusable, so that they can be used
* to make standalone builds of other Babel plugins/presets (such as babel-minify)
*/
const path = require("path");
const pump = require("pump");
const chalk = require("chalk");
const through = require("through2");
const fancyLog = require("fancy-log");
const rename = require("gulp-rename");
const RootMostResolvePlugin = require("webpack-dependency-suite")
.RootMostResolvePlugin;
const webpack = require("webpack");
const webpackStream = require("webpack-stream");
const uglify = require("gulp-uglify");
function webpackBuild(opts) {
const plugins = opts.plugins || [];
let babelVersion = require("../packages/babel-core/package.json").version;
let version = opts.version || babelVersion;
// If this build is part of a pull request, include the pull request number in
// the version number.
if (process.env.CIRCLE_PR_NUMBER) {
const prVersion = "+pr." + process.env.CIRCLE_PR_NUMBER;
babelVersion += prVersion;
version += prVersion;
}
const config = {
module: {
rules: [
{
test: /\.js$/,
loader: "babel-loader",
options: {
// Use the bundled config so that module syntax is passed through
// for Webpack.
envName: "standalone",
},
},
],
},
node: {
// Mock Node.js modules that Babel require()s but that we don't
// particularly care about.
fs: "empty",
module: "empty",
net: "empty",
},
output: {
filename: opts.filename,
library: opts.library,
libraryTarget: "umd",
},
plugins: [
new webpack.DefinePlugin({
"process.env.NODE_ENV": '"production"',
"process.env": JSON.stringify({ NODE_ENV: "production" }),
BABEL_VERSION: JSON.stringify(babelVersion),
VERSION: JSON.stringify(version),
}),
/*new webpack.NormalModuleReplacementPlugin(
/..\/..\/package/,
"../../../../src/babel-package-shim"
),*/
new webpack.optimize.ModuleConcatenationPlugin(),
].concat(plugins),
resolve: {
plugins: [
// Dedupe packages that are used across multiple plugins.
// This replaces DedupePlugin from Webpack 1.x
new RootMostResolvePlugin(__dirname, true),
],
},
};
if (opts.library !== "Babel") {
config.externals = {
"@babel/standalone": "Babel",
};
}
return webpackStream(config, webpack);
// To write JSON for debugging:
/*return webpackStream(config, webpack, (err, stats) => {
require('fancy-log')(stats.toString({colors: true}));
require('fs').writeFileSync('webpack-debug.json', JSON.stringify(stats.toJson()));
});*/
}
function logUglify() {
return through.obj(function(file, enc, callback) {
fancyLog(
`Minifying '${chalk.cyan(
path.relative(path.join(__dirname, ".."), file.path)
)}'...`
);
callback(null, file);
});
}
function logNoUglify() {
return through.obj(function(file, enc, callback) {
fancyLog(
chalk.yellow(
`Skipped minification of '${chalk.cyan(
path.relative(path.join(__dirname, ".."), file.path)
)}' because not publishing`
)
);
callback(null, file);
});
}
function registerStandalonePackageTask(
gulp,
name,
exportName,
pathname,
version,
plugins
) {
const standaloneName = name + "-standalone";
const standalonePath = path.join(pathname, standaloneName);
gulp.task("build-" + standaloneName, cb => {
pump(
[
gulp.src(path.join(standalonePath, "src/index.js")),
webpackBuild({
filename: name + ".js",
library: exportName,
version,
plugins,
}),
gulp.dest(standalonePath),
].concat(
// Minification is super slow, so we skip it unless we are publishing
process.env.IS_PUBLISH ? logUglify() : logNoUglify(),
process.env.IS_PUBLISH ? uglify() : [],
rename({ extname: ".min.js" }),
gulp.dest(standalonePath)
),
cb
);
});
}
module.exports = {
webpackBuild: webpackBuild,
registerStandalonePackageTask: registerStandalonePackageTask,
};