Skip to content

Commit 28445af

Browse files
committed
Make @ alias optional
1 parent b0be71e commit 28445af

File tree

8 files changed

+87
-31
lines changed

8 files changed

+87
-31
lines changed

src/index.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ type Config = {
7777
includeExamples: boolean
7878
includeEsLint: boolean
7979
includeEsLintStylistic: boolean
80+
includeAtAliases: boolean
8081
}
8182

8283
type Args = {
@@ -245,6 +246,7 @@ async function init() {
245246
const includeGithubPages = includeDocs && await togglePrompt('Include GitHub Pages config for documentation?')
246247
const includePlayground = await togglePrompt('Include playground application for development?', true)
247248
const includeExamples = await togglePrompt('Include example code?', true, 'Yes', 'No, just configs')
249+
const includeAtAliases = await togglePrompt('Configure @ as an alias for src?')
248250

249251
function suggestExtended() {
250252
if (!extended) {
@@ -294,7 +296,8 @@ async function init() {
294296
includePlayground,
295297
includeExamples,
296298
includeEsLint,
297-
includeEsLintStylistic
299+
includeEsLintStylistic,
300+
includeAtAliases
298301
}
299302

300303
copyTemplate('base', config)

src/template/base/config/packages/@projectName@/tsconfig.app.json renamed to src/template/base/config/packages/@projectName@/tsconfig.app.json.ejs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
"include": ["env.d.ts", "src/**/*", "src/**/*.vue"],
44
"exclude": ["src/**/__tests__/*"],
55
"compilerOptions": {
6-
"tsBuildInfoFile": "./node_modules/.tmp/tsconfig.app.tsbuildinfo",
6+
"tsBuildInfoFile": "./node_modules/.tmp/tsconfig.app.tsbuildinfo"<%_ if (config.includeAtAliases) { _%>,
77
"paths": {
88
"@/*": ["./src/*"]
9-
}
9+
}<%_ } %>
1010
}
1111
}

src/template/base/config/packages/@projectName@/vite.config.mts renamed to src/template/base/config/packages/@projectName@/vite.config.mts.ejs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import { resolve } from 'node:path'
2+
<%_ if (config.includeAtAliases) { _%>
23
import { fileURLToPath, URL } from 'node:url'
4+
<%_ } _%>
35

46
import { defineConfig, type UserConfig } from 'vite'
57
import replace from '@rollup/plugin-replace'
@@ -29,22 +31,24 @@ export default defineConfig(({ mode }): UserConfig => {
2931
vue(),
3032
dtsPlugin
3133
],
34+
<%_ if (config.includeAtAliases) { _%>
3235
resolve: {
3336
alias: {
3437
'@': fileURLToPath(new URL('./src', import.meta.url))
3538
}
3639
},
40+
<%_ } _%>
3741
build: {
3842
target: 'es2019',
3943
emptyOutDir: false,
4044
minify: mode === 'production',
4145
lib: {
4246
entry: resolve(__dirname, 'src/index.ts'),
43-
name: '@globalVariableName@',
47+
name: '<%- config.globalVariableName %>',
4448
formats: mode === 'neutral' ? ['cjs', 'es'] : ['es', 'iife'],
4549
4650
fileName(format) {
47-
let name = '@unscopedPackageName@'
51+
let name = '<%- config.unscopedPackageName %>'
4852
let extension = 'js'
4953
5054
if (format === 'iife') {

src/template/playground/config/packages/playground/tsconfig.app.json renamed to src/template/playground/config/packages/playground/tsconfig.app.json.ejs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@
44
"compilerOptions": {
55
"tsBuildInfoFile": "./node_modules/.tmp/tsconfig.app.tsbuildinfo",
66
"paths": {
7+
<%_ if (config.includeAtAliases) { _%>
78
"@/*": ["./src/*"],
8-
"@scopedPackageName@": ["../@projectName@/src/index.ts"]
9+
<%_ } _%>
10+
"<%- config.scopedPackageName %>": ["../<%- config.mainPackageDirName %>/src/index.ts"]
911
}
1012
}
1113
}

src/template/playground/config/packages/playground/vite.config.mts

Lines changed: 0 additions & 22 deletions
This file was deleted.
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import { fileURLToPath, URL } from 'node:url'
2+
3+
import { defineConfig, type UserConfig } from 'vite'
4+
import vue from '@vitejs/plugin-vue'
5+
import vueDevTools from 'vite-plugin-vue-devtools'
6+
7+
<%_ if (config.includeAtAliases) { _%>
8+
const librarySrc = fileURLToPath(new URL('../<%- config.mainPackageDirName %>/src/', import.meta.url))
9+
const playgroundSrc = fileURLToPath(new URL('./src/', import.meta.url))
10+
11+
<%_ } _%>
12+
export default defineConfig(({ mode }): UserConfig => ({
13+
plugins: [
14+
vue(),
15+
vueDevTools()
16+
],
17+
resolve: {
18+
<%_ if (config.includeAtAliases) { _%>
19+
alias: [
20+
{
21+
find: '@',
22+
replacement: '@',
23+
customResolver(source, importer) {
24+
return source.replace(
25+
/^@\//,
26+
importer?.startsWith(librarySrc) ? librarySrc : playgroundSrc
27+
)
28+
}
29+
}, {
30+
find: '<%- config.scopedPackageName %>',
31+
replacement: librarySrc
32+
}
33+
]
34+
<%_ } else { _%>
35+
alias: {
36+
'<%- config.scopedPackageName %>': fileURLToPath(new URL('../<%- config.mainPackageDirName %>/src/', import.meta.url))
37+
}
38+
<%_ } _%>
39+
},
40+
define: {
41+
__DEV__: JSON.stringify(mode !== 'production')
42+
}
43+
}))

src/template/vitepress/config/packages/docs/.vitepress/config.mts.ejs

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
1-
import { resolve } from 'node:path'
1+
import { fileURLToPath, URL } from 'node:url'
22

33
import { defineConfigWithTheme } from 'vitepress'
44

5+
<%_ if (config.includeAtAliases) { _%>
6+
const librarySrc = fileURLToPath(new URL('../../<%- config.mainPackageDirName %>/src/', import.meta.url))
7+
const playgroundSrc = fileURLToPath(new URL('../src/', import.meta.url))
8+
9+
<%_ } _%>
510
export default ({ mode }: { mode: string }) => defineConfigWithTheme({
611
srcDir: './src',
712
outDir: './dist',
@@ -30,9 +35,27 @@ export default ({ mode }: { mode: string }) => defineConfigWithTheme({
3035
<%_ } _%>
3136
vite: {
3237
resolve: {
38+
<%_ if (config.includeAtAliases) { _%>
39+
alias: [
40+
{
41+
find: '@',
42+
replacement: '@',
43+
customResolver(source, importer) {
44+
return source.replace(
45+
/^@\//,
46+
importer?.startsWith(librarySrc) ? librarySrc : playgroundSrc
47+
)
48+
}
49+
}, {
50+
find: '<%- config.scopedPackageName %>',
51+
replacement: librarySrc
52+
}
53+
]
54+
<%_ } else { _%>
3355
alias: {
34-
'<%- config.scopedPackageName %>': resolve(__dirname, '../../<%- config.mainPackageDirName %>/src/index.ts')
56+
'<%- config.scopedPackageName %>': fileURLToPath(new URL('../../<%- config.mainPackageDirName %>/src/', import.meta.url))
3557
}
58+
<%_ } _%>
3659
},
3760
3861
define: {

src/template/vitepress/config/packages/docs/tsconfig.app.json renamed to src/template/vitepress/config/packages/docs/tsconfig.app.json.ejs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,10 @@
1010
],
1111
"compilerOptions": {
1212
"paths": {
13-
"@scopedPackageName@": ["../@projectName@/src/index.ts"]
13+
<%_ if (config.includeAtAliases) { _%>
14+
"@/*": ["./src/*"],
15+
<%_ } _%>
16+
"<%- config.scopedPackageName %>": ["../<%- config.mainPackageDirName %>/src/index.ts"]
1417
}
1518
},
1619
"vueCompilerOptions": {

0 commit comments

Comments
 (0)