Skip to content

Commit

Permalink
Improve React Native support without extra files
Browse files Browse the repository at this point in the history
  • Loading branch information
ai committed Mar 23, 2020
1 parent 6a3e497 commit 9d8b073
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 33 deletions.
37 changes: 13 additions & 24 deletions process-dir.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ let fs = require('fs')

let writeFile = promisify(fs.writeFile)
let readFile = promisify(fs.readFile)
let unlink = promisify(fs.unlink)

const NAME = /(^|\n)(let\s+|const\s+|var\s+)(\S+|{[^}]+})\s*=/m

Expand Down Expand Up @@ -106,23 +105,6 @@ async function replaceToESM (dir, file, source) {
await writeFile(join(dir, file), esm)
}

async function replaceToNative (dir, file, source) {
let cjs = replaceRequire(
source,
exported => exported,
(named, prefix, varType, name) => {
let path = getPath(file, named, 'cjs')
return `${ prefix }${ varType }${ name } = require(${ path })`
},
(nameless, prefix) => {
let path = getPath(file, nameless, 'cjs')
return `${ prefix }require(${ path })`
}
)
await writeFile(join(dir, file.replace(/\.native\.js$/, '.cjs$&')), cjs)
await unlink(join(dir, file))
}

async function replaceToCJS (dir, file, source) {
let cjs = replaceRequire(
source,
Expand All @@ -136,10 +118,7 @@ async function replaceToCJS (dir, file, source) {
return `${ prefix }require(${ path })`
}
)
await Promise.all([
writeFile(join(dir, file.replace(/\.js$/, '.cjs')), cjs),
writeFile(join(dir, file.replace(/\.js$/, '.cjs.js')), cjs)
])
await writeFile(join(dir, file.replace(/\.js$/, '.cjs')), cjs)
}

async function replacePackage (dir, file, files) {
Expand All @@ -151,6 +130,8 @@ async function replacePackage (dir, file, files) {
pkg.type = 'module'
pkg.main = 'index.cjs'
pkg.module = 'index.js'
pkg['react-native'] = 'index.js'

if (file === 'index.js') {
pkg.exports = { }
for (let i of files) {
Expand All @@ -166,11 +147,19 @@ async function replacePackage (dir, file, files) {
pkg.exports[path].browser = path + '/index.browser.js'
}
}
} else if (files.includes(file.replace(/\.js$/, '.browser.js'))) {
}

if (files.includes(file.replace(/\.js$/, '.browser.js'))) {
pkg.browser = {
'./index.js': './index.browser.js'
}
}
if (files.includes(file.replace(/\.js$/, '.native.js'))) {
pkg['react-native'] = {
'./index.js': './index.native.js'
}
}

await writeFile(pkgFile, JSON.stringify(pkg, null, 2))
}

Expand Down Expand Up @@ -209,7 +198,7 @@ async function process (dir) {
if (file.endsWith('index.browser.js')) {
await replaceToESM(dir, file, source)
} else if (file.endsWith('index.native.js')) {
await replaceToNative(dir, file, source)
await replaceToESM(dir, file, source)
} else {
await Promise.all([
replaceToCJS(dir, file, source),
Expand Down
9 changes: 9 additions & 0 deletions test/fixtures/rn-lib/a/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
let { b } = require('../b')
require('../c')

let a = () => {
b()
console.log('cjs a')
}

module.exports = { a }
4 changes: 2 additions & 2 deletions test/fixtures/rn-lib/a/index.native.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
let b = require('../b')
let { b } = require('../b')
require('../c')

a = () => {
let a = () => {
b()
console.log('native a')
}
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/rn-lib/b/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
b = () => {
let b = () => {
console.log('cjs b')
}

Expand Down
14 changes: 8 additions & 6 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,9 +160,8 @@ it('compiles for React Native', async () => {
await replaceConsole(lib)
await exec(`yarn add rn-lib@${ lib }`, { cwd: runner })

let cfg = await metro.loadConfig()
cfg = {
...cfg,
let config = {
...await metro.loadConfig(),
projectRoot: runner,
watchFolders: [
runner,
Expand All @@ -171,18 +170,21 @@ it('compiles for React Native', async () => {
reporter: { update: () => {} },
cacheStores: [],
resetCache: true,
resolver: {
resolverMainFields: ['react-native', 'browser', 'main']
},
transformer: {
babelTransformerPath: 'metro-react-native-babel-transformer'
}
}
let { code } = await metro.runBuild(cfg, {
let { code } = await metro.runBuild(config, {
entry: 'index.js',
minify: false,
sourceMap: false
})
expect(code).toContain('console.log(\'native a\')')
expect(code).toContain('console.log(\'cjs b\')')
expect(code).toContain('console.log(\'cjs c\')')
expect(code).toContain('console.log(\'esm b\')')
expect(code).toContain('console.log(\'esm c\')')
})

it('works with require in webpack', async () => {
Expand Down

0 comments on commit 9d8b073

Please sign in to comment.