Skip to content

Commit

Permalink
feat: allow multiple ext for bundlers config file (#4130)
Browse files Browse the repository at this point in the history
  • Loading branch information
Julien-R44 committed Apr 24, 2023
1 parent 3688ca6 commit effd75f
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 7 deletions.
19 changes: 15 additions & 4 deletions src/internal_helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* file that was distributed with this source code.
*/

import fs from 'node:fs'
import { existsSync } from 'node:fs'
import { ApplicationService } from './types.js'

/**
Expand All @@ -32,6 +32,15 @@ export async function importTypeScript(
} catch {}
}

/**
* Generates an array of filenames with different JavaScript
* extensions for the given filename
*/
function generateJsFilenames(filename: string) {
const extensions = ['.js', '.ts', '.cjs', '.mjs', '.cts', '.mts']
return extensions.map((extension) => filename + extension)
}

/**
* Detects the assets bundler in use. The rcFile.assetsBundler is
* used when exists.
Expand All @@ -41,19 +50,21 @@ export async function detectAssetsBundler(app: ApplicationService) {
return app.rcFile.assetsBundler
}

if (fs.existsSync(app.makePath('vite.config.js'))) {
const possibleViteConfigFiles = generateJsFilenames('vite.config')
if (possibleViteConfigFiles.some((config) => existsSync(app.makePath(config)))) {
return {
name: 'vite',
devServerCommand: 'vite',
buildCommand: 'vite build',
}
}

if (fs.existsSync(app.makePath('webpack.config.js'))) {
const possibleEncoreConfigFiles = generateJsFilenames('webpack.config')
if (possibleEncoreConfigFiles.some((config) => existsSync(app.makePath(config)))) {
return {
name: 'encore',
devServerCommand: 'encore dev-server',
buildCommand: 'encore',
buildCommand: 'encore production',
}
}
}
22 changes: 19 additions & 3 deletions tests/internal_helpers.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { IgnitorFactory } from '../factories/core/ignitor.js'
import { detectAssetsBundler } from '../src/internal_helpers.js'

test.group('Internal helpers | detect package manager', () => {
test('return "vite" when vite.config.js file exists', async ({ fs, assert }) => {
test('return "vite" when vite.config.* file exists', async ({ fs, assert }) => {
const app = new IgnitorFactory()
.create(fs.baseUrl, {
importer: () => {},
Expand All @@ -26,9 +26,17 @@ test.group('Internal helpers | detect package manager', () => {
buildCommand: 'vite build',
devServerCommand: 'vite',
})
await fs.remove('vite.config.js')

await fs.create('vite.config.ts', '')
assert.deepEqual(await detectAssetsBundler(app), {
name: 'vite',
buildCommand: 'vite build',
devServerCommand: 'vite',
})
})

test('return "encore" when webpack.config.js file exists', async ({ fs, assert }) => {
test('return "encore" when webpack.config.* file exists', async ({ fs, assert }) => {
const app = new IgnitorFactory()
.create(fs.baseUrl, {
importer: () => {},
Expand All @@ -39,7 +47,15 @@ test.group('Internal helpers | detect package manager', () => {
await fs.create('webpack.config.js', '')
assert.deepEqual(await detectAssetsBundler(app), {
name: 'encore',
buildCommand: 'encore',
buildCommand: 'encore production',
devServerCommand: 'encore dev-server',
})
await fs.remove('webpack.config.js')

await fs.create('webpack.config.cjs', '')
assert.deepEqual(await detectAssetsBundler(app), {
name: 'encore',
buildCommand: 'encore production',
devServerCommand: 'encore dev-server',
})
})
Expand Down

0 comments on commit effd75f

Please sign in to comment.