Skip to content

Commit 7f59e56

Browse files
authored
feat(json): add includeArbitraryNames option (#1641)
1 parent d49bbe8 commit 7f59e56

File tree

8 files changed

+47
-3
lines changed

8 files changed

+47
-3
lines changed

.eslintignore

+4
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,7 @@ packages/typescript/test/fixtures/syntax-error
77

88
# temporary workaround for eslint bug where package.json is a directory
99
packages/node-resolve/test/fixtures/package-json-in-path
10+
11+
# temporary workaround for TypeScript as it doesn't support "Arbitrary module namespace identifier names"
12+
# https://github.com/microsoft/TypeScript/issues/40594
13+
packages/json/test/fixtures/arbitrary/main.js

packages/json/README.md

+7
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,13 @@ Default: `null`
7575
7676
A [picomatch pattern](https://github.com/micromatch/picomatch), or array of patterns, which specifies the files in the build the plugin should operate on. By default all files are targeted.
7777
78+
### `includeArbitraryNames`
79+
80+
Type: `Boolean`<br>
81+
Default: `false`
82+
83+
If `true` and `namedExports` is `true`, generates a named export for not a valid identifier properties of the JSON object by leveraging the ["Arbitrary Module Namespace Identifier Names" feature](https://github.com/tc39/ecma262/pull/2154).
84+
7885
### `indent`
7986
8087
Type: `String`<br>

packages/json/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@
6161
}
6262
},
6363
"dependencies": {
64-
"@rollup/pluginutils": "^5.0.1"
64+
"@rollup/pluginutils": "^5.1.0"
6565
},
6666
"devDependencies": {
6767
"@rollup/plugin-buble": "^1.0.0",

packages/json/src/index.js

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ export default function json(options = {}) {
1818
preferConst: options.preferConst,
1919
compact: options.compact,
2020
namedExports: options.namedExports,
21+
includeArbitraryNames: options.includeArbitraryNames,
2122
indent
2223
}),
2324
map: { mappings: '' }
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"foo.bar": "baz"
3+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export { 'foo.bar' as bar } from './foo.json';
2+
3+
result = exports; // eslint-disable-line no-undef

packages/json/test/test.js

+11
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,17 @@ test('generates named exports', async (t) => {
5454
t.is(code.indexOf('this-should-be-excluded'), -1, 'should exclude unused properties');
5555
});
5656

57+
test('generates named exports including arbitrary names', async (t) => {
58+
const bundle = await rollup({
59+
input: 'fixtures/arbitrary/main.js',
60+
plugins: [json({ includeArbitraryNames: true })]
61+
});
62+
63+
const { result } = await testBundle(t, bundle, { inject: { exports: {} } });
64+
65+
t.is(result.bar, 'baz');
66+
});
67+
5768
test('resolves extensionless imports in conjunction with the node-resolve plugin', async (t) => {
5869
const bundle = await rollup({
5970
input: 'fixtures/extensionless/main.js',

pnpm-lock.yaml

+17-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)