From 213091bea56cc8f8bb90d7a5cd659ed4ec43978c Mon Sep 17 00:00:00 2001 From: high1 Date: Thu, 12 Nov 2020 01:10:34 +0100 Subject: [PATCH] File builder url refactor (#1454) * Updated FileBuilder to use URLs instead of paths * Updated scan-imports.ts * Added missing coma * Removed unused line --- .../snowpack.config.js | 4 +-- snowpack/src/build/build-pipeline.ts | 9 +++--- snowpack/src/commands/build.ts | 31 +++++++++---------- snowpack/src/commands/dev.ts | 5 +-- snowpack/src/config.ts | 4 ++- snowpack/src/scan-imports.ts | 3 +- snowpack/src/util.ts | 2 +- 7 files changed, 31 insertions(+), 27 deletions(-) diff --git a/create-snowpack-app/app-scripts-lit-element/snowpack.config.js b/create-snowpack-app/app-scripts-lit-element/snowpack.config.js index 85789c670c..482c6b995a 100644 --- a/create-snowpack-app/app-scripts-lit-element/snowpack.config.js +++ b/create-snowpack-app/app-scripts-lit-element/snowpack.config.js @@ -1,8 +1,8 @@ const fs = require('fs'); const path = require('path'); +const url = require('url'); -const cwd = process.cwd(); -const isTS = fs.existsSync(path.join(cwd, 'tsconfig.json')); +const isTS = fs.existsSync(url.pathToFileURL(path.join(process.cwd(), 'tsconfig.json'))); module.exports = { mount: { diff --git a/snowpack/src/build/build-pipeline.ts b/snowpack/src/build/build-pipeline.ts index dc644bced7..1c7707f080 100644 --- a/snowpack/src/build/build-pipeline.ts +++ b/snowpack/src/build/build-pipeline.ts @@ -1,4 +1,5 @@ import path from 'path'; +import url from 'url'; import {validatePluginLoadResult} from '../config'; import {logger} from '../logger'; import { @@ -106,7 +107,7 @@ async function runPipelineLoadStep( return { [srcExt]: { - code: await readFile(srcPath), + code: await readFile(url.pathToFileURL(srcPath)), }, }; } @@ -246,13 +247,13 @@ export async function runPipelineCleanupStep({plugins}: SnowpackConfig) { /** Core Snowpack file pipeline builder */ export async function buildFile( - srcPath: string, + srcURL: URL, buildFileOptions: BuildFileOptions, ): Promise { // Pass 1: Find the first plugin to load this file, and return the result - const loadResult = await runPipelineLoadStep(srcPath, buildFileOptions); + const loadResult = await runPipelineLoadStep(url.fileURLToPath(srcURL), buildFileOptions); // Pass 2: Pass that result through every plugin transform() method. - const transformResult = await runPipelineTransformStep(loadResult, srcPath, buildFileOptions); + const transformResult = await runPipelineTransformStep(loadResult, url.fileURLToPath(srcURL), buildFileOptions); // Return the final build result. return transformResult; } diff --git a/snowpack/src/commands/build.ts b/snowpack/src/commands/build.ts index 821951bf7b..11e9c5c112 100644 --- a/snowpack/src/commands/build.ts +++ b/snowpack/src/commands/build.ts @@ -47,7 +47,7 @@ function getIsHmrEnabled(config: SnowpackConfig) { } function handleFileError(err: Error, builder: FileBuilder) { - logger.error(`✘ ${builder.filepath}\n ${err.stack ? err.stack : err.message}`); + logger.error(`✘ ${builder.fileURL}\n ${err.stack ? err.stack : err.message}`); process.exit(1); } @@ -95,26 +95,26 @@ class FileBuilder { filesToResolve: Record = {}; filesToProxy: string[] = []; - readonly filepath: string; + readonly fileURL: URL; readonly mountEntry: MountEntry; readonly outDir: string; readonly config: SnowpackConfig; readonly lockfile: ImportMap | null; constructor({ - filepath, + fileURL, mountEntry, outDir, config, lockfile, }: { - filepath: string; + fileURL: URL; mountEntry: MountEntry; outDir: string; config: SnowpackConfig; lockfile: ImportMap | null; }) { - this.filepath = filepath; + this.fileURL = fileURL; this.mountEntry = mountEntry; this.outDir = outDir; this.config = config; @@ -124,10 +124,10 @@ class FileBuilder { async buildFile() { this.filesToResolve = {}; const isSSR = this.config.experiments.ssr; - const srcExt = path.extname(this.filepath); + const srcExt = path.extname(url.fileURLToPath(this.fileURL)); const fileOutput = this.mountEntry.static - ? {[srcExt]: {code: await readFile(this.filepath)}} - : await buildFile(this.filepath, { + ? {[srcExt]: {code: await readFile(this.fileURL)}} + : await buildFile(this.fileURL, { plugins: this.config.plugins, isDev: false, isSSR, @@ -140,7 +140,7 @@ class FileBuilder { if (!code) { continue; } - const outFilename = replaceExt(path.basename(this.filepath), srcExt, fileExt); + const outFilename = replaceExt(path.basename(url.fileURLToPath(this.fileURL)), srcExt, fileExt); const outLoc = path.join(this.outDir, outFilename); const sourceMappingURL = outFilename + '.map'; if (this.mountEntry.resolve && typeof code === 'string') { @@ -151,7 +151,7 @@ class FileBuilder { baseExt: fileExt, expandedExt: fileExt, contents: code, - locOnDisk: this.filepath, + locOnDisk: url.fileURLToPath(this.fileURL), }; break; } @@ -168,7 +168,7 @@ class FileBuilder { baseExt: fileExt, expandedExt: fileExt, contents: code, - locOnDisk: this.filepath, + locOnDisk: url.fileURLToPath(this.fileURL), }; break; } @@ -186,7 +186,7 @@ class FileBuilder { baseExt: fileExt, expandedExt: fileExt, contents: code, - locOnDisk: this.filepath, + locOnDisk: url.fileURLToPath(this.fileURL), }; break; } @@ -392,7 +392,7 @@ export async function command(commandOptions: CommandOptions) { !installedFileLoc.endsWith('import-map.json') && path.extname(installedFileLoc) !== '.js' ) { - const proxiedCode = await readFile(installedFileLoc); + const proxiedCode = await readFile(url.pathToFileURL(installedFileLoc)); const importProxyFileLoc = installedFileLoc + '.proxy.js'; const proxiedUrl = installedFileLoc.substr(buildDirectoryLoc.length).replace(/\\/g, '/'); const proxyCode = await wrapImportProxy({ @@ -422,7 +422,7 @@ export async function command(commandOptions: CommandOptions) { const finalDestLoc = path.join(buildDirectoryLoc, finalUrl); const outDir = path.dirname(finalDestLoc); const buildPipelineFile = new FileBuilder({ - filepath: fileLoc, + fileURL: url.pathToFileURL(fileLoc), mountEntry, outDir, config, @@ -530,9 +530,8 @@ export async function command(commandOptions: CommandOptions) { const finalUrl = getUrlForFileMount({fileLoc, mountKey, mountEntry, config})!; const finalDest = path.join(buildDirectoryLoc, finalUrl); const outDir = path.dirname(finalDest); - const changedPipelineFile = new FileBuilder({ - filepath: fileLoc, + fileURL: url.pathToFileURL(fileLoc), mountEntry, outDir, config, diff --git a/snowpack/src/commands/dev.ts b/snowpack/src/commands/dev.ts index 8387abd735..5ae5b3f015 100644 --- a/snowpack/src/commands/dev.ts +++ b/snowpack/src/commands/dev.ts @@ -652,7 +652,7 @@ export async function startDevServer(commandOptions: CommandOptions): Promise { - const builtFileOutput = await _buildFile(fileLoc, { + const builtFileOutput = await _buildFile(url.pathToFileURL(fileLoc), { plugins: config.plugins, isDev: true, isSSR, @@ -942,7 +942,8 @@ export async function startDevServer(commandOptions: CommandOptions): Promise { const result = await build({ ...options, - contents: await readFile(options.filePath), + contents: await readFile(url.pathToFileURL(options.filePath)), }).catch((err) => { logger.error( `[${plugin.name}] There was a problem running this older plugin. Please update the plugin to the latest version.`, diff --git a/snowpack/src/scan-imports.ts b/snowpack/src/scan-imports.ts index 0c87ce7f88..fce3d98422 100644 --- a/snowpack/src/scan-imports.ts +++ b/snowpack/src/scan-imports.ts @@ -3,6 +3,7 @@ import {InstallTarget} from 'esinstall'; import glob from 'glob'; import path from 'path'; import stripComments from 'strip-comments'; +import url from 'url'; import {logger} from './logger'; import {SnowpackConfig, SnowpackSourceFile} from './types/snowpack'; import { @@ -259,7 +260,7 @@ export async function scanImports( baseExt, expandedExt, locOnDisk: filePath, - contents: await readFile(filePath), + contents: await readFile(url.pathToFileURL(filePath)), }; }); const loadedFiles: (SnowpackSourceFile | null)[] = await Promise.all( diff --git a/snowpack/src/util.ts b/snowpack/src/util.ts index 6482aa9af5..9ba2a78213 100644 --- a/snowpack/src/util.ts +++ b/snowpack/src/util.ts @@ -41,7 +41,7 @@ export const CSS_REGEX = /@import\s*['"](.*?)['"];/gs; export const SVELTE_VUE_REGEX = /(]*>)(.*?)<\/script>/gims; /** Read file from disk; return a string if it’s a code file */ -export async function readFile(filepath: string): Promise { +export async function readFile(filepath: URL): Promise { const data = await fs.promises.readFile(filepath); const isBinary = await isBinaryFile(data); return isBinary ? data : data.toString('utf-8');