Skip to content

Commit

Permalink
feat(json): add includeArbitraryNames option (rollup#1641)
Browse files Browse the repository at this point in the history
  • Loading branch information
sapphi-red authored Dec 12, 2023
1 parent d49bbe8 commit 7f59e56
Show file tree
Hide file tree
Showing 8 changed files with 47 additions and 3 deletions.
4 changes: 4 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,7 @@ packages/typescript/test/fixtures/syntax-error

# temporary workaround for eslint bug where package.json is a directory
packages/node-resolve/test/fixtures/package-json-in-path

# temporary workaround for TypeScript as it doesn't support "Arbitrary module namespace identifier names"
# https://github.com/microsoft/TypeScript/issues/40594
packages/json/test/fixtures/arbitrary/main.js
7 changes: 7 additions & 0 deletions packages/json/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,13 @@ Default: `null`
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.
### `includeArbitraryNames`
Type: `Boolean`<br>
Default: `false`
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).
### `indent`
Type: `String`<br>
Expand Down
2 changes: 1 addition & 1 deletion packages/json/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
}
},
"dependencies": {
"@rollup/pluginutils": "^5.0.1"
"@rollup/pluginutils": "^5.1.0"
},
"devDependencies": {
"@rollup/plugin-buble": "^1.0.0",
Expand Down
1 change: 1 addition & 0 deletions packages/json/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export default function json(options = {}) {
preferConst: options.preferConst,
compact: options.compact,
namedExports: options.namedExports,
includeArbitraryNames: options.includeArbitraryNames,
indent
}),
map: { mappings: '' }
Expand Down
3 changes: 3 additions & 0 deletions packages/json/test/fixtures/arbitrary/foo.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"foo.bar": "baz"
}
3 changes: 3 additions & 0 deletions packages/json/test/fixtures/arbitrary/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export { 'foo.bar' as bar } from './foo.json';

result = exports; // eslint-disable-line no-undef
11 changes: 11 additions & 0 deletions packages/json/test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,17 @@ test('generates named exports', async (t) => {
t.is(code.indexOf('this-should-be-excluded'), -1, 'should exclude unused properties');
});

test('generates named exports including arbitrary names', async (t) => {
const bundle = await rollup({
input: 'fixtures/arbitrary/main.js',
plugins: [json({ includeArbitraryNames: true })]
});

const { result } = await testBundle(t, bundle, { inject: { exports: {} } });

t.is(result.bar, 'baz');
});

test('resolves extensionless imports in conjunction with the node-resolve plugin', async (t) => {
const bundle = await rollup({
input: 'fixtures/extensionless/main.js',
Expand Down
19 changes: 17 additions & 2 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 7f59e56

Please sign in to comment.