Skip to content

Commit

Permalink
Add esm module target
Browse files Browse the repository at this point in the history
  • Loading branch information
sinclairzx81 committed Sep 2, 2021
1 parent e4cadf4 commit a6cdba1
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 5 deletions.
4 changes: 3 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
```
5 changes: 3 additions & 2 deletions src/build/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ export interface BuilderOptions {
minify: boolean
sourcemap: boolean
watch: boolean
esm: boolean
}

export class Build implements Dispose {
Expand Down Expand Up @@ -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
Expand Down
8 changes: 7 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ class Hammer implements Dispose {
sourcemap: options.sourcemap,
target: options.target,
external: options.external,
esm: options.esm,
bundle: true,
watch: false
})
Expand All @@ -86,6 +87,7 @@ class Hammer implements Dispose {
sourcemap: options.sourcemap,
target: options.target,
external: options.external,
esm: options.esm,
bundle: true,
watch: true
})
Expand Down Expand Up @@ -119,6 +121,7 @@ class Hammer implements Dispose {
sourcemap: options.sourcemap,
target: options.target,
external: options.external,
esm: options.esm,
bundle: true,
watch: true
})
Expand Down Expand Up @@ -154,6 +157,7 @@ class Hammer implements Dispose {
sourcemap: options.sourcemap,
target: options.target,
external: options.external,
esm: options.esm,
bundle: true,
watch: true
})
Expand Down Expand Up @@ -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) {
Expand Down
12 changes: 12 additions & 0 deletions src/options/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ export interface BuildOptions {
sourcePaths: string[]
target: string[]
dist: string
esm: boolean
platform: 'browser' | 'node'
minify: boolean
sourcemap: boolean
Expand All @@ -67,6 +68,7 @@ export interface WatchOptions {
sourcePaths: string[]
target: string[]
dist: string
esm: boolean
platform: 'browser' | 'node'
minify: boolean
sourcemap: boolean
Expand All @@ -79,6 +81,7 @@ export interface RunOptions {
entryPath: string
args: string[]
dist: string
esm: boolean
target: string[]
minify: boolean
sourcemap: boolean
Expand All @@ -90,6 +93,7 @@ export interface ServeOptions {
sourcePaths: string[]
port: number
dist: string
esm: boolean
target: string[]
minify: boolean
sourcemap: boolean
Expand Down Expand Up @@ -135,6 +139,7 @@ function defaultBuildOptions(): BuildOptions {
return {
type: 'build',
dist: path.join(process.cwd(), 'dist'),
esm: false,
minify: false,
platform: 'browser',
sourcePaths: [],
Expand All @@ -148,6 +153,7 @@ function defaultWatchOptions(): WatchOptions {
return {
type: 'watch',
dist: path.join(process.cwd(), 'dist'),
esm: false,
minify: false,
platform: 'browser',
sourcePaths: [],
Expand All @@ -162,6 +168,7 @@ function defaultServeOptions(): ServeOptions {
type: 'serve',
sourcePaths: [],
dist: path.join(process.cwd(), 'dist'),
esm: false,
target: ['esnext'],
port: 5000,
sourcemap: false,
Expand All @@ -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,
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/resolve/resolve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 <script> and contains a 'module'
// type specifier. In these cases, we call to the functions resolveTypeScript(...) and
Expand Down

0 comments on commit a6cdba1

Please sign in to comment.