From d0652b1086132490f89456569ed7f9b3d47f448e Mon Sep 17 00:00:00 2001 From: coderaiser Date: Tue, 18 Jun 2019 19:58:15 +0300 Subject: [PATCH] feature(putout) add bundle dist/putout.js --- packages/putout/.babelrc | 5 ++ packages/putout/.gitignore | 1 + packages/putout/.webpack/index.js | 5 ++ packages/putout/.webpack/module.js | 7 +++ packages/putout/.webpack/require-all.js | 18 ++++++ packages/putout/.webpack/webpack.config.js | 65 ++++++++++++++++++++++ packages/putout/lib/get-plugins/index.js | 6 ++ packages/putout/madrun.js | 4 ++ packages/putout/package.json | 18 +++++- 9 files changed, 126 insertions(+), 3 deletions(-) create mode 100644 packages/putout/.babelrc create mode 100644 packages/putout/.gitignore create mode 100644 packages/putout/.webpack/index.js create mode 100644 packages/putout/.webpack/module.js create mode 100644 packages/putout/.webpack/require-all.js create mode 100644 packages/putout/.webpack/webpack.config.js diff --git a/packages/putout/.babelrc b/packages/putout/.babelrc new file mode 100644 index 000000000..6d586aa01 --- /dev/null +++ b/packages/putout/.babelrc @@ -0,0 +1,5 @@ +{ + "plugins": [ + "module:babel-plugin-macros", + ] +} diff --git a/packages/putout/.gitignore b/packages/putout/.gitignore new file mode 100644 index 000000000..1521c8b76 --- /dev/null +++ b/packages/putout/.gitignore @@ -0,0 +1 @@ +dist diff --git a/packages/putout/.webpack/index.js b/packages/putout/.webpack/index.js new file mode 100644 index 000000000..8464efb19 --- /dev/null +++ b/packages/putout/.webpack/index.js @@ -0,0 +1,5 @@ +'use strict'; + +require('module'); +module.exports = require('../lib/putout'); + diff --git a/packages/putout/.webpack/module.js b/packages/putout/.webpack/module.js new file mode 100644 index 000000000..0e231c6cc --- /dev/null +++ b/packages/putout/.webpack/module.js @@ -0,0 +1,7 @@ +'use strict'; + +module.exports._findPath = (a) => a; + +const codegen = require('codegen.macro'); +module.exports.plugins = codegen`module.exports = require('./require-all')();`; + diff --git a/packages/putout/.webpack/require-all.js b/packages/putout/.webpack/require-all.js new file mode 100644 index 000000000..cb694bbcc --- /dev/null +++ b/packages/putout/.webpack/require-all.js @@ -0,0 +1,18 @@ +'use strict'; + +const {plugins} = require('../putout.json'); + +module.exports = () => { + const result = plugins + .map((a) => `plugins['${a}'] = require('@putout/plugin-${a}');`) + .join('\n'); + + return ` + const plugins = {}; + + ${result}; + + return plugins; +` +}; + diff --git a/packages/putout/.webpack/webpack.config.js b/packages/putout/.webpack/webpack.config.js new file mode 100644 index 000000000..8cab192e6 --- /dev/null +++ b/packages/putout/.webpack/webpack.config.js @@ -0,0 +1,65 @@ +'use strict'; + +const path = require('path'); +const webpack = require('webpack'); +const dir = './lib'; + +const {env} = process; +const isDev = env.NODE_ENV === 'development'; + +const dist = path.resolve(__dirname, '..', 'dist'); +const distDev = path.resolve(__dirname, '..', 'dist-dev'); +const devtool = isDev ? 'eval' : 'source-map'; + +const babelDev = { + plugins: [ + 'module:babel-plugin-macros', + ], +}; + +const rules = [{ + test: /\.js$/, + exclude: /node_modules/, + loader: 'babel-loader', + options: babelDev, +}]; + +module.exports = { + devtool, + entry: { + putout: path.join(__dirname, `./index.js`), + }, + output: { + library: 'putout', + filename: '[name].js', + path: isDev ? distDev : dist, + pathinfo: isDev, + libraryTarget: 'umd', + devtoolModuleFilenameTemplate, + }, + plugins: [ + new webpack.IgnorePlugin({ + checkResource(context) { + return /^esprima|fixture|^acorn|espree|tape|@putout\/test/.test(context); + }, + }), + ], + module: { + rules, + }, + resolve: { + alias: { + module: path.resolve(__dirname, 'module.js'), + } + }, + performance: { + maxEntrypointSize: 1024000, + maxAssetSize: 1024000, + }, +}; + +function devtoolModuleFilenameTemplate(info) { + const resource = info.absoluteResourcePath.replace(__dirname + path.sep, ''); + return `file://putout/${resource}`; +} + diff --git a/packages/putout/lib/get-plugins/index.js b/packages/putout/lib/get-plugins/index.js index 0b8642871..3b18e5424 100644 --- a/packages/putout/lib/get-plugins/index.js +++ b/packages/putout/lib/get-plugins/index.js @@ -113,6 +113,12 @@ function requirePlugin(name, fn) { userPlugin, ]; + if (Module.plugins && Module.plugins[name]) + return [ + name, + Module.plugins[name], + ]; + throw Error(`Plugin "putout-plugin-${name} could not be found!`); } diff --git a/packages/putout/madrun.js b/packages/putout/madrun.js index 661353f97..c20cc4000 100644 --- a/packages/putout/madrun.js +++ b/packages/putout/madrun.js @@ -8,6 +8,10 @@ const lintScripts = [ ]; module.exports = { + "prepublishOnly": () => run('build'), + 'build:base': () => 'webpack --config ./.webpack/webpack.config.js', + 'build': () => run('build:base', '--mode production'), + 'build:dev': () => run('build:base', '--mode development'), 'test': () => `tape 'test/*.js' 'lib/**/*.spec.js'`, 'watch:test': () => `nodemon -w lib -w test -x ${run('test')}`, 'lint:lib': () => `eslint lib`, diff --git a/packages/putout/package.json b/packages/putout/package.json index e0cb976ca..ad2cb76b4 100644 --- a/packages/putout/package.json +++ b/packages/putout/package.json @@ -13,12 +13,18 @@ "url": "git://github.com/coderaiser/putout.git" }, "scripts": { + "prepublishOnly": "madrun prepublishOnly", + "build:base": "madrun build:base", + "build": "madrun build", + "build:dev": "madrun build:dev", "test": "madrun test", "watch:test": "madrun watch:test", + "lint:lib": "madrun lint:lib", + "lint:test": "madrun lint:test", + "lint:bin": "madrun lint:bin", "lint": "madrun lint", "fix:lint": "madrun fix:lint", - "putout": "bin/putout.js bin lib test", - "putout:fix": "bin/putout.js --fix bin lib test", + "putout": "madrun putout", "coverage": "madrun coverage", "report": "madrun report" }, @@ -86,10 +92,14 @@ "unused" ], "devDependencies": { + "@babel/core": "^7.4.5", "@cloudcmd/stub": "^2.3.1", "acorn": "^6.1.1", "acorn-jsx": "^5.0.1", "acorn-stage3": "^2.0.0", + "babel-loader": "^8.0.6", + "babel-plugin-macros": "^2.6.1", + "codegen.macro": "^3.0.0", "coveralls": "^3.0.0", "eslint": "^6.0.0-rc.0", "eslint-plugin-node": "^8.0.1", @@ -101,7 +111,9 @@ "mock-require": "^3.0.2", "nodemon": "^1.14.12", "nyc": "^14.0.0", - "supertape": "^1.0.3" + "supertape": "^1.0.3", + "webpack": "^4.34.0", + "webpack-cli": "^3.3.4" }, "license": "MIT", "engines": {