Skip to content

Commit

Permalink
add programattic install interface
Browse files Browse the repository at this point in the history
  • Loading branch information
FredKSchott committed Jun 20, 2020
1 parent 630585c commit 4515b0a
Show file tree
Hide file tree
Showing 5 changed files with 138 additions and 76 deletions.
21 changes: 14 additions & 7 deletions src/commands/build.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import chalk from 'chalk';
import merge from 'deepmerge';
import {EventEmitter} from 'events';
import execa from 'execa';
import {promises as fs} from 'fs';
Expand Down Expand Up @@ -27,17 +28,20 @@ export async function command(commandOptions: CommandOptions) {
const {cwd, config} = commandOptions;

// Start with a fresh install of your dependencies, for production
commandOptions.config.installOptions.env.NODE_ENV = process.env.NODE_ENV || 'production';
commandOptions.config.installOptions.dest = BUILD_DEPENDENCIES_DIR;
commandOptions.config.installOptions.treeshake =
commandOptions.config.installOptions.treeshake !== undefined
? commandOptions.config.installOptions.treeshake
: true;
const installCommandOptions = merge(commandOptions, {
config: {
installOptions: {
dest: BUILD_DEPENDENCIES_DIR,
env: {NODE_ENV: process.env.NODE_ENV || 'production'},
treeshake: config.installOptions.treeshake ?? true,
},
},
});
const dependencyImportMapLoc = path.join(config.installOptions.dest, 'import-map.json');

// Start with a fresh install of your dependencies, always.
console.log(chalk.yellow('! rebuilding dependencies...'));
await installCommand(commandOptions);
await installCommand(installCommandOptions);

const messageBus = new EventEmitter();
const relevantWorkers: BuildScript[] = [];
Expand Down Expand Up @@ -223,6 +227,8 @@ export async function command(commandOptions: CommandOptions) {
}
}

const webModulesScript = config.scripts.find((script) => script.id === 'mount:web_modules')!;
const webModulesPath = webModulesScript.args.toUrl;
const allBuiltFromFiles = new Set<string>();
for (const workerConfig of relevantWorkers) {
const {id, match, type} = workerConfig;
Expand Down Expand Up @@ -279,6 +285,7 @@ export async function command(commandOptions: CommandOptions) {
}
const resolveImportSpecifier = createImportResolver({
fileLoc,
webModulesPath,
dependencyImportMap,
isDev: false,
isBundled,
Expand Down
23 changes: 17 additions & 6 deletions src/commands/dev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import cacache from 'cacache';
import chalk from 'chalk';
import isCompressible from 'compressible';
import etag from 'etag';
import merge from 'deepmerge';
import {EventEmitter} from 'events';
import execa from 'execa';
import {existsSync, promises as fs, readFileSync} from 'fs';
Expand Down Expand Up @@ -228,7 +229,7 @@ export async function command(commandOptions: CommandOptions) {
currentlyRunningCommand.stdout.on('data', (data) => process.stdout.write(data));
currentlyRunningCommand.stderr.on('data', (data) => process.stderr.write(data));
await currentlyRunningCommand;
currentlyRunningCommand = installCommand(commandOptions);
currentlyRunningCommand = installCommand(installCommandOptions);
await currentlyRunningCommand;
await updateLockfileHash(DEV_DEPENDENCIES_DIR);
await cacache.rm.all(BUILD_CACHE);
Expand All @@ -246,14 +247,21 @@ export async function command(commandOptions: CommandOptions) {
});

// Set the proper install options, in case an install is needed.
commandOptions.config.installOptions.dest = DEV_DEPENDENCIES_DIR;
commandOptions.config.installOptions.env.NODE_ENV = process.env.NODE_ENV || 'development';
const dependencyImportMapLoc = path.join(config.installOptions.dest, 'import-map.json');
const dependencyImportMapLoc = path.join(DEV_DEPENDENCIES_DIR, 'import-map.json');
const installCommandOptions = merge(commandOptions, {
config: {
installOptions: {
dest: DEV_DEPENDENCIES_DIR,
env: {NODE_ENV: process.env.NODE_ENV || 'development'},
treeshake: false,
},
},
});

// Start with a fresh install of your dependencies, if needed.
if (!(await checkLockfileHash(DEV_DEPENDENCIES_DIR)) || !existsSync(dependencyImportMapLoc)) {
console.log(chalk.yellow('! updating dependencies...'));
await installCommand(commandOptions);
await installCommand(installCommandOptions);
await updateLockfileHash(DEV_DEPENDENCIES_DIR);
}

Expand All @@ -271,7 +279,7 @@ export async function command(commandOptions: CommandOptions) {
if (!currentlyRunningCommand) {
isLiveReloadPaused = true;
messageBus.emit('INSTALLING');
currentlyRunningCommand = installCommand(commandOptions);
currentlyRunningCommand = installCommand(installCommandOptions);
await currentlyRunningCommand.then(async () => {
dependencyImportMap = JSON.parse(
await fs
Expand Down Expand Up @@ -333,8 +341,11 @@ export async function command(commandOptions: CommandOptions) {
const ext = path.extname(fileLoc).substr(1);
if (ext === 'js' || srcFileExtensionMapping[ext] === 'js') {
let missingWebModule: {spec: string; pkgName: string} | null = null;
const webModulesScript = config.scripts.find((script) => script.id === 'mount:web_modules');
const webModulesPath = webModulesScript ? webModulesScript.args.toUrl : '/web_modules';
const resolveImportSpecifier = createImportResolver({
fileLoc,
webModulesPath,
dependencyImportMap,
isDev: true,
isBundled: false,
Expand Down
15 changes: 7 additions & 8 deletions src/commands/import-resolver.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import {Stats, statSync} from 'fs';
import path from 'path';
import {SnowpackConfig} from '../config';
import {findMatchingMountScript} from '../util';
import {findMatchingMountScript, ImportMap} from '../util';
import srcFileExtensionMapping from './src-file-extension-mapping';
const cwd = process.cwd();
const URL_HAS_PROTOCOL_REGEX = /^\w:\/\./;

interface ImportResolverOptions {
fileLoc: string;
dependencyImportMap: any;
webModulesPath: string;
dependencyImportMap: ImportMap | null | undefined;
isDev: boolean;
isBundled: boolean;
config: SnowpackConfig;
Expand Down Expand Up @@ -51,14 +52,12 @@ function resolveSourceSpecifier(spec: string, stats: Stats | false, isBundled: b
*/
export function createImportResolver({
fileLoc,
webModulesPath,
dependencyImportMap,
isDev,
isBundled,
config,
}: ImportResolverOptions) {
const webModulesScript = config.scripts.find((script) => script.id === 'mount:web_modules');
const webModulesLoc = webModulesScript ? webModulesScript.args.toUrl : '/web_modules';

return function importResolver(spec: string): string | false {
if (URL_HAS_PROTOCOL_REGEX.test(spec)) {
return spec;
Expand All @@ -76,12 +75,12 @@ export function createImportResolver({
spec = resolveSourceSpecifier(spec, importStats, isBundled);
return spec;
}
if (dependencyImportMap.imports[spec]) {
if (dependencyImportMap && dependencyImportMap.imports[spec]) {
let resolvedImport = isDev
? path.posix.resolve(webModulesLoc, dependencyImportMap.imports[spec])
? path.posix.resolve(webModulesPath, dependencyImportMap.imports[spec])
: path.posix.join(
config.buildOptions.baseUrl,
webModulesLoc,
webModulesPath,
dependencyImportMap.imports[spec],
);
const extName = path.extname(resolvedImport);
Expand Down
Loading

0 comments on commit 4515b0a

Please sign in to comment.