Skip to content

Commit

Permalink
chore: align to TC39 name
Browse files Browse the repository at this point in the history
"Import assertions" has been renamed "import attributes" in TC39.
The commit reflects this change in the plugin.
  • Loading branch information
swiing committed Dec 22, 2024
1 parent 27e64df commit 9d347bc
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 40 deletions.
20 changes: 10 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
[![libera manifesto](https://img.shields.io/badge/libera-manifesto-lightgrey.svg)](https://liberamanifesto.com)

# rollup-plugin-import-assertions
# rollup-plugin-import-attributes

🍣 A Rollup plugin which bundles [import assertions](https://github.com/tc39/proposal-import-assertions).
🍣 A Rollup plugin which bundles [import attributes](https://github.com/tc39/proposal-import-attributes).

Two types of assertions are supported: `json` and `css`.
Two types of attributes are supported: `json` and `css`.

Currently, dynamic imports are not supported (PR welcomed).

Expand All @@ -13,23 +13,23 @@ Currently, dynamic imports are not supported (PR welcomed).
Using npm:

```console
npm install rollup-plugin-import-assertions --save-dev
npm install rollup-plugin-import-attributes --save-dev
```

## Usage

Create a `rollup.config.js` [configuration file](https://www.rollupjs.org/guide/en/#configuration-files) and import the plugin:

```js
import importAssertions from 'rollup-plugin-import-assertions';
import importAttributes from 'rollup-plugin-import-attributes';

export default {
input: 'src/index.js',
output: {
dir: 'output',
format: 'cjs'
},
plugins: [importAssertions()]
plugins: [importAttributes()]
};
```

Expand Down Expand Up @@ -63,11 +63,11 @@ customElements.define('my-element', MyElement);

## Options

For the `json` type of assertions, this plugin accepts the same options
For the `json` type of aattribute, this plugin accepts the same options
as those of [@rollup/plugin-json](https://github.com/rollup/plugins/tree/master/packages/json/).
This makes it straight-forward to move to import assertions, should one wish so.
This makes it straight-forward to move to import attributes, should one wish so.

For the `css` type of assertions, this plugin accepts the usual `include` and `exclude` options.
For the `css` type of attribute, this plugin accepts the usual `include` and `exclude` options.

### `compact` (type: 'json')

Expand Down Expand Up @@ -122,4 +122,4 @@ Credits to:

## License

![license](https://img.shields.io/github/license/swiing/rollup-plugin-import-assertions)
![license](https://img.shields.io/github/license/swiing/rollup-plugin-import-attributes)
13 changes: 7 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
{
"name": "rollup-plugin-import-assertions",
"name": "rollup-plugin-import-attributes",
"version": "0.5.0",
"description": "Bundles TC39 import assertions",
"description": "Bundles TC39 import attributes",
"license": "MIT",
"repository": "swiing/rollup-plugin-import-assertions",
"repository": "swiing/rollup-plugin-import-attributes",
"author": "swiing",
"homepage": "https://github.com/swiing/rollup-plugin-import-assertions#readme",
"bugs": "https://github.com/swiing/rollup-plugin-import-assertions/issues",
"homepage": "https://github.com/swiing/rollup-plugin-import-attributes#readme",
"bugs": "https://github.com/swiing/rollup-plugin-import-attributes/issues",
"main": "dist/index.js",
"module": "dist/index.es.js",
"scripts": {
Expand All @@ -33,7 +33,8 @@
"rollup-plugin",
"import",
"assertions",
"import assertions",
"attributes",
"import attributes",
"json",
"css"
],
Expand Down
18 changes: 9 additions & 9 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,32 +11,32 @@ import convert from './convert';

// Implementation principle:
//
// When a module is processed, we look for import assertions;
// When a module is processed, we look for import attributes;
// for each one found, we attach adhoc information to the corresponding module.
// When a module is transformed, we check for such adhoc information;
// if present, we transform accordingly.
//
// In rollup v2, adhoc information is set/found in `meta: { 'import-assertions': <type> }`
// In rollup v2, adhoc information is set/found in `meta: { 'import-attributes': <type> }`
// In rollup v3, there is some support of import assertions, so we leverage this.
// In that case, adhoc information is set/found in `assertions : { type: <type> }`
//
// Supported meta information is "json" and "css".

// options are same as for @rollup/plugin-json
// see https://github.com/rollup/plugins/tree/master/packages/json
export default function importAssertions(options = {}) {
export default function importAttributes(options = {}) {
const filter = createFilter(options.include, options.exclude);
const indent = 'indent' in options ? options.indent : '\t';
const treatAsExternal = [];

return {
name: 'import-assertions',
name: 'import-attributes',

// we want to make sure acorn knows how to parse import assertions
// we want to make sure acorn knows how to parse import attributes

// For rollup v2 or v2,
// the acorn parser only implements stage 4 js proposals.
// At the moment "import assertions" are a stage 3 proposal and as such
// At the moment "import attributes" are a stage 3 proposal and as such
// cannot be parsed by acorn. However, there exist a plugin,
// so we inject the adhoc plugin into the options
// by leveraging https://rollupjs.org/guide/en/#acorninjectplugins
Expand Down Expand Up @@ -73,7 +73,7 @@ export default function importAssertions(options = {}) {
? moduleInfo.attributes.type /* rollup v4 */
: 'assertions' in moduleInfo
? moduleInfo.assertions.type /* rollup v3 */
: moduleInfo.meta['import-assertions']; /* rollup v<=2 */
: moduleInfo.meta['import-attributes']; /* rollup v<=2 */

if (assertType === 'json')
// from @rollup/plugin-json
Expand Down Expand Up @@ -122,7 +122,7 @@ export default sheet;`;
node.assertions
) {
// As per https://github.com/xtuc/acorn-import-assertions/blob/main/src/index.js#L167
// an import assertions node has (amongst others):
// an import attributes node has (amongst others):
// - a source node, whose value is the path
const sourceNode = node.source;
// - an (array of) assertions node, whose value is a Literal node, whose value is the type (i.e. "json"|"css")
Expand Down Expand Up @@ -153,7 +153,7 @@ export default sheet;`;
}
if (resolvedId.external) return;

const meta = { 'import-assertions': type };
const meta = { 'import-attributes': type };
const moduleInfo = this.getModuleInfo(resolvedId.id);
// case where the module has not been loaded yet.
if (!moduleInfo) {
Expand Down
4 changes: 2 additions & 2 deletions test/test.css.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const test = require('ava');

const { rollup } = require('rollup');

const importAssertions = require('../dist'); // eslint-disable-line import/no-unresolved
const importAttributes = require('../dist'); // eslint-disable-line import/no-unresolved

const { testBundle } = require('./util.js');

Expand All @@ -11,7 +11,7 @@ process.chdir(__dirname);
test('converts css', async (t) => {
const bundle = await rollup({
input: 'fixtures/css/main.js',
plugins: [importAssertions()]
plugins: [importAttributes()]
});
t.plan(1);
return testBundle(t, bundle);
Expand Down
22 changes: 11 additions & 11 deletions test/test.json.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const { nodeResolve } = require('@rollup/plugin-node-resolve');

const acorn = require('acorn');

const importAssertions = require('../dist'); // eslint-disable-line import/no-unresolved
const importAttributes = require('../dist'); // eslint-disable-line import/no-unresolved

// require('../../../util/test');
const { testBundle } = require('./util.js');
Expand All @@ -22,7 +22,7 @@ process.chdir(__dirname);
test('converts json', async (t) => {
const bundle = await rollup({
input: 'fixtures/basic/main.js',
plugins: [importAssertions()]
plugins: [importAttributes()]
});
t.plan(1);
return testBundle(t, bundle);
Expand All @@ -31,7 +31,7 @@ test('converts json', async (t) => {
test('handles arrays', async (t) => {
const bundle = await rollup({
input: 'fixtures/array/main.js',
plugins: [importAssertions()]
plugins: [importAttributes()]
});
t.plan(1);
return testBundle(t, bundle);
Expand All @@ -40,7 +40,7 @@ test('handles arrays', async (t) => {
test('handles literals', async (t) => {
const bundle = await rollup({
input: 'fixtures/literal/main.js',
plugins: [importAssertions()]
plugins: [importAttributes()]
});
t.plan(1);
return testBundle(t, bundle);
Expand All @@ -49,7 +49,7 @@ test('handles literals', async (t) => {
test('generates named exports', async (t) => {
const bundle = await rollup({
input: 'fixtures/named/main.js',
plugins: [importAssertions()]
plugins: [importAttributes()]
});

const { code, result } = await testBundle(t, bundle, { exports: {} });
Expand All @@ -61,7 +61,7 @@ test('generates named exports', async (t) => {
test('resolves extensionless imports in conjunction with the node-resolve plugin', async (t) => {
const bundle = await rollup({
input: 'fixtures/extensionless/main.js',
plugins: [nodeResolve({ extensions: ['.js', '.json'] }), importAssertions()]
plugins: [nodeResolve({ extensions: ['.js', '.json'] }), importAttributes()]
});
t.plan(2);
return testBundle(t, bundle);
Expand All @@ -70,7 +70,7 @@ test('resolves extensionless imports in conjunction with the node-resolve plugin
test('handles JSON objects with no valid keys (#19)', async (t) => {
const bundle = await rollup({
input: 'fixtures/no-valid-keys/main.js',
plugins: [importAssertions()]
plugins: [importAttributes()]
});
t.plan(1);
return testBundle(t, bundle);
Expand All @@ -81,14 +81,14 @@ test('handles garbage', async (t) => {

await rollup({
input: 'fixtures/garbage/main.js',
plugins: [importAssertions()],
plugins: [importAttributes()],
onwarn: (warning) => warns.push(warning)
}).catch(() => {});

const [{ message, id, position, plugin }] = warns;

t.is(warns.length, 1);
t.is(plugin, 'import-assertions');
t.is(plugin, 'import-attributes');
t.is(position, 1);
t.is(message, 'Could not parse JSON file');
t.regex(id, /(.*)bad.json$/);
Expand All @@ -99,15 +99,15 @@ test('handles garbage', async (t) => {
*/

const transform = (fixture, options = {}) =>
importAssertions(options).transform.call(
importAttributes(options).transform.call(
{
parse: (code) =>
acorn.parse(code, {
ecmaVersion: 6
}),
getModuleInfo: () => {
return {
meta: { 'import-assertions': 'json' }
meta: { 'import-attributes': 'json' }
};
}
},
Expand Down
4 changes: 2 additions & 2 deletions test/types.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { RollupOptions } from 'rollup';

import importAssertions from '..';
import importAttributes from '..';

const config: RollupOptions = {
input: 'main.js',
Expand All @@ -9,7 +9,7 @@ const config: RollupOptions = {
format: 'iife'
},
plugins: [
importAssertions({
importAttributes({
include: 'node_modules/**',
exclude: ['node_modules/foo/**', 'node_modules/bar/**'],
preferConst: true,
Expand Down

0 comments on commit 9d347bc

Please sign in to comment.