Skip to content

Commit 34ff5e4

Browse files
jugglinmikerwaldron
authored andcommitted
Allow modules to import files with .json ext.
1 parent d180999 commit 34ff5e4

File tree

3 files changed

+22
-4
lines changed

3 files changed

+22
-4
lines changed

lib/dependencies.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
const fs = require('fs');
44
const path = require('path');
55
const rawSourceCache = new Map();
6+
const dependencySuffixes = /\.js|\.mjs|\.json/;
7+
const dependencySuffixesRight = new RegExp(`(${dependencySuffixes.source})$`);
68

79
function escapeModuleSpecifier(string) {
810
return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
@@ -25,13 +27,11 @@ function findModuleSpecifiers(source) {
2527
// be parseable.
2628
let lines = source.split(/\r?\n/g);
2729
return lines.reduce((accum, line) => {
28-
if (line.includes('.js') || line.includes('.mjs')) {
30+
if (dependencySuffixes.test(line)) {
2931
let parsed = /(import|import\(|from)(\s*)('(.*?)'|"(.*?)")/g.exec(line);
3032
if (parsed && parsed.length) {
3133
for (let entry of parsed) {
32-
if (entry &&
33-
(entry.endsWith('.js') || entry.endsWith('.mjs')) &&
34-
!accum.includes(entry)) {
34+
if (entry && dependencySuffixesRight.test(entry) && !accum.includes(entry)) {
3535
accum.push(entry.replace('./', ''));
3636
}
3737
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Copyright (C) 2021 the V8 project authors. All rights reserved.
2+
// This code is governed by the BSD license found in the LICENSE file.
3+
/*---
4+
description: This is a test for loading files with a `.json` extension.
5+
esid: sec-parsemodule
6+
flags: [module]
7+
---*/
8+
9+
import './json-module_FIXTURE.json';
10+
11+
/**
12+
* At the time of this writing, neither the "import assertions" proposal [1]
13+
* nor the "JSON modules" proposal [2] are widely implemented. In order to
14+
* verify that the harness correctly identifies JSON dependencies without
15+
* requiring the new semantics, this test simply loads a JSON file which
16+
* happens to parse as JavaScript, and it does not validate the exported value.
17+
*/
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
"This file is valid JSON and valid JavaScript."

0 commit comments

Comments
 (0)