Skip to content

Commit

Permalink
feat: support import attributes (vercel#65749)
Browse files Browse the repository at this point in the history
Co-authored-by: C. T. Lin <chentsulin@gmail.com>
  • Loading branch information
2 people authored and panteliselef committed May 20, 2024
1 parent f6c4441 commit c7016bd
Show file tree
Hide file tree
Showing 20 changed files with 118 additions and 35 deletions.
2 changes: 1 addition & 1 deletion packages/next/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@
"@babel/plugin-proposal-object-rest-spread": "7.20.7",
"@babel/plugin-syntax-bigint": "7.8.3",
"@babel/plugin-syntax-dynamic-import": "7.8.3",
"@babel/plugin-syntax-import-assertions": "7.22.5",
"@babel/plugin-syntax-import-attributes": "7.22.5",
"@babel/plugin-syntax-jsx": "7.22.5",
"@babel/plugin-transform-modules-commonjs": "7.22.5",
"@babel/plugin-transform-runtime": "7.22.5",
Expand Down
7 changes: 6 additions & 1 deletion packages/next/src/build/babel/preset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,12 @@ export default (
},
],
require('next/dist/compiled/babel/plugin-syntax-dynamic-import'),
require('next/dist/compiled/babel/plugin-syntax-import-assertions'),
[
require('next/dist/compiled/babel/plugin-syntax-import-attributes'),
{
deprecatedAssertSyntax: true,
},
],
require('./plugins/react-loadable-plugin'),
[
require('next/dist/compiled/babel/plugin-proposal-class-properties'),
Expand Down
6 changes: 3 additions & 3 deletions packages/next/src/bundles/babel/bundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ function pluginSyntaxDynamicImport() {
return require('next/dist/compiled/babel-packages').pluginSyntaxDynamicImport()
}

function pluginSyntaxImportAssertions() {
return require('next/dist/compiled/babel-packages').pluginSyntaxImportAssertions()
function pluginSyntaxImportAttributes() {
return require('next/dist/compiled/babel-packages').pluginSyntaxImportAttributes()
}

function pluginSyntaxJsx() {
Expand Down Expand Up @@ -127,7 +127,7 @@ module.exports = {
pluginProposalObjectRestSpread,
pluginSyntaxBigint,
pluginSyntaxDynamicImport,
pluginSyntaxImportAssertions,
pluginSyntaxImportAttributes,
pluginSyntaxJsx,
pluginTransformDefine,
pluginTransformModulesCommonjs,
Expand Down
6 changes: 3 additions & 3 deletions packages/next/src/bundles/babel/packages-bundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ function pluginSyntaxDynamicImport() {
return require('@babel/plugin-syntax-dynamic-import')
}

function pluginSyntaxImportAssertions() {
return require('@babel/plugin-syntax-import-assertions')
function pluginSyntaxImportAttributes() {
return require('@babel/plugin-syntax-import-attributes')
}

function pluginSyntaxJsx() {
Expand Down Expand Up @@ -72,7 +72,7 @@ module.exports = {
pluginProposalObjectRestSpread,
pluginSyntaxBigint,
pluginSyntaxDynamicImport,
pluginSyntaxImportAssertions,
pluginSyntaxImportAttributes,
pluginSyntaxJsx,
pluginTransformDefine,
pluginTransformModulesCommonjs,
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require('./bundle').pluginSyntaxImportAttributes()
6 changes: 3 additions & 3 deletions packages/next/src/compiled/babel-packages/packages-bundle.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion packages/next/src/compiled/babel/bundle.js

Large diffs are not rendered by default.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require('./bundle').pluginSyntaxImportAttributes()
31 changes: 16 additions & 15 deletions pnpm-lock.yaml

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

6 changes: 4 additions & 2 deletions test/integration/import-assertion/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ function basic(context) {
it('should handle json assertions', async () => {
const esHtml = await renderViaHTTP(context.appPort, '/es')
const tsHtml = await renderViaHTTP(context.appPort, '/ts')
expect(esHtml).toContain('foo')
expect(tsHtml).toContain('foo')
// checking json value `foo` is not suffecient, since parse error
// will include code stack include those values as source
expect(esHtml).toContain(`<div id="__next">foo</div>`)
expect(tsHtml).toContain(`<div id="__next">foo</div>`)
})
}

Expand Down
3 changes: 3 additions & 0 deletions test/integration/import-attributes/data
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"foo": "foo"
}
2 changes: 2 additions & 0 deletions test/integration/import-attributes/data.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
declare const data: any
export default data
5 changes: 5 additions & 0 deletions test/integration/import-attributes/pages/es.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import data from '../data' with { type: 'json' }

export default function Es() {
return data.foo
}
5 changes: 5 additions & 0 deletions test/integration/import-attributes/pages/ts.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import data from '../data' with { type: 'json' }

export default function Ts() {
return data.foo
}
18 changes: 18 additions & 0 deletions test/integration/import-attributes/test/index.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { join } from 'path'
import { renderViaHTTP, runDevSuite, runProdSuite } from 'next-test-utils'

const appDir = join(__dirname, '../')

function basic(context) {
it('should handle json attributes', async () => {
const esHtml = await renderViaHTTP(context.appPort, '/es')
const tsHtml = await renderViaHTTP(context.appPort, '/ts')
// checking json value `foo` is not suffecient, since parse error
// will include code stack include those values as source
expect(esHtml).toContain(`<div id="__next">foo</div>`)
expect(tsHtml).toContain(`<div id="__next">foo</div>`)
})
}

runDevSuite('import-attributes', appDir, { runTests: basic })
runProdSuite('import-attributes', appDir, { runTests: basic })
20 changes: 20 additions & 0 deletions test/integration/import-attributes/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"compilerOptions": {
"target": "es5",
"lib": ["dom", "dom.iterable", "esnext"],
"allowJs": true,
"skipLibCheck": true,
"strict": false,
"forceConsistentCasingInFileNames": true,
"noEmit": true,
"incremental": true,
"esModuleInterop": true,
"module": "esnext",
"moduleResolution": "bundler",
"resolveJsonModule": true,
"isolatedModules": true,
"jsx": "preserve"
},
"exclude": ["node_modules"],
"include": ["**/*.d.ts", "**/*.ts", "**/*.tsx"]
}
13 changes: 12 additions & 1 deletion test/turbopack-build-tests-manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -11339,14 +11339,25 @@
"runtimeError": false
},
"test/integration/import-assertion/test/index.test.js": {
"passed": ["import-assertion dev should handle json assertions"],
"passed": [],
"failed": [
"import-assertion dev should handle json assertions",
"production mode import-assertion prod should handle json assertions"
],
"pending": [],
"flakey": [],
"runtimeError": false
},
"test/integration/import-attributes/test/index.test.js": {
"passed": [],
"failed": [
"import-attributes dev should handle json attributes",
"production mode import-attributes prod should handle json attributes"
],
"pending": [],
"flakey": [],
"runtimeError": false
},
"test/integration/index-index/test/index.test.js": {
"passed": [
"nested index.js production mode should 404 on /index/index/index",
Expand Down
17 changes: 14 additions & 3 deletions test/turbopack-dev-tests-manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -13426,11 +13426,22 @@
"runtimeError": false
},
"test/integration/import-assertion/test/index.test.js": {
"passed": ["import-assertion dev should handle json assertions"],
"failed": [],
"pending": [
"passed": [],
"failed": [
"import-assertion dev should handle json assertions",
"production mode import-assertion prod should handle json assertions"
],
"pending": [],
"flakey": [],
"runtimeError": false
},
"test/integration/import-attributes/test/index.test.js": {
"passed": [],
"failed": [
"import-attributes dev should handle json attributes",
"production mode import-attributes prod should handle json attributes"
],
"pending": [],
"flakey": [],
"runtimeError": false
},
Expand Down

0 comments on commit c7016bd

Please sign in to comment.