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

Experimental feature for allowing importing Typescript files outside of the root directory #22867

Merged
merged 7 commits into from
Mar 19, 2021
Merged
Show file tree
Hide file tree
Changes from 2 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
10 changes: 9 additions & 1 deletion packages/next/build/webpack-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -732,6 +732,12 @@ export default async function getBaseWebpackConfig(

const emacsLockfilePattern = '**/.#*'

// Allowing importing TS/TSX files from outside of the root dir.
let externalDir
if (config.experimental.externalDir) {
externalDir = path.join(dir, config.experimental.externalDir)
shuding marked this conversation as resolved.
Show resolved Hide resolved
}

let webpackConfig: webpack.Configuration = {
externals: !isServer
? // make sure importing "next" is handled gracefully for client
Expand Down Expand Up @@ -924,7 +930,9 @@ export default async function getBaseWebpackConfig(
: []),
{
test: /\.(tsx|ts|js|mjs|jsx)$/,
include: [dir, ...babelIncludeRegexes],
include: externalDir
? [dir, externalDir, ...babelIncludeRegexes]
: [dir, ...babelIncludeRegexes],
exclude: (excludePath: string) => {
if (babelIncludeRegexes.some((r) => r.test(excludePath))) {
return false
Expand Down
1 change: 1 addition & 0 deletions packages/next/next-server/server/config-shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ export const defaultConfig: NextConfig = {
scrollRestoration: false,
scriptLoader: false,
stats: false,
externalDir: false,
},
future: {
strictPostcssConfiguration: false,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import React from 'react'

export function World(): JSX.Element {
return <>World</>
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module.exports = {
onDemandEntries: {
// Make sure entries are not getting disposed.
maxInactiveAge: 1000 * 60 * 60,
},
experimental: {
externalDir: '../shared',
},
}
16 changes: 16 additions & 0 deletions test/integration/typescript-external-dir/project/pages/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import React from 'react'

import { World } from 'components/world'

// External
import { Counter } from '../../shared/components/counter'

export default function HelloPage(): JSX.Element {
return (
<div>
Hello <World />!
<br />
<Counter />
</div>
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/* eslint-env jest */

import { join } from 'path'
import cheerio from 'cheerio'
import { renderViaHTTP, findPort, launchApp, killApp } from 'next-test-utils'

jest.setTimeout(1000 * 60 * 2)

const appDir = join(__dirname, '..')
let appPort
let app

async function get$(path, query) {
const html = await renderViaHTTP(appPort, path, query)
return cheerio.load(html)
}

describe('TypeScript Features', () => {
describe('default behavior', () => {
beforeAll(async () => {
appPort = await findPort()
app = await launchApp(appDir, appPort, {})
})
afterAll(() => killApp(app))

it('should render the page with external TS/TSX dependencies', async () => {
const $ = await get$('/')
expect($('body').text()).toMatch(/Hello World!Counter: 0/)
})
})
})
20 changes: 20 additions & 0 deletions test/integration/typescript-external-dir/project/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"compilerOptions": {
"baseUrl": ".",
"esModuleInterop": true,
"module": "esnext",
"jsx": "preserve",
"target": "es5",
"lib": ["dom", "dom.iterable", "esnext"],
"allowJs": true,
"skipLibCheck": true,
"strict": true,
"forceConsistentCasingInFileNames": true,
"noEmit": true,
"moduleResolution": "node",
"resolveJsonModule": true,
"isolatedModules": true
},
"exclude": ["node_modules"],
"include": ["next-env.d.ts", "components", "pages"]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import React, { useState } from 'react'

import inc from '../libs/inc'

export function Counter(): JSX.Element {
const [x, setX] = useState(0)
return (
<button id="counter" onClick={() => setX(inc(x))}>
Counter: {x}
</button>
)
}
3 changes: 3 additions & 0 deletions test/integration/typescript-external-dir/shared/libs/inc.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default function inc(x: number) {
return x + 1
}
18 changes: 18 additions & 0 deletions test/integration/typescript-external-dir/shared/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"compilerOptions": {
"esModuleInterop": true,
"module": "esnext",
"jsx": "preserve",
"target": "es5",
"lib": ["dom", "dom.iterable", "esnext"],
"allowJs": true,
"skipLibCheck": true,
"strict": true,
"forceConsistentCasingInFileNames": true,
"noEmit": true,
"moduleResolution": "node",
"resolveJsonModule": true,
"isolatedModules": true
},
"include": ["components", "libs"]
}