Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions babel-plugins/add-module-export/src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
"use strict";

module.exports = function({ types: t }) {
return {
visitor: {
Program(path, state) {
const id = path.scope.generateUidIdentifier('uid')
const constNumDecl = t.variableDeclaration('const', [
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jridgewell is this the right way to add export someModuleUniqueId = 42;? would appreciate advice

Copy link

@jridgewell jridgewell Oct 4, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Almost. Drop the t.exportSpecifier:

export default function (babel) {
  const { types: t } = babel;
  
  return {
    visitor: {
      Program(path) {
        const uid = path.scope.generateUidIdentifier('uid');
        const declar = t.variableDeclaration('const', [
          t.variableDeclarator(uid, t.numericLiteral(42)),
        ]);
        
        const ex = t.exportNamedDeclaration(declar, []);
        path.unshiftContainer("body", ex);
      }
    }
  };
}

http://astexplorer.net/#/gist/9d4d2830b0b2b476627117b6a7798929/e97ac0bf49dd086154e1fd3c3657d90bf330d55c

t.variableDeclarator(
t.identifier(id.name),
t.numericLiteral(42)
)
]);
const exported = t.identifier(id.name);
const local = exported;
path.pushContainer('body', [
t.exportNamedDeclaration(constNumDecl, [
t.exportSpecifier(local, exported)])
])
}
}
};
};
3 changes: 3 additions & 0 deletions splittable.js
Original file line number Diff line number Diff line change
Expand Up @@ -228,9 +228,11 @@ exports.getGraph = function(entryModules, config) {
// We register 2 separate transforms. The initial stage are
// transforms that closure compiler does not support.
.transform(babel, {
global: true,
babelrc: !!config.babel.babelrc,
plugins: config.babel.plugins || [
require.resolve("babel-plugin-transform-react-jsx"),
require.resolve("./babel-plugins/add-module-export/src"),
],
presets: config.babel.presets,
}).
Expand All @@ -240,6 +242,7 @@ exports.getGraph = function(entryModules, config) {
babelrc: false,
plugins: [
require.resolve("babel-plugin-transform-es2015-modules-commonjs"),
'transform-remove-strict-mode',
]
});

Expand Down