@tarojs/rn-runner 3.5.0-beta.3 → 3.5.0-beta.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.eslintrc.js +2 -1
- package/__tests__/__snapshots__/components.spec.ts.snap +523 -0
- package/__tests__/components.spec.ts +103 -0
- package/__tests__/global.d.ts +11 -0
- package/__tests__/mock/components_testdata.ts +58 -0
- package/__tests__/mock/config/index.js +1 -0
- package/__tests__/mock/src/app.config.ts +13 -0
- package/__tests__/mock/src/app.scss +0 -0
- package/__tests__/mock/src/app.tsx +15 -0
- package/__tests__/mock/src/components/cell/index.scss +25 -0
- package/__tests__/mock/src/components/cell/index.tsx +38 -0
- package/__tests__/mock/src/components/navbar/icon_back.webp +0 -0
- package/__tests__/mock/src/components/navbar/index.scss +29 -0
- package/__tests__/mock/src/components/navbar/index.tsx +36 -0
- package/__tests__/mock/src/components/navbar/resolver.rn.ts +1 -0
- package/__tests__/mock/src/components/navbar/resolver.ts +1 -0
- package/__tests__/mock/src/components/svg/index.tsx +6 -0
- package/__tests__/mock/src/components/svg/rollup-logo.svg +3 -0
- package/__tests__/mock/src/utils/dynamicImport/index.ts +3 -0
- package/__tests__/mock/src/utils/namedExport/index.js +3 -0
- package/__tests__/mock/src/utils/requireLodash/index.ts +5 -0
- package/__tests__/mock/src/utils/requireReactNative/index.js +3 -0
- package/__tests__/tsconfig.json +8 -0
- package/dist/config/build-component.js +162 -0
- package/dist/config/build-component.js.map +1 -0
- package/dist/index.js +7 -3
- package/dist/index.js.map +1 -1
- package/package.json +15 -7
- package/src/config/build-component.ts +171 -0
- package/src/config/index.ts +11 -11
- package/src/index.ts +14 -3
- package/tsconfig.json +1 -1
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tarojs/rn-runner",
|
|
3
|
-
"version": "3.5.0-beta.
|
|
3
|
+
"version": "3.5.0-beta.4",
|
|
4
4
|
"description": "ReactNative build tool for taro",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"repository": {
|
|
@@ -32,10 +32,16 @@
|
|
|
32
32
|
"dependencies": {
|
|
33
33
|
"@react-native-community/cli-plugin-metro": "^7.0.0",
|
|
34
34
|
"@react-native-community/cli-server-api": "^7.0.0",
|
|
35
|
-
"@
|
|
36
|
-
"@
|
|
37
|
-
"@
|
|
38
|
-
"@
|
|
35
|
+
"@rollup/plugin-babel": "^5.3.1",
|
|
36
|
+
"@rollup/plugin-commonjs": "^20.0.0",
|
|
37
|
+
"@rollup/plugin-json": "^4.1.0",
|
|
38
|
+
"@rollup/plugin-node-resolve": "^13.3.0",
|
|
39
|
+
"@rollup/plugin-replace": "^4.0.0",
|
|
40
|
+
"@tarojs/helper": "3.5.0-beta.4",
|
|
41
|
+
"@tarojs/rn-style-transformer": "3.5.0-beta.4",
|
|
42
|
+
"@tarojs/rn-supporter": "3.5.0-beta.4",
|
|
43
|
+
"@tarojs/rn-transformer": "3.5.0-beta.4",
|
|
44
|
+
"acorn-jsx": "^5.3.2",
|
|
39
45
|
"fs-extra": "^8.0.1",
|
|
40
46
|
"lodash": "^4.17.21",
|
|
41
47
|
"metro": "^0.67.0",
|
|
@@ -45,7 +51,9 @@
|
|
|
45
51
|
"metro-react-native-babel-transformer": "^0.67.0",
|
|
46
52
|
"metro-resolver": "^0.67.0",
|
|
47
53
|
"mime-types": "^2.1.27",
|
|
48
|
-
"qrcode-terminal": "^0.12.0"
|
|
54
|
+
"qrcode-terminal": "^0.12.0",
|
|
55
|
+
"rollup-plugin-clear": "^2.0.7",
|
|
56
|
+
"rollup-plugin-image-file": "^1.0.2"
|
|
49
57
|
},
|
|
50
|
-
"gitHead": "
|
|
58
|
+
"gitHead": "7aca7841ed7e2af1b182ff5d24e91f44730ce783"
|
|
51
59
|
}
|
|
@@ -0,0 +1,171 @@
|
|
|
1
|
+
import babel from '@rollup/plugin-babel'
|
|
2
|
+
import * as commonjs from '@rollup/plugin-commonjs'
|
|
3
|
+
import * as json from '@rollup/plugin-json'
|
|
4
|
+
import nodeResolve from '@rollup/plugin-node-resolve'
|
|
5
|
+
import * as pluginReplace from '@rollup/plugin-replace'
|
|
6
|
+
import { recursiveMerge } from '@tarojs/helper'
|
|
7
|
+
import { rollupTransform as styleTransformer } from '@tarojs/rn-style-transformer'
|
|
8
|
+
import { resolveExtFile, rollupResolver as taroResolver } from '@tarojs/rn-supporter'
|
|
9
|
+
import { getAppConfig } from '@tarojs/rn-transformer'
|
|
10
|
+
import * as jsx from 'acorn-jsx'
|
|
11
|
+
import * as path from 'path'
|
|
12
|
+
import { rollup, RollupOptions } from 'rollup'
|
|
13
|
+
import * as clear from 'rollup-plugin-clear'
|
|
14
|
+
import image from 'rollup-plugin-image-file'
|
|
15
|
+
|
|
16
|
+
type ExternalFn = (arr: Array<string | RegExp>) => Array<string | RegExp>
|
|
17
|
+
|
|
18
|
+
interface IComponentConfig {
|
|
19
|
+
input: string[] | string
|
|
20
|
+
output?: string
|
|
21
|
+
sourceRootPath?: string
|
|
22
|
+
external?: Array<string | RegExp> | ExternalFn
|
|
23
|
+
externalResolve?: (importee, importer) => string | null | void
|
|
24
|
+
modifyRollupConfig?: (
|
|
25
|
+
config: RollupOptions,
|
|
26
|
+
innerplugins?: [typeof taroResolver, typeof styleTransformer]
|
|
27
|
+
) => RollupOptions
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
const DEFAULT_CONFIG: Pick<
|
|
31
|
+
IComponentConfig,
|
|
32
|
+
'external' | 'output' | 'externalResolve' | 'sourceRootPath' | 'modifyRollupConfig'
|
|
33
|
+
> = {
|
|
34
|
+
external: [/^react(\/.*)?$/, /^react-native(\/.*)?$/, /^@react-native/],
|
|
35
|
+
output: 'dist',
|
|
36
|
+
externalResolve: importee => (likeDependent(importee) ? importee : null),
|
|
37
|
+
sourceRootPath: process.cwd(),
|
|
38
|
+
modifyRollupConfig: config => config
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export const build = async (projectConfig, componentConfig: IComponentConfig) => {
|
|
42
|
+
const mergedConfig = recursiveMerge({ ...DEFAULT_CONFIG }, componentConfig)
|
|
43
|
+
const { input, external, output, externalResolve, sourceRootPath, modifyRollupConfig } = mergedConfig
|
|
44
|
+
|
|
45
|
+
const getInputOption = () => {
|
|
46
|
+
const components: string[] = Array.isArray(input) ? input : [input]
|
|
47
|
+
|
|
48
|
+
const inputOptions = components.reduce((pre, cur) => {
|
|
49
|
+
let absolutePath = cur
|
|
50
|
+
if (!path.isAbsolute(cur)) {
|
|
51
|
+
absolutePath = path.resolve(sourceRootPath, cur)
|
|
52
|
+
}
|
|
53
|
+
const realPath = resolveExtFile({ originModulePath: absolutePath }, absolutePath)
|
|
54
|
+
const relativePath = path
|
|
55
|
+
.relative(sourceRootPath, realPath)
|
|
56
|
+
.replace(/\.(js|ts|jsx|tsx)$/, '')
|
|
57
|
+
.replace(/\.{1,}\//g, '')
|
|
58
|
+
|
|
59
|
+
return {
|
|
60
|
+
...pre,
|
|
61
|
+
[relativePath]: realPath
|
|
62
|
+
}
|
|
63
|
+
}, {})
|
|
64
|
+
if (components.length === 1) {
|
|
65
|
+
return {
|
|
66
|
+
index: Object.values(inputOptions)[0]
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
return inputOptions
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
const getExternal = () => {
|
|
73
|
+
if (typeof external === 'function') {
|
|
74
|
+
return external(DEFAULT_CONFIG.external)
|
|
75
|
+
}
|
|
76
|
+
const _external = Array.isArray(external) ? external : [external]
|
|
77
|
+
return _external.filter(Boolean).map(item => {
|
|
78
|
+
if (((item as unknown) as RegExp).test) return item
|
|
79
|
+
const match = (item as string).match(/^\/(.+)\/$/)
|
|
80
|
+
return match ? new RegExp(match[1]) : item
|
|
81
|
+
})
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
const rollupOptions: RollupOptions = {
|
|
85
|
+
input: getInputOption(),
|
|
86
|
+
output: {
|
|
87
|
+
format: 'es',
|
|
88
|
+
dir: output
|
|
89
|
+
},
|
|
90
|
+
external: getExternal(),
|
|
91
|
+
// @ts-ignore react native 相关的一些库中可能包含 jsx 语法
|
|
92
|
+
acornInjectPlugins: [jsx()],
|
|
93
|
+
plugins: [
|
|
94
|
+
clear({ targets: [output] }),
|
|
95
|
+
// TODO: 使用 react-native-svg-transformer 处理
|
|
96
|
+
// @ts-ignore
|
|
97
|
+
image({
|
|
98
|
+
extensions: ['.jpg', '.jpeg', '.png', '.webp', '.gif', '.svg', '.svgx']
|
|
99
|
+
}),
|
|
100
|
+
// @ts-ignore
|
|
101
|
+
json(),
|
|
102
|
+
taroResolver({
|
|
103
|
+
externalResolve
|
|
104
|
+
}),
|
|
105
|
+
nodeResolve({
|
|
106
|
+
extensions: ['.mjs', '.js', '.json', '.node', '.ts', '.tsx']
|
|
107
|
+
}),
|
|
108
|
+
// @ts-ignore
|
|
109
|
+
pluginReplace({
|
|
110
|
+
'process.env.NODE_ENV': JSON.stringify('production')
|
|
111
|
+
}),
|
|
112
|
+
// @ts-ignore
|
|
113
|
+
commonjs(),
|
|
114
|
+
babel({
|
|
115
|
+
babelHelpers: 'runtime',
|
|
116
|
+
presets: [
|
|
117
|
+
[
|
|
118
|
+
'babel-preset-taro',
|
|
119
|
+
{
|
|
120
|
+
framework: 'react',
|
|
121
|
+
ts: true,
|
|
122
|
+
reactJsxRuntime: projectConfig.reactJsxRuntime || 'automatic',
|
|
123
|
+
disableImportExportTransform: true
|
|
124
|
+
}
|
|
125
|
+
]
|
|
126
|
+
],
|
|
127
|
+
extensions: ['js', 'ts', 'jsx', 'tsx']
|
|
128
|
+
}),
|
|
129
|
+
styleTransformer({ config: projectConfig })
|
|
130
|
+
]
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
const newRollupOptions = modifyRollupConfig(rollupOptions, {
|
|
134
|
+
taroResolver,
|
|
135
|
+
styleTransformer
|
|
136
|
+
})
|
|
137
|
+
const { output: outputOptions, ...inputOptions } = newRollupOptions
|
|
138
|
+
|
|
139
|
+
let bundle
|
|
140
|
+
try {
|
|
141
|
+
bundle = await rollup(inputOptions)
|
|
142
|
+
|
|
143
|
+
const result = await bundle.write(outputOptions)
|
|
144
|
+
return result
|
|
145
|
+
} catch (error) {
|
|
146
|
+
console.error(error)
|
|
147
|
+
}
|
|
148
|
+
if (bundle) {
|
|
149
|
+
await bundle.close()
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
function likeDependent (str: string) {
|
|
154
|
+
return !str.match(/^\.?\.\//) && !path.isAbsolute(str)
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
export default async function (projectPath: string, config: any) {
|
|
158
|
+
const { sourceRoot, entry, nativeComponents } = config
|
|
159
|
+
const appPath = path.join(projectPath, sourceRoot, entry)
|
|
160
|
+
const appConfig = getAppConfig(appPath)
|
|
161
|
+
const { output = DEFAULT_CONFIG.output } = nativeComponents || {}
|
|
162
|
+
|
|
163
|
+
const componentConfig = {
|
|
164
|
+
...nativeComponents,
|
|
165
|
+
input: appConfig?.components,
|
|
166
|
+
output: path.join(projectPath, output),
|
|
167
|
+
sourceRootPath: path.join(projectPath, sourceRoot)
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
return build(config, componentConfig)
|
|
171
|
+
}
|
package/src/config/index.ts
CHANGED
|
@@ -14,21 +14,21 @@ type GetModulesRunBeforeMainModuleFunc = () => any
|
|
|
14
14
|
type GetPolyfillsFunc = () => any
|
|
15
15
|
interface MetroConfig {
|
|
16
16
|
transformer: {
|
|
17
|
-
dynamicDepsInPackages: string
|
|
18
|
-
babelTransformerPath: string
|
|
19
|
-
assetRegistryPath: string
|
|
20
|
-
}
|
|
17
|
+
dynamicDepsInPackages: string
|
|
18
|
+
babelTransformerPath: string
|
|
19
|
+
assetRegistryPath: string
|
|
20
|
+
}
|
|
21
21
|
resolver: {
|
|
22
|
-
sourceExts: string[]
|
|
23
|
-
resolveRequest?: ResolveRequestFunc
|
|
24
|
-
}
|
|
22
|
+
sourceExts: string[]
|
|
23
|
+
resolveRequest?: ResolveRequestFunc
|
|
24
|
+
}
|
|
25
25
|
serializer: {
|
|
26
|
-
getModulesRunBeforeMainModule: GetModulesRunBeforeMainModuleFunc
|
|
26
|
+
getModulesRunBeforeMainModule: GetModulesRunBeforeMainModuleFunc
|
|
27
27
|
getPolyfills: GetPolyfillsFunc
|
|
28
|
-
}
|
|
29
|
-
cacheStores: ConditionalFileStore<any>[]
|
|
28
|
+
}
|
|
29
|
+
cacheStores: ConditionalFileStore<any>[]
|
|
30
30
|
server: {
|
|
31
|
-
port: number
|
|
31
|
+
port: number
|
|
32
32
|
}
|
|
33
33
|
}
|
|
34
34
|
|
package/src/index.ts
CHANGED
|
@@ -12,6 +12,7 @@ import * as readline from 'readline'
|
|
|
12
12
|
import * as url from 'url'
|
|
13
13
|
|
|
14
14
|
import getMetroConfig from './config'
|
|
15
|
+
import buildComponent from './config/build-component'
|
|
15
16
|
import { getRNConfigEntry } from './config/config-holder'
|
|
16
17
|
import preview from './config/preview'
|
|
17
18
|
import { TerminalReporter } from './config/terminal-reporter'
|
|
@@ -108,7 +109,12 @@ export default async function build (_appPath: string, config: any): Promise<any
|
|
|
108
109
|
if (error instanceof Error) throw error
|
|
109
110
|
}
|
|
110
111
|
|
|
111
|
-
if (config.
|
|
112
|
+
if (config.isBuildNativeComp) {
|
|
113
|
+
return buildComponent(
|
|
114
|
+
_appPath,
|
|
115
|
+
config
|
|
116
|
+
)
|
|
117
|
+
} else if (config.isWatch) {
|
|
112
118
|
if (!metroConfig.server || (metroConfig.server.useGlobalHotkey === undefined)) {
|
|
113
119
|
if (!metroConfig.server) {
|
|
114
120
|
metroConfig.server = {}
|
|
@@ -119,7 +125,11 @@ export default async function build (_appPath: string, config: any): Promise<any
|
|
|
119
125
|
metroConfig.server.port = config.port
|
|
120
126
|
}
|
|
121
127
|
|
|
122
|
-
const {
|
|
128
|
+
const {
|
|
129
|
+
middleware,
|
|
130
|
+
messageSocketEndpoint,
|
|
131
|
+
websocketEndpoints
|
|
132
|
+
} = createDevServerMiddleware({
|
|
123
133
|
port: metroConfig.server.port,
|
|
124
134
|
watchFolders: metroConfig.watchFolders
|
|
125
135
|
})
|
|
@@ -144,7 +154,8 @@ export default async function build (_appPath: string, config: any): Promise<any
|
|
|
144
154
|
// 支持host
|
|
145
155
|
return Metro.runServer(metroConfig, {
|
|
146
156
|
...commonOptions,
|
|
147
|
-
hmrEnabled: true
|
|
157
|
+
hmrEnabled: true,
|
|
158
|
+
websocketEndpoints
|
|
148
159
|
}).then(server => {
|
|
149
160
|
console.log(`React-Native Dev server is running on port: ${metroConfig.server.port}`)
|
|
150
161
|
console.log('\n\nTo reload the app press "r"\nTo open developer menu press "d"\n')
|