Skip to content

Commit

Permalink
Allow modules to import files with .json ext.
Browse files Browse the repository at this point in the history
  • Loading branch information
jugglinmike authored and rwaldron committed May 28, 2021
1 parent d180999 commit 34ff5e4
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 4 deletions.
8 changes: 4 additions & 4 deletions lib/dependencies.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
const fs = require('fs');
const path = require('path');
const rawSourceCache = new Map();
const dependencySuffixes = /\.js|\.mjs|\.json/;
const dependencySuffixesRight = new RegExp(`(${dependencySuffixes.source})$`);

function escapeModuleSpecifier(string) {
return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
Expand All @@ -25,13 +27,11 @@ function findModuleSpecifiers(source) {
// be parseable.
let lines = source.split(/\r?\n/g);
return lines.reduce((accum, line) => {
if (line.includes('.js') || line.includes('.mjs')) {
if (dependencySuffixes.test(line)) {
let parsed = /(import|import\(|from)(\s*)('(.*?)'|"(.*?)")/g.exec(line);
if (parsed && parsed.length) {
for (let entry of parsed) {
if (entry &&
(entry.endsWith('.js') || entry.endsWith('.mjs')) &&
!accum.includes(entry)) {
if (entry && dependencySuffixesRight.test(entry) && !accum.includes(entry)) {
accum.push(entry.replace('./', ''));
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Copyright (C) 2021 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
description: This is a test for loading files with a `.json` extension.
esid: sec-parsemodule
flags: [module]
---*/

import './json-module_FIXTURE.json';

/**
* At the time of this writing, neither the "import assertions" proposal [1]
* nor the "JSON modules" proposal [2] are widely implemented. In order to
* verify that the harness correctly identifies JSON dependencies without
* requiring the new semantics, this test simply loads a JSON file which
* happens to parse as JavaScript, and it does not validate the exported value.
*/
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"This file is valid JSON and valid JavaScript."

0 comments on commit 34ff5e4

Please sign in to comment.