Skip to content

Commit 479e6ca

Browse files
committed
fix: convert all modules running in loader thread to ESM
This is a second attempt at fixing the double-loader issue in #205. The tests from that commit are included.
1 parent ffb5682 commit 479e6ca

File tree

8 files changed

+34
-752
lines changed

8 files changed

+34
-752
lines changed

hook.js

Lines changed: 0 additions & 498 deletions
This file was deleted.

hook.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//
33
// This product includes software developed at Datadog (https://www.datadoghq.com/). Copyright 2021 Datadog, Inc.
44

5-
import { createHook } from './hook.js'
5+
import { createHook } from './create-hook.mjs'
66

77
const { initialize, load, resolve, getFormat, getSource } = createHook(import.meta)
88

lib/get-esm-exports.js

Lines changed: 0 additions & 118 deletions
This file was deleted.

lib/get-exports.js

Lines changed: 0 additions & 129 deletions
This file was deleted.

test/fixtures/double-loader.mjs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { readFile } from 'fs/promises'
2+
3+
export async function load (url, context, nextLoad) {
4+
const result = await nextLoad(url, context)
5+
if (!result.source && url.startsWith('file:')) {
6+
result.source = await readFile(new URL(url))
7+
}
8+
return result
9+
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
// v18.19.0 backported ESM hook execution to a separate thread,
22
// thus being equivalent to >=v20.
3-
require('./v20-get-esm-exports')
3+
import './v20-get-esm-exports.mjs'

test/get-esm-exports/v20-get-esm-exports.js renamed to test/get-esm-exports/v20-get-esm-exports.mjs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
'use strict'
22

3-
const getEsmExports = require('../../lib/get-esm-exports.js')
4-
const fs = require('fs')
5-
const assert = require('assert')
6-
const path = require('path')
3+
import getEsmExports from '../../lib/get-esm-exports.mjs'
4+
import fs from 'fs'
5+
import assert from 'assert'
6+
import path from 'path'
7+
import { fileURLToPath } from 'url'
78

8-
const fixturePath = path.join(__dirname, '../fixtures/esm-exports.txt')
9+
const dirname = path.dirname(fileURLToPath(import.meta.url))
10+
11+
const fixturePath = path.join(dirname, '../fixtures/esm-exports.txt')
912
const fixture = fs.readFileSync(fixturePath, 'utf8')
1013

1114
fixture.split('\n').forEach(line => {

test/other/double-loading.mjs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Unless explicitly stated otherwise all files in this repository are licensed under the Apache 2.0 License.
2+
//
3+
// This product includes software developed at Datadog (https://www.datadoghq.com/). Copyright 2021 Datadog, Inc.
4+
5+
import { execSync } from 'child_process'
6+
import { doesNotThrow } from 'assert'
7+
8+
const env = {
9+
...process.env,
10+
NODE_OPTIONS: '--no-warnings --experimental-loader ./test/fixtures/double-loader.mjs --experimental-loader ./hook.mjs'
11+
}
12+
13+
doesNotThrow(() => {
14+
execSync('node -p 0', { env })
15+
})

0 commit comments

Comments
 (0)