Skip to content

Commit

Permalink
Add development mode
Browse files Browse the repository at this point in the history
  • Loading branch information
mobily authored and radex committed Aug 30, 2018
1 parent 6be530f commit 7e995b1
Show file tree
Hide file tree
Showing 8 changed files with 145 additions and 93 deletions.
45 changes: 25 additions & 20 deletions .babelrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,30 +44,35 @@ const plugins = [
],
]

const importRedirect = [
'import-redirect',
{
redirect: {
'(adapters|decorators|utils|observation)(.+(?=\\/index\\.js)|.+(?=\\.js)|.+)': redirect(
'$1$2',
),
Collection$: redirect('Collection'),
CollectionMap$: redirect('CollectionMap'),
Database$: redirect('Database'),
Model$: redirect('Model'),
Query$: redirect('Query'),
QueryDescription$: redirect('QueryDescription'),
RawRecord$: redirect('RawRecord'),
Relation$: redirect('Relation'),
Schema$: redirect('Schema'),
},
suppressResolveWarning: true,
},
]

module.exports = {
env: {
development: {
plugins: [importRedirect, ...plugins],
},
production: {
plugins: [
[
'import-redirect',
{
redirect: {
'(adapters|decorators|utils|observation)(.+(?=\\/index\\.js)|.+(?=\\.js)|.+)': redirect(
'$1$2',
),
Collection$: redirect('Collection'),
CollectionMap$: redirect('CollectionMap'),
Database$: redirect('Database'),
Model$: redirect('Model'),
Query$: redirect('Query'),
QueryDescription$: redirect('QueryDescription'),
RawRecord$: redirect('RawRecord'),
Relation$: redirect('Relation'),
Schema$: redirect('Schema'),
},
suppressResolveWarning: true,
},
],
importRedirect,
...plugins,
// console.log is expensive for performance on native
// we don't want it on web either, but it's useful for development
Expand Down
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
node_modules/
dist/
flow-typed/
dev/
1 change: 1 addition & 0 deletions .flowconfig
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
<PROJECT_ROOT>/.cache
<PROJECT_ROOT>/__tests__
<PROJECT_ROOT>/dist
<PROJECT_ROOT>/dev

[include]

Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@ npm-debug.log
yarn-error.log

dist/
dev/

*.tgz
2 changes: 1 addition & 1 deletion jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,5 @@ module.exports = {
restoreMocks: true,
testMatch: ['**/__tests__/**/?(spec|test).js', '**/?(*.)(spec|test).js'],
moduleFileExtensions: ['js'],
modulePathIgnorePatterns: ['<rootDir>/dist'],
modulePathIgnorePatterns: ['<rootDir>/dist', '<rootDir>/dev'],
}
5 changes: 2 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@
"description": "Build powerful React Native and React web apps that scale from hundreds to tens of thousands of records and remain fast",
"version": "0.1.14",
"scripts": {
"clean": "shx rm -rf ./dist",
"make": "NODE_ENV=production node ./scripts/make.js",
"build": "NODE_ENV=production node ./scripts/make.js",
"dev": "NODE_ENV=development node ./scripts/make.js",
"release": "node ./scripts/publish.js",
"build": "npm-run-all clean make",
"flow": "flow check",
"eslint": "eslint ./src -c ./.eslintrc.yml --cache --cache-location ./.cache/.eslintcache",
"test": "jest --config=./jest.config.js --forceExit --detectOpenHandles --no-cache"
Expand Down
116 changes: 79 additions & 37 deletions scripts/make.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,14 @@ const {
mapAsync,
endsWith,
both,
includes,
prop,
all,
replace,
join,
reduce,
omit,
merge,
forEach,
tail,
} = require('rambdax')

const rollup = require('rollup')
Expand All @@ -24,33 +23,38 @@ const mkdirp = require('mkdirp')
const path = require('path')
const fs = require('fs-extra')
const prettyJson = require('json-stringify-pretty-compact')
const chokidar = require('chokidar')
const anymatch = require('anymatch')
const rimraf = require('rimraf')

const pkg = require('../package.json')
const rollupConfig = require('./rollup.config')
const createRollupConfig = require('./rollup.config')

const resolvePath = (...paths) => path.resolve(__dirname, '..', ...paths)
const isDevelopment = process.env.NODE_ENV === 'development'
const rollupConfig = createRollupConfig({ env: process.env.NODE_ENV })

const ESM_MODULES = 'esm'
const CJS_MODULES = 'cjs'

const SOURCE_PATH = resolvePath('src')
const DIST_PATH = resolvePath('dist')
const DEV_PATH = resolvePath('dev')

const DIR_PATH = isDevelopment ? DEV_PATH : DIST_PATH

const DO_NOT_BUILD_PATHS = [
'adapters/__tests__',
'test.js',
'type.js',
'integrationTest.js',
'__mocks__',
'Collection/RecordCache.js',
/adapters\/__tests__/,
/test\.js/,
/type\.js/,
/integrationTest\.js/,
/__mocks__/,
/Collection\/RecordCache\.js/,
]

const createModulePath = format => {
const modulePath = resolvePath(DIST_PATH, format)
return replace(SOURCE_PATH, modulePath)
}
const isNotIncludedInBuildPaths = value => !anymatch(DO_NOT_BUILD_PATHS, value)

const isNotIncludedInBuildPaths = value =>
all(buildPath => !includes(buildPath, value), DO_NOT_BUILD_PATHS)
const cleanFolder = dir => rimraf.sync(dir)

const takeFiles = pipe(
prop('path'),
Expand All @@ -63,7 +67,6 @@ const takeModules = pipe(
)

const removeSourcePath = replace(SOURCE_PATH, '')

const toStringKeyValue = module => `'${module.key}': '${module.value}'`
const indentLine = line => ` ${line},`
const toStringObject = pipe(
Expand All @@ -87,12 +90,20 @@ ${toStringObject(obj)}
}
`

const createModulePath = format => {
const modulePath = resolvePath(DIR_PATH, format)
return replace(SOURCE_PATH, modulePath)
}

const createPathName = file => {
const value = removeSourcePath(file)
return endsWith('index.js', value) ? path.dirname(value) : replace('.js', '', value)
}

const createModuleName = name => `${pkg.name}${name}`
const createModuleName = name => {
const module = tail(name)
return `${pkg.name}${module === '' ? module : `/${module}`}`
}

const buildPathMapping = format =>
pipe(
Expand All @@ -101,14 +112,14 @@ const buildPathMapping = format =>

return {
key: createModuleName(name),
value: `${pkg.name}/${format}${name}`,
value: `${isDevelopment ? DEV_PATH : pkg.name}/${format}${name}`,
}
}),
pathMappingTemplate,
content => {
try {
mkdirp.sync(resolvePath(DIST_PATH, format))
fs.writeFileSync(resolvePath(DIST_PATH, format, 'path-mapping.js'), content)
mkdirp.sync(resolvePath(DIR_PATH, format))
fs.writeFileSync(resolvePath(DIR_PATH, format, 'path-mapping.js'), content)
} catch (err) {
// eslint-disable-next-line
console.error(err)
Expand Down Expand Up @@ -158,25 +169,56 @@ const prepareJson = pipe(
obj => prettyJson(obj),
)

const createDistFolder = () => mkdirp.sync(resolvePath(DIST_PATH))
const createFolder = dir => mkdirp.sync(resolvePath(dir))

const createPackageJson = obj => {
const createPackageJson = (dir, obj) => {
const json = prepareJson(obj)
fs.writeFileSync(resolvePath(DIST_PATH, 'package.json'), json)
fs.writeFileSync(resolvePath(dir, 'package.json'), json)
}

const copyFilesToDistFolder = forEach(file =>
fs.copySync(resolvePath(file), resolvePath(DIST_PATH, file)),
)
const copyFiles = (dir, files) =>
forEach(file => fs.copySync(resolvePath(file), resolvePath(dir, file)), files)

if (isDevelopment) {
const buildCjsModule = buildModule(CJS_MODULES)
const buildEsmModule = buildModule(ESM_MODULES)

const buildModules = format => mapAsync(buildModule(format))
const buildCjsModules = buildModules(CJS_MODULES)
const buildEsmModules = buildModules(ESM_MODULES)

createDistFolder()
createPackageJson(pkg)
copyFilesToDistFolder(['LICENSE', 'README.md', 'yarn.lock', 'docs', 'src', 'native'])
buildCjsPathMapping(modules)
buildEsmPathMapping(modules)
buildEsmModules(modules)
buildCjsModules(modules)
const buildModules = file => {
buildCjsModule(file)
buildEsmModule(file)
}

cleanFolder(DEV_PATH)
createFolder(DEV_PATH)
buildCjsPathMapping(modules)
buildEsmPathMapping(modules)

chokidar
.watch(resolvePath('src'), { ignored: [...DO_NOT_BUILD_PATHS, /\.DS_Store/] })
.on('all', (event, fileOrDir) => {
// eslint-disable-next-line
switch (event) {
case 'add':
case 'change':
// eslint-disable-next-line
console.log(`✓ ${removeSourcePath(fileOrDir)}`)
buildModules(fileOrDir)
break
default:
break
}
})
} else {
const buildModules = format => mapAsync(buildModule(format))
const buildCjsModules = buildModules(CJS_MODULES)
const buildEsmModules = buildModules(ESM_MODULES)

cleanFolder(DIST_PATH)
createFolder(DIST_PATH)
createPackageJson(DIST_PATH, pkg)
copyFiles(DIST_PATH, ['LICENSE', 'README.md', 'yarn.lock', 'docs', 'src', 'native'])
buildCjsPathMapping(modules)
buildEsmPathMapping(modules)
buildEsmModules(modules)
buildCjsModules(modules)
}
67 changes: 35 additions & 32 deletions scripts/rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,37 +6,40 @@ const { terser } = require('rollup-plugin-terser')

const { rollupRx, rxExternalPaths } = require('./rollup.rx')

const env = process.env.NODE_ENV || 'development'
module.exports = options => {
const { env } = options
const isDevelopment = env === 'development'

module.exports = {
external: [
'lokijs',
'lokijs/src/loki-indexed-adapter',
'react-native',
'async',
'rxjs',
'rxjs/operators',
'rambdax',
'sql-escape-string',
...rxExternalPaths,
],
experimentalCodeSplitting: true,
treeshake: true,
plugins: [
rollupRx(),
resolve({
customResolveOptions: {
moduleDirectory: ['node_modules'],
},
}),
babel({
runtimeHelpers: true,
exclude: 'node_modules/**',
}),
commonjs(),
replace({
'process.env.NODE_ENV': JSON.stringify(env),
}),
terser(),
],
return {
external: [
'lokijs',
'lokijs/src/loki-indexed-adapter',
'react-native',
'async',
'rxjs',
'rxjs/operators',
'rambdax',
'sql-escape-string',
...rxExternalPaths,
],
experimentalCodeSplitting: true,
treeshake: true,
plugins: [
rollupRx(),
resolve({
customResolveOptions: {
moduleDirectory: ['node_modules'],
},
}),
babel({
runtimeHelpers: true,
exclude: 'node_modules/**',
}),
commonjs(),
replace({
'process.env.NODE_ENV': JSON.stringify(env),
}),
...(isDevelopment ? [] : [terser()]),
],
}
}

0 comments on commit 7e995b1

Please sign in to comment.