Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use standard rollup resolution #11

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.

### [2.1.4](https://github.com/calebdwilliams/rollup-plugin-import-assert/compare/v2.1.3...v2.1.4) (2022-12-02)

### [2.1.3](https://github.com/calebdwilliams/rollup-plugin-import-assert/compare/v2.1.2...v2.1.3) (2022-11-08)

### [2.1.2](https://github.com/calebdwilliams/rollup-plugin-import-assert/compare/v2.1.0...v2.1.2) (2022-10-27)
Expand Down
73 changes: 48 additions & 25 deletions package-lock.json

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

3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "rollup-plugin-import-assert",
"version": "2.1.3",
"version": "2.1.4",
"description": "A Rollup plugin to add import assertion behavior and syntax support",
"main": "dist/import-assert.js",
"module": "dist/import-assert.js",
Expand Down Expand Up @@ -33,7 +33,6 @@
"devDependencies": {
"@rollup/plugin-commonjs": "^21.0.1",
"@rollup/plugin-node-resolve": "^13.1.2",
"@types/node": "^16.0.0",
"acorn-import-assertions": "^1.7.1",
"ava": "^3.15.0",
"rollup": "^2.52.7",
Expand Down
6 changes: 2 additions & 4 deletions rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,8 @@ export default {
file: 'dist/import-assert.cjs'
},
plugins: [
nodeResolve({
preferBuiltins: true
}),
nodeResolve(),
commonjs()

]
}
17 changes: 7 additions & 10 deletions src/import-assert.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import path from 'path';
import { Plugin } from 'rollup';
import convert from 'string-to-template-literal';

Expand Down Expand Up @@ -29,12 +28,10 @@ type Assertion = { type: 'css'|'json' };
const assertionMap = new Map<string, Assertion>();
const filePattern = /\.(js|ts|jsx|tsx)$/;

const getImportPath = (id: string, source: string): string => path.resolve(path.dirname(id), source);

export function importAssertionsPlugin(): Plugin {
return {
name: 'rollup-plugin-import-assert',
transform(data: string, id: string) {
async transform(data: string, id: string) {
let code = data;
/** If the file is a JS-like file, continue */
if (filePattern.exec(id)) {
Expand All @@ -46,16 +43,16 @@ export function importAssertionsPlugin(): Plugin {
const importDeclarations = getObjects(body, 'type', 'ImportDeclaration');
const importExpressions = getObjects(body, 'type', 'ImportExpression');

importDeclarations.forEach(node => {
for (const node of importDeclarations) {
if (node.assertions) {
const [ assertion ] = node.assertions as any;
const assert: Assertion = { type: assertion.value.value };
const importPath = getImportPath(id, node.source.value);
const importPath = (await this.resolve(node.source.value, id)).id
assertionMap.set(importPath, assert);
}
});
};

importExpressions.forEach(node => {
for (const node of importExpressions) {
// Skip dynamic imports with expressions
// @example: import(`./foo/${bar}.js`); // NOK
// @example: import(`./foo/bar.js`); // OK
Expand All @@ -67,7 +64,7 @@ export function importAssertionsPlugin(): Plugin {
if(!node.source.value) return;

const source = node.source.value || node.source.quasis[0].value.raw;
const importPath = getImportPath(id, source);
const importPath = (await this.resolve(source, id)).id;

// TODO: We can still make this better
if (node.hasOwnProperty('arguments') && getObjects(node, 'name', 'assert')) {
Expand All @@ -89,7 +86,7 @@ export function importAssertionsPlugin(): Plugin {
});
}
}
});
}
}

const assertion = assertionMap.get(id);
Expand Down