Skip to content

Commit

Permalink
feat: add --carbon flag to build react-native supernova packages (#749)
Browse files Browse the repository at this point in the history
  • Loading branch information
vcellu committed Feb 8, 2022
1 parent 016b6bd commit 369844d
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 1 deletion.
26 changes: 25 additions & 1 deletion commands/build/lib/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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');

Expand All @@ -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',
Expand All @@ -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;
}
Expand Down Expand Up @@ -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');
Expand All @@ -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,
Expand Down
28 changes: 28 additions & 0 deletions commands/build/lib/resolveNative.js
Original file line number Diff line number Diff line change
@@ -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;
},
});

0 comments on commit 369844d

Please sign in to comment.