Skip to content

Commit

Permalink
If defaults + named exports are detected, only export default and pru…
Browse files Browse the repository at this point in the history
…ne named. Enable pure properties for tree-shaking (since they're already enabled for uglify)
  • Loading branch information
developit committed Jan 14, 2018
1 parent d52bd6c commit 8960114
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 5 deletions.
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -51,10 +51,10 @@
"rollup-plugin-flow": "^1.1.1",
"rollup-plugin-node-resolve": "^3.0.2",
"rollup-plugin-nodent": "^0.1.3",
"rollup-plugin-post-replace": "^1.0.0",
"rollup-plugin-postcss": "^1.1.0",
"rollup-plugin-preserve-shebang": "^0.1.4",
"rollup-plugin-sizes": "^0.4.2",
"rollup-plugin-strict-alias": "^1.0.0",
"rollup-plugin-uglify": "^2.0.1",
"sade": "^1.3.1",
"uglify-es": "^3.3.6"
Expand Down
26 changes: 22 additions & 4 deletions src/index.js
Expand Up @@ -12,8 +12,7 @@ import nodeResolve from 'rollup-plugin-node-resolve';
import buble from 'rollup-plugin-buble';
import uglify from 'rollup-plugin-uglify';
import postcss from 'rollup-plugin-postcss';
// import replace from 'rollup-plugin-post-replace';
import es3 from 'rollup-plugin-es3';
import alias from 'rollup-plugin-strict-alias';
import gzipSize from 'gzip-size';
import prettyBytes from 'pretty-bytes';
import shebangPlugin from 'rollup-plugin-preserve-shebang';
Expand Down Expand Up @@ -176,11 +175,25 @@ function createConfig(options, entry, format, writeMeta) {
let nameCache = {};
let mangleOptions = options.pkg.mangle || false;

let exportType;
if (format!='es') {
try {
let file = fs.readFileSync(entry, 'utf-8');
let hasDefault = /\bexport\s*default\s*[a-zA-Z_$]/.test(file);
let hasNamed = /\bexport\s*(let|const|var|async|function\*?)\s*[a-zA-Z_$*]/.test(file) || /^\s*export\s*\{/m.test(file);
if (hasDefault && hasNamed) exportType = 'default';
}
catch (e) {}
}

let config = {
inputOptions: {
input: entry,
input: exportType ? resolve(__dirname, '../src/lib/__entry__.js') : entry,
external,
plugins: [].concat(
alias({
__microbundle_entry__: entry
}),
postcss({
plugins: [
autoprefixer()
Expand Down Expand Up @@ -220,7 +233,6 @@ function createConfig(options, entry, format, writeMeta) {
jsnext: true,
browser: options.target!=='node'
}),
es3(),
// We should upstream this to rollup
// format==='cjs' && replace({
// [`module.exports = ${rollupName};`]: '',
Expand Down Expand Up @@ -269,10 +281,16 @@ function createConfig(options, entry, format, writeMeta) {
},

outputOptions: {
exports: exportType ? 'default' : undefined,
paths: aliases,
globals,
strict: options.strict===true,
legacy: true,
freeze: false,
sourcemap: true,
treeshake: {
propertyReadSideEffects: false

This comment has been minimized.

Copy link
@texastoland

texastoland Aug 20, 2018

Contributor

@developit what's going on here?

This comment has been minimized.

Copy link
@Andarist

Andarist Aug 21, 2018

Collaborator

treeshake.propertyReadSideEffects true/false (default: true). If false, assume reading a property of an object never has side-effects. Depending on your code, disabling this option can significantly reduce bundle size but can potentially break functionality if you rely on getters or errors from illegal property access.

This comment has been minimized.

Copy link
@texastoland

texastoland Aug 21, 2018

Contributor

Sorry that was a super ambiguous question. I meant how does hiding named exports facilitate propertyReadSideEffects? I'm wondering if the indirection is really necessary for #193. I guess I could remove it and compare bundle size for Preact? I'll add my question to the linked PR.

},
format,
name: options.name || pkg.amdName || safeVariableName(pkg.name),
file: resolve(options.cwd, (format==='es' && moduleMain) || (format==='umd' && umdMain) || cjsMain)
Expand Down
1 change: 1 addition & 0 deletions src/lib/__entry__.js
@@ -0,0 +1 @@
export { default } from '__microbundle_entry__';

1 comment on commit 8960114

@egoist
Copy link

@egoist egoist commented on 8960114 Jan 15, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Brilliant.

Please sign in to comment.