Skip to content

Commit

Permalink
chore: update optimizer sourcemaps, getTypeScriptPath()
Browse files Browse the repository at this point in the history
  • Loading branch information
adamdbradley committed Jul 17, 2021
1 parent 91806b1 commit 0def28c
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 45 deletions.
6 changes: 1 addition & 5 deletions src/optimizer/esbuild/builder.ts
Expand Up @@ -6,11 +6,7 @@ import { createTimer } from '../utils';
/**
* @alpha
*/
export function createEsbuilder(opts: {
outDir: string;
clientOpts?: BuildOptions;
serverOpts?: BuildOptions;
}) {
export function createEsbuilder(opts: { outDir: string; clientOpts?: any; serverOpts?: any }) {
const results: EsBuildPluginData = {};

const builder = {
Expand Down
6 changes: 3 additions & 3 deletions src/optimizer/loader.ts
@@ -1,7 +1,7 @@
const QWIK_LOADER_DEFAULT_MINIFIED: string = (globalThis as any).QWIK_LOADER_DEFAULT_MINIFIED;
const QWIK_LOADER_DEFAULT_DEBUG: string = (globalThis as any).QWIK_LOADER_DEFAULT_DEBUG;
const QWIK_LOADER_EVENTS_MINIFIED: string = (globalThis as any).QWIK_LOADER_EVENTS_MINIFIED;
const QWIK_LOADER_EVENTS_DEBUG: string = (globalThis as any).QWIK_LOADER_EVENTS_DEBUG;
const QWIK_LOADER_OPTIMIZE_MINIFIED: string = (globalThis as any).QWIK_LOADER_OPTIMIZE_MINIFIED;
const QWIK_LOADER_OPTIMIZE_DEBUG: string = (globalThis as any).QWIK_LOADER_OPTIMIZE_DEBUG;

/**
* Provides the qwikloader.js file as a string. Useful for tooling to inline the qwikloader
Expand All @@ -11,7 +11,7 @@ const QWIK_LOADER_EVENTS_DEBUG: string = (globalThis as any).QWIK_LOADER_EVENTS_
export function getQwikLoaderScript(opts: { events?: string[]; debug?: boolean } = {}) {
if (Array.isArray(opts.events) && opts.events.length > 0) {
// inject exact known events used
const loader = opts.debug ? QWIK_LOADER_EVENTS_DEBUG : QWIK_LOADER_EVENTS_MINIFIED;
const loader = opts.debug ? QWIK_LOADER_OPTIMIZE_DEBUG : QWIK_LOADER_OPTIMIZE_MINIFIED;
return loader.replace('globalThis.qEvents', JSON.stringify(opts.events));
}

Expand Down
32 changes: 26 additions & 6 deletions src/optimizer/optimizer.ts
Expand Up @@ -17,7 +17,7 @@ import {
getTypeScriptSync,
} from './typescript-platform';
import { getEntryPoints, normalizeOptions, normalizeUrl, platform } from './utils';
import { transformModule } from './transform';
import { transformModuleSync } from './transform';
import type TypeScript from 'typescript';
import { postBuild } from './post-build';

Expand All @@ -28,7 +28,7 @@ import { postBuild } from './post-build';
export class Optimizer {
private baseUrl = normalizeUrl('/');
private rootDir: string | null = null;
private sourceMapOpt: SourceMapOption = true;
private sourceMapOpt: SourceMapOption = null;
private enabledCache = true;
private internalCache: InternalCache = {
modules: [],
Expand All @@ -37,6 +37,7 @@ export class Optimizer {
private mode: Mode = 'development';
private ts: any = null;
private tsconfig: any = null;
private typescriptPath: string | null = null;
private entryInputs: string[] | null = null;
private moduleResolveCache: TypeScript.ModuleResolutionCache | null = null;

Expand Down Expand Up @@ -126,13 +127,13 @@ export class Optimizer {
async transformModule(opts: TransformModuleOptions): Promise<TransformModuleResult> {
const ts = await this.getTypeScript();
const tsconfig: TypeScript.ParsedCommandLine = await this.getTsconfig();
return transformModule(this, this.internalCache, opts, ts, tsconfig.options);
return transformModuleSync(this, this.internalCache, opts, ts, tsconfig.options);
}

transformModuleSync(opts: TransformModuleOptions): TransformModuleResult {
const ts = this.getTypeScriptSync();
const tsconfig: TypeScript.ParsedCommandLine = this.getTsconfigSync();
return transformModule(this, this.internalCache, opts, ts, tsconfig.options);
return transformModuleSync(this, this.internalCache, opts, ts, tsconfig.options);
}

async getTsconfig() {
Expand All @@ -155,19 +156,38 @@ export class Optimizer {

async getTypeScript() {
if (!this.ts) {
this.ts = await getTypeScript(this.getRootDir());
this.ts = await getTypeScript(this);
}
return this.ts;
}

getTypeScriptSync() {
if (!this.ts) {
this.ts = getTypeScriptSync();
this.ts = getTypeScriptSync(this);
}
return this.ts;
}

setTypeScript(ts: any) {
this.ts = ts;
}

getTypeScriptPath() {
if (this.typescriptPath) {
return this.typescriptPath;
}
if (platform === 'node') {
return `typescript`;
}
if (platform === 'browser-main' || platform === 'browser-webworker') {
throw Error(
`TypeScript CDN URL must be set using the "typescriptPath" option, such as: https://cdn.jsdelivr.net/npm/typescript@4.2.0/lib/typescript.js`
);
}
throw Error(`unsupported platform`);
}

setTypeScriptPath(typescriptPath: string) {
this.typescriptPath = typescriptPath;
}
}
15 changes: 10 additions & 5 deletions src/optimizer/transform.ts
Expand Up @@ -7,7 +7,7 @@ import type {
import type TypeScript from 'typescript';
import { isJsxFile, toBase64 } from './utils';

export function transformModule(
export function transformModuleSync(
optimizer: Optimizer,
c: InternalCache,
opts: TransformModuleOptions,
Expand Down Expand Up @@ -106,6 +106,9 @@ function getCompilerOptions(
allowNonTsExtensions: true,
noLib: true,
noResolve: true,
sourceMap: false,
inlineSourceMap: false,
inlineSources: false,
};

if (typeof compilerOpts.esModuleInterop !== 'boolean') {
Expand All @@ -118,11 +121,13 @@ function getCompilerOptions(
compilerOpts.target = ts.ScriptTarget.ES2017;
}

if (opts.sourcemap === true || opts.sourcemap === 'inline') {
const sourcemap = opts.sourcemap || optimizer.getSourceMapOption();

if (sourcemap === 'inline') {
compilerOpts.inlineSourceMap = true;
compilerOpts.inlineSources = true;
} else if (sourcemap === 'external') {
compilerOpts.sourceMap = true;
} else if (compilerOpts.sourceMap !== false) {
const sourceMapOpt = optimizer.getSourceMapOption();
compilerOpts.sourceMap = sourceMapOpt === true || sourceMapOpt === 'inline';
}

if (isJsxFile(opts.filePath)) {
Expand Down
3 changes: 2 additions & 1 deletion src/optimizer/types.ts
Expand Up @@ -8,6 +8,7 @@ export interface OptimizerOptions {
sourcemap?: SourceMapOption;
ts?: any;
tsconfig?: any;
typescriptPath?: string;
}

export interface TransformModuleOptions {
Expand All @@ -31,7 +32,7 @@ export type Mode = 'development' | 'production';

export type OutputPlatform = 'client' | 'server';

export type SourceMapOption = boolean | 'inline';
export type SourceMapOption = 'external' | 'inline' | null;

export type { Optimizer };

Expand Down
38 changes: 15 additions & 23 deletions src/optimizer/typescript-platform.ts
@@ -1,4 +1,5 @@
import type TypeScript from 'typescript';
import type { Optimizer } from './optimizer';
import type { ResolveModuleOptions, ResolveModuleResult } from './types';
import { platform } from './utils';

Expand All @@ -24,27 +25,28 @@ export function getTsconfig(
return null;
}

export async function getTypeScript(rootDir: string): Promise<typeof TypeScript | null> {
if (platform === 'node' && typeof require !== 'function') {
export async function getTypeScript(optimizer: Optimizer): Promise<typeof TypeScript | null> {
if (platform === 'node') {
try {
return require(optimizer.getTypeScriptPath());
} catch (e) {
/**/
}
const module = await import('module');
const require = module.Module.createRequire(rootDir);
return require(getTypeScriptPath());
const moduleRequire = module.Module.createRequire(optimizer.getRootDir());
return moduleRequire(optimizer.getTypeScriptPath());
}
return getTypeScriptSync();
return getTypeScriptSync(optimizer);
}

export function getTypeScriptSync(): typeof TypeScript | null {
export function getTypeScriptSync(optimizer: Optimizer): typeof TypeScript | null {
if (platform === 'node') {
if (typeof require === 'function') {
// NodeJs (CJS)
return require(getTypeScriptPath());
} else {
throw new Error('NodeJs require() not available');
}
// NodeJs (CJS)
return require(optimizer.getTypeScriptPath());
} else if (platform === 'browser-webworker') {
// Browser (Web Worker)
if (!self.ts) {
(self as any).importScripts(getTypeScriptPath());
(self as any).importScripts(optimizer.getTypeScriptPath());
}
return self.ts as any;
} else if (platform === 'browser-main') {
Expand All @@ -56,16 +58,6 @@ export function getTypeScriptSync(): typeof TypeScript | null {
return null;
}

function getTypeScriptPath() {
if (platform === 'node') {
return `typescript`;
}
if (platform === 'browser-main' || platform === 'browser-webworker') {
return `https://cdn.jsdelivr.net/npm/typescript@__TYPESCRIPT__/lib/typescript.js`;
}
throw Error(`unsupported platform`);
}

export function resolveModuleSync(
ts: typeof TypeScript,
compilerOpts: TypeScript.CompilerOptions,
Expand Down
4 changes: 2 additions & 2 deletions src/optimizer/utils.ts
Expand Up @@ -3,12 +3,12 @@ import type { Optimizer, OptimizerOptions, EntryPointOptions } from './types';
export function normalizeOptions(optimizer: Optimizer, opts?: OptimizerOptions) {
if (opts) {
if (typeof opts.cache === 'boolean') optimizer.enableCache(opts.cache);
if (typeof opts.sourcemap === 'string' || typeof opts.sourcemap === 'boolean')
optimizer.setSourceMapOption(opts.sourcemap);
if (typeof opts.sourcemap === 'string') optimizer.setSourceMapOption(opts.sourcemap);
if (typeof opts.mode === 'string') optimizer.setMode(opts.mode);
if (typeof opts.rootDir === 'string') optimizer.setRootDir(opts.rootDir);
if (opts.ts) optimizer.setTypeScript(opts.ts);
if (opts.tsconfig) optimizer.setTsconfig(opts.tsconfig);
if (typeof opts.typescriptPath === 'string') optimizer.setTypeScriptPath(opts.typescriptPath);
}
}

Expand Down

0 comments on commit 0def28c

Please sign in to comment.