From d839f38f87e7d266d48dd26ec0d9b501353f71cc Mon Sep 17 00:00:00 2001 From: insane Date: Fri, 20 Sep 2019 11:29:22 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E8=A7=A3=E5=86=B3babel=E6=8F=92?= =?UTF-8?q?=E4=BB=B6path.hub=20=E4=B8=AD=E6=97=A0=E6=B3=95=E8=8E=B7?= =?UTF-8?q?=E5=8F=96=E6=96=87=E4=BB=B6=E5=90=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../__tests__/babel.spec.ts | 44 +++++++++++++++++++ packages/taro-transformer-wx/src/options.ts | 1 + 2 files changed, 45 insertions(+) create mode 100644 packages/taro-transformer-wx/__tests__/babel.spec.ts diff --git a/packages/taro-transformer-wx/__tests__/babel.spec.ts b/packages/taro-transformer-wx/__tests__/babel.spec.ts new file mode 100644 index 000000000000..e5751cf81125 --- /dev/null +++ b/packages/taro-transformer-wx/__tests__/babel.spec.ts @@ -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); + }); +}); diff --git a/packages/taro-transformer-wx/src/options.ts b/packages/taro-transformer-wx/src/options.ts index 6da2202ceda3..ad691b66ce4b 100644 --- a/packages/taro-transformer-wx/src/options.ts +++ b/packages/taro-transformer-wx/src/options.ts @@ -38,6 +38,7 @@ export const buildBabelTransformOptions: () => TransformOptions = () => { plugins.push(buildVistor()) } return { + filename: transformOptions.sourcePath, parserOpts: { sourceType: 'module', plugins: [