Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Customizable Babel Configuration for Modules #246

Closed
Closed
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
7 changes: 7 additions & 0 deletions src/__tests__/module.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -172,3 +172,10 @@ it('has __dirname available', () => {

expect(mod.exports).toBe(path.dirname(mod.filename));
});

it('can alterate the babel configuration', () => {
const mod = new Module(path.resolve(__dirname, '../__fixtures__/test.js'));
const initialBabelConfig = mod.$$babelConfig;
mod.$$babelConfig = {};
expect(mod.$$babelConfig).not.toEqual(initialBabelConfig);
});
5 changes: 3 additions & 2 deletions src/babel/evaluate.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ const resolve = (path, t, requirements) => {
module.exports = function evaluate(
path /* : any */,
t /* : any */,
filename /* : string */
filename /* : string */,
module /* : ?Module */ = null
Copy link
Member

Choose a reason for hiding this comment

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

Is there a reason you want to customize the module instance? I was thinking that you just add an argument for a custom transform function here, and then do something like m.transform = transform: https://github.com/callstack/linaria/blob/master/src/babel/evaluate.js#L150

Copy link
Contributor Author

Choose a reason for hiding this comment

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

because otherwise I cannot patch transform when i use evaluate.js - you create an instance of Module inside of the function

Copy link
Contributor Author

Choose a reason for hiding this comment

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

but yeah i could pass a transform function instead

) {
const requirements = [];

Expand Down Expand Up @@ -144,7 +145,7 @@ module.exports = function evaluate(
t.blockStatement([expression])
);

const m = new Module(filename);
const m = module || new Module(filename);

m.evaluate(
dedent`
Expand Down
31 changes: 17 additions & 14 deletions src/babel/module.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ class Module {
exports: any;

sourceMap: any;
$$babelConfig: Object;
*/

constructor(filename /* : string */) {
Expand All @@ -54,6 +55,21 @@ class Module {
},
});

this.$$babelConfig = {
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 }],
],
sourceMaps: true,
exclude: /node_modules/,
};

this.exports = {};
this.require = this.require.bind(this);
this.require.resolve = this.resolve.bind(this);
Expand Down Expand Up @@ -109,20 +125,7 @@ class Module {

evaluate(text /* : string */) {
// For JavaScript files, we need to transpile it and to get the exports of the module
const { code, map } = 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 }],
],
sourceMaps: true,
exclude: /node_modules/,
});
const { code, map } = babel.transformSync(text, this.$$babelConfig);

this.sourceMap = map;

Expand Down