diff --git a/packages/build/build/build.js b/packages/build/build/build.js index 991e845..6816966 100644 --- a/packages/build/build/build.js +++ b/packages/build/build/build.js @@ -1,5 +1,5 @@ const {realpath} = require('fs/promises'); -const {dirname, join} = require('path'); +const {dirname, join, relative} = require('path'); const D = require('@flecks/core/build/debug'); const {Flecks} = require('@flecks/core/build/flecks'); @@ -279,7 +279,7 @@ module.exports = class Build extends Flecks { return this.resolver.resolve(join(fleck, 'build', config)); } - async runtimeCompiler(runtime, config, {allowlist = []} = {}) { + async runtimeCompiler(runtime, config, {additionalModuleDirs = [], allowlist = []} = {}) { // Compile? const needCompilation = Object.entries(this.compiled); if (needCompilation.length > 0) { @@ -300,13 +300,10 @@ module.exports = class Build extends Flecks { // Alias. config.resolve.alias[path] = source || path; // Root aliases. - if (root) { - config.resolve.alias[ - join(root, 'node_modules') - ] = join(FLECKS_CORE_ROOT, 'node_modules'); - config.resolve.fallback[path] = root; - } - includes.push(root || path); + config.resolve.fallback[path] = root; + config.resolve.modules.push(relative(FLECKS_CORE_ROOT, join(root, 'node_modules'))); + additionalModuleDirs.push(relative(FLECKS_CORE_ROOT, join(root, 'node_modules'))); + includes.push(root); }), ); // Compile. diff --git a/packages/build/build/explicate.js b/packages/build/build/explicate.js index 6889a9f..5126828 100644 --- a/packages/build/build/explicate.js +++ b/packages/build/build/explicate.js @@ -1,7 +1,8 @@ +const {join, relative, resolve} = require('path'); + const { - join, - resolve, -} = require('path'); + FLECKS_CORE_ROOT = process.cwd(), +} = process.env; module.exports = async function explicate( maybeAliasedPaths, @@ -83,8 +84,15 @@ module.exports = async function explicate( if (resolved) { await doExplication(descriptor); } + let descriptorRequest = descriptor.request; + if (areDescriptorsTheSame) { + descriptorRequest = join(descriptorRequest, 'src'); + } if (descriptor.path !== descriptor.request) { - resolver.addAlias(descriptor.path, descriptor.request); + resolver.addAlias(descriptor.path, descriptorRequest); + if (descriptorRequest !== descriptor.request) { + resolver.addFallback(descriptor.path, descriptor.request); + } } await Promise.all( platforms @@ -100,10 +108,10 @@ module.exports = async function explicate( resolver.addAlias(path, request); } } - else if (await resolver.resolve(join(descriptor.request, 'src', platform))) { + else if (await resolver.resolve(join(descriptorRequest, 'src', platform))) { const [path, request] = [ join(descriptor.path, platform), - join(descriptor.request, 'src', platform), + join(descriptorRequest, 'src', platform), ]; await doExplication({path, request}); if (path !== request) { @@ -119,7 +127,10 @@ module.exports = async function explicate( if (!rootDescriptor || roots[rootDescriptor.request]) { return; } - const {request} = rootDescriptor; + const {path, request} = rootDescriptor; + if (path !== request) { + resolver.addModules(relative(FLECKS_CORE_ROOT, join(request, 'node_modules'))); + } roots[request] = true; // Import bootstrap script. const bootstrapPath = await resolver.resolve(join(request, 'build', 'flecks.bootstrap')); diff --git a/packages/build/build/resolver.js b/packages/build/build/resolver.js index 02f6ac1..45a08c5 100644 --- a/packages/build/build/resolver.js +++ b/packages/build/build/resolver.js @@ -4,6 +4,7 @@ const D = require('@flecks/core/build/debug'); const {CachedInputFileSystem, ResolverFactory} = require('enhanced-resolve'); const AppendPlugin = require('enhanced-resolve/lib/AppendPlugin'); const AliasPlugin = require('enhanced-resolve/lib/AliasPlugin'); +const ModulesInHierarchicalDirectoriesPlugin = require('enhanced-resolve/lib/ModulesInHierarchicalDirectoriesPlugin'); const fs = require('graceful-fs'); const debug = D('@flecks/build/build/resolver'); @@ -27,10 +28,7 @@ module.exports = class Resolver { extensions: ['.js', '.json', '.node'], fileSystem: nodeFileSystem, symlinks: false, - ...{ - modules: [join(FLECKS_CORE_ROOT, 'node_modules')], - ...options, - }, + ...options, }); } @@ -59,6 +57,15 @@ module.exports = class Resolver { ).apply(this.resolver); } + addModules(path) { + debugSilly("adding modules: '%s'", path); + new ModulesInHierarchicalDirectoriesPlugin( + "raw-module", + path, + "module" + ).apply(this.resolver); + } + static isResolutionError(error) { return error.message.startsWith("Can't resolve"); } diff --git a/packages/build/build/webpack.js b/packages/build/build/webpack.js index 22ca5f2..ece8153 100644 --- a/packages/build/build/webpack.js +++ b/packages/build/build/webpack.js @@ -44,6 +44,7 @@ exports.defaultConfig = (flecks, specializedConfig) => { alias: {}, extensions, fallback: {}, + modules: ['node_modules'], }, stats: { colors: true,