-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat!: output pure ESM for
.mjs
files (netlify/zip-it-and-ship-it#1198
) * feat: output pure ESM for .mjs files * chore: add comment to test * refactor: simplify `outExtension` generation * chore: update test * refactor: remove unused import
- Loading branch information
1 parent
132de91
commit 21dc4ae
Showing
19 changed files
with
154 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 6 additions & 26 deletions
32
packages/zip-it-and-ship-it/src/runtimes/node/utils/entry_file.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,43 +1,23 @@ | ||
import { basename, extname } from 'path' | ||
|
||
import { ModuleFormat } from './module_format.js' | ||
import { normalizeFilePath } from './normalize_path.js' | ||
|
||
export interface EntryFile { | ||
contents: string | ||
filename: string | ||
} | ||
|
||
const getEntryFileContents = (mainPath: string, moduleFormat: string) => { | ||
const importPath = `.${mainPath.startsWith('/') ? mainPath : `/${mainPath}`}` | ||
|
||
if (moduleFormat === ModuleFormat.COMMONJS) { | ||
return `module.exports = require('${importPath}')` | ||
} | ||
|
||
return `export { handler } from '${importPath}'` | ||
} | ||
|
||
export const getEntryFile = ({ | ||
commonPrefix, | ||
filename, | ||
mainFile, | ||
moduleFormat, | ||
userNamespace, | ||
}: { | ||
commonPrefix: string | ||
filename: string | ||
mainFile: string | ||
moduleFormat: ModuleFormat | ||
userNamespace: string | ||
}): EntryFile => { | ||
}) => { | ||
const mainPath = normalizeFilePath({ commonPrefix, path: mainFile, userNamespace }) | ||
const extension = extname(filename) | ||
const entryFilename = `${basename(filename, extension)}.js` | ||
const contents = getEntryFileContents(mainPath, moduleFormat) | ||
const importPath = `.${mainPath.startsWith('/') ? mainPath : `/${mainPath}`}` | ||
|
||
return { | ||
contents, | ||
filename: entryFilename, | ||
if (moduleFormat === ModuleFormat.COMMONJS) { | ||
return `module.exports = require('${importPath}')` | ||
} | ||
|
||
return `export { handler } from '${importPath}'` | ||
} |
19 changes: 19 additions & 0 deletions
19
packages/zip-it-and-ship-it/src/runtimes/node/utils/module_format.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,23 @@ | ||
import type { FeatureFlags } from '../../../feature_flags.js' | ||
|
||
export const enum ModuleFormat { | ||
COMMONJS = 'cjs', | ||
ESM = 'esm', | ||
} | ||
|
||
export const enum ModuleFileExtension { | ||
CJS = '.cjs', | ||
JS = '.js', | ||
MJS = '.mjs', | ||
} | ||
|
||
export const getFileExtensionForFormat = ( | ||
moduleFormat: ModuleFormat, | ||
featureFlags: FeatureFlags, | ||
): ModuleFileExtension => { | ||
if (moduleFormat === ModuleFormat.ESM && featureFlags.zisi_pure_esm_mjs) { | ||
return ModuleFileExtension.MJS | ||
} | ||
|
||
return ModuleFileExtension.JS | ||
} |
Oops, something went wrong.