googlearchive / IMD

An AMD-compatible module registry for use with HTML Imports
73 stars 24 forks source link

Automatically add js extension for relative module IDs #7

Open antonmoiseev opened 9 years ago

antonmoiseev commented 9 years ago

What do you think about automatically adding .js extension while resolving relative module IDs? Relative URLs always represent a file so it should be safe to append it.

Motivation

We use TypeScript and ES6 modules for writing application code. For importing local modules we use relative paths:

import Logger from './lib/logger';

After transpiling it turns into an AMD module:

define(["require", "exports", './lib/logger'], callbackFn);

The module itself is loaded as part of an HTML Import:

<script src="lib/logger.js"></script>

And registered within the module registry with following name:

http://localhost:3000/lib/logger.js

However when IMD attempts to resolve the imported module it cannot be found:

The module "http://localhost:3000/lib/logger" has not been loaded

If we specify .js extension as part of the import statement it breaks the tooling since original file has .ts extension.

antonmoiseev commented 9 years ago

Here is how it could look like: https://github.com/Farata/IMD/commit/7751481d228829fde9cfc1ab544ef17c131bc74d

It could be a configurable option:

define.imd({defaultExtension: 'js'})
// or
imd.config({defaultExtension: 'js'})
justinfagnani commented 8 years ago

I generally use the "as" attribute on script tags to give it a short module name. For implicit names, I'd be more inclined to say that the extension should be stripped from the implicit name when registered, rather than adding one on lookup.