diff --git a/commands/build/lib/build.js b/commands/build/lib/build.js index 394fc00e8..d87808185 100644 --- a/commands/build/lib/build.js +++ b/commands/build/lib/build.js @@ -2,6 +2,7 @@ const path = require('path'); const chalk = require('chalk'); const readline = require('readline'); +const fs = require('fs'); const extend = require('extend'); const yargs = require('yargs'); const rollup = require('rollup'); @@ -18,6 +19,7 @@ const commonjs = require('@rollup/plugin-commonjs'); const babelPreset = require('@babel/preset-env'); const { terser } = require('rollup-plugin-terser'); +const resolveNative = require('./resolveNative'); const initConfig = require('./init-config'); @@ -28,6 +30,22 @@ const resolveReplacementStrings = (replacementStrings) => { return replacementStrings; }; +const setupReactnative = (argv) => { + const { reactNative } = argv; + let reactNativePath; + if (reactNative) { + reactNativePath = argv.reactNativePath || './react-native'; + if (!fs.existsSync(`${reactNativePath}/package.json`)) { + // eslint-disable-next-line no-console + console.warn( + `WARNING: No ${reactNativePath}/package.json was found. If you really intended to build a react-native version of this package, please provide one.\nOther wise, to supress this warning, omitt the --reactNative flag.` + ); + return false; + } + } + return { reactNative, reactNativePath }; +}; + const config = ({ mode = 'production', format = 'umd', @@ -36,13 +54,17 @@ const config = ({ core, } = {}) => { const CWD = argv.cwd || cwd; + const { reactNative, reactNativePath } = setupReactnative(argv); let dir = CWD; let pkg = require(path.resolve(CWD, 'package.json')); // eslint-disable-line const corePkg = core ? require(path.resolve(core, 'package.json')) : null; // eslint-disable-line + pkg = reactNative ? require(path.resolve(reactNativePath, 'package.json')) : pkg; // eslint-disable-line const { name, version, license, author } = pkg; const { sourcemap, replacementStrings = {}, typescript } = argv; - if (corePkg) { + if (reactNative) { + dir = `${dir}/${reactNativePath}`; + } else if (corePkg) { pkg = corePkg; dir = core; } @@ -72,6 +94,7 @@ const config = ({ // stardust should always be external if (!peers['@nebula.js/stardust']) { + // eslint-disable-next-line no-console console.warn('@nebula.js/stardust should be specified as a peer dependency'); } else if (external.indexOf('@nebula.js/stardust') === -1) { external.push('@nebula.js/stardust'); @@ -82,6 +105,7 @@ const config = ({ input: path.resolve(CWD, 'src/index'), external, plugins: [ + resolveNative({ reactNative }), replace({ 'process.env.NODE_ENV': JSON.stringify(mode === 'development' ? 'development' : 'production'), preventAssignment: true, diff --git a/commands/build/lib/resolveNative.js b/commands/build/lib/resolveNative.js new file mode 100644 index 000000000..b1aea98ca --- /dev/null +++ b/commands/build/lib/resolveNative.js @@ -0,0 +1,28 @@ +const path = require('path'); +const fs = require('fs'); +// this plugin will check if there is a corresponding .native file for the input file and output the contents of the native file +// This plugin should be loaded first. + +module.exports = ({ carbon }) => ({ + async load(id) { + if (!carbon) { + return null; + } + if (id) { + const parsed = path.parse(id); + if (parsed) { + if (parsed.name.length > 0) { + const nativeTest = `${parsed.dir}/${parsed.name}.native${parsed.ext}`; + try { + if (fs.existsSync(nativeTest)) { + return fs.readFileSync(nativeTest, { encoding: 'utf8', flag: 'r' }); + } + } catch (err) { + console.log('there was a problem reading the file', nativeTest, err); + } + } + } + } + return null; + }, +});