Skip to content

feat: use type=module in (most) generated projects #159

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

Merged
Merged
Show file tree
Hide file tree
Changes from all 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
45 changes: 43 additions & 2 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import generateIndex from './utils/generateIndex'
import getCommand from './utils/getCommand'
import getLanguage from './utils/getLanguage'
import renderEslint from './utils/renderEslint'
import { FILES_TO_FILTER } from './utils/filterList'

function isValidPackageName(projectName) {
return /^(?:@[a-z0-9-*~][a-z0-9-*._~]*\/)?[a-z0-9-~][a-z0-9-._~]*$/.test(projectName)
Expand Down Expand Up @@ -405,24 +404,66 @@ async function init() {

// Render tsconfigs
render('tsconfig/base')
// The content of the root `tsconfig.json` is a bit complicated,
// So here we are programmatically generating it.
const rootTsConfig = {
// It doesn't target any specific files because they are all configured in the referenced ones.
files: [],
// All templates contain at least a `.node` and a `.app` tsconfig.
references: [
{
path: './tsconfig.node.json'
},
{
path: './tsconfig.app.json'
}
]
}

if (needsCypress) {
render('tsconfig/cypress')
// Cypress uses `ts-node` internally, which doesn't support solution-style tsconfig.
// So we have to set a dummy `compilerOptions` in the root tsconfig to make it work.
// I use `NodeNext` here instead of `ES2015` because that's what the actual environment is.
// (Cypress uses the ts-node/esm loader when `type: module` is specified in package.json.)
// @ts-ignore
rootTsConfig.compilerOptions = {
module: 'NodeNext'
}
}
if (needsCypressCT) {
render('tsconfig/cypress-ct')
// Cypress Component Testing needs a standalone tsconfig.
rootTsConfig.references.push({
path: './tsconfig.cypress-ct.json'
})
}
if (needsPlaywright) {
render('tsconfig/playwright')
}
if (needsVitest) {
render('tsconfig/vitest')
// Vitest needs a standalone tsconfig.
rootTsConfig.references.push({
path: './tsconfig.vitest.json'
})
}
if (needsNightwatch) {
render('tsconfig/nightwatch')
// Nightwatch needs a standalone tsconfig, but in a different folder.
rootTsConfig.references.push({
path: './nightwatch/tsconfig.json'
})
}
if (needsNightwatchCT) {
render('tsconfig/nightwatch-ct')
}

fs.writeFileSync(
path.resolve(root, 'tsconfig.json'),
JSON.stringify(rootTsConfig, null, 2) + '\n',
'utf-8'
)
}

if (needsVueUse) {
Expand Down Expand Up @@ -504,7 +545,7 @@ async function init() {
root,
() => {},
(filepath) => {
if (filepath.endsWith('.js') && !FILES_TO_FILTER.includes(path.basename(filepath))) {
if (filepath.endsWith('.js')) {
const tsFilePath = filepath.replace(/\.js$/, '.ts')
if (fs.existsSync(tsFilePath)) {
fs.unlinkSync(filepath)
Expand Down
1 change: 1 addition & 0 deletions template/base/package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"private": true,
"type": "module",
"scripts": {
"dev": "vite",
"build": "vite build",
Expand Down
4 changes: 2 additions & 2 deletions template/config/cypress-ct/cypress.config.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const { defineConfig } = require('cypress')
import { defineConfig } from 'cypress'

module.exports = defineConfig({
export default defineConfig({
e2e: {
specPattern: 'cypress/e2e/**/*.{cy,spec}.{js,jsx,ts,tsx}',
baseUrl: 'http://localhost:4173'
Expand Down
15 changes: 0 additions & 15 deletions template/config/cypress-ct/cypress.config.ts

This file was deleted.

4 changes: 2 additions & 2 deletions template/config/cypress/cypress.config.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const { defineConfig } = require('cypress')
import { defineConfig } from 'cypress'

module.exports = defineConfig({
export default defineConfig({
e2e: {
specPattern: 'cypress/e2e/**/*.{cy,spec}.{js,jsx,ts,tsx}',
baseUrl: 'http://localhost:4173'
Expand Down
8 changes: 0 additions & 8 deletions template/config/cypress/cypress.config.ts

This file was deleted.

3 changes: 2 additions & 1 deletion template/config/nightwatch/package.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
{
"type": "commonjs",
"scripts": {
"test:e2e": "nightwatch tests/e2e/*"
},
"devDependencies": {
"nightwatch": "^3.3.2",
"@nightwatch/vue": "0.4.5",
"@nightwatch/vue": "^0.4.5",
"@vitejs/plugin-vue": "^4.5.1",
"@types/nightwatch": "^2.3.30",
"geckodriver": "^4.2.1",
Expand Down
2 changes: 1 addition & 1 deletion template/config/playwright/e2e/vue.spec.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { test, expect } = require('@playwright/test');
import { test, expect } from '@playwright/test';

// See here how to get started:
// https://playwright.dev/docs/intro
Expand Down
13 changes: 5 additions & 8 deletions template/config/playwright/playwright.config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
// @ts-check
const { devices } = require('@playwright/test')
import { defineConfig, devices } from '@playwright/test'

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

/**
* @see https://playwright.dev/docs/test-configuration
* @type {import('@playwright/test').PlaywrightTestConfig}
* See https://playwright.dev/docs/test-configuration.
*/
const config = {
export default defineConfig({
testDir: './e2e',
/* Maximum time one test can run for. */
timeout: 30 * 1000,
Expand Down Expand Up @@ -102,11 +100,10 @@ const config = {
/**
* Use the dev server by default for faster feedback loop.
* Use the preview server on CI for more realistic testing.
* Playwright will re-use the local server if there is already a dev-server running.
*/
command: process.env.CI ? 'vite preview --port 5173' : 'vite dev',
port: 5173,
reuseExistingServer: !process.env.CI
}
}

module.exports = config
})
112 changes: 0 additions & 112 deletions template/config/playwright/playwright.config.ts

This file was deleted.

11 changes: 0 additions & 11 deletions template/tsconfig/base/tsconfig.json

This file was deleted.

14 changes: 0 additions & 14 deletions template/tsconfig/cypress-ct/tsconfig.json

This file was deleted.

14 changes: 0 additions & 14 deletions template/tsconfig/nightwatch-ct/tsconfig.json

This file was deleted.

17 changes: 0 additions & 17 deletions template/tsconfig/nightwatch/tsconfig.json

This file was deleted.

14 changes: 0 additions & 14 deletions template/tsconfig/vitest/tsconfig.json

This file was deleted.

1 change: 0 additions & 1 deletion utils/filterList.ts

This file was deleted.