You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

63 lines
1.1 KiB

4 years ago
  1. # babel-plugin-dynamic-import-node
  2. Babel plugin to transpile `import()` to a deferred `require()`, for node. Matches the [proposed spec](https://github.com/domenic/proposal-import-function).
  3. **NOTE:** Babylon >= v6.12.0 is required to correctly parse dynamic imports.
  4. ## Installation
  5. ```sh
  6. npm install babel-plugin-dynamic-import-node --save-dev
  7. ```
  8. ## Usage
  9. ### Via `.babelrc` (Recommended)
  10. **.babelrc**
  11. ```json
  12. {
  13. "plugins": ["dynamic-import-node"]
  14. }
  15. ```
  16. #### Options
  17. - *`noInterop`* - A boolean value, that if true will not interop the require calls. Useful to avoid using `require('module').default` on commonjs modules.
  18. ```json
  19. {
  20. "plugins": [
  21. ["dynamic-import-node", { "noInterop": true }]
  22. ]
  23. }
  24. ```
  25. ### Via CLI
  26. ```sh
  27. $ babel --plugins dynamic-import-node script.js
  28. ```
  29. ### Via Node API
  30. ```javascript
  31. require('babel-core').transform('code', {
  32. plugins: ['dynamic-import-node']
  33. });
  34. ```
  35. ### Code Example
  36. ```javascript
  37. Promise.all([
  38. import('./lib/import1'),
  39. import('./lib/import2')
  40. ]).then(([
  41. Import1,
  42. Import2
  43. ]) => {
  44. console.log(Import1);
  45. /* CODE HERE*/
  46. });
  47. ```