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

fix: 解决babel插件path.hub 中无法获取文件名 #4495

Merged
merged 1 commit into from
Oct 21, 2019
Merged
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
44 changes: 44 additions & 0 deletions packages/taro-transformer-wx/__tests__/babel.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import transform from '../src';
import { buildComponent, baseCode, baseOptions } from './utils';
const options = require('../src/options');

// babel-traverse 无法生成 Hub
// 导致 Path#getSource|buildCodeFrameError 都无法直接使用
// 模拟 babel 插件
const pluginHelper = (result: any) => {
return () => {
return {
visitor: {
Program(path) {
result.hub = path.hub;
}
}
};
};
};

const injectPlugin = plugin => {
const buildBabelTransformOptions = options.buildBabelTransformOptions;

options.buildBabelTransformOptions = () => {
const opts = buildBabelTransformOptions();

opts.plugins.push(plugin);
return opts;
};
};

describe('babel plugin', () => {
test('path state', () => {
const result: any = {};
const plugin = pluginHelper(result);
injectPlugin(plugin);
const { code, ast, template } = transform({
...baseOptions,
sourcePath: __filename,
code: buildComponent(baseCode)
});

expect(result.hub.file.opts.filename).toBe(__filename);
});
});
1 change: 1 addition & 0 deletions packages/taro-transformer-wx/src/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ export const buildBabelTransformOptions: () => TransformOptions = () => {
plugins.push(buildVistor())
}
return {
filename: transformOptions.sourcePath,
parserOpts: {
sourceType: 'module',
plugins: [
Expand Down