tc39 / proposal-dynamic-import

import() proposal for JavaScript
https://tc39.github.io/proposal-dynamic-import/
MIT License
1.87k stars 47 forks source link

Suggestion for README: when folder structures change often, it helps to have a dynamically evaluated import #51

Closed geoyws closed 6 years ago

geoyws commented 7 years ago

This feature will help a lot in terms of saving dev time.

domenic commented 7 years ago

I don't understand this suggestion. Perhaps a pull request would be clearer.

hollowdoor commented 7 years ago

@domenic I think he's suggesting that a use case for dyanimic import() is when a directory tree is modified a single variable can be set to the correct module ID; versus having to change all lines of import something from 'old/directory/tree' in every sibling module file.

The way that problem is currently solved is by using injection of imported module contents into other imported module members which is not so parsimonious.

domenic commented 7 years ago

I still don't understand and still suggest a pull request with e.g. example code or spec changes so we can judge more clearly.

ljharb commented 7 years ago

I think the suggestion is to add an example to the readme like the following to illustrate the dynamic nature of the import() operand:

// constants.js
export const COMPONENTS = '/path/to/components';
// module.js
import { COMPONENTS } from './constants';

export default function gimmeComponent(name) {
  return import(`${COMPONENTS}/name.js`);
}
hollowdoor commented 7 years ago

@ljharb Thanks. That's what I was trying to explain.

markcellus commented 7 years ago

So the idea is to show that template strings containing variables can be used when using import() ?

ljharb commented 7 years ago

@mkay581 i think the idea is specifically to convey that paths to modules can be stored in a constant, and used in import(), which gives you all the benefits that constantization normally does, but newly gives them for importing modules.

markcellus commented 7 years ago

Ah okay, I thought that this "constantization" was implied, given that import() operates like other function calls and does not have to appear at the top of the module file like legacy imports. But thanks for the explanation.

hollowdoor commented 7 years ago

@mkay581 It is implied. Though it might be of interest for persuading interested parties.

domenic commented 6 years ago

I think this is adequately illustrated by https://github.com/tc39/proposal-dynamic-import#example, so I'll close this.