Skip to content
This repository has been archived by the owner on Mar 7, 2024. It is now read-only.

Commit

Permalink
fix: 修复自定义组件中 js 文件 resolve 问题
Browse files Browse the repository at this point in the history
  • Loading branch information
Darmody committed Mar 18, 2020
1 parent 37b6ba4 commit ef2dc43
Show file tree
Hide file tree
Showing 14 changed files with 48 additions and 11 deletions.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict';

var _fmtEvent = _interopRequireDefault(require('./fmtEvent'));
var moduleA= require('module-a');

function _interopRequireDefault(obj) {
return obj && obj.__esModule ? obj : { default: obj };
Expand All @@ -9,6 +10,7 @@ function _interopRequireDefault(obj) {
Component({
data: {},
props: {
moduleA: moduleA,
className: '',
style: '',
onClick: function onClick(e) {},
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = 'module-a';
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict';

var _fmtEvent = _interopRequireDefault(require('./fmtEvent'));
var moduleA= require('module-a');

function _interopRequireDefault(obj) {
return obj && obj.__esModule ? obj : { default: obj };
Expand All @@ -9,6 +10,7 @@ function _interopRequireDefault(obj) {
Component({
data: {},
props: {
moduleA: moduleA,
className: '',
style: '',
onClick: function onClick(e) {},
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = 'module-a';

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion packages/remax-cli/src/__tests__/lerna.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,7 @@ import runTest from './helpers/runTest';

describe('lerna app', () => {
const cwd = path.resolve(__dirname, 'fixtures/lerna/packages/app1/expected');
runTest('lerna/packages/app1', 'alipay', cwd, { include: ['npm/cjs'] });
runTest('lerna/packages/app1', 'alipay', cwd, {
include: ['npm/cjs', 'npm/module-a'],
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,7 @@ describe('use native components in alipay app', () => {
__dirname,
'../fixtures/nativeComponent/expected/alipay'
);
runTest('nativeComponent', 'alipay', cwd, { include: ['npm/cjs'] });
runTest('nativeComponent', 'alipay', cwd, {
include: ['npm/cjs', 'npm/module-a'],
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export default (options: RemaxOptions, pages: string[]): Plugin => {
name: 'nativeComponents',
load(id) {
if (isNativeComponent(id)) {
jsModule(id);
jsModule(options, id);
jsHelper(id);
style(id);
json(id);
Expand Down
27 changes: 19 additions & 8 deletions packages/remax-cli/src/build/plugins/nativeComponents/modules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,20 @@ import * as fs from 'fs';
import * as babelParser from '@babel/parser';
import traverse from '@babel/traverse';
import { get } from 'lodash';
import resolve from 'resolve';
import { getPath, pushArray, readFile } from './util';
import { RemaxOptions } from 'remax-types';

const modules: string[] = [];

const walk = (jsPath: string) => {
const walk = (jsPath: string, options: RemaxOptions) => {
const jsContent = readFile(jsPath);
const ast = babelParser.parse(jsContent, {
sourceType: 'module',
});

const extract = ({ node }: any) => {
const importPath =
let importPath =
(get(node, 'callee.name') === 'require'
? get(node, 'arguments[0].value')
: '') || get(node, 'source.value');
Expand All @@ -22,9 +24,18 @@ const walk = (jsPath: string) => {
return;
}

try {
// 尝试 resolve,失败跳过
importPath = resolve.sync(importPath, { basedir: options.cwd });
} catch {
// ignore
}

const absoluteId = getPath(jsPath, importPath);

let absolutePath = absoluteId + '.js';
let absolutePath = /.js$/.test(absoluteId)
? absoluteId
: absoluteId + '.js';

if (!fs.existsSync(absolutePath)) {
absolutePath = absoluteId + '/index.js';
Expand All @@ -36,7 +47,7 @@ const walk = (jsPath: string) => {

pushArray(modules, absolutePath);

walk(absolutePath);
walk(absolutePath, options);
};

traverse(ast, {
Expand All @@ -45,15 +56,15 @@ const walk = (jsPath: string) => {
});
};

const parseTemplate = (filePath: string) => {
walk(filePath);
const parseTemplate = (filePath: string, options: RemaxOptions) => {
walk(filePath, options);
pushArray(modules, filePath);
};

export default function jsModule(id: string) {
export default function jsModule(options: RemaxOptions, id: string) {
const templatePath = id;

parseTemplate(templatePath);
parseTemplate(templatePath, options);
}

export const getJsModules = () => modules;

0 comments on commit ef2dc43

Please sign in to comment.