Skip to content

Commit

Permalink
fix: babel target error
Browse files Browse the repository at this point in the history
  • Loading branch information
hemengke1997 committed Nov 16, 2023
1 parent 4c72707 commit ebd888d
Show file tree
Hide file tree
Showing 16 changed files with 41 additions and 9 deletions.
2 changes: 2 additions & 0 deletions playground/spa-file-mode/.browserslistrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
chrome 32

12 changes: 11 additions & 1 deletion playground/spa-file-mode/__tests__/file-mode.spec.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import { listFiles, readFile } from '~utils'
import fs from 'fs-extra'
import path from 'node:path'
import glob from 'tiny-glob'
import { beforeAll, describe, expect, test } from 'vitest'

const manifestPath = 'node_modules/.vite-plugin-public-typescript/manifest.json'

const out = path.resolve(__dirname, '../public/out')

describe('file-mode', () => {
let jsFiles: string[]
let manifest: string
Expand All @@ -15,10 +19,16 @@ describe('file-mode', () => {
} catch {}
})
test('should output js file to publicDir', () => {
expect(jsFiles).toHaveLength(3)
expect(jsFiles).toHaveLength(fs.readdirSync(out).length)

const values = Object.values(JSON.parse(manifest)).map((v: string) => path.basename(v))

expect(jsFiles).toEqual(values)
})

test('should babel transform', async () => {
const babel = (await glob(`${out}/babel.?(*.)js`, { absolute: true }))[0]

expect(fs.readFileSync(babel, 'utf8')).toContain('@babel/helpers - typeof')
})
})
3 changes: 3 additions & 0 deletions playground/spa-file-mode/public-typescript/babel.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const tmp = '1'

console.log(typeof tmp)
1 change: 1 addition & 0 deletions playground/spa-file-mode/public/out/babel.f2af9419.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion playground/spa-file-mode/public/out/define.0852dc5a.js

This file was deleted.

1 change: 1 addition & 0 deletions playground/spa-file-mode/public/out/define.0e709972.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
(function(){var o={hello:"world"};window.VITE_DEFINE={};window.VITE_DEFINE["custom-define"]="custom define!";window.VITE_DEFINE["hello-world"]=o;})();
1 change: 1 addition & 0 deletions playground/spa-file-mode/public/out/env.16b274f5.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
(function(){var e={VITE_ENV_FROM_ENVFILE:"imfromdotenv",BASE_URL:"/vite-plugin-public-typescript/",MODE:"development",DEV:!0,PROD:!1};window.VITE_ENV=e;})();
1 change: 0 additions & 1 deletion playground/spa-file-mode/public/out/env.e6648627.js

This file was deleted.

1 change: 0 additions & 1 deletion playground/spa-file-mode/public/out/hmr.c01ee876.js

This file was deleted.

1 change: 1 addition & 0 deletions playground/spa-file-mode/public/out/hmr.ee6857f2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
(function(){console.log("hmr");window.hmr="hmr original text";})();
1 change: 1 addition & 0 deletions playground/spa-file-mode/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export default defineConfig(() => ({
hash: true,
outputDir: 'out',
destination: 'file',
babel: true,
}),
injectScripts((manifest) => [
{
Expand Down
2 changes: 1 addition & 1 deletion playground/spa/.browserslistrc
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
not dead
last 4 versions
not ie <= 11
chrome 64
chrome 40
edge 79
firefox 67
opera 51
Expand Down
1 change: 1 addition & 0 deletions playground/spa/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"preview": "vite preview"
},
"dependencies": {
"modern-flexible": "^0.0.7",
"react": "^18.2.0",
"react-dom": "^18.2.0"
},
Expand Down
1 change: 1 addition & 0 deletions playground/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"~utils": ["./test-utils.ts"]
},
"types": ["vite/client", "node"],
"isolatedModules": true,
"strict": false
},
"include": ["."],
Expand Down
15 changes: 15 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 2 additions & 4 deletions src/node/build/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -199,9 +199,8 @@ export async function esbuildTypescript(buildOptions: IBuildOptions) {
let esbuildTarget: string[] = []
if (enableBabel) {
const { default: browserslist } = await import('browserslist')
const browsersConfig = browserslist.loadConfig({ path: viteConfig.root })
babelTarget = browserslist(browsersConfig)
esbuildTarget = resolveToEsbuildTarget(babelTarget, { printUnknownTargets: false })
babelTarget = browserslist.loadConfig({ path: viteConfig.root }) || []
esbuildTarget = resolveToEsbuildTarget(browserslist(babelTarget), { printUnknownTargets: false })
}

const esbuildPlugins = enableBabel
Expand Down Expand Up @@ -241,7 +240,6 @@ export async function esbuildTypescript(buildOptions: IBuildOptions) {

debug('esbuild success:', filename)
} catch (error) {
console.log(error, 'eeee')
if (error instanceof Error) {
const babelPluginNotFound = /ERROR: \[plugin: babel\] Cannot find package '(.*)'/
if (error?.message.match(babelPluginNotFound)) {
Expand Down

0 comments on commit ebd888d

Please sign in to comment.