Skip to content

Add domains.js as an explicit domain artifact. #186

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .core/babel.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ ReactiumBabel.ModuleAliases = ReactiumBabel.Utils.registryFactory(
ReactiumBabel.Utils.Registry.MODES.CLEAN,
);
ReactiumBabel.ModuleAliases.register('externals', {
path: './.tmp/externals-manifest',
path: './src/externals-manifest',
});
ReactiumBabel.ModuleAliases.register('manifest', {
path: './src/manifest',
Expand Down
3 changes: 2 additions & 1 deletion .core/gulp.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,10 @@ const defaultConfig = {
includes: ['./node_modules'],
appdir: path.resolve(__dirname, 'src/app'),
rootdir: path.resolve(__dirname),
domainManifest: path.normalize(`${rootPath}/src/domains.js`),
manifest: path.normalize(`${rootPath}/src/manifest.js`),
externalsManifest: path.normalize(
`${rootPath}/.tmp/externals-manifest.js`,
`${rootPath}/src/externals-manifest.js`,
),
reactiumModules: path.normalize(`${rootPath}/reactium_modules`),
},
Expand Down
31 changes: 25 additions & 6 deletions .core/gulp.tasks.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const sourcemaps = require('gulp-sourcemaps');
const rename = require('gulp-rename');
const chalk = require('chalk');
const reactiumConfig = require('./reactium-config');
const regenManifest = require('./manifest/manifest-tools');
const { regenManifest } = require('./manifest/manifest-tools');
const umdWebpackGenerator = require('./umd.webpack.config');
const rootPath = path.resolve(__dirname, '..');
const { fork, spawn, execSync } = require('child_process');
Expand Down Expand Up @@ -217,6 +217,7 @@ const reactium = (gulp, config, webpackConfig) => {
crossEnvPackage.bin['cross-env'],
);

await gulp.task('domainsManifest')(() => Promise.resolve());
await gulp.task('mainManifest')(() => Promise.resolve());

command('node', [crossEnvBin, 'NODE_ENV=development', 'gulp'], done);
Expand Down Expand Up @@ -308,7 +309,7 @@ const reactium = (gulp, config, webpackConfig) => {

const manifest = gulp.series(
gulp.parallel(
task('mainManifest'),
gulp.series(task('domainsManifest'), task('mainManifest')),
task('externalsManifest'),
task('umdManifest'),
),
Expand All @@ -318,6 +319,21 @@ const reactium = (gulp, config, webpackConfig) => {

const sw = gulp.series(task('umd'), task('serviceWorker'));

const domainsManifest = done => {
// Generate domains.js file
regenManifest({
manifestFilePath: config.src.domainManifest,
manifestConfig: reactiumConfig.manifest.domains,
manifestTemplateFilePath: path.resolve(
__dirname,
'manifest/templates/domains.hbs',
),
manifestProcessor: require('./manifest/processors/domains'),
});

done();
};

const mainManifest = done => {
// Generate manifest.js file
regenManifest({
Expand Down Expand Up @@ -810,7 +826,13 @@ $assets: map.set($assets, "{{key}}", "{{{dataURL}}}");

const watchFork = done => {
const watchers = {};

// Watch for file changes
watchers['manifest'] = gulp.watch(
config.watch.js,
gulp.parallel(task('manifest')),
);

watchers['styles:colors'] = gulp.watch(
config.watch.colors,
gulp.task('styles:colors'),
Expand All @@ -829,10 +851,6 @@ $assets: map.set($assets, "{{key}}", "{{{dataURL}}}");
);
gulpwatch(config.watch.markup, watcher);
gulpwatch(config.watch.assets, watcher);
const scriptWatcher = gulp.watch(
config.watch.js,
gulp.parallel(task('manifest')),
);

watchLogger(watchers);
done();
Expand Down Expand Up @@ -866,6 +884,7 @@ $assets: map.set($assets, "{{key}}", "{{{dataURL}}}");
default: defaultTask,
json,
manifest,
domainsManifest,
mainManifest,
externalsManifest,
umd,
Expand Down
31 changes: 27 additions & 4 deletions .core/manifest/manifest-tools.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ const fs = require('fs-extra');
const _ = require('underscore');
const op = require('object-path');
const prettier = require('prettier');
const moment = require('moment');
const chalk = require('chalk');
const diff = require('fast-diff');
const hb = require('handlebars');
const rootPath = path.resolve(__dirname, '../..');

// flatten a tree of files from directory-tree module
const flattenRegistry = (registry = { children: [] }, manifest = []) => {
op.get(registry, 'children', []).forEach(item => {
const type = op.get(item, 'type');
Expand All @@ -29,8 +30,13 @@ const flattenRegistry = (registry = { children: [] }, manifest = []) => {
return manifest;
};

const sources = (sourcePath, searchParams) =>
flattenRegistry(tree(sourcePath, searchParams));
/**
* For a given sourcepath relative to the project directory, returns a flat list of matching files.
*/
const sources = (sourcePath, searchParams) => {
const t = tree(sourcePath, searchParams);
return flattenRegistry(t);
};

const isRegExp = regEx =>
typeof regEx === 'object' && regEx.constructor == RegExp;
Expand Down Expand Up @@ -168,7 +174,7 @@ const find = (searches = [], sourceMappings = [], searchParams = {}) => {
return mappings;
};

module.exports = function({
const regenManifest = function({
manifestFilePath,
manifestConfig,
manifestTemplateFilePath,
Expand Down Expand Up @@ -220,3 +226,20 @@ module.exports = function({
fs.writeFileSync(manifestFilePath, fileContents);
}
};

const domainRegExp = new RegExp('/([A-Za-z_0-9-]+?)[\\/][A-Za-z_0-9-]+$');
const fileToDomain = file => {
let [, domain] = file.match(domainRegExp) || [];
const relativeOriginalPath = path.resolve(rootPath, file);

return domain;
};

module.exports = {
regenManifest,
flattenRegistry,
sources,
isRegExp,
find,
fileToDomain,
};
27 changes: 27 additions & 0 deletions .core/manifest/processors/domains.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
const path = require('path');
const op = require('object-path');
const { fileToDomain } = require('../manifest-tools');
const rootPath = path.resolve(__dirname, '../../..');

module.exports = data => {
const domains = {};
const relative = {};

for (const [file, original] of Object.entries(
op.get(data, 'manifest.allDomains.originals'),
)) {
const relativeOriginalPath = path.resolve(rootPath, original);

const domainObj = require(relativeOriginalPath);
const impliedDomain = fileToDomain(file);
const domain = op.get(domainObj, 'name', impliedDomain);

op.set(domains, [domain], domainObj);
op.set(domains, [domain, 'name'], domain);
op.set(domains, [domain, 'implied'], impliedDomain);
op.set(domains, [domain, 'original'], original);
op.set(relative, [path.dirname(original)], domain);
}

return JSON.stringify({ domains, relative });
};
33 changes: 13 additions & 20 deletions .core/manifest/processors/manifest.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,20 @@ const path = require('path');
const rootPath = path.resolve(__dirname, '../../..');
const chalk = require('chalk');
module.exports = data => {
const explicitDomains = require(path.resolve(rootPath, 'src/domains.js'));

const types = Object.entries(data.manifest).map(([name, typeDomains]) => {
const domainRegExp = new RegExp('/([A-Za-z_0-9-]+?)/[A-Za-z_0-9-]+$');
const { fileToDomain } = require('../manifest-tools');
const { imports, type } = typeDomains;

const fileToDomain = file => {
let [, domain] = file.match(domainRegExp) || [];
try {
const relativeOriginalPath = path.resolve(
rootPath,
typeDomains.originals[file],
);
const potentialDomainFile = path.normalize(
path.dirname(relativeOriginalPath) + '/domain.js',
);
if (fs.existsSync(potentialDomainFile)) {
const { name } = require(potentialDomainFile);
if (name) domain = name;
}
} catch (error) {
// intentionally blank
}
const mapDomain = file => {
let domain = fileToDomain(file);
const relative = domain;
domain = op.get(
explicitDomains,
['relative', path.dirname(typeDomains.originals[file])],
domain,
);

if (!domain)
console.log(
Expand All @@ -36,9 +29,9 @@ module.exports = data => {
const domains = [];
imports
.map(file => file.replace(/\\/g, '/'))
.filter(fileToDomain)
.filter(mapDomain)
.forEach(file => {
const domain = fileToDomain(file);
const domain = mapDomain(file);
let existing;
if (
(existing = domains.find(({ domain: d }) => d === domain))
Expand Down
6 changes: 6 additions & 0 deletions .core/manifest/templates/domains.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/**
* Generated by Reactium
* DO NOT directly edit this file !!!!!!
*/

module.exports = {{{this}}};
29 changes: 28 additions & 1 deletion .core/reactium-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,33 @@ const defaultManifestConfig = {
exclude: [/\.ds_store/i, /\.core/i, /\.cli\//i, /src\/assets/],
},
},
domains: {
patterns: [
{
name: 'allDomains',
type: 'domain',
pattern: /domain.js$/,
},
],
sourceMappings: [
{
from: 'src/app/',
to: '../src/app/',
},
{
from: '.core/',
to: '../.core/',
},
{
from: 'reactium_modules/',
to: '../reactium_modules/',
},
{
node_modules: true,
ignore: /^((?!reactium-plugin).)*$/,
},
],
},
};

const overrides = config => {
Expand All @@ -179,7 +206,7 @@ const manifestConfig = overrides(defaultManifestConfig);
*/
module.exports = {
version,
semver: '^3.0.0',
semver: '^5.0.0',
build: gulpConfig,
update: {
package: {
Expand Down
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,9 @@ Thumbs.db
.cli-cache

# Manifest
src/domains.js
src/manifest.js
src/externals-manifest.js
src/app/server/webpack-manifest.json

# Translation .mo
Expand Down