Skip to content

Commit 648081b

Browse files
committed
allow importing of json files via import assert
1 parent 4d0df90 commit 648081b

File tree

5 files changed

+44
-0
lines changed

5 files changed

+44
-0
lines changed

hook.mjs

+5
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,11 @@ export async function resolve (specifier, context, parentResolve) {
9090
return url
9191
}
9292

93+
if (context.importAssertions && context.importAssertions.type === 'json') {
94+
return url
95+
}
96+
97+
9398
specifiers.set(url.url, specifier)
9499

95100
return {

test/fixtures/json.mjs

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
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 coolFile from './something.json' assert { type: 'json' };
6+
7+
export default {
8+
data: coolFile.data
9+
};

test/fixtures/something.json

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"data": "dog"
3+
}
+15
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 Hook from '../../index.js'
6+
import jsonMjs from '../fixtures/json.mjs'
7+
import { strictEqual } from 'assert'
8+
9+
Hook((exports, name) => {
10+
if (name.match(/json\.mjs/)) {
11+
exports.default.data += '-dawg'
12+
}
13+
})
14+
15+
strictEqual(jsonMjs.data, 'dog-dawg')

test/runtest

+12
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,18 @@ const args = [
1515
'--unhandled-rejections=strict',
1616
...process.argv.slice(2)
1717
]
18+
19+
const [processMajor] = process.versions.node.split('.').map(Number)
20+
21+
const match = filename.match(/v([0-9]+)/)
22+
23+
const versionRequirement = match ? match[1] : 0;
24+
25+
if (processMajor < versionRequirement) {
26+
console.log(`skipping ${filename} as this is Node.js v${processMajor} and test wants v${versionRequirement}`);
27+
process.exit(0);
28+
}
29+
1830
if (!filename.includes('disabled')) {
1931
const isTypescript = path.extname(filename).slice(-2) === 'ts'
2032
const loaderPath = isTypescript

0 commit comments

Comments
 (0)