Skip to content

Commit 4bc94d1

Browse files
feat: use type=module in (most) generated projects
With the exception of Nighwatch templates due to nightwatchjs/nightwatch#3959 Closes #389 Largely inspired by @cexbrayat's work in that PR. I've also made the generation of the root `tsconfig.json` programmatic because it's becoming more and more convoluted. Co-authored-by: Cédric Exbrayat <[email protected]>
1 parent 4092ff2 commit 4bc94d1

File tree

16 files changed

+54
-222
lines changed

16 files changed

+54
-222
lines changed

Diff for: index.ts

+41-2
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ import generateReadme from './utils/generateReadme'
1717
import getCommand from './utils/getCommand'
1818
import getLanguage from './utils/getLanguage'
1919
import renderEslint from './utils/renderEslint'
20-
import { FILES_TO_FILTER } from './utils/filterList'
2120

2221
function isValidPackageName(projectName) {
2322
return /^(?:@[a-z0-9-*~][a-z0-9-*._~]*\/)?[a-z0-9-~][a-z0-9-._~]*$/.test(projectName)
@@ -371,24 +370,64 @@ async function init() {
371370

372371
// Render tsconfigs
373372
render('tsconfig/base')
373+
// The content of the root `tsconfig.json` is a bit complicated,
374+
// So here we are programmatically generating it.
375+
const rootTsConfig = {
376+
// It doesn't target any specific files because they are all configured in the referenced ones.
377+
files: [],
378+
// All templates contain at least a `.node` and a `.app` tsconfig.
379+
references: [
380+
{
381+
path: './tsconfig.node.json'
382+
},
383+
{
384+
path: './tsconfig.app.json'
385+
}
386+
]
387+
}
374388
if (needsCypress) {
375389
render('tsconfig/cypress')
390+
// Cypress uses `ts-node` internally, which doesn't support solution-style tsconfig.
391+
// So we have to set a dummy `compilerOptions` in the root tsconfig to make it work.
392+
// I use `NodeNext` here instead of `ES2015` because that's what the actual environment is.
393+
// (Cypress uses the ts-node/esm loader when `type: module` is specified in package.json.)
394+
// @ts-ignore
395+
rootTsConfig.compilerOptions = {
396+
module: 'NodeNext'
397+
}
376398
}
377399
if (needsCypressCT) {
378400
render('tsconfig/cypress-ct')
401+
// Cypress Component Testing needs a standalone tsconfig.
402+
rootTsConfig.references.push({
403+
path: './tsconfig.cypress-ct.json'
404+
})
379405
}
380406
if (needsPlaywright) {
381407
render('tsconfig/playwright')
382408
}
383409
if (needsVitest) {
384410
render('tsconfig/vitest')
411+
// Vitest needs a standalone tsconfig.
412+
rootTsConfig.references.push({
413+
path: './tsconfig.vitest.json'
414+
})
385415
}
386416
if (needsNightwatch) {
387417
render('tsconfig/nightwatch')
418+
// Nightwatch needs a standalone tsconfig, but in a different folder.
419+
rootTsConfig.references.push({
420+
path: './nightwatch/tsconfig.json'
421+
})
388422
}
389423
if (needsNightwatchCT) {
390424
render('tsconfig/nightwatch-ct')
391425
}
426+
fs.writeFileSync(
427+
path.resolve(root, 'tsconfig.json'),
428+
JSON.stringify(rootTsConfig, null, 2) + '\n',
429+
'utf-8'
430+
)
392431
}
393432

394433
// Render ESLint config
@@ -456,7 +495,7 @@ async function init() {
456495
root,
457496
() => {},
458497
(filepath) => {
459-
if (filepath.endsWith('.js') && !FILES_TO_FILTER.includes(path.basename(filepath))) {
498+
if (filepath.endsWith('.js')) {
460499
const tsFilePath = filepath.replace(/\.js$/, '.ts')
461500
if (fs.existsSync(tsFilePath)) {
462501
fs.unlinkSync(filepath)

Diff for: template/base/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{
22
"private": true,
3+
"type": "module",
34
"scripts": {
45
"dev": "vite",
56
"build": "vite build",

Diff for: template/config/cypress-ct/cypress.config.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
const { defineConfig } = require('cypress')
1+
import { defineConfig } from 'cypress'
22

3-
module.exports = defineConfig({
3+
export default defineConfig({
44
e2e: {
55
specPattern: 'cypress/e2e/**/*.{cy,spec}.{js,jsx,ts,tsx}',
66
baseUrl: 'http://localhost:4173'

Diff for: template/config/cypress-ct/cypress.config.ts

-15
This file was deleted.

Diff for: template/config/cypress/cypress.config.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
const { defineConfig } = require('cypress')
1+
import { defineConfig } from 'cypress'
22

3-
module.exports = defineConfig({
3+
export default defineConfig({
44
e2e: {
55
specPattern: 'cypress/e2e/**/*.{cy,spec}.{js,jsx,ts,tsx}',
66
baseUrl: 'http://localhost:4173'

Diff for: template/config/cypress/cypress.config.ts

-8
This file was deleted.

Diff for: template/config/nightwatch/package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
{
2+
"type": "commonjs",
23
"scripts": {
34
"test:e2e": "nightwatch tests/e2e/*"
45
},
56
"devDependencies": {
67
"nightwatch": "^3.3.2",
7-
"@nightwatch/vue": "0.4.5",
8+
"@nightwatch/vue": "^0.4.5",
89
"@vitejs/plugin-vue": "^4.5.1",
910
"@types/nightwatch": "^2.3.30",
1011
"geckodriver": "^4.2.1",

Diff for: template/config/playwright/e2e/vue.spec.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const { test, expect } = require('@playwright/test');
1+
import { test, expect } from '@playwright/test';
22

33
// See here how to get started:
44
// https://playwright.dev/docs/intro

Diff for: template/config/playwright/playwright.config.js

+5-8
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
// @ts-check
2-
const { devices } = require('@playwright/test')
1+
import { defineConfig, devices } from '@playwright/test'
32

43
/**
54
* Read environment variables from file.
@@ -8,10 +7,9 @@ const { devices } = require('@playwright/test')
87
// require('dotenv').config();
98

109
/**
11-
* @see https://playwright.dev/docs/test-configuration
12-
* @type {import('@playwright/test').PlaywrightTestConfig}
10+
* See https://playwright.dev/docs/test-configuration.
1311
*/
14-
const config = {
12+
export default defineConfig({
1513
testDir: './e2e',
1614
/* Maximum time one test can run for. */
1715
timeout: 30 * 1000,
@@ -102,11 +100,10 @@ const config = {
102100
/**
103101
* Use the dev server by default for faster feedback loop.
104102
* Use the preview server on CI for more realistic testing.
103+
* Playwright will re-use the local server if there is already a dev-server running.
105104
*/
106105
command: process.env.CI ? 'vite preview --port 5173' : 'vite dev',
107106
port: 5173,
108107
reuseExistingServer: !process.env.CI
109108
}
110-
}
111-
112-
module.exports = config
109+
})

Diff for: template/config/playwright/playwright.config.ts

-112
This file was deleted.

Diff for: template/tsconfig/base/tsconfig.json

-11
This file was deleted.

Diff for: template/tsconfig/cypress-ct/tsconfig.json

-14
This file was deleted.

Diff for: template/tsconfig/nightwatch-ct/tsconfig.json

-14
This file was deleted.

Diff for: template/tsconfig/nightwatch/tsconfig.json

-17
This file was deleted.

Diff for: template/tsconfig/vitest/tsconfig.json

-14
This file was deleted.

Diff for: utils/filterList.ts

-1
This file was deleted.

0 commit comments

Comments
 (0)