This repository has been archived by the owner on Apr 16, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
esm: add experimental .json support to loader
With the new flag `--experimental-json-modules` it is now possible to import .json files. It piggy backs on the current cjs loader implementation, so it only exports a default. This is a bit of a hack, and it should potentially have it's own loader, especially if we change the cjs loader at all. The behavior for .json in the cjs loader matches the current planned behavior if json modules were to be standardized, specifically that a .json module only exports a default. Refs: nodejs/modules#255 Refs: whatwg/html#4315 Refs: WICG/webcomponents#770
- Loading branch information
1 parent
b4abd3a
commit 3cfe571
Showing
10 changed files
with
109 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
// Flags: --experimental-modules --experimental-json-modules | ||
/* eslint-disable node-core/required-modules */ | ||
import '../common/index.mjs'; | ||
|
||
import { strictEqual, deepStrictEqual } from 'assert'; | ||
|
||
import { createRequireFromPath as createRequire } from 'module'; | ||
import { fileURLToPath as fromURL } from 'url'; | ||
|
||
import mod from '../fixtures/es-modules/json-cache/mod.cjs'; | ||
import another from '../fixtures/es-modules/json-cache/another.cjs'; | ||
import test from '../fixtures/es-modules/json-cache/test.json'; | ||
|
||
const require = createRequire(fromURL(import.meta.url)); | ||
|
||
const modCjs = require('../fixtures/es-modules/json-cache/mod.cjs'); | ||
const anotherCjs = require('../fixtures/es-modules/json-cache/another.cjs'); | ||
const testCjs = require('../fixtures/es-modules/json-cache/test.json'); | ||
|
||
strictEqual(mod.one, 1); | ||
strictEqual(another.one, 'zalgo'); | ||
strictEqual(test.one, 'it comes'); | ||
|
||
deepStrictEqual(mod, modCjs); | ||
deepStrictEqual(another, anotherCjs); | ||
deepStrictEqual(test, testCjs); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
// Flags: --experimental-modules --experimental-json-modules | ||
/* eslint-disable node-core/required-modules */ | ||
|
||
import '../common/index.mjs'; | ||
import { strictEqual } from 'assert'; | ||
|
||
import secret from '../fixtures/experimental.json'; | ||
|
||
strictEqual(secret.ofLife, 42); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
const test = require('./test.json'); | ||
|
||
module.exports = { | ||
...test | ||
}; | ||
|
||
test.one = 'it comes'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
const test = require('./test.json'); | ||
|
||
module.exports = { | ||
...test | ||
}; | ||
|
||
test.one = 'zalgo'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"one": 1, | ||
"two": 2, | ||
"three": 3 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"ofLife": 42 | ||
} |