Skip to content

Commit fcec1bb

Browse files
giuseppegsatya164
authored andcommitted
feat: make Module.transform configurable from evaluate (#247)
Replacement of #246
1 parent 4d220b1 commit fcec1bb

File tree

1 file changed

+19
-16
lines changed

1 file changed

+19
-16
lines changed

src/babel/evaluate.js

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,22 @@ const generator = require('@babel/generator').default;
55
const babel = require('@babel/core');
66
const Module = require('./module');
77

8+
function defaultTransformModule(text) {
9+
return babel.transformSync(text, {
10+
filename: this.filename,
11+
plugins: [
12+
// Include this plugin to avoid extra config when using { module: false } for webpack
13+
'@babel/plugin-transform-modules-commonjs',
14+
'@babel/plugin-proposal-export-namespace-from',
15+
// We don't support dynamic imports when evaluating, but don't wanna syntax error
16+
// This will replace dynamic imports with an object that does nothing
17+
require.resolve('./dynamic-import-noop'),
18+
[require.resolve('./extract'), { evaluate: true }],
19+
],
20+
exclude: /node_modules/,
21+
});
22+
}
23+
824
const resolve = (path, t, requirements) => {
925
const binding = path.scope.getBinding(path.node.name);
1026

@@ -58,7 +74,8 @@ const resolve = (path, t, requirements) => {
5874
module.exports = function evaluate(
5975
path /* : any */,
6076
t /* : any */,
61-
filename /* : string */
77+
filename /* : string */,
78+
transformModule /* : (text: string) => { code: string } */ = defaultTransformModule
6279
) {
6380
const requirements = [];
6481

@@ -147,21 +164,7 @@ module.exports = function evaluate(
147164

148165
const m = new Module(filename);
149166

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

166169
m.evaluate(
167170
dedent`

0 commit comments

Comments
 (0)