├── test ├── fixtures │ ├── actual.js │ └── expected.js └── index.js ├── .gitignore ├── .npmignore ├── .editorconfig ├── src └── index.js ├── README.md └── package.json /test/fixtures/actual.js: -------------------------------------------------------------------------------- 1 | hiThere(); 2 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | /dist 3 | *.log 4 | -------------------------------------------------------------------------------- /.npmignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | /src 3 | *.log 4 | -------------------------------------------------------------------------------- /test/fixtures/expected.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | "use helloworld"; 4 | hiThere(); 5 | -------------------------------------------------------------------------------- /.editorconfig: -------------------------------------------------------------------------------- 1 | root = true 2 | 3 | [*] 4 | charset = utf-8 5 | trim_trailing_whitespace = true 6 | indent_style = tab 7 | indent_size = 4 8 | insert_final_newline = true 9 | 10 | [*.{json,babelrc,eslintrc}] 11 | indent_style = space 12 | indent_size = 2 13 | -------------------------------------------------------------------------------- /src/index.js: -------------------------------------------------------------------------------- 1 | import 'better-log/install'; 2 | 3 | module.exports = function ({ types: t }) { 4 | return { 5 | visitor: { 6 | Program(path, file) { 7 | path.unshiftContainer('body', t.expressionStatement(t.stringLiteral('use helloworld'))); 8 | } 9 | } 10 | }; 11 | }; 12 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # babel-plugin-hello-world 2 | 3 | Extended plugin sample for Babel. 4 | 5 | ## Installation 6 | 7 | ```sh 8 | $ npm install babel-plugin-hello-world 9 | ``` 10 | 11 | ## Usage 12 | 13 | ### Via `.babelrc` (Recommended) 14 | 15 | **.babelrc** 16 | 17 | ```json 18 | { 19 | "plugins": ["hello-world"] 20 | } 21 | ``` 22 | 23 | ### Via CLI 24 | 25 | ```sh 26 | $ babel --plugins hello-world script.js 27 | ``` 28 | 29 | ### Via Node API 30 | 31 | ```javascript 32 | require('babel').transform('code', { 33 | plugins: ['hello-world'] 34 | }); 35 | ``` 36 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "babel-plugin-hello-world", 3 | "version": "1.0.0", 4 | "description": "Extended plugin sample for Babel", 5 | "repository": "RReverser/babel-plugin-hello-world", 6 | "license": "MIT", 7 | "main": "dist/index.js", 8 | "dependencies": { 9 | "babel-runtime": "^5.6.17", 10 | "better-log": "^1.3.1" 11 | }, 12 | "devDependencies": { 13 | "babel": "^5.6.14", 14 | "chalk": "^1.1.0", 15 | "clear": "0.0.1", 16 | "diff": "^1.4.0", 17 | "watch": "^0.16.0" 18 | }, 19 | "scripts": { 20 | "release": "babel src --out-dir dist", 21 | "test": "node test", 22 | "watch": "node test --watch" 23 | }, 24 | "keywords": [ 25 | "babel-plugin", 26 | "sample", 27 | "example" 28 | ] 29 | } 30 | -------------------------------------------------------------------------------- /test/index.js: -------------------------------------------------------------------------------- 1 | var assert = require('assert'); 2 | var babel = require('babel'); 3 | var chalk = require('chalk'); 4 | var clear = require('clear'); 5 | var diff = require('diff'); 6 | var fs = require('fs'); 7 | 8 | require('babel/register'); 9 | 10 | var pluginPath = require.resolve('../src'); 11 | 12 | function runTest() { 13 | var output = babel.transformFileSync(__dirname + '/fixtures/actual.js', { 14 | optional: ['runtime'], 15 | plugins: [pluginPath] 16 | }); 17 | 18 | var expected = fs.readFileSync(__dirname + '/fixtures/expected.js', 'utf-8'); 19 | 20 | function normalizeLines(str) { 21 | return str.trimRight().replace(/\r\n/g, '\n'); 22 | } 23 | 24 | diff.diffLines(normalizeLines(output.code), normalizeLines(expected)) 25 | .forEach(function (part) { 26 | var value = part.value; 27 | if (part.added) { 28 | value = chalk.green(part.value); 29 | } else if (part.removed) { 30 | value = chalk.red(part.value); 31 | } 32 | process.stdout.write(value); 33 | }); 34 | 35 | console.log(); 36 | } 37 | 38 | if (process.argv.indexOf('--watch') >= 0) { 39 | require('watch').watchTree(__dirname + '/..', function () { 40 | delete require.cache[pluginPath]; 41 | clear(); 42 | console.log('Press Ctrl+C to stop watching...'); 43 | console.log('================================'); 44 | try { 45 | runTest(); 46 | } catch (e) { 47 | console.error(chalk.magenta(e.stack)); 48 | } 49 | }); 50 | } else { 51 | runTest(); 52 | } 53 | --------------------------------------------------------------------------------