Skip to content

Commit ffa4e31

Browse files
authored
🐛 wrong root on prod (#26)
* 🐛 wrong root on prod * 🐛 __filename not found * 🐛 interop * 💚 ci name
1 parent 69afc15 commit ffa4e31

File tree

8 files changed

+85
-65
lines changed

8 files changed

+85
-65
lines changed

.changeset/new-dryers-exercise.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"vsit": patch
3+
---
4+
5+
fix root on prod mode

.changeset/quiet-numbers-mix.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"vsit": patch
3+
---
4+
5+
fix \_\_filename not found

.github/workflows/snapshot-release.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
name: Release
1+
name: Snapshot Release
22
on:
33
push:
44
paths:

.gitignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -163,4 +163,5 @@ dist
163163
dts
164164
dist
165165
dist-client
166-
dev-dist
166+
dev-dist
167+
vendors

packages/vsit/package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@
2626
"files": [
2727
"dts",
2828
"dist",
29-
"dist-client"
29+
"dist-client",
30+
"vendors"
3031
],
3132
"bin": {
3233
"vsit": "bin/index.mjs"

packages/vsit/rollup.config.mjs

+51-59
Original file line numberDiff line numberDiff line change
@@ -10,49 +10,62 @@ import ce from 'rollup-plugin-condition-exports'
1010
import esbuild from 'rollup-plugin-esbuild'
1111
import { externals } from 'rollup-plugin-node-externals'
1212
import size from 'rollup-plugin-size'
13+
import { createRequire } from 'node:module'
14+
15+
const require = createRequire(import.meta.url)
16+
17+
const plugins = [
18+
externals({
19+
devDeps: false,
20+
builtinsPrefix: 'add',
21+
}),
22+
commonjs(),
23+
replace({
24+
delimiters: ['', ''],
25+
preventAssignment: true,
26+
values: {
27+
'import \'source-map-support/register.js\';': '',
28+
},
29+
}),
30+
esbuild({
31+
minify: false,
32+
sourceMap: process.env.BUILD !== 'production',
33+
target: 'es2021',
34+
}),
35+
alias({
36+
customResolver: resolve({ extensions: ['.tsx', '.ts'] }),
37+
entries: Object.entries({
38+
'@/*': ['./src/*'],
39+
}).map(([alias, value]) => ({
40+
find: new RegExp(`${alias.replace('/*', '')}`),
41+
replacement: path.resolve(process.cwd(), `${value[0].replace('/*', '')}`),
42+
})),
43+
}),
44+
resolve({ browser: false, exportConditions: ['node', 'default'] }),
45+
json(),
46+
size(),
47+
]
1348

1449
export default defineConfig([
15-
// CommonJS (for Node) and ES module (for bundlers) build.
16-
// (We could have three entries in the configuration array
17-
// instead of two, but it's quicker to generate multiple
18-
// builds from a single configuration where possible, using
19-
// an array for the `output` option, where we can specify
20-
// `file` and `format` for each target)
50+
{
51+
input: {
52+
'write-yaml-file/index': require.resolve('write-yaml-file')
53+
},
54+
plugins,
55+
output: [
56+
{
57+
entryFileNames: '[name].cjs',
58+
dir: 'vendors',
59+
chunkFileNames: 'chunks/[name].cjs',
60+
format: 'cjs',
61+
},
62+
],
63+
},
2164
{
2265
input: 'src/cli.ts',
2366
preserveEntrySignatures: 'strict',
2467
external: ['source-map-support/register.js'],
25-
plugins: [
26-
externals({
27-
devDeps: false,
28-
builtinsPrefix: 'add',
29-
}),
30-
commonjs(),
31-
replace({
32-
delimiters: ['', ''],
33-
preventAssignment: true,
34-
values: {
35-
'import \'source-map-support/register.js\';': '',
36-
},
37-
}),
38-
esbuild({
39-
minify: false,
40-
sourceMap: process.env.BUILD !== 'production',
41-
target: 'es2021',
42-
}),
43-
alias({
44-
customResolver: resolve({ extensions: ['.tsx', '.ts'] }),
45-
entries: Object.entries({
46-
'@/*': ['./src/*'],
47-
}).map(([alias, value]) => ({
48-
find: new RegExp(`${alias.replace('/*', '')}`),
49-
replacement: path.resolve(process.cwd(), `${value[0].replace('/*', '')}`),
50-
})),
51-
}),
52-
resolve({ browser: false, exportConditions: ['node', 'default'] }),
53-
json(),
54-
size(),
55-
],
68+
plugins,
5669
watch: {
5770
exclude: ['./package.json'],
5871
},
@@ -68,32 +81,11 @@ export default defineConfig([
6881
},
6982
{
7083
plugins: [
71-
externals({
72-
devDeps: false,
73-
builtinsPrefix: 'add',
74-
}),
75-
commonjs(),
76-
esbuild({
77-
minify: false,
78-
sourceMap: true,
79-
target: 'es2021',
80-
}),
81-
alias({
82-
customResolver: resolve({ extensions: ['.tsx', '.ts'] }),
83-
entries: Object.entries({
84-
'@/*': ['./src/*'],
85-
}).map(([alias, value]) => ({
86-
find: new RegExp(`${alias.replace('/*', '')}`),
87-
replacement: path.resolve(process.cwd(), `${value[0].replace('/*', '')}`),
88-
})),
89-
}),
90-
resolve({ browser: false, exportConditions: ['node', 'default'] }),
91-
json(),
9284
ce({
9385
outDir: 'dist',
9486
declarationDir: 'dts',
9587
}),
96-
size(),
88+
...plugins,
9789
],
9890
watch: {
9991
exclude: ['./package.json'],

packages/vsit/src/common/store/persist-cache.ts

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { createRequire } from 'node:module'
12
import { resolve } from 'node:path'
23

34
import {
@@ -6,9 +7,9 @@ import {
67
readFile,
78
} from 'fs-extra'
89
import readYaml from 'read-yaml-file'
9-
import writeYaml from 'write-yaml-file'
1010

1111
import { version } from '../../../package.json'
12+
import { pkgRoot } from '../path'
1213
import {
1314
LOCK_FILE,
1415
STORE_PACKAGES_DIR,
@@ -17,6 +18,8 @@ import {
1718
import { computeCacheKey } from './utils'
1819
import { debug } from '@/common/log'
1920

21+
const require = createRequire(import.meta.url)
22+
2023
interface Options {
2124
storePath?: string
2225
}
@@ -55,6 +58,9 @@ interface LockFileYaml {
5558
packages?: Record<string, Package>
5659
}
5760

61+
let writeYaml = require(resolve(pkgRoot, './vendors/write-yaml-file/index.cjs'))
62+
writeYaml = writeYaml.default ?? writeYaml
63+
5864
export class LockFile {
5965
options: ResolvedLockFileOptions
6066
lockFile: LockFileYaml = { version }

packages/vsit/src/plugins/preview/index.ts

+12-2
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,22 @@ import { pkgRoot } from '@/common/path'
77
import type { Plugin } from 'vite'
88

99
const clientDir = 'dist-client'
10+
const resolvedClientDir = path.resolve(pkgRoot, clientDir)
1011
export const PluginPreview = (): Plugin => {
1112
return {
1213
name: 'vsit:preview',
14+
config() {
15+
return {
16+
// if prod, use dist-client as root
17+
root: resolvedClientDir,
18+
// prevent throw pre-bundling warnings on preview and prod
19+
optimizeDeps: {
20+
disabled: true,
21+
},
22+
}
23+
},
1324
configureServer(server) {
14-
const resolvedStaticPath = path.resolve(pkgRoot, clientDir)
15-
const client = sirv(resolvedStaticPath, { dev: true })
25+
const client = sirv(resolvedClientDir, { dev: true })
1626
server.middlewares.use('/', client)
1727
},
1828
}

0 commit comments

Comments
 (0)