Skip to content

Commit

Permalink
feat: make Module.transform configurable from evaluate (#247)
Browse files Browse the repository at this point in the history
Replacement of #246
  • Loading branch information
giuseppeg authored and satya164 committed Oct 12, 2018
1 parent 4d220b1 commit fcec1bb
Showing 1 changed file with 19 additions and 16 deletions.
35 changes: 19 additions & 16 deletions src/babel/evaluate.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,22 @@ const generator = require('@babel/generator').default;
const babel = require('@babel/core');
const Module = require('./module');

function defaultTransformModule(text) {
return babel.transformSync(text, {
filename: this.filename,
plugins: [
// Include this plugin to avoid extra config when using { module: false } for webpack
'@babel/plugin-transform-modules-commonjs',
'@babel/plugin-proposal-export-namespace-from',
// We don't support dynamic imports when evaluating, but don't wanna syntax error
// This will replace dynamic imports with an object that does nothing
require.resolve('./dynamic-import-noop'),
[require.resolve('./extract'), { evaluate: true }],
],
exclude: /node_modules/,
});
}

const resolve = (path, t, requirements) => {
const binding = path.scope.getBinding(path.node.name);

Expand Down Expand Up @@ -58,7 +74,8 @@ const resolve = (path, t, requirements) => {
module.exports = function evaluate(
path /* : any */,
t /* : any */,
filename /* : string */
filename /* : string */,
transformModule /* : (text: string) => { code: string } */ = defaultTransformModule
) {
const requirements = [];

Expand Down Expand Up @@ -147,21 +164,7 @@ module.exports = function evaluate(

const m = new Module(filename);

m.transform = function transform(text) {
return babel.transformSync(text, {
filename: this.filename,
plugins: [
// Include this plugin to avoid extra config when using { module: false } for webpack
'@babel/plugin-transform-modules-commonjs',
'@babel/plugin-proposal-export-namespace-from',
// We don't support dynamic imports when evaluating, but don't wanna syntax error
// This will replace dynamic imports with an object that does nothing
require.resolve('./dynamic-import-noop'),
[require.resolve('./extract'), { evaluate: true }],
],
exclude: /node_modules/,
});
};
m.transform = transformModule;

m.evaluate(
dedent`
Expand Down

0 comments on commit fcec1bb

Please sign in to comment.