Skip to content

Instantly share code, notes, and snippets.

@JamesMGreene
Created July 15, 2021 22:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JamesMGreene/774b210bccbb547aefa64991c1555712 to your computer and use it in GitHub Desktop.
Save JamesMGreene/774b210bccbb547aefa64991c1555712 to your computer and use it in GitHub Desktop.
Test ESM importing with the `github-script` Action
name: Test ESM with github-script
on:
workflow_dispatch:
jobs:
require-esm:
name: Try CommonJS require on ESM modules
runs-on: ubuntu-latest
timeout-minutes: 1
steps:
- uses: actions/checkout@5a4ac9002d0be2fb38bd78e4b4dbde5606d7042f
- uses: actions/setup-node@38d90ce44d5275ad62cc48384b3d8a58c500bb5f
with:
node-version: 16.x
cache: npm
- run: npm ci
- uses: actions/github-script@2b34a689ec86a68d8ab9478298f91d5401337b7d
with:
script: |
try {
console.log('Before require')
const { default: printStuff } = require('./src/print-stuff.js')
console.log('After require')
await printStuff()
console.log('At the end')
} catch (error) {
console.error('Threw an error!')
throw error
}
esm-import-statements:
name: Try ESM import statements
runs-on: ubuntu-latest
timeout-minutes: 1
steps:
- uses: actions/checkout@5a4ac9002d0be2fb38bd78e4b4dbde5606d7042f
- uses: actions/setup-node@38d90ce44d5275ad62cc48384b3d8a58c500bb5f
with:
node-version: 16.x
cache: npm
- run: npm ci
- uses: actions/github-script@2b34a689ec86a68d8ab9478298f91d5401337b7d
with:
script: |
try {
console.log('Before import statement')
import printStuff from './src/print-stuff.js'
console.log('After import statement')
await printStuff()
console.log('At the end')
} catch (error) {
console.error('Threw an error!')
throw error
}
esm-dynamic-imports:
name: Try ESM dynamic imports
runs-on: ubuntu-latest
timeout-minutes: 1
steps:
- uses: actions/checkout@5a4ac9002d0be2fb38bd78e4b4dbde5606d7042f
- uses: actions/setup-node@38d90ce44d5275ad62cc48384b3d8a58c500bb5f
with:
node-version: 16.x
cache: npm
- run: npm ci
- uses: actions/github-script@2b34a689ec86a68d8ab9478298f91d5401337b7d
with:
script: |
try {
console.log('Before dynamic import')
const printStuff = await import('./src/print-stuff.js')
console.log('After dynamic import')
await printStuff()
console.log('At the end')
} catch (error) {
console.error('Threw an error!')
throw error
}
node-es-module-loader-package:
name: Try node-es-module-loader package
runs-on: ubuntu-latest
timeout-minutes: 1
steps:
- uses: actions/checkout@5a4ac9002d0be2fb38bd78e4b4dbde5606d7042f
- uses: actions/setup-node@38d90ce44d5275ad62cc48384b3d8a58c500bb5f
with:
node-version: 16.x
cache: npm
- run: npm ci
- uses: actions/github-script@2b34a689ec86a68d8ab9478298f91d5401337b7d
with:
script: |
try {
const NodeESModuleLoader = require('node-es-module-loader')
const loader = new NodeESModuleLoader(process.cwd())
console.log('Before loader import')
const printStuff = await loader.import('./src/print-stuff.js')
console.log('After loader import')
await printStuff()
console.log('At the end')
} catch (error) {
console.error('Threw an error!')
throw error
}
esm-package:
name: Try esm package
runs-on: ubuntu-latest
timeout-minutes: 1
steps:
- uses: actions/checkout@5a4ac9002d0be2fb38bd78e4b4dbde5606d7042f
- uses: actions/setup-node@38d90ce44d5275ad62cc48384b3d8a58c500bb5f
with:
node-version: 16.x
cache: npm
- run: npm ci
- uses: actions/github-script@2b34a689ec86a68d8ab9478298f91d5401337b7d
with:
script: |
try {
const esm = require('esm')
require = esm(module)
console.log('Before loader import')
const printStuff = require('./src/print-stuff.js')
console.log('After loader import')
await printStuff()
console.log('At the end')
} catch (error) {
console.error('Threw an error!')
throw error
}
esm-package-with-workarounds:
name: Try esm package with workarounds
runs-on: ubuntu-latest
timeout-minutes: 1
steps:
- uses: actions/checkout@5a4ac9002d0be2fb38bd78e4b4dbde5606d7042f
- uses: actions/setup-node@38d90ce44d5275ad62cc48384b3d8a58c500bb5f
with:
node-version: 16.x
cache: npm
- run: npm ci
- uses: actions/github-script@2b34a689ec86a68d8ab9478298f91d5401337b7d
with:
script: |
try {
// Suppress Node.js core warnings about having ESM in a file with a *.js extension
// See: https://github.com/standard-things/esm/issues/855
const module = require('module')
const fs = require('fs')
module.Module._extensions['.js'] = function (module, filename) {
const contents = fs.readFileSync(filename, 'utf8')
module._compile(require('fs').readFileSync(filename, 'utf8'), filename)
}
const esm = require('esm')
require = esm({})
console.log('Before loader import')
const { default: printStuff } = require('./src/print-stuff.js')
console.log('After loader import')
await printStuff()
console.log('At the end')
} catch (error) {
console.error('Threw an error!')
throw error
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment