mirror of
https://github.com/developit/microbundle.git
synced 2026-01-18 13:56:35 +00:00
Dev Branch (#400)
This commit is contained in:
commit
700d9549ac
@ -8,6 +8,7 @@ module.exports = {
|
||||
targets: {
|
||||
node: 'current',
|
||||
},
|
||||
exclude: ['transform-async-to-generator', 'transform-regenerator'],
|
||||
},
|
||||
],
|
||||
],
|
||||
|
||||
49
package.json
49
package.json
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "microbundle",
|
||||
"version": "0.11.0",
|
||||
"version": "0.12.0-next.1",
|
||||
"description": "Zero-configuration bundler for tiny JS libs, powered by Rollup.",
|
||||
"main": "dist/microbundle.js",
|
||||
"source": "src/index.js",
|
||||
@ -42,7 +42,7 @@
|
||||
"author": "Jason Miller <jason@developit.ca> (http://jasonformat.com)",
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"@babel/core": "^7.4.4",
|
||||
"@babel/core": "^7.4.5",
|
||||
"@babel/plugin-proposal-class-properties": "7.4.4",
|
||||
"@babel/plugin-syntax-jsx": "^7.2.0",
|
||||
"@babel/plugin-transform-flow-strip-types": "^7.4.4",
|
||||
@ -50,53 +50,56 @@
|
||||
"@babel/polyfill": "^7.4.4",
|
||||
"@babel/preset-flow": "^7.0.0",
|
||||
"asyncro": "^3.0.0",
|
||||
"autoprefixer": "^9.5.1",
|
||||
"babel-plugin-transform-async-to-promises": "^0.8.10",
|
||||
"autoprefixer": "^9.6.0",
|
||||
"babel-plugin-macros": "^2.4.2",
|
||||
"babel-plugin-transform-async-to-promises": "^0.8.12",
|
||||
"babel-plugin-transform-replace-expressions": "^0.2.0",
|
||||
"brotli-size": "^0.1.0",
|
||||
"camelcase": "^5.3.1",
|
||||
"cssnano": "^4.1.10",
|
||||
"es6-promisify": "^6.0.1",
|
||||
"filesize": "^4.1.2",
|
||||
"gzip-size": "^5.1.0",
|
||||
"gzip-size": "^5.1.1",
|
||||
"kleur": "^3.0.3",
|
||||
"lodash.merge": "^4.6.1",
|
||||
"module-details-from-path": "^1.0.3",
|
||||
"pretty-bytes": "^5.2.0",
|
||||
"rollup": "^1.11.3",
|
||||
"rollup-plugin-alias": "^1.5.1",
|
||||
"rollup": "^1.15.1",
|
||||
"rollup-plugin-alias": "^1.5.2",
|
||||
"rollup-plugin-babel": "^4.3.2",
|
||||
"rollup-plugin-bundle-size": "^1.0.1",
|
||||
"rollup-plugin-commonjs": "^9.0.0",
|
||||
"rollup-plugin-commonjs": "^10.0.0",
|
||||
"rollup-plugin-es3": "^1.1.0",
|
||||
"rollup-plugin-json": "^4.0.0",
|
||||
"rollup-plugin-node-resolve": "^4.2.3",
|
||||
"rollup-plugin-node-resolve": "^5.0.1",
|
||||
"rollup-plugin-postcss": "^2.0.3",
|
||||
"rollup-plugin-terser": "^4.0.4",
|
||||
"rollup-plugin-typescript2": "^0.21.0",
|
||||
"sade": "^1.4.0",
|
||||
"rollup-plugin-terser": "^5.0.0",
|
||||
"rollup-plugin-typescript2": "^0.21.1",
|
||||
"sade": "^1.5.0",
|
||||
"tiny-glob": "^0.2.6",
|
||||
"tslib": "^1.9.0",
|
||||
"typescript": "^3.4.5"
|
||||
"tslib": "^1.10.0",
|
||||
"typescript": "^3.5.1"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@babel/cli": "^7.4.4",
|
||||
"@babel/node": "^7.2.2",
|
||||
"@babel/node": "^7.4.5",
|
||||
"@babel/plugin-proposal-throw-expressions": "^7.2.0",
|
||||
"@babel/preset-env": "^7.4.4",
|
||||
"@babel/preset-env": "^7.4.5",
|
||||
"babel-core": "^7.0.0-bridge.0",
|
||||
"babel-jest": "^24.8.0",
|
||||
"core-js": "^3.1.2",
|
||||
"cross-env": "^5.2.0",
|
||||
"directory-tree": "^2.2.2",
|
||||
"directory-tree": "^2.2.3",
|
||||
"eslint": "^5.16.0",
|
||||
"eslint-config-developit": "^1.1.1",
|
||||
"eslint-config-prettier": "^4.2.0",
|
||||
"eslint-plugin-prettier": "^3.0.1",
|
||||
"eslint-config-prettier": "^4.3.0",
|
||||
"eslint-plugin-prettier": "^3.1.0",
|
||||
"esm": "^3.2.22",
|
||||
"fs-extra": "^7.0.1",
|
||||
"husky": "^2.2.0",
|
||||
"fs-extra": "^8.0.1",
|
||||
"husky": "^2.4.0",
|
||||
"jest": "^24.8.0",
|
||||
"lint-staged": "^8.1.6",
|
||||
"prettier": "^1.17.0",
|
||||
"lint-staged": "^8.2.0",
|
||||
"prettier": "^1.18.2",
|
||||
"regenerator-runtime": "^0.13.2",
|
||||
"rimraf": "^2.6.3",
|
||||
"shell-quote": "^1.6.1",
|
||||
|
||||
80
src/index.js
80
src/index.js
@ -8,6 +8,7 @@ import cssnano from 'cssnano';
|
||||
import { rollup, watch } from 'rollup';
|
||||
import commonjs from 'rollup-plugin-commonjs';
|
||||
import babel from 'rollup-plugin-babel';
|
||||
import customBabel from './lib/babel-custom';
|
||||
import nodeResolve from 'rollup-plugin-node-resolve';
|
||||
import { terser } from 'rollup-plugin-terser';
|
||||
import alias from 'rollup-plugin-alias';
|
||||
@ -18,7 +19,7 @@ import prettyBytes from 'pretty-bytes';
|
||||
import typescript from 'rollup-plugin-typescript2';
|
||||
import json from 'rollup-plugin-json';
|
||||
import logError from './log-error';
|
||||
import { readFile, isDir, isFile, stdout, stderr } from './utils';
|
||||
import { readFile, isDir, isFile, stdout, stderr, isTruthy } from './utils';
|
||||
import camelCase from 'camelcase';
|
||||
|
||||
const removeScope = name => name.replace(/^@.*\//, '');
|
||||
@ -164,6 +165,12 @@ export default async function microbundle(inputOptions) {
|
||||
|
||||
options.multipleEntries = options.entries.length > 1;
|
||||
|
||||
// to disable compress you can put in false or 0 but it's a string so our boolean checks won't work
|
||||
options.compress =
|
||||
typeof options.compress !== 'boolean'
|
||||
? options.compress !== 'false' && options.compress !== '0'
|
||||
: options.compress;
|
||||
|
||||
let formats = (options.format || options.formats).split(',');
|
||||
// always compile cjs first if it's there:
|
||||
formats.sort((a, b) => (a === 'cjs' ? -1 : a > b ? 1 : 0));
|
||||
@ -537,61 +544,38 @@ function createConfig(options, entry, format, writeMeta) {
|
||||
},
|
||||
},
|
||||
}),
|
||||
babel({
|
||||
babelrc: false,
|
||||
configFile: false,
|
||||
compact: false,
|
||||
include: 'node_modules/**',
|
||||
plugins: [
|
||||
[
|
||||
require.resolve('babel-plugin-transform-replace-expressions'),
|
||||
{ replace: defines },
|
||||
// if defines is not set, we shouldn't run babel through node_modules
|
||||
isTruthy(defines) &&
|
||||
babel({
|
||||
babelrc: false,
|
||||
configFile: false,
|
||||
compact: false,
|
||||
include: 'node_modules/**',
|
||||
plugins: [
|
||||
[
|
||||
require.resolve('babel-plugin-transform-replace-expressions'),
|
||||
{ replace: defines },
|
||||
],
|
||||
],
|
||||
],
|
||||
}),
|
||||
babel({
|
||||
}),
|
||||
customBabel({
|
||||
extensions: EXTENSIONS,
|
||||
exclude: 'node_modules/**',
|
||||
passPerPreset: true, // @see https://babeljs.io/docs/en/options#passperpreset
|
||||
presets: [
|
||||
[
|
||||
'@babel/preset-env',
|
||||
{
|
||||
loose: true,
|
||||
modules: false,
|
||||
targets:
|
||||
options.target === 'node' ? { node: '8' } : undefined,
|
||||
exclude: ['transform-async-to-generator'],
|
||||
},
|
||||
],
|
||||
!useTypescript && ['@babel/preset-flow', { all: true }],
|
||||
].filter(Boolean),
|
||||
plugins: [
|
||||
[
|
||||
require.resolve('@babel/plugin-transform-react-jsx'),
|
||||
{
|
||||
pragma: options.jsx || 'h',
|
||||
pragmaFrag: options.jsxFragment || 'Fragment',
|
||||
},
|
||||
],
|
||||
[
|
||||
require.resolve('babel-plugin-transform-replace-expressions'),
|
||||
{ replace: defines },
|
||||
],
|
||||
[
|
||||
require.resolve('babel-plugin-transform-async-to-promises'),
|
||||
{ inlineHelpers: true, externalHelpers: true },
|
||||
],
|
||||
[
|
||||
require.resolve('@babel/plugin-proposal-class-properties'),
|
||||
{ loose: true },
|
||||
],
|
||||
],
|
||||
custom: {
|
||||
defines,
|
||||
targets: options.target === 'node' ? { node: '8' } : undefined,
|
||||
pragma: options.jsx || 'h',
|
||||
pragmaFrag: options.jsxFragment || 'Fragment',
|
||||
typescript: !!useTypescript,
|
||||
},
|
||||
}),
|
||||
options.compress !== false && [
|
||||
terser({
|
||||
sourcemap: true,
|
||||
output: { comments: false },
|
||||
output: {
|
||||
comments: (node, comment) => /[@#]__PURE__/.test(comment.value),
|
||||
},
|
||||
compress: Object.assign(
|
||||
{
|
||||
keep_infinity: true,
|
||||
|
||||
138
src/lib/babel-custom.js
Normal file
138
src/lib/babel-custom.js
Normal file
@ -0,0 +1,138 @@
|
||||
import { createConfigItem } from '@babel/core';
|
||||
import babelPlugin from 'rollup-plugin-babel';
|
||||
import merge from 'lodash.merge';
|
||||
import { isTruthy } from '../utils';
|
||||
|
||||
const mergeConfigItems = (type, ...configItemsToMerge) => {
|
||||
const mergedItems = [];
|
||||
|
||||
configItemsToMerge.forEach(configItemToMerge => {
|
||||
configItemToMerge.forEach(item => {
|
||||
const itemToMergeWithIndex = mergedItems.findIndex(
|
||||
mergedItem => mergedItem.file.resolved === item.file.resolved,
|
||||
);
|
||||
|
||||
if (itemToMergeWithIndex === -1) {
|
||||
mergedItems.push(item);
|
||||
return;
|
||||
}
|
||||
|
||||
mergedItems[itemToMergeWithIndex] = createConfigItem(
|
||||
[
|
||||
mergedItems[itemToMergeWithIndex].file.resolved,
|
||||
merge(mergedItems[itemToMergeWithIndex].options, item.options),
|
||||
],
|
||||
{
|
||||
type,
|
||||
},
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
return mergedItems;
|
||||
};
|
||||
|
||||
const createConfigItems = (type, items) => {
|
||||
return items.map(({ name, ...options }) => {
|
||||
return createConfigItem([require.resolve(name), options], { type });
|
||||
});
|
||||
};
|
||||
|
||||
export default babelPlugin.custom(babelCore => {
|
||||
return {
|
||||
// Passed the plugin options.
|
||||
options({ custom: customOptions, ...pluginOptions }) {
|
||||
return {
|
||||
// Pull out any custom options that the plugin might have.
|
||||
customOptions,
|
||||
|
||||
// Pass the options back with the two custom options removed.
|
||||
pluginOptions,
|
||||
};
|
||||
},
|
||||
|
||||
config(config, { customOptions }) {
|
||||
const defaultPlugins = createConfigItems(
|
||||
'plugin',
|
||||
[
|
||||
{
|
||||
name: '@babel/plugin-transform-react-jsx',
|
||||
pragma: customOptions.jsx || 'h',
|
||||
pragmaFrag: customOptions.jsxFragment || 'Fragment',
|
||||
},
|
||||
isTruthy(customOptions.defines) && {
|
||||
name: 'babel-plugin-transform-replace-expressions',
|
||||
replace: customOptions.defines,
|
||||
},
|
||||
{
|
||||
name: 'babel-plugin-transform-async-to-promises',
|
||||
inlineHelpers: true,
|
||||
externalHelpers: true,
|
||||
},
|
||||
{
|
||||
name: '@babel/plugin-proposal-class-properties',
|
||||
loose: true,
|
||||
},
|
||||
{
|
||||
name: '@babel/plugin-transform-regenerator',
|
||||
async: false,
|
||||
},
|
||||
{
|
||||
name: 'babel-plugin-macros',
|
||||
},
|
||||
].filter(Boolean),
|
||||
);
|
||||
|
||||
const babelOptions = config.options || {};
|
||||
|
||||
const envIdx = (babelOptions.presets || []).findIndex(preset =>
|
||||
preset.file.request.includes('@babel/preset-env'),
|
||||
);
|
||||
|
||||
if (envIdx !== -1) {
|
||||
const preset = babelOptions.presets[envIdx];
|
||||
babelOptions.presets[envIdx] = createConfigItem(
|
||||
[
|
||||
preset.file.resolved,
|
||||
merge(
|
||||
{
|
||||
loose: true,
|
||||
targets: customOptions.targets,
|
||||
},
|
||||
preset.options,
|
||||
{
|
||||
modules: false,
|
||||
exclude: merge(
|
||||
['transform-async-to-generator', 'transform-regenerator'],
|
||||
preset.options.exclude || [],
|
||||
),
|
||||
},
|
||||
),
|
||||
],
|
||||
{
|
||||
type: `preset`,
|
||||
},
|
||||
);
|
||||
} else {
|
||||
babelOptions.presets = createConfigItems('preset', [
|
||||
{
|
||||
name: '@babel/preset-env',
|
||||
targets: customOptions.targets,
|
||||
modules: false,
|
||||
loose: true,
|
||||
exclude: ['transform-async-to-generator', 'transform-regenerator'],
|
||||
},
|
||||
]);
|
||||
}
|
||||
|
||||
// Merge babelrc & our plugins together
|
||||
babelOptions.plugins = mergeConfigItems(
|
||||
'plugin',
|
||||
defaultPlugins,
|
||||
babelOptions.plugins || [],
|
||||
);
|
||||
|
||||
return babelOptions;
|
||||
},
|
||||
};
|
||||
});
|
||||
@ -14,3 +14,11 @@ export const isFile = name =>
|
||||
.catch(() => false);
|
||||
export const stdout = console.log.bind(console); // eslint-disable-line no-console
|
||||
export const stderr = console.error.bind(console);
|
||||
|
||||
export const isTruthy = obj => {
|
||||
if (!obj) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return obj.constructor !== Object || Object.keys(obj).length > 0;
|
||||
};
|
||||
|
||||
File diff suppressed because one or more lines are too long
1
test/fixtures/basic-babelrc/src/index.js
vendored
1
test/fixtures/basic-babelrc/src/index.js
vendored
@ -3,3 +3,4 @@ function test(param = throw new Error('required!')) {
|
||||
}
|
||||
|
||||
test(true);
|
||||
test(false);
|
||||
|
||||
6
test/fixtures/basic-compress-false/package.json
vendored
Normal file
6
test/fixtures/basic-compress-false/package.json
vendored
Normal file
@ -0,0 +1,6 @@
|
||||
{
|
||||
"name": "basic-no-compress",
|
||||
"scripts": {
|
||||
"build": "microbundle --compress=false"
|
||||
}
|
||||
}
|
||||
5
test/fixtures/basic-compress-false/src/index.js
vendored
Normal file
5
test/fixtures/basic-compress-false/src/index.js
vendored
Normal file
@ -0,0 +1,5 @@
|
||||
import { two } from './two';
|
||||
|
||||
export default async function(...args) {
|
||||
return [await two(...args), await two(...args)];
|
||||
}
|
||||
3
test/fixtures/basic-compress-false/src/two.js
vendored
Normal file
3
test/fixtures/basic-compress-false/src/two.js
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
export async function two(...args) {
|
||||
return args.reduce((total, value) => total + value, 0);
|
||||
}
|
||||
6
test/fixtures/basic-no-compress/package.json
vendored
Normal file
6
test/fixtures/basic-no-compress/package.json
vendored
Normal file
@ -0,0 +1,6 @@
|
||||
{
|
||||
"name": "basic-no-compress",
|
||||
"scripts": {
|
||||
"build": "microbundle --no-compress"
|
||||
}
|
||||
}
|
||||
5
test/fixtures/basic-no-compress/src/index.js
vendored
Normal file
5
test/fixtures/basic-no-compress/src/index.js
vendored
Normal file
@ -0,0 +1,5 @@
|
||||
import { two } from './two';
|
||||
|
||||
export default async function(...args) {
|
||||
return [await two(...args), await two(...args)];
|
||||
}
|
||||
3
test/fixtures/basic-no-compress/src/two.js
vendored
Normal file
3
test/fixtures/basic-no-compress/src/two.js
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
export async function two(...args) {
|
||||
return args.reduce((total, value) => total + value, 0);
|
||||
}
|
||||
24
test/fixtures/custom-babelrc/.babelrc
vendored
Normal file
24
test/fixtures/custom-babelrc/.babelrc
vendored
Normal file
@ -0,0 +1,24 @@
|
||||
{
|
||||
"presets": [
|
||||
[
|
||||
"@babel/preset-env",
|
||||
{
|
||||
"loose": true,
|
||||
"modules": false,
|
||||
"useBuiltIns": "usage",
|
||||
"corejs": 3,
|
||||
"targets": {
|
||||
"esmodules": true
|
||||
}
|
||||
}
|
||||
]
|
||||
],
|
||||
"plugins": [
|
||||
[
|
||||
"@babel/plugin-proposal-class-properties",
|
||||
{
|
||||
"loose": false
|
||||
}
|
||||
]
|
||||
]
|
||||
}
|
||||
3
test/fixtures/custom-babelrc/package.json
vendored
Normal file
3
test/fixtures/custom-babelrc/package.json
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
{
|
||||
"name": "custom-babelrc"
|
||||
}
|
||||
6
test/fixtures/custom-babelrc/src/index.js
vendored
Normal file
6
test/fixtures/custom-babelrc/src/index.js
vendored
Normal file
@ -0,0 +1,6 @@
|
||||
export class MyClass {
|
||||
myFields = ['foo', 'bar'];
|
||||
async foo() {
|
||||
return this.myFields.find(item => item === 'bar');
|
||||
}
|
||||
}
|
||||
3
test/fixtures/macro/package.json
vendored
Normal file
3
test/fixtures/macro/package.json
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
{
|
||||
"name": "macro-lib"
|
||||
}
|
||||
5
test/fixtures/macro/src/index.js
vendored
Normal file
5
test/fixtures/macro/src/index.js
vendored
Normal file
@ -0,0 +1,5 @@
|
||||
import { macro } from './macro';
|
||||
|
||||
const name = macro();
|
||||
|
||||
export default name;
|
||||
20
test/fixtures/macro/src/macro.js
vendored
Normal file
20
test/fixtures/macro/src/macro.js
vendored
Normal file
@ -0,0 +1,20 @@
|
||||
import { createMacro } from 'babel-plugin-macros';
|
||||
|
||||
function myMacro({ references, state, babel }) {
|
||||
const { types: t } = babel;
|
||||
|
||||
references.macro.forEach(referencePath => {
|
||||
const parentPath = referencePath.findParent(t.isCallExpression);
|
||||
let variableName = referencePath.findParent(t.isVariableDeclarator).node.id
|
||||
.name;
|
||||
if (
|
||||
parentPath.node.arguments.length > 0 &&
|
||||
parentPath.node.arguments[0] !== ''
|
||||
) {
|
||||
variableName = parentPath.node.arguments[0].value;
|
||||
}
|
||||
parentPath.replaceWith(t.stringLiteral(`${variableName}-macro`));
|
||||
});
|
||||
}
|
||||
|
||||
module.exports = createMacro(myMacro);
|
||||
Loading…
x
Reference in New Issue
Block a user