diff --git a/README.md b/README.md index bf882fb..8df65f5 100644 --- a/README.md +++ b/README.md @@ -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). @@ -13,7 +13,7 @@ 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 @@ -21,7 +21,7 @@ npm install rollup-plugin-import-assertions --save-dev 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', @@ -29,7 +29,7 @@ export default { dir: 'output', format: 'cjs' }, - plugins: [importAssertions()] + plugins: [importAttributes()] }; ``` @@ -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') @@ -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) diff --git a/package.json b/package.json index 7a6e636..8093a83 100644 --- a/package.json +++ b/package.json @@ -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": { @@ -33,7 +33,8 @@ "rollup-plugin", "import", "assertions", - "import assertions", + "attributes", + "import attributes", "json", "css" ], diff --git a/src/index.js b/src/index.js index 86baa4d..6834ed1 100644 --- a/src/index.js +++ b/src/index.js @@ -11,12 +11,12 @@ 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': }` +// In rollup v2, adhoc information is set/found in `meta: { 'import-attributes': }` // 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: }` // @@ -24,19 +24,19 @@ import convert from './convert'; // 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 @@ -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 @@ -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") @@ -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) { diff --git a/test/test.css.js b/test/test.css.js index d9cbdc6..5d00fb7 100644 --- a/test/test.css.js +++ b/test/test.css.js @@ -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'); @@ -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); diff --git a/test/test.json.js b/test/test.json.js index 7bde81e..79a7b9f 100644 --- a/test/test.json.js +++ b/test/test.json.js @@ -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'); @@ -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); @@ -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); @@ -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); @@ -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: {} }); @@ -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); @@ -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); @@ -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$/); @@ -99,7 +99,7 @@ test('handles garbage', async (t) => { */ const transform = (fixture, options = {}) => - importAssertions(options).transform.call( + importAttributes(options).transform.call( { parse: (code) => acorn.parse(code, { @@ -107,7 +107,7 @@ const transform = (fixture, options = {}) => }), getModuleInfo: () => { return { - meta: { 'import-assertions': 'json' } + meta: { 'import-attributes': 'json' } }; } }, diff --git a/test/types.ts b/test/types.ts index c6e9dec..cb88853 100644 --- a/test/types.ts +++ b/test/types.ts @@ -1,6 +1,6 @@ import { RollupOptions } from 'rollup'; -import importAssertions from '..'; +import importAttributes from '..'; const config: RollupOptions = { input: 'main.js', @@ -9,7 +9,7 @@ const config: RollupOptions = { format: 'iife' }, plugins: [ - importAssertions({ + importAttributes({ include: 'node_modules/**', exclude: ['node_modules/foo/**', 'node_modules/bar/**'], preferConst: true,