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

feat: 新增 Node.js 的构建 API remax/build #1040

Merged
merged 1 commit into from
Jun 5, 2020
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
25 changes: 25 additions & 0 deletions docs/api/remax-build.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
---
title: remax/build
---

`remax/build` 下包含构建 Remax 应用的 Node.js API,方便把 Remax 集成到你自己的构建流程中。

_带有 `?` 的选项表示非必填项_

## buildApp(options)

构建 Remax 应用。

```javascript
const { buildApp } = require('remax/build');

buildApp({ target: 'ali', output: 'build' });
```

#### 参数

- `options` - `remax.config.js` 里支持的配置和命令行的参数

#### 返回值

webpack 的 compiler 对象。
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import * as fs from 'fs';
import * as path from 'path';
import { compilation } from 'webpack';
import { Options } from '@remax/types';
import { nativeComponents } from '@remax/macro';
import { slash } from '@remax/shared';
import { isPluginPath } from '../../../utils/nativeComponent';

const NATIVE_COMPONENT_OUTPUT_DIR = 'remaxVendors';

function getNativeComponentAssetOutputPath(sourcePath: string, options: Options) {
return (
NATIVE_COMPONENT_OUTPUT_DIR +
'/' +
slash(sourcePath)
.replace(slash(options.cwd) + '/', '')
.replace(slash(options.rootDir) + '/', '')
.replace(/@/g, '_')
.replace(/node_modules/g, 'npm')
);
}

export default function getUsingComponents(modules: string[], options: Options, compilation: compilation.Compilation) {
const components = Array.from(nativeComponents.values()).filter(component =>
component.importers.some(importer => modules.includes(importer))
);

return components.reduce<any>((config, component) => {
component.assets.forEach(asset => {
const assetPath = getNativeComponentAssetOutputPath(asset, options);
const source = fs.readFileSync(asset);

compilation.assets[assetPath] = {
source: () => source,
size: () => Buffer.byteLength(source),
};
});

if (isPluginPath(component.sourcePath)) {
config[component.id] = component.sourcePath;
} else {
const usingPath = '/' + getNativeComponentAssetOutputPath(component.sourcePath, options);
const ext = path.extname(usingPath);
config[component.id] = usingPath.replace(new RegExp(`\\${ext}$`), '');
}

return config;
}, {});
}
1 change: 1 addition & 0 deletions packages/remax/build.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { build as buildApp } from '@remax/cli/lib/build';
1 change: 1 addition & 0 deletions packages/remax/build.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports.buildApp = require('@remax/cli/lib/build').build;