Skip to content

Commit

Permalink
Fix rollup build
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolo-ribaudo committed Dec 26, 2019
1 parent e5b14c4 commit 7d82ffa
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 106 deletions.
167 changes: 84 additions & 83 deletions Gulpfile.js
Expand Up @@ -86,7 +86,7 @@ function buildRollup(packages) {
const minify = !!process.env.IS_PUBLISH;
return Promise.all(
packages.map(
({ src, format, dest, name, filename, version = babelVersion }) => {
async ({ src, format, dest, name, filename, version = babelVersion }) => {
const extraPlugins = [];
let inputExternal = undefined,
outputGlobals = undefined,
Expand Down Expand Up @@ -147,90 +147,91 @@ function buildRollup(packages) {
}
const input = getIndexFromPackage(src);
fancyLog(`Compiling '${chalk.cyan(input)}' with rollup ...`);
return rollup
.rollup({
input,
external: inputExternal,
plugins: [
...extraPlugins,
rollupBabelSource(),
rollupReplace({
"process.env.NODE_ENV": JSON.stringify(process.env.NODE_ENV),
BABEL_VERSION: JSON.stringify(babelVersion),
VERSION: JSON.stringify(version),
}),
rollupBabel({
envName: babelEnvName,
babelrc: false,
extends: "./babel.config.js",
}),
rollupNodeResolve({
browser: nodeResolveBrowser,
preferBuiltins: true,
//todo: When Yarn workspaces is enabled, remove `dedupe` option
dedupe(importee) {
return (
importee.startsWith("lodash/") ||
[
"babel-plugin-dynamic-import-node/utils",
"esutils",
"semver",
"source-map",
].includes(importee)
);
},
}),
rollupCommonJs({
include: [/node_modules/, "packages/babel-preset-env/data/**"],
namedExports: {
"babel-plugin-dynamic-import-node/utils.js": [
"createDynamicImportTransform",
"getImportSource",
],
"@babel/standalone": ["availablePlugins", "registerPlugin"],
},
}),
rollupJson(),
rollupNodeBuiltins(),
rollupNodeGlobals({ sourceMap: sourcemap }),
],
})
.then(bundle => {
const outputFile = path.resolve(src, dest, filename || "index.js");
return bundle
.write({
file: outputFile,
format,
name,
globals: outputGlobals,
sourcemap: sourcemap,
})
.then(() => {
if (!process.env.IS_PUBLISH) {
fancyLog(
chalk.yellow(
`Skipped minification of '${chalk.cyan(
path.relative(path.join(__dirname, ".."), outputFile)
)}' because not publishing`
)
);
return undefined;
}
fancyLog(
`Minifying '${chalk.cyan(
path.relative(path.join(__dirname, ".."), outputFile)
)}'...`

const bundle = await rollup.rollup({
input,
external: inputExternal,
plugins: [
...extraPlugins,
rollupBabelSource(),
rollupReplace({
"process.env.NODE_ENV": JSON.stringify(process.env.NODE_ENV),
BABEL_VERSION: JSON.stringify(babelVersion),
VERSION: JSON.stringify(version),
}),
rollupBabel({
envName: babelEnvName,
babelrc: false,
extends: "./babel.config.js",
}),
rollupNodeResolve({
browser: nodeResolveBrowser,
preferBuiltins: true,
//todo: When Yarn workspaces is enabled, remove `dedupe` option
dedupe(importee) {
return (
importee.startsWith("lodash/") ||
[
"babel-plugin-dynamic-import-node/utils",
"esutils",
"semver",
"source-map",
].includes(importee)
);
},
}),
rollupCommonJs({
include: [
/node_modules/,
"packages/babel-preset-env/data/**",
"packages/babel-runtime/regenerator/**",
],
namedExports: {
"babel-plugin-dynamic-import-node/utils.js": [
"createDynamicImportTransform",
"getImportSource",
],
"@babel/standalone": ["availablePlugins", "registerPlugin"],
},
}),
rollupJson(),
rollupNodeBuiltins(),
rollupNodeGlobals({ sourceMap: sourcemap }),
],
});

const outputFile = path.resolve(src, dest, filename || "index.js");
await bundle.write({
file: outputFile,
format,
name,
globals: outputGlobals,
sourcemap: sourcemap,
});

if (!process.env.IS_PUBLISH) {
fancyLog(
chalk.yellow(
`Skipped minification of '${chalk.cyan(
path.relative(path.join(__dirname, ".."), outputFile)
)}' because not publishing`
)
);
return undefined;
}
fancyLog(
`Minifying '${chalk.cyan(
path.relative(path.join(__dirname, ".."), outputFile)
)}'...`
);

return bundle.write({
file: outputFile.replace(/\.js$/, ".min.js"),
format,
name,
globals: outputGlobals,
sourcemap: sourcemap,
});
});
});
await bundle.write({
file: outputFile.replace(/\.js$/, ".min.js"),
format,
name,
globals: outputGlobals,
sourcemap: sourcemap,
});
}
)
);
Expand Down
24 changes: 5 additions & 19 deletions babel.config.js
@@ -1,7 +1,5 @@
"use strict";

const path = require("path");

module.exports = function(api) {
const env = api.env();

Expand Down Expand Up @@ -34,31 +32,20 @@ module.exports = function(api) {
];

switch (env) {
// Configs used during bundling builds.
case "rollup":
convertESM = false;
ignoreLib = false;
// rollup-commonjs will converts node_modules to ESM
unambiguousSources.push(
"**/node_modules",
// todo: remove this after it is rewritten into ESM
"packages/babel-preset-env/data"
);
envOpts.targets = {
node: nodeVersion,
};
break;
case "standalone":
includeRegeneratorRuntime = true;
// Configs used during bundling builds.
unambiguousSources.push("packages/babel-runtime/regenerator");
case "rollup":
convertESM = false;
ignoreLib = false;
includeRegeneratorRuntime = true;
// rollup-commonjs will converts node_modules to ESM
unambiguousSources.push(
"**/node_modules",
// todo: remove this after it is rewritten into ESM
"packages/babel-preset-env/data"
);
// targets to browserslists: defaults
if (env === "rollup") envOpts.targets = { node: nodeVersion };
break;
case "production":
// Config during builds before publish.
Expand Down Expand Up @@ -86,7 +73,6 @@ module.exports = function(api) {
helpers: false, // Helpers are handled by rollup when needed
regenerator: true,
version: require(babelRuntimePkgPath).version,
absoluteRuntime: path.dirname(babelRuntimePkgPath),
};
}

Expand Down
15 changes: 11 additions & 4 deletions scripts/rollup-plugin-babel-source.js
Expand Up @@ -37,13 +37,20 @@ module.exports = function() {
return null;
},
resolveId(importee) {
let packageFolderName;
if (importee === "@babel/runtime/regenerator") {
return path.join(
dirname,
"packages",
"babel-runtime",
"regenerator",
"index.js"
);
}

const matches = importee.match(/^@babel\/([^/]+)$/);
if (matches) {
packageFolderName = `babel-${matches[1]}`;
}
const packageFolderName = `babel-${matches[1]}`;

if (packageFolderName) {
// resolve babel package names to their src index file
const packageFolder = path.join(dirname, "packages", packageFolderName);
const packageJson = require(path.join(packageFolder, "package.json"));
Expand Down

0 comments on commit 7d82ffa

Please sign in to comment.