├── .gitignore ├── .npmignore ├── test ├── fixtures │ └── basic │ │ ├── actual.js │ │ └── expected.js └── index.js ├── .babelrc ├── .editorconfig ├── src └── index.js ├── README.md └── package.json /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | /dist 3 | *.log 4 | -------------------------------------------------------------------------------- /.npmignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | /src 3 | *.log 4 | -------------------------------------------------------------------------------- /test/fixtures/basic/actual.js: -------------------------------------------------------------------------------- 1 | hiThere(); 2 | -------------------------------------------------------------------------------- /.babelrc: -------------------------------------------------------------------------------- 1 | { 2 | "presets": ["es2015"], 3 | "plugins": ["transform-runtime"], 4 | } 5 | -------------------------------------------------------------------------------- /test/fixtures/basic/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": "^6.3.19", 10 | "better-log": "^1.3.1" 11 | }, 12 | "devDependencies": { 13 | "babel-cli": "^6.3.17", 14 | "babel-core": "^6.3.21", 15 | "babel-plugin-transform-runtime": "^6.3.13", 16 | "babel-preset-es2015": "^6.3.13", 17 | "babel-register": "^6.3.13", 18 | "chalk": "^1.1.0", 19 | "clear": "0.0.1", 20 | "diff": "^1.4.0", 21 | "watch": "^0.16.0" 22 | }, 23 | "scripts": { 24 | "release": "babel src --out-dir dist", 25 | "test": "node test", 26 | "watch": "node test --watch", 27 | "prepublish": "npm test && npm run release" 28 | }, 29 | "keywords": [ 30 | "babel-plugin", 31 | "sample", 32 | "example" 33 | ] 34 | } 35 | -------------------------------------------------------------------------------- /test/index.js: -------------------------------------------------------------------------------- 1 | var assert = require('assert'); 2 | var babel = require('babel-core'); 3 | var chalk = require('chalk'); 4 | var clear = require('clear'); 5 | var diff = require('diff'); 6 | var fs = require('fs'); 7 | var path = require('path'); 8 | 9 | require('babel-register'); 10 | 11 | var pluginPath = require.resolve('../src'); 12 | 13 | function runTests() { 14 | var testsPath = __dirname + '/fixtures/'; 15 | 16 | fs.readdirSync(testsPath).map(function(item) { 17 | return { 18 | path: path.join(testsPath, item), 19 | name: item, 20 | }; 21 | }).filter(function(item) { 22 | return fs.statSync(item.path).isDirectory(); 23 | }).forEach(runTest); 24 | } 25 | 26 | function runTest(dir) { 27 | var output = babel.transformFileSync(dir.path + '/actual.js', { 28 | plugins: [pluginPath] 29 | }); 30 | 31 | var expected = fs.readFileSync(dir.path + '/expected.js', 'utf-8'); 32 | 33 | function normalizeLines(str) { 34 | return str.trimRight().replace(/\r\n/g, '\n'); 35 | } 36 | 37 | process.stdout.write(chalk.bgWhite.black(dir.name)); 38 | process.stdout.write('\n\n'); 39 | 40 | diff.diffLines(normalizeLines(output.code), normalizeLines(expected)) 41 | .forEach(function (part) { 42 | var value = part.value; 43 | if (part.added) { 44 | value = chalk.green(part.value); 45 | } else if (part.removed) { 46 | value = chalk.red(part.value); 47 | } 48 | 49 | 50 | process.stdout.write(value); 51 | }); 52 | 53 | process.stdout.write('\n\n\n'); 54 | } 55 | 56 | if (process.argv.indexOf('--watch') >= 0) { 57 | require('watch').watchTree(__dirname + '/..', function () { 58 | delete require.cache[pluginPath]; 59 | clear(); 60 | console.log('Press Ctrl+C to stop watching...'); 61 | console.log('================================'); 62 | try { 63 | runTests(); 64 | } catch (e) { 65 | console.error(chalk.magenta(e.stack)); 66 | } 67 | }); 68 | } else { 69 | runTests(); 70 | } 71 | --------------------------------------------------------------------------------