Skip to content

Commit

Permalink
fix: upgraded babel plugin rollup, added option to exclude some babel…
Browse files Browse the repository at this point in the history
… preset env plugins
  • Loading branch information
teclone committed Jul 8, 2020
1 parent 953980d commit 11ea27f
Show file tree
Hide file tree
Showing 6 changed files with 1,220 additions and 1,148 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -74,15 +74,16 @@
"@babel/preset-env": "7.9.6",
"@babel/preset-typescript": "7.9.0",
"@babel/runtime": "7.9.6",
"@rollup/plugin-babel": "5.0.4",
"@rollup/plugin-commonjs": "12.0.0",
"@rollup/plugin-json": "4.0.3",
"@rollup/plugin-node-resolve": "8.0.0",
"@teclone/node-utils": "1.0.5",
"@teclone/utils": "2.17.2",
"args": "5.0.1",
"chalk": "4.0.0",
"glob-to-regexp": "0.4.1",
"rollup": "2.10.5",
"rollup-plugin-babel": "4.4.0",
"rollup-plugin-terser": "5.3.0"
}
}
23 changes: 17 additions & 6 deletions src/@types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ export interface Config {
moduleName?: string;

/**
* allowed file extensions. defaults to .js, .ts
* allowed file extensions. defaults to .js, .ts, .jsx, .tsx
*/
extensions?: string[];

Expand All @@ -117,7 +117,7 @@ export interface Config {

/**
* boolean indicating if sourcemap should be generated for all builds,
* can be true, false, or 'inline'
* can be true, false, or 'inline', defaults to true
*/
sourcemap?: true | false | 'inline';

Expand All @@ -138,17 +138,17 @@ export interface Config {
globals?: object;

/**
* defines config settings for generating distributed codes
* defines config settings for generating distributed browser codes
*/
distConfig?: DistConfig;

/**
* defines config settings for generating cjs files
* defines config settings for generating cjs files, for node js or comonjs modules
*/
cjsConfig?: CJSConfig;

/**
* defines config settings for generating esm files
* defines config settings for generating esm files, es modules
*/
esmConfig?: ESMConfig;
}
Expand Down Expand Up @@ -194,10 +194,21 @@ export interface ModuleFiles {
typeDefinitionFiles: Module[];
}

export interface BabelPresetsConfig {
presets?: any[];
exclude?: Array<string | RegExp>;
include?: Array<string | RegExp>;
}

export interface BabelPluginsConfig {
plugins?: any[];
}

export interface GeneralConfig {
config?: Config;
babelConfig?: {
presets?: any[];
presetsConfig?: BabelPresetsConfig;
pluginsConfig?: BabelPluginsConfig;
plugins?: any[];
};
}
Expand Down
1 change: 1 addition & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Config } from './@types';

export const config: Config = {
plugins: [],

Expand Down
30 changes: 5 additions & 25 deletions src/modules/Bundler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { mkDirSync, getEntryPath } from '@teclone/node-utils';
import { rollup } from 'rollup';
import { getRollupPlugins } from './utils';
import chalk from 'chalk';
import globToRegex from 'glob-to-regexp';

const allExternal = () => true;

Expand All @@ -32,10 +33,6 @@ class Bundler {

private bundlerOptions: BundlerOptions;

/**
* @param plugins array of extra plugins to be applied
* @param config path to user defined build config or the user defined config object
*/
constructor(generalConfig: GeneralConfig = {}, bundlerOptions: BundlerOptions) {
this.entryPath = getEntryPath();
this.generalConfig = generalConfig;
Expand All @@ -52,26 +49,7 @@ class Bundler {
*/
private resolveRegex(pattern: string | RegExp) {
if (isString(pattern)) {
// match everything
if (pattern === '*') {
return new RegExp('^.*', 'i');
} else {
pattern = pattern
.split('/')
.map((current) => {
if (current === '*') {
return '[^/]+';
} else if (current === '**') {
return '.*';
} else {
return current;
}
})
.join('/');

pattern = pattern.replace(/^\/+/, '^');
return new RegExp(pattern, 'i');
}
return globToRegex(pattern, { extended: true, globstar: true, flags: 'i' });
} else {
return pattern;
}
Expand Down Expand Up @@ -238,7 +216,9 @@ class Bundler {
};

let src = '';
const regexMatches = (regex) => regex.test(path.join(config.srcDir, src));
const regexMatches = (regex) => {
return regex.test(path.join(src));
};

for (const current of modules) {
const { ext, isBuildFile, oldRelativePath } = current;
Expand Down
65 changes: 46 additions & 19 deletions src/modules/utils.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
import resolve from '@rollup/plugin-node-resolve';
import commonjs from '@rollup/plugin-commonjs';
import babel from 'rollup-plugin-babel';
import babel from '@rollup/plugin-babel';
import json from '@rollup/plugin-json';
import { terser } from 'rollup-plugin-terser';
import { Config, GeneralConfig, DistConfig, ESMConfig, CJSConfig } from '../@types';
import {
Config,
GeneralConfig,
DistConfig,
ESMConfig,
CJSConfig,
BabelPresetsConfig,
BabelPluginsConfig,
} from '../@types';
import { getEntryPath } from '@teclone/node-utils';

import path from 'path';
Expand Down Expand Up @@ -33,17 +41,14 @@ export const loadFile = (entryPath: string, file: string) => {
};

export const getBabelPlugins = (
extraPlugins: Array<any> = [],
pluginsConfig: BabelPluginsConfig,
internalNodeModulesDir: string,
useESModules: boolean,
) => {
return [
[
resolveDependency(internalNodeModulesDir, '@babel/plugin-transform-runtime'),
{
useESModules,
},
],
/**
* all these are now available by default in babel preset env
*/
resolveDependency(internalNodeModulesDir, '@babel/plugin-proposal-class-properties'),
resolveDependency(
internalNodeModulesDir,
Expand All @@ -61,23 +66,41 @@ export const getBabelPlugins = (
'@babel/plugin-proposal-optional-chaining',
),
],
...extraPlugins,
...(pluginsConfig?.plugins || []),
[
resolveDependency(internalNodeModulesDir, '@babel/plugin-transform-runtime'),
{
useESModules,
},
],
];
};

export const getBabelPresets = (
extraPresets: Array<any> = [],
presetsConfig: BabelPresetsConfig,
internalNodeModulesDir: string,
) => {
return [
// here we are including babel preset env
[
resolveDependency(internalNodeModulesDir, '@babel/preset-env'),
{
// we do not want babel to change es modules to another module type
// so that rollup can be able to process it
modules: false,

// this defines babel preset shipped plugins to exclude, see
// https://github.com/babel/babel/blob/master/packages/babel-compat-data/scripts/data/plugin-features.js
//for all the included plugins
exclude: presetsConfig?.exclude || [],
},
],

// support for typescript
resolveDependency(internalNodeModulesDir, '@babel/preset-typescript'),
...extraPresets,

// user defined preset.
...(presetsConfig?.presets || []),
];
};

Expand All @@ -97,24 +120,28 @@ export const getRollupPlugins = (
}),

babel({
// we do not want to use any local babelrc
babelrc: false,

presets: getBabelPresets(
generalConfig?.babelConfig?.presets || [],
plugins: getBabelPlugins(
generalConfig?.babelConfig?.pluginsConfig || {},
internalNodeModulesDir,
buildConfig.format === 'esm',
),

plugins: getBabelPlugins(
generalConfig?.babelConfig?.plugins || [],
presets: getBabelPresets(
generalConfig?.babelConfig?.presetsConfig || {},
internalNodeModulesDir,
buildConfig.format === 'esm',
),

// we are using runtime because it is assumed you are building a library
// this should be made configurable
babelHelpers: 'runtime',

// do not process node module files, it is believed that all files in the node modules directory has been properly worked on
exclude: 'node_modules/**',

extensions: mainConfig.extensions,

runtimeHelpers: true,
}),

json(),
Expand Down
Loading

0 comments on commit 11ea27f

Please sign in to comment.