├── .babelrc ├── .eslintignore ├── .eslintrc ├── .gitignore ├── .npmignore ├── .travis.yml ├── LICENSE ├── README.md ├── package.json ├── src └── index.js └── test ├── .eslintrc ├── fixtures ├── basic-import │ ├── actual.js │ └── expected.js ├── chained-import │ ├── actual.js │ └── expected.js ├── dynamic-argument │ ├── actual.js │ └── expected.js ├── nested-import │ ├── actual.js │ └── expected.js └── non-string-argument │ ├── actual.js │ └── expected.js ├── index.js ├── mocha.opts └── testPlugin.js /.babelrc: -------------------------------------------------------------------------------- 1 | { 2 | "presets": [ 3 | "airbnb" 4 | ], 5 | } 6 | -------------------------------------------------------------------------------- /.eslintignore: -------------------------------------------------------------------------------- 1 | test/fixtures 2 | lib/ 3 | -------------------------------------------------------------------------------- /.eslintrc: -------------------------------------------------------------------------------- 1 | { 2 | "parser": "babel-eslint", 3 | "extends": "airbnb-base", 4 | "root": true 5 | } -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Logs 2 | logs 3 | *.log 4 | npm-debug.log* 5 | 6 | # Runtime data 7 | pids 8 | *.pid 9 | *.seed 10 | 11 | # Directory for instrumented libs generated by jscoverage/JSCover 12 | lib-cov 13 | 14 | # Coverage directory used by tools like istanbul 15 | coverage 16 | 17 | # nyc test coverage 18 | .nyc_output 19 | 20 | # Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) 21 | .grunt 22 | 23 | # node-waf configuration 24 | .lock-wscript 25 | 26 | # Compiled binary addons (http://nodejs.org/api/addons.html) 27 | build/Release 28 | 29 | # Dependency directories 30 | node_modules 31 | jspm_packages 32 | 33 | # Optional npm cache directory 34 | .npm 35 | 36 | # Optional REPL history 37 | .node_repl_history 38 | 39 | # Build library 40 | lib/ 41 | 42 | # Only apps should have lockfiles 43 | yarn.lock 44 | package-lock.json 45 | npm-shrinkwrap.json -------------------------------------------------------------------------------- /.npmignore: -------------------------------------------------------------------------------- 1 | src/ 2 | test/ 3 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | os: 3 | - linux 4 | node_js: 5 | - "9" 6 | - "8" 7 | - "7" 8 | - "6" 9 | - "5" 10 | - "4" 11 | - "iojs-v3" 12 | - "iojs-v2" 13 | - "iojs-v1" 14 | - "0.12" 15 | before_install: 16 | - 'nvm install-latest-npm' 17 | install: 18 | - 'if [ "${TRAVIS_NODE_VERSION}" = "0.6" ] || [ "${TRAVIS_NODE_VERSION}" = "0.9" ]; then nvm install --latest-npm 0.8 && npm install && nvm use "${TRAVIS_NODE_VERSION}"; else npm install; fi;' 19 | script: 20 | - 'if [ -n "${PRETEST-}" ]; then npm run pretest ; fi' 21 | - 'if [ -n "${POSTTEST-}" ]; then npm run posttest ; fi' 22 | - 'if [ -n "${COVERAGE-}" ]; then npm run coverage ; fi' 23 | - 'if [ -n "${TEST-}" ]; then npm run tests-only ; fi' 24 | sudo: false 25 | env: 26 | - TEST=true 27 | matrix: 28 | fast_finish: true 29 | include: 30 | - node_js: "lts/*" 31 | env: PRETEST=true 32 | allow_failures: 33 | - node_js: "9" 34 | - node_js: "7" 35 | - node_js: "5" 36 | - node_js: "iojs-v3" 37 | - node_js: "iojs-v2" 38 | - node_js: "iojs-v1" 39 | - node_js: "0.12" 40 | - os: osx 41 | - env: TEST=true ALLOW_FAILURE=true 42 | - env: COVERAGE=true -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2016 Airbnb 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # babel-plugin-dynamic-import-node-sync 2 | 3 | Babel 7 plugin to transpile async `import()` to sync a `require()`, for node. Matches the [proposed spec](https://github.com/domenic/proposal-import-function). 4 | 5 | I am using it for server-side rendering. 6 | 7 | ## Difference from babel-plugin-dynamic-import-node 8 | 9 | **babel-plugin-dynamic-import-node-sync** 10 | ``` 11 | import(SOURCE) => require(SOURCE) 12 | ``` 13 | 14 | **babel-plugin-dynamic-import-node** 15 | ``` 16 | import(SOURCE) => Promise.resolve().then(() => require(SOURCE)) 17 | ``` 18 | 19 | ## Installation 20 | 21 | ```sh 22 | $ npm install babel-plugin-dynamic-import-node-sync --save-dev 23 | ``` 24 | 25 | ## Usage 26 | 27 | ### Via `.babelrc` (Recommended) 28 | 29 | **.babelrc** 30 | 31 | ```json 32 | { 33 | "plugins": ["dynamic-import-node-sync"] 34 | } 35 | ``` 36 | 37 | ### Via CLI 38 | 39 | ```sh 40 | $ babel --plugins dynamic-import-node-sync script.js 41 | ``` -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "babel-plugin-dynamic-import-node-sync", 3 | "version": "2.0.1", 4 | "description": "Babel 7 plugin to transpile import() to a require(), for node", 5 | "main": "lib/index.js", 6 | "directories": { 7 | "test": "test" 8 | }, 9 | "scripts": { 10 | "clean": "rimraf lib", 11 | "prebuild": "npm run clean", 12 | "build": "babel src --out-dir lib", 13 | "prepare": "npm run build", 14 | "pretest": "npm run lint", 15 | "test": "npm run tests-only", 16 | "tests-only": "mocha", 17 | "lint": "eslint .", 18 | "check-changelog": "expr $(git status --porcelain 2>/dev/null| grep \"^\\s*M.*CHANGELOG.md\" | wc -l) >/dev/null || (echo 'Please edit CHANGELOG.md' && exit 1)", 19 | "check-only-changelog-changed": "(expr $(git status --porcelain 2>/dev/null| grep -v \"CHANGELOG.md\" | wc -l) >/dev/null && echo 'Only CHANGELOG.md may have uncommitted changes' && exit 1) || exit 0", 20 | "version:major": "npm --no-git-tag-version version major", 21 | "version:minor": "npm --no-git-tag-version version minor", 22 | "version:patch": "npm --no-git-tag-version version patch", 23 | "postversion": "git commit package.json CHANGELOG.md -m \"v$npm_package_version\" && npm run tag && git push && git push --tags", 24 | "preversion": "npm run test && npm run check-changelog && npm run check-only-changelog-changed", 25 | "tag": "git tag v$npm_package_version" 26 | }, 27 | "repository": { 28 | "type": "git", 29 | "url": "git+https://github.com/seeden/babel-plugin-dynamic-import-node-sync.git" 30 | }, 31 | "keywords": [ 32 | "babel", 33 | "plugin", 34 | "dynamic", 35 | "import", 36 | "node", 37 | "sync", 38 | "ssr", 39 | "react-router" 40 | ], 41 | "author": "Zlatko Fedor ", 42 | "license": "MIT", 43 | "bugs": { 44 | "url": "https://github.com/seeden/babel-plugin-dynamic-import-node-sync/issues" 45 | }, 46 | "homepage": "https://github.com/seeden/babel-plugin-dynamic-import-node-sync#readme", 47 | "devDependencies": { 48 | "@babel/core": "^7.0.0-beta.39", 49 | "airbnb-js-shims": "^1.4.1", 50 | "babel-cli": "^6.26.0", 51 | "babel-core": "^6.26.0", 52 | "babel-eslint": "^8.2.1", 53 | "babel-plugin-transform-es2015-template-literals": "^6.22.0", 54 | "babel-preset-airbnb": "^2.4.0", 55 | "babel-preset-es2015": "^6.24.1", 56 | "babel-register": "^6.26.0", 57 | "chai": "^4.1.2", 58 | "eslint": "^4.17.0", 59 | "eslint-config-airbnb-base": "^12.1.0", 60 | "eslint-plugin-import": "^2.8.0", 61 | "in-publish": "^2.0.0", 62 | "mocha": "^5.0.0", 63 | "rimraf": "^2.6.2", 64 | "safe-publish-latest": "^1.1.1" 65 | }, 66 | "dependencies": { 67 | "@babel/plugin-syntax-dynamic-import": "^7.0.0-beta.39" 68 | } 69 | } 70 | -------------------------------------------------------------------------------- /src/index.js: -------------------------------------------------------------------------------- 1 | import syntax from '@babel/plugin-syntax-dynamic-import'; 2 | 3 | export default function ({ template, types: t }) { 4 | const buildImport = template(` 5 | require(SOURCE) 6 | `); 7 | 8 | return { 9 | inherits: syntax, 10 | 11 | visitor: { 12 | Import(path) { 13 | const importArguments = path.parentPath.node.arguments; 14 | const isString = t.isStringLiteral(importArguments[0]) 15 | || t.isTemplateLiteral(importArguments[0]); 16 | if (isString) { 17 | t.removeComments(importArguments[0]); 18 | } 19 | const newImport = buildImport({ 20 | SOURCE: (isString) 21 | ? importArguments 22 | : t.templateLiteral([ 23 | t.templateElement({ raw: '', cooked: '' }), 24 | t.templateElement({ raw: '', cooked: '' }, true), 25 | ], importArguments), 26 | }); 27 | path.parentPath.replaceWith(newImport); 28 | }, 29 | }, 30 | }; 31 | } 32 | -------------------------------------------------------------------------------- /test/.eslintrc: -------------------------------------------------------------------------------- 1 | { 2 | "env": { 3 | "mocha": true, 4 | }, 5 | } 6 | -------------------------------------------------------------------------------- /test/fixtures/basic-import/actual.js: -------------------------------------------------------------------------------- 1 | const testModule = import('test-module'); 2 | -------------------------------------------------------------------------------- /test/fixtures/basic-import/expected.js: -------------------------------------------------------------------------------- 1 | const testModule = require('test-module'); 2 | -------------------------------------------------------------------------------- /test/fixtures/chained-import/actual.js: -------------------------------------------------------------------------------- 1 | import('test-module').then(() => ( 2 | import('test-module-2') 3 | )); 4 | 5 | Promise.all([ 6 | import('test-1'), 7 | import('test-2'), 8 | import('test-3'), 9 | ]).then(() => {}); 10 | -------------------------------------------------------------------------------- /test/fixtures/chained-import/expected.js: -------------------------------------------------------------------------------- 1 | require('test-module').then(() => require('test-module-2')); 2 | 3 | Promise.all([require('test-1'), require('test-2'), require('test-3')]).then(() => {}); 4 | -------------------------------------------------------------------------------- /test/fixtures/dynamic-argument/actual.js: -------------------------------------------------------------------------------- 1 | const MODULE = 'test-module'; 2 | 3 | import(MODULE); 4 | import(`test-${MODULE}`); 5 | -------------------------------------------------------------------------------- /test/fixtures/dynamic-argument/expected.js: -------------------------------------------------------------------------------- 1 | const MODULE = 'test-module'; 2 | 3 | require(`${MODULE}`); 4 | require(`test-${MODULE}`); 5 | -------------------------------------------------------------------------------- /test/fixtures/nested-import/actual.js: -------------------------------------------------------------------------------- 1 | function getModule(path) { 2 | return import('test-module'); 3 | } 4 | 5 | getModule().then(() => {}); 6 | -------------------------------------------------------------------------------- /test/fixtures/nested-import/expected.js: -------------------------------------------------------------------------------- 1 | function getModule(path) { 2 | return require('test-module'); 3 | } 4 | 5 | getModule().then(() => {}); 6 | -------------------------------------------------------------------------------- /test/fixtures/non-string-argument/actual.js: -------------------------------------------------------------------------------- 1 | import({ 'answer': 42 }); 2 | import(['foo', 'bar']); 3 | import(42); 4 | import(void 0); 5 | import(undefined); 6 | import(null); 7 | import(true); 8 | import(Symbol()); 9 | -------------------------------------------------------------------------------- /test/fixtures/non-string-argument/expected.js: -------------------------------------------------------------------------------- 1 | require(`${{ 'answer': 42 }}`); 2 | require(`${['foo', 'bar']}`); 3 | require(`${42}`); 4 | require(`${void 0}`); 5 | require(`${undefined}`); 6 | require(`${null}`); 7 | require(`${true}`); 8 | require(`${Symbol()}`); 9 | -------------------------------------------------------------------------------- /test/index.js: -------------------------------------------------------------------------------- 1 | import { expect } from 'chai'; 2 | import { join } from 'path'; 3 | import { readdirSync, statSync, readFileSync } from 'fs'; 4 | 5 | import testPlugin from './testPlugin'; 6 | 7 | const FIXTURE_PATH = join(__dirname, 'fixtures'); 8 | 9 | const testFolders = readdirSync(FIXTURE_PATH).filter(file => ( 10 | statSync(join(FIXTURE_PATH, file)).isDirectory() 11 | )); 12 | 13 | describe('babel-plugin-dynamic-import-node', () => { 14 | testFolders.forEach((folderName) => { 15 | const actual = readFileSync(join(FIXTURE_PATH, folderName, 'actual.js'), 'utf8'); 16 | const expected = readFileSync(join(FIXTURE_PATH, folderName, 'expected.js'), 'utf8'); 17 | it(`works with ${folderName}`, () => { 18 | const result = testPlugin(actual); 19 | expect(result.trim()).to.equal(expected.trim()); 20 | }); 21 | }); 22 | }); 23 | -------------------------------------------------------------------------------- /test/mocha.opts: -------------------------------------------------------------------------------- 1 | --require babel-register 2 | --require airbnb-js-shims 3 | -------------------------------------------------------------------------------- /test/testPlugin.js: -------------------------------------------------------------------------------- 1 | import { transform } from 'babel-core'; 2 | 3 | export default function testPlugin(code) { 4 | const result = transform(code, { 5 | plugins: ['./src/index.js'], 6 | }); 7 | 8 | return result.code; 9 | } 10 | --------------------------------------------------------------------------------