diff --git a/readme.md b/readme.md index 8e6ff93..77b4791 100644 --- a/readme.md +++ b/readme.md @@ -165,7 +165,9 @@ Options: --platform platform The target platform. --dist directory The target directory. --port port The port to listen on. + --external Omits external packages. + --esm Use esm module target. --minify Minifies the output. --sourcemap Generate sourcemaps. - --external External packages. + ``` diff --git a/src/build/build.ts b/src/build/build.ts index 3c7e958..ba96108 100644 --- a/src/build/build.ts +++ b/src/build/build.ts @@ -39,6 +39,7 @@ export interface BuilderOptions { minify: boolean sourcemap: boolean watch: boolean + esm: boolean } export class Build implements Dispose { @@ -106,9 +107,9 @@ export class Build implements Dispose { // esbuild only supports code-splitting for ESM modules. // -------------------------------------------------------------------------- - const entry = (asset.type === 'typescript' || asset.type === 'javascript') && asset.esm ? { + const entry = (asset.type === 'typescript' || asset.type === 'javascript') && (this.options.esm || asset.esm) ? { outdir: path.dirname(asset.targetPath), - format: 'esm' as Format, + format: 'esm' as Format, splitting: true } : { outfile: asset.targetPath diff --git a/src/index.ts b/src/index.ts index cbbbc47..dc241d0 100644 --- a/src/index.ts +++ b/src/index.ts @@ -62,6 +62,7 @@ class Hammer implements Dispose { sourcemap: options.sourcemap, target: options.target, external: options.external, + esm: options.esm, bundle: true, watch: false }) @@ -86,6 +87,7 @@ class Hammer implements Dispose { sourcemap: options.sourcemap, target: options.target, external: options.external, + esm: options.esm, bundle: true, watch: true }) @@ -119,6 +121,7 @@ class Hammer implements Dispose { sourcemap: options.sourcemap, target: options.target, external: options.external, + esm: options.esm, bundle: true, watch: true }) @@ -154,6 +157,7 @@ class Hammer implements Dispose { sourcemap: options.sourcemap, target: options.target, external: options.external, + esm: options.esm, bundle: true, watch: true }) @@ -234,9 +238,11 @@ class Hammer implements Dispose { ` ${blue}--platform${esc} platform The target platform.`, ` ${blue}--dist${esc} directory The target directory.`, ` ${blue}--port${esc} port The port to listen on.`, + ` ${blue}--external${esc} packages Omits external packages.`, + ` ${blue}--esm${esc} Use esm module target.`, ` ${blue}--minify${esc} Minifies the output.`, ` ${blue}--sourcemap${esc} Generate sourcemaps.`, - ` ${blue}--external${esc} names External packages.`, + ``, ].join(`\n`)) if (options.message) { diff --git a/src/options/options.ts b/src/options/options.ts index 2bbc75a..8451fe4 100644 --- a/src/options/options.ts +++ b/src/options/options.ts @@ -56,6 +56,7 @@ export interface BuildOptions { sourcePaths: string[] target: string[] dist: string + esm: boolean platform: 'browser' | 'node' minify: boolean sourcemap: boolean @@ -67,6 +68,7 @@ export interface WatchOptions { sourcePaths: string[] target: string[] dist: string + esm: boolean platform: 'browser' | 'node' minify: boolean sourcemap: boolean @@ -79,6 +81,7 @@ export interface RunOptions { entryPath: string args: string[] dist: string + esm: boolean target: string[] minify: boolean sourcemap: boolean @@ -90,6 +93,7 @@ export interface ServeOptions { sourcePaths: string[] port: number dist: string + esm: boolean target: string[] minify: boolean sourcemap: boolean @@ -135,6 +139,7 @@ function defaultBuildOptions(): BuildOptions { return { type: 'build', dist: path.join(process.cwd(), 'dist'), + esm: false, minify: false, platform: 'browser', sourcePaths: [], @@ -148,6 +153,7 @@ function defaultWatchOptions(): WatchOptions { return { type: 'watch', dist: path.join(process.cwd(), 'dist'), + esm: false, minify: false, platform: 'browser', sourcePaths: [], @@ -162,6 +168,7 @@ function defaultServeOptions(): ServeOptions { type: 'serve', sourcePaths: [], dist: path.join(process.cwd(), 'dist'), + esm: false, target: ['esnext'], port: 5000, sourcemap: false, @@ -177,6 +184,7 @@ function defaultRunOptions(): RunOptions { entryPath: path.join(process.cwd(), 'dist', 'index.js'), args: [], dist: path.join(process.cwd(), 'dist'), + esm: false, target: ['esnext'], sourcemap: false, minify: false, @@ -308,6 +316,7 @@ export function parseBuildOptions(params: string[]): BuildOptions { case '--platform': options.platform = parsePlatform(params); break; case '--target': options.target = [...parseTarget(params)]; break; case '--dist': options.dist = parseDist(params); break; + case '--esm': options.esm = true; break; case '--external': options.external = parseExternal(params); break; case '--sourcemap': options.sourcemap = true; break; case '--minify': options.minify = true; break; @@ -326,6 +335,7 @@ export function parseWatchOptions(params: string[]): WatchOptions { case '--platform': options.platform = parsePlatform(params); break; case '--target': options.target = [...parseTarget(params)]; break; case '--dist': options.dist = parseDist(params); break; + case '--esm': options.esm = true; break; case '--external': options.external = parseExternal(params); break; case '--sourcemap': options.sourcemap = true; break; case '--minify': options.minify = true; break; @@ -343,6 +353,7 @@ export function parseRunOptions(params: string[]): RunOptions { const next = params.shift() switch (next) { case '--dist': options.dist = parseDist(params); break; + case '--esm': options.esm = true; break; case '--external': options.external = parseExternal(params); break; case '--target': options.target = [...parseTarget(params)]; break; case '--sourcemap': options.sourcemap = true; break; @@ -361,6 +372,7 @@ export function parseServeOptions(params: string[]): ServeOptions { const next = params.shift() switch (next) { case '--dist': options.dist = parseDist(params); break; + case '--esm': options.esm = true; break; case '--external': options.external = parseExternal(params); break; case '--minify': options.minify = true; break; case '--sourcemap': options.sourcemap = true; break; diff --git a/src/resolve/resolve.ts b/src/resolve/resolve.ts index 33e4533..3d4a436 100644 --- a/src/resolve/resolve.ts +++ b/src/resolve/resolve.ts @@ -144,7 +144,7 @@ export class Resolver { if (!fs.existsSync(htmlTag.sourcePath)) continue // ----------------------------------------------------------------------------------------- - // Note: ESM modules are only resolved from HTML files. Because the getHtmlTags(...) + // Note: ESM modules can be inferred from HTML files. However because the getHtmlTags(...) // function resolves agnostically for any tag that contains a 'src' or 'href' attribute, // we need to explicitly check that the tag is both a