Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add Rollup.js tree-shaking and minification
Uses `opichals/rollup-plugin-espruino-modules` * Add Config.ROLLUP which replaces `loadModules` The core/modules.js transformForEspruino directly use the `espruinoRollup.loadModulesRollup` instead of `loadModules` * Separately add TERSER minification option * Srap with UMD preamble for browser compat
- Loading branch information
Showing
11 changed files
with
269 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,4 +2,5 @@ | |
.settings | ||
*~ | ||
node_modules | ||
package-lock.json | ||
tmp |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export default () => () => undefined |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
const { writeFileSync, vol } = require('fs'); | ||
const rollup = require('rollup'); | ||
const espruinoModules = require('rollup-plugin-espruino-modules'); | ||
|
||
function bundle(options) { | ||
const opts = { ...options }; | ||
|
||
if (typeof vol !== 'undefined') { // only in browser (fs = memfs) | ||
vol.fromJSON({'/modules': null}); | ||
|
||
if (opts.modules) { | ||
try { | ||
opts.modules.forEach(([name, code]) => writeFileSync(name, code)); | ||
} catch (err) { | ||
console.error('Write file failed:', err); | ||
} | ||
delete opts.modules; | ||
} | ||
} | ||
|
||
const warnings = []; | ||
opts.onwarn = warning => (warnings.push( warning ), (options.onwarn && options.onwarn(warning))); | ||
|
||
const config = espruinoModules.buildRollupConfig(opts); | ||
|
||
return rollup.rollup(config).then(bundle => | ||
bundle.generate(config.output).then(generated => { | ||
generated.warnings = warnings; | ||
return generated; | ||
}) | ||
); | ||
} | ||
|
||
function minify(code, options) { | ||
return new Promise((resolve, reject) => { | ||
try { | ||
const minifyOptions = espruinoModules.buildMinifyConfig(options) | ||
const generated = espruinoModules.minify(code, minifyOptions); | ||
resolve(generated); | ||
} catch(e) { | ||
reject(e); | ||
} | ||
}); | ||
} | ||
|
||
module.exports = { | ||
bundle, | ||
minify | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
(function (root, factory) { | ||
'use strict'; | ||
|
||
if (typeof define === 'function' && define.amd) { | ||
define(['exports'], factory); | ||
} else if (typeof exports !== 'undefined') { | ||
factory(exports); | ||
} else { | ||
factory((root.rollupTools = {})); | ||
} | ||
}(this, function(exports) { | ||
|
||
// ========================================================= | ||
|
||
function loadModulesRollup(code, callback) { | ||
var board = Espruino.Core.Env.getBoardData(); | ||
var env = Espruino.Core.Env.getData(); | ||
var modules = []; | ||
|
||
var entryFilename = env.FILE; | ||
|
||
// the env.FILE is only present in the espruino-cli | ||
if (!entryFilename) { | ||
// the 'modules' contents is written the filesystem in the espruinoRollup() | ||
// for in-browser setup with filesystem simulation | ||
entryFilename = 'main.js'; | ||
modules.push([entryFilename, code]); | ||
} | ||
|
||
return espruinoRollup.bundle({ | ||
modules, | ||
input: entryFilename, | ||
output: { | ||
format: 'cjs' | ||
}, | ||
espruino: { | ||
job: Espruino.Config, | ||
|
||
board: board.BOARD, | ||
mergeModules: Espruino.Config.MODULE_MERGE, | ||
minify: !!Espruino.Config.MINIFICATION_LEVEL, | ||
minifyModules: !!Espruino.Config.MODULE_MINIFICATION_LEVEL | ||
|
||
// TODO: handle opts MINIFICATION_Xyz | ||
} | ||
}) | ||
} | ||
|
||
function minifyCodeTerser(code, callback) { | ||
return espruinoRollup.minify(code, { | ||
// TODO: handle opts MINIFICATION_Xyz | ||
}) | ||
} | ||
|
||
exports.loadModulesRollup = loadModulesRollup | ||
exports.minifyCodeTerser = minifyCodeTerser; | ||
|
||
})); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
{ | ||
"name": "espruino-rollup", | ||
"version": "0.1.0", | ||
"description": "Espruino rollup bundling pipeline", | ||
"main": "espruino-rollup.js", | ||
"scripts": { | ||
"build": "rollup -c" | ||
}, | ||
"keywords": [], | ||
"author": "Standa Opichal", | ||
"license": "MIT", | ||
"dependencies": { | ||
"memfs": "=2.12.1", | ||
"rollup": "^0.67.4", | ||
"rollup-plugin-espruino-modules": "opichals/rollup-plugin-espruino-modules" | ||
}, | ||
"devDependencies": { | ||
"rollup-plugin-alias": "^1.4.0", | ||
"rollup-plugin-commonjs": "^9.1.8", | ||
"rollup-plugin-json": "^3.1.0", | ||
"rollup-plugin-node-builtins": "^2.1.2", | ||
"rollup-plugin-node-globals": "^1.4.0", | ||
"rollup-plugin-node-resolve": "^3.4.0", | ||
"rollup-plugin-terser": "^3.0.0" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import json from 'rollup-plugin-json'; | ||
import alias from 'rollup-plugin-alias'; | ||
import resolve from 'rollup-plugin-node-resolve'; | ||
import builtins from 'rollup-plugin-node-builtins'; | ||
import globals from 'rollup-plugin-node-globals'; | ||
import commonjs from 'rollup-plugin-commonjs'; | ||
import { terser } from 'rollup-plugin-terser'; | ||
|
||
const buildPlugins = opts => [ | ||
json(), | ||
resolve({ | ||
preferBuiltins: true, | ||
...opts.resolve | ||
}), | ||
commonjs({ | ||
namedExports: { | ||
'node_modules/resolve/index.js': [ 'sync' ], | ||
'node_modules/async/dist/async.js': [ 'eachSeries' ], | ||
...opts.commonjs.namedExports | ||
} | ||
}), | ||
]; | ||
|
||
const config = { | ||
input : 'espruino-rollup.js', | ||
output : { | ||
file: 'espruino-rollup.browser.js', | ||
name: 'espruinoRollup', | ||
format: 'umd', | ||
}, | ||
plugins: [ | ||
alias({ | ||
fs: require.resolve('memfs'), | ||
debug: require.resolve('./debug-shim') | ||
}), | ||
...buildPlugins({ | ||
resolve: { | ||
browser: true, | ||
}, | ||
commonjs: { | ||
namedExports: { | ||
'node_modules/memfs/lib/index.js': [ | ||
'statSync', 'lstatSync', 'realpathSync', | ||
'mkdirSync', 'readdirSync', | ||
'readFileSync', | ||
'writeFile', 'writeFileSync', | ||
'watch', | ||
] | ||
} | ||
} | ||
}), | ||
builtins(), | ||
globals({ | ||
dirname: false | ||
}), | ||
terser(), | ||
] | ||
}; | ||
|
||
// console.log( config ); | ||
export default config; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters