diff --git a/gulpfile.js b/gulpfile.js index 0f1fb132..f3ea772c 100644 --- a/gulpfile.js +++ b/gulpfile.js @@ -130,7 +130,7 @@ gulp.task('clean_test_cache_server', () => del(TEST_CACHE_SERVER)); */ gulp.task('build', ['lint', 'build_dist_client', 'build_dist_server', 'build_dist_style', 'build_dist_legacy_lib']); -gulp.task('build_dist_client', ['clean_dist_client', 'build_obj_client', 'build_obj_lib'], function () { +gulp.task('build_dist_client', ['clean_dist_client', 'build_obj_client', 'build_obj_lib', 'build_dist_legacy_lib'], function () { const root = './karen.js'; const ENTRY_POINT = path.resolve(OBJ_CLIENT, root); diff --git a/package.json b/package.json index ab42d885..05f9ea49 100644 --- a/package.json +++ b/package.json @@ -21,6 +21,7 @@ "build_production": "cross-env NODE_ENV=production npm run build", "build_development": "cross-env NODE_ENV=development npm run build", "build": "gulp build", + "rollup": "cross-env ENABLE_ROLLUP=1 npm run gulp -- build_dist_client", "test": "gulp test", "gulp": "gulp" }, @@ -78,6 +79,7 @@ "babel-cli": "^6.14.0", "babel-core": "^6.14.0", "babel-loader": "^6.2.5", + "babel-plugin-external-helpers": "^6.8.0", "babel-plugin-transform-es2015-modules-commonjs": "^6.14.0", "babel-plugin-transform-inline-environment-variables": "^6.8.0", "babel-plugin-transform-node-env-inline": "^6.8.0", @@ -101,6 +103,11 @@ "postcss-cli": "^2.6.0", "postcss-custom-properties": "^5.0.1", "postcss-import": "^8.1.2", + "rollup": "^0.35.14", + "rollup-plugin-babel": "^2.6.1", + "rollup-plugin-commonjs": "^5.0.3", + "rollup-plugin-node-globals": "^1.0.7", + "rollup-plugin-node-resolve": "^2.0.0", "stylelint": "^7.2.0", "tslint": "3.15.0-dev.0", "typescript": "2.1.0-dev.20160918", diff --git a/rollup.config.js b/rollup.config.js new file mode 100644 index 00000000..cee3582b --- /dev/null +++ b/rollup.config.js @@ -0,0 +1,100 @@ +/// + +'use strict'; + +const assert = require('assert'); +const path = require('path'); + +const nodeResolve = require('rollup-plugin-node-resolve'); +const commonjs = require('rollup-plugin-commonjs'); +const babel = require('rollup-plugin-babel'); +const nodeGlobals = require('rollup-plugin-node-globals'); + +const isRelease = process.env.NODE_ENV === 'production'; + +const KAREN_ENTRY_POINT = process.env.KAREN_ENTRY_POINT; +assert.ok(!!KAREN_ENTRY_POINT, 'not found process.env.KAREN_ENTRY_POINT'); + +const KAREN_CLIENT_DIST_DIR = process.env.KAREN_CLIENT_DIST_DIR; +assert.ok(!!KAREN_CLIENT_DIST_DIR, 'not found process.env.KAREN_CLIENT_DIST_DIR'); + +const babelPresets = [ +]; + +let babelPlugins = [ + // For our target browsers, we need not some transforms. + + // es2016 level + 'babel-plugin-transform-exponentiation-operator', + // es2017 level + 'babel-plugin-syntax-trailing-function-commas', + 'babel-plugin-transform-async-to-generator', + + // for React + 'syntax-jsx', + 'transform-react-jsx', + + // some utilitis + 'transform-inline-environment-variables', + 'transform-node-env-inline', + + // to remove duplicated helper methods inserted by babel. + 'external-helpers', +]; + +if (isRelease) { + babelPlugins = babelPlugins.concat([ + 'transform-react-constant-elements', + 'transform-react-inline-elements', + ]); +} +else { + babelPlugins = babelPlugins.concat([ + 'transform-react-jsx-source', + ]); +} + +// https://github.com/rollup/rollup/wiki/JavaScript-API +// https://github.com/rollup/rollup/wiki/Command-Line-Interface +module.exports = { + entry: KAREN_ENTRY_POINT, + dest: path.resolve(KAREN_CLIENT_DIST_DIR, 'karen.js'), + format: 'cjs', + exports: 'none', + useStrict: true, + sourceMap: true, + treeshake: true, + + plugins: [ + // https://github.com/rollup/rollup-plugin-node-resolve + nodeResolve({ + module: true, + main: true, + browser: true, // for browser + preferBuiltins: false, // linking for browser + + // rollup does not have 'extensions' option, + // so we need to specify this option at here to import jsx file. + extensions: ['.js', '.jsx'], + }), + + // https://github.com/rollup/rollup-plugin-commonjs + commonjs({ + include: 'node_modules/**', + ignoreGlobal: true, + namedExports: { + 'node_modules/option-t/src/index.js': ['Some', 'None', 'OptionBase'], // FIXME + }, + }), + + // https://github.com/rollup/rollup-plugin-babel + babel({ + exclude: 'node_modules/**', + presets: babelPresets, + plugins: babelPlugins, + }), + + // https://github.com/calvinmetcalf/rollup-plugin-node-builtins + nodeGlobals(), + ] +}; diff --git a/tools/build/script.js b/tools/build/script.js index 0a44f7d4..80aaba58 100644 --- a/tools/build/script.js +++ b/tools/build/script.js @@ -39,8 +39,19 @@ const { spawnChildProcess, assertReturnCode } = require('../spawn'); * @returns {NodeJS.ReadableStream} */ function runLinkerForClient(cwd, nodeModDir, entryPoint, distDir) { - const bin = path.resolve(nodeModDir, './.bin', getSuffixedCommandName('./webpack')); - const args = []; + let bin = ''; + let args = []; + if (!!process.env.ENABLE_ROLLUP) { + bin = path.resolve(nodeModDir, './.bin', getSuffixedCommandName('./rollup')); + args = [ + '-c', + path.resolve(cwd, 'rollup.config.js'), + ]; + } + else { + bin = path.resolve(nodeModDir, './.bin', getSuffixedCommandName('./webpack')); + } + const option = { cwd, stdio: 'inherit',