esperantojs / esperanto

DEPRECATED: An easier way to convert ES6 modules to AMD and CommonJS
http://esperantojs.org
234 stars 21 forks source link

Add `transformPath` option #107

Open eventualbuddha opened 9 years ago

eventualbuddha commented 9 years ago

This can already be done by using the second argument to transform, reading the new path yourself, and returning the source from the new file. Having this option be separate is more efficient and separates the concerns nicely.

This issue is here partly so I don't forget to implement it and partly to ask for feedback on the idea.

Rich-Harris commented 9 years ago

What's the difference between this and resolvePath?

eventualbuddha commented 9 years ago

This would get called for all paths, just as transform is, not only those that can't be found. This would probably supercede #32, though that is the more general of the two.

eventualbuddha commented 9 years ago

Here's my work in progress test case for it:

diff --git a/test/bundle/input/37/_config.js b/test/bundle/input/37/_config.js
new file mode 100644
index 0000000..2c79818
--- /dev/null
+++ b/test/bundle/input/37/_config.js
@@ -0,0 +1,16 @@
+var path = require( 'path' );
+var sander = require( 'sander' );
+
+module.exports = {
+   description: 'All module paths can be transformed with transformPath option',
+   transformPath: function ( importee, importer ) {
+       var browserPath = path.join( path.dirname( importee ), path.basename( importee, '.js' ) + '.browser.js' );
+       return sander.exists( browserPath ).then( function ( exists ) {
+           if ( exists ) {
+               return browserPath;
+           } else {
+               return importee;
+           }
+       });
+   }
+};
\ No newline at end of file
diff --git a/test/bundle/input/37/main.js b/test/bundle/input/37/main.js
new file mode 100644
index 0000000..0495f06
--- /dev/null
+++ b/test/bundle/input/37/main.js
@@ -0,0 +1,2 @@
+import getUserAgent from './ua';
+console.log(getUserAgent());
diff --git a/test/bundle/input/37/ua.browser.js b/test/bundle/input/37/ua.browser.js
new file mode 100644
index 0000000..9425cb6
--- /dev/null
+++ b/test/bundle/input/37/ua.browser.js
@@ -0,0 +1,3 @@
+export default function getUserAgent() {
+   return navigator.userAgent;
+}
\ No newline at end of file
diff --git a/test/bundle/input/37/ua.js b/test/bundle/input/37/ua.js
new file mode 100644
index 0000000..e634d99
--- /dev/null
+++ b/test/bundle/input/37/ua.js
@@ -0,0 +1,3 @@
+export default function getUserAgent() {
+   return `node ${process.version}`;
+}