Skip to content

Commit f628aa6

Browse files
feat: support import attributes (#65749)
Co-authored-by: C. T. Lin <[email protected]>
1 parent 0b261f0 commit f628aa6

20 files changed

+118
-35
lines changed

packages/next/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@
134134
"@babel/plugin-proposal-object-rest-spread": "7.20.7",
135135
"@babel/plugin-syntax-bigint": "7.8.3",
136136
"@babel/plugin-syntax-dynamic-import": "7.8.3",
137-
"@babel/plugin-syntax-import-assertions": "7.22.5",
137+
"@babel/plugin-syntax-import-attributes": "7.22.5",
138138
"@babel/plugin-syntax-jsx": "7.22.5",
139139
"@babel/plugin-transform-modules-commonjs": "7.22.5",
140140
"@babel/plugin-transform-runtime": "7.22.5",

packages/next/src/build/babel/preset.ts

+6-1
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,12 @@ export default (
154154
},
155155
],
156156
require('next/dist/compiled/babel/plugin-syntax-dynamic-import'),
157-
require('next/dist/compiled/babel/plugin-syntax-import-assertions'),
157+
[
158+
require('next/dist/compiled/babel/plugin-syntax-import-attributes'),
159+
{
160+
deprecatedAssertSyntax: true,
161+
},
162+
],
158163
require('./plugins/react-loadable-plugin'),
159164
[
160165
require('next/dist/compiled/babel/plugin-proposal-class-properties'),

packages/next/src/bundles/babel/bundle.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,8 @@ function pluginSyntaxDynamicImport() {
7272
return require('next/dist/compiled/babel-packages').pluginSyntaxDynamicImport()
7373
}
7474

75-
function pluginSyntaxImportAssertions() {
76-
return require('next/dist/compiled/babel-packages').pluginSyntaxImportAssertions()
75+
function pluginSyntaxImportAttributes() {
76+
return require('next/dist/compiled/babel-packages').pluginSyntaxImportAttributes()
7777
}
7878

7979
function pluginSyntaxJsx() {
@@ -127,7 +127,7 @@ module.exports = {
127127
pluginProposalObjectRestSpread,
128128
pluginSyntaxBigint,
129129
pluginSyntaxDynamicImport,
130-
pluginSyntaxImportAssertions,
130+
pluginSyntaxImportAttributes,
131131
pluginSyntaxJsx,
132132
pluginTransformDefine,
133133
pluginTransformModulesCommonjs,

packages/next/src/bundles/babel/packages-bundle.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ function pluginSyntaxDynamicImport() {
2828
return require('@babel/plugin-syntax-dynamic-import')
2929
}
3030

31-
function pluginSyntaxImportAssertions() {
32-
return require('@babel/plugin-syntax-import-assertions')
31+
function pluginSyntaxImportAttributes() {
32+
return require('@babel/plugin-syntax-import-attributes')
3333
}
3434

3535
function pluginSyntaxJsx() {
@@ -72,7 +72,7 @@ module.exports = {
7272
pluginProposalObjectRestSpread,
7373
pluginSyntaxBigint,
7474
pluginSyntaxDynamicImport,
75-
pluginSyntaxImportAssertions,
75+
pluginSyntaxImportAttributes,
7676
pluginSyntaxJsx,
7777
pluginTransformDefine,
7878
pluginTransformModulesCommonjs,

packages/next/src/bundles/babel/packages/plugin-syntax-import-assertions.js

-1
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = require('./bundle').pluginSyntaxImportAttributes()

packages/next/src/compiled/babel-packages/packages-bundle.js

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

packages/next/src/compiled/babel/bundle.js

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

packages/next/src/compiled/babel/plugin-syntax-import-assertions.js

-1
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = require('./bundle').pluginSyntaxImportAttributes()

pnpm-lock.yaml

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

test/integration/import-assertion/test/index.test.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@ function basic(context) {
77
it('should handle json assertions', async () => {
88
const esHtml = await renderViaHTTP(context.appPort, '/es')
99
const tsHtml = await renderViaHTTP(context.appPort, '/ts')
10-
expect(esHtml).toContain('foo')
11-
expect(tsHtml).toContain('foo')
10+
// checking json value `foo` is not suffecient, since parse error
11+
// will include code stack include those values as source
12+
expect(esHtml).toContain(`<div id="__next">foo</div>`)
13+
expect(tsHtml).toContain(`<div id="__next">foo</div>`)
1214
})
1315
}
1416

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"foo": "foo"
3+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
declare const data: any
2+
export default data
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import data from '../data' with { type: 'json' }
2+
3+
export default function Es() {
4+
return data.foo
5+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import data from '../data' with { type: 'json' }
2+
3+
export default function Ts() {
4+
return data.foo
5+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { join } from 'path'
2+
import { renderViaHTTP, runDevSuite, runProdSuite } from 'next-test-utils'
3+
4+
const appDir = join(__dirname, '../')
5+
6+
function basic(context) {
7+
it('should handle json attributes', async () => {
8+
const esHtml = await renderViaHTTP(context.appPort, '/es')
9+
const tsHtml = await renderViaHTTP(context.appPort, '/ts')
10+
// checking json value `foo` is not suffecient, since parse error
11+
// will include code stack include those values as source
12+
expect(esHtml).toContain(`<div id="__next">foo</div>`)
13+
expect(tsHtml).toContain(`<div id="__next">foo</div>`)
14+
})
15+
}
16+
17+
runDevSuite('import-attributes', appDir, { runTests: basic })
18+
runProdSuite('import-attributes', appDir, { runTests: basic })
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"compilerOptions": {
3+
"target": "es5",
4+
"lib": ["dom", "dom.iterable", "esnext"],
5+
"allowJs": true,
6+
"skipLibCheck": true,
7+
"strict": false,
8+
"forceConsistentCasingInFileNames": true,
9+
"noEmit": true,
10+
"incremental": true,
11+
"esModuleInterop": true,
12+
"module": "esnext",
13+
"moduleResolution": "bundler",
14+
"resolveJsonModule": true,
15+
"isolatedModules": true,
16+
"jsx": "preserve"
17+
},
18+
"exclude": ["node_modules"],
19+
"include": ["**/*.d.ts", "**/*.ts", "**/*.tsx"]
20+
}

test/turbopack-build-tests-manifest.json

+12-1
Original file line numberDiff line numberDiff line change
@@ -11339,14 +11339,25 @@
1133911339
"runtimeError": false
1134011340
},
1134111341
"test/integration/import-assertion/test/index.test.js": {
11342-
"passed": ["import-assertion dev should handle json assertions"],
11342+
"passed": [],
1134311343
"failed": [
11344+
"import-assertion dev should handle json assertions",
1134411345
"production mode import-assertion prod should handle json assertions"
1134511346
],
1134611347
"pending": [],
1134711348
"flakey": [],
1134811349
"runtimeError": false
1134911350
},
11351+
"test/integration/import-attributes/test/index.test.js": {
11352+
"passed": [],
11353+
"failed": [
11354+
"import-attributes dev should handle json attributes",
11355+
"production mode import-attributes prod should handle json attributes"
11356+
],
11357+
"pending": [],
11358+
"flakey": [],
11359+
"runtimeError": false
11360+
},
1135011361
"test/integration/index-index/test/index.test.js": {
1135111362
"passed": [
1135211363
"nested index.js production mode should 404 on /index/index/index",

test/turbopack-dev-tests-manifest.json

+14-3
Original file line numberDiff line numberDiff line change
@@ -13426,11 +13426,22 @@
1342613426
"runtimeError": false
1342713427
},
1342813428
"test/integration/import-assertion/test/index.test.js": {
13429-
"passed": ["import-assertion dev should handle json assertions"],
13430-
"failed": [],
13431-
"pending": [
13429+
"passed": [],
13430+
"failed": [
13431+
"import-assertion dev should handle json assertions",
1343213432
"production mode import-assertion prod should handle json assertions"
1343313433
],
13434+
"pending": [],
13435+
"flakey": [],
13436+
"runtimeError": false
13437+
},
13438+
"test/integration/import-attributes/test/index.test.js": {
13439+
"passed": [],
13440+
"failed": [
13441+
"import-attributes dev should handle json attributes",
13442+
"production mode import-attributes prod should handle json attributes"
13443+
],
13444+
"pending": [],
1343413445
"flakey": [],
1343513446
"runtimeError": false
1343613447
},

0 commit comments

Comments
 (0)