Skip to content
This repository was archived by the owner on Mar 5, 2025. It is now read-only.

Commit 6159b6e

Browse files
committed
Add support for import assertion resolution
1 parent d330719 commit 6159b6e

8 files changed

+1944
-826
lines changed

package-lock.json

+1,895-799
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+14-14
Original file line numberDiff line numberDiff line change
@@ -29,33 +29,33 @@
2929
"license": "BSD-3-Clause",
3030
"author": "The Polymer Project Authors",
3131
"devDependencies": {
32-
"@babel/types": "^7.4.4",
33-
"@types/babel__traverse": "^7.0.6",
32+
"@babel/types": "^7.23.6",
33+
"@types/babel__traverse": "^7.20.5",
3434
"@types/get-stream": "^3.0.2",
35-
"@types/koa": "^2.0.48",
36-
"@types/koa-route": "^3.2.4",
37-
"@types/node": "^12.0.2",
38-
"@types/path-is-inside": "^1.0.0",
39-
"@types/resolve": "0.0.8",
35+
"@types/koa": "^2.13.12",
36+
"@types/koa-route": "^3.2.8",
37+
"@types/node": "^20.10.6",
38+
"@types/path-is-inside": "^1.0.3",
39+
"@types/resolve": "^1.20.6",
4040
"@types/supertest": "^2.0.7",
4141
"@types/tape": "^4.2.33",
4242
"clang-format": "^1.2.4",
43-
"depcheck": "^0.8.0",
44-
"koa": "^2.7.0",
43+
"depcheck": "^1.4.7",
44+
"koa": "^2.15.0",
4545
"koa-route": "^3.2.0",
4646
"rimraf": "^2.6.3",
4747
"source-map-support": "^0.5.12",
4848
"supertest": "^4.0.2",
4949
"tap-diff": "^0.1.1",
5050
"tape": "^4.10.1",
5151
"tslint": "^5.16.0",
52-
"typescript": "^3.4.5"
52+
"typescript": "^5.3.3"
5353
},
5454
"dependencies": {
55-
"@types/babel__generator": "^7.6.1",
56-
"@babel/generator": "^7.4.4",
57-
"@babel/parser": "^7.4.5",
58-
"@babel/traverse": "^7.4.5",
55+
"@types/babel__generator": "^7.6.8",
56+
"@babel/generator": "^7.23.6",
57+
"@babel/parser": "^7.23.6",
58+
"@babel/traverse": "^7.23.7",
5959
"@types/parse5": "^5.0.0",
6060
"get-stream": "^5.1.0",
6161
"parse5": "^5.1.0",

src/koa-module-specifier-transform.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -66,12 +66,14 @@ const defaultJSParser = (js: string): BabelNode =>
6666
'exportDefaultFrom',
6767
'exportNamespaceFrom',
6868
'importMeta',
69+
['importAttributes', { deprecatedAssertSyntax: true }],
6970
],
7071
}) as BabelNode;
7172

7273
const defaultJSSerializer = (ast: BabelNode): string =>
7374
babelSerialize(ast, {
7475
concise: false,
76+
importAttributesKeyword: 'with',
7577
jsescOption: {
7678
quotes: 'single',
7779
},
@@ -100,7 +102,7 @@ export const moduleSpecifierTransform =
100102
return;
101103
}
102104

103-
const body = await getBodyAsString(ctx.body);
105+
const body = await getBodyAsString(ctx.body as string | Stream | Buffer);
104106
// When there's no body to return, there's nothing to transform.
105107
if (!body) {
106108
return;

src/support/resolve-node-specifier.ts

+2-7
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,8 @@ export const resolveNodeSpecifier =
2626
const dependencyPath = nodeResolve.sync(specifier, {
2727
basedir: dirname(modulePath),
2828
extensions: ['.js', '.json', '.node'],
29-
packageFilter: (packageJson: {
30-
main?: string,
31-
module?: string,
32-
'jsnext:main'?: string,
33-
}) => Object.assign(packageJson, {
34-
main: packageJson.module || packageJson['jsnext:main'] ||
35-
packageJson.main
29+
packageFilter: (pkg) => Object.assign(pkg, {
30+
main: (pkg.module || pkg['jsnext:main'] || pkg.main) as string
3631
})
3732
});
3833
const resolvedURL = relativePathToURL(modulePath, dependencyPath);

src/test/koa-module-specifier-transform.test.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -136,14 +136,14 @@ test('moduleSpecifierTransform will convert dynamic imports', async (t) => {
136136
// retain parenthesis around single parameter anonymous functions,
137137
// so `(x) => ...` is unavoidably transformed to `x => ...`
138138
squeeze(`
139-
import('./node_modules/x/index.js').then(x => x.doStuff());
139+
import('./node_modules/x/index.js').then((x) => x.doStuff());
140140
`),
141141
'should transform dynamic import in external module');
142142
t.equal(
143143
squeeze((await request(server).get('/my-page.html')).text),
144144
squeeze(`
145145
<script type="module">
146-
import('./node_modules/x/index.js').then(x => x.doStuff());
146+
import('./node_modules/x/index.js').then((x) => x.doStuff());
147147
</script>
148148
`),
149149
'should transform dynamic import in inline module script');
@@ -227,8 +227,8 @@ test('moduleSpecifierTransform middleware logs errors', async (t) => {
227227
t.deepEqual(
228228
logger.errors.map((args: unknown[]) => args.join(' ')),
229229
[
230-
'Unable to transform module specifiers in "/my-module.js" due to SyntaxError: Unexpected token, expected ";" (2:17)',
231-
'Unable to transform module specifiers in "/my-page.html" due to SyntaxError: Unexpected token, expected ";" (2:19)',
230+
'Unable to transform module specifiers in "/my-module.js" due to SyntaxError: Missing semicolon. (2:16)',
231+
'Unable to transform module specifiers in "/my-page.html" due to SyntaxError: Missing semicolon. (2:18)',
232232
],
233233
'should log every error thrown');
234234
});

src/test/koa-node-resolve.test.ts

+21
Original file line numberDiff line numberDiff line change
@@ -140,3 +140,24 @@ test('nodeResolve middleware ignores unresolvable specifiers', async (t) => {
140140
'should leave unresolvable specifier in inline scripts alone');
141141
});
142142
});
143+
144+
test('nodeResolve middleware resolves imports with attributes', async (t) => {
145+
t.plan(1);
146+
const logger = testLogger();
147+
createAndServe(
148+
{
149+
middleware:
150+
[nodeResolve({root: fixturesPath, logger, logLevel: 'debug'})],
151+
routes: {
152+
'/my-module.js': `import styles from 'x/styles.css' with { type: 'css' };`,
153+
},
154+
},
155+
async (server) => {
156+
t.equal(
157+
squeeze((await request(server).get('/my-module.js')).text),
158+
squeeze(`
159+
import styles from './node_modules/x/styles.css' with { type: 'css' };
160+
`),
161+
'should transform specifiers in CSS module script');
162+
});
163+
});

test/fixtures/node_modules/x/styles.css

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

tsconfig.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
"noImplicitReturns": true,
1818
"noFallthroughCasesInSwitch": true,
1919
"moduleResolution": "node",
20-
"esModuleInterop": true
20+
"esModuleInterop": true,
21+
"skipLibCheck": true
2122
}
2223
}

0 commit comments

Comments
 (0)