Skip to content
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

Add programmatic install interface #525

Merged
merged 3 commits into from
Jun 23, 2020
Merged
Show file tree
Hide file tree
Changes from 2 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
25 changes: 16 additions & 9 deletions src/commands/build.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
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 dependencyImportMapLoc = path.join(config.installOptions.dest, 'import-map.json');
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(BUILD_DEPENDENCIES_DIR, 'import-map.json');

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

const messageBus = new EventEmitter();
const relevantWorkers: BuildScript[] = [];
Expand Down Expand Up @@ -193,7 +197,7 @@ export async function command(commandOptions: CommandOptions) {
allFiles.map(async (f) => {
f = path.resolve(f); // this is necessary since glob.sync() returns paths with / on windows. path.resolve() will switch them to the native path separator.
if (
!f.startsWith(commandOptions.config.installOptions.dest) &&
!f.startsWith(BUILD_DEPENDENCIES_DIR) &&
(allBuildExtensions.includes(path.extname(f).substr(1)) ||
path.extname(f) === '.jsx' ||
path.extname(f) === '.tsx' ||
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 @@ -27,6 +27,7 @@
import cacache from 'cacache';
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 @@ -230,7 +231,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 @@ -248,14 +249,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(colors.yellow('! updating dependencies...'));
await installCommand(commandOptions);
await installCommand(installCommandOptions);
await updateLockfileHash(DEV_DEPENDENCIES_DIR);
}

Expand All @@ -273,7 +281,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 @@ -335,8 +343,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