-
-
Notifications
You must be signed in to change notification settings - Fork 160
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: non-breaking ESM support 🤝 (#613)
Closes #608 This adds some esm-vs-commonjs "smartness" to umzug: 1. use `require` for `.cjs` migrations and `import` for `.mjs` migrations (and their typescript equivalents) 2. use `require` for `.js` migrations _if_ `typeof require.main === 'object'`, and `import` to `.js` migrations otherwise 3. use the same criteria to create (c)js vs mjs templates when creating migration files 4. add `"moduleResolution": "Node16"` to tsconfig.lib.json to make sure `import(filepath)` doesn't get transpiled into `__importStar(require(filepath))` (see [here](microsoft/TypeScript#43329 (comment)) and [here](microsoft/TypeScript#43329 (comment))) Tests/examples: - add a `vanilla-esm` example to make sure using `import` / top-level await works - add a step to the `test_pkg` job to make sure vitest isn't hiding gnarly import problems - this is installing the compiled library as a `.tgz`, and with no other dev/prod dependencies like vitest or ts-node having been installed, so should be very close to what end users will do Didn't: - add a wrapper.mjs file in the compiled folder as suggested in #608 (comment), mostly just because it didn't seem to be necessary? It seems to work fine when imported from an ES-module, using top-level await, etc., even though umzug is itself a commonjs module. <details> <summary>original body</summary> ~Related to #608 - although does not close it.~ ~This adds built-in support for `.mjs` and `.mts` files. `.mjs` should "just work" - write migrations as ESM modules and they'll be imported in the right way. For the current major version, `.js` will continue to be assumed commonjs. ESM-fans will just have to type that extra `m` in their filenames.~ ~This PR _doesn't_ add a wrapper file so that the umzug library itself can be imported as an ES module. That can be done in a follow-up PR. In the meantime, ESM users can use `createRequire` as in the [existing ESM example](https://github.com/sequelize/umzug/tree/main/examples/2.es-modules).~ </details> --------- Co-authored-by: Misha Kaletsky <[email protected]>
- Loading branch information
Showing
14 changed files
with
248 additions
and
63 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { Umzug, JSONStorage } from 'umzug'; | ||
|
||
const __dirname = new URL('.', import.meta.url).pathname.replace(/\/$/, ''); | ||
|
||
export const migrator = new Umzug({ | ||
migrations: { | ||
glob: 'migrations/*.*js', | ||
}, | ||
context: { directory: __dirname + '/ignoreme' }, | ||
storage: new JSONStorage({ path: __dirname + '/ignoreme/storage.json' }), | ||
logger: console, | ||
}); | ||
|
||
await migrator.runAsCLI(); |
12 changes: 12 additions & 0 deletions
12
examples/0.5-vanilla-esm/migrations/2023.11.03T16.52.04.users-table.mjs
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 |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { promises as fs } from 'fs'; | ||
|
||
/** @type {typeof import('../migrate.mjs').migrator['_types']['migration']} */ | ||
export const up = async ({ context }) => { | ||
await fs.mkdir(context.directory, { recursive: true }); | ||
await fs.writeFile(context.directory + '/users.json', JSON.stringify([], null, 2)); | ||
}; | ||
|
||
/** @type {typeof import('../migrate.mjs').migrator['_types']['migration']} */ | ||
export const down = async ({ context }) => { | ||
await fs.unlink(context.directory + '/users.json'); | ||
}; |
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 |
---|---|---|
@@ -0,0 +1,16 @@ | ||
This example shows the simplest possible, node-only setup for Umzug. No typescript, no database, no dependencies. | ||
|
||
Note: | ||
- The `context` for the migrations just contains a (gitignored) directory. | ||
- The example migration just writes an empty file to the directory | ||
|
||
```bash | ||
node migrate.mjs --help # show CLI help | ||
|
||
node migrate.mjs up # apply migrations | ||
node migrate.mjs down # revert the last migration | ||
node migrate.mjs create --name new-migration.mjs # create a new migration file | ||
|
||
node migrate.mjs up # apply migrations again | ||
node migrate.mjs down --to 0 # revert all migrations | ||
``` |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
Oops, something went wrong.