Skip to content

Commit

Permalink
feat: enable lib mode tests (#64)
Browse files Browse the repository at this point in the history
  • Loading branch information
sapphi-red committed Nov 29, 2024
1 parent 448f8e0 commit 03ef4bc
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 23 deletions.
20 changes: 10 additions & 10 deletions packages/vite/src/node/__tests__/build.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,9 +182,9 @@ describe('resolveBuildOutputs', () => {
{
format: 'es',
},
// {
// format: 'umd',
// },
{
format: 'umd',
},
])
})

Expand Down Expand Up @@ -223,7 +223,7 @@ describe('resolveBuildOutputs', () => {
expect(resolveBuild).toThrowError(/Option "build\.lib\.name" is required/)
})

test.skip('throws an error when lib.name is missing on umd format', () => {
test('throws an error when lib.name is missing on umd format', () => {
const logger = createLogger()
const libOptions: LibraryOptions = { ...baseLibOptions, formats: ['umd'] }
const resolveBuild = () => resolveBuildOutputs(void 0, libOptions, logger)
Expand All @@ -242,7 +242,7 @@ describe('resolveBuildOutputs', () => {
)
})

test.skip('throws an error when output.name is missing on umd format', () => {
test('throws an error when output.name is missing on umd format', () => {
const logger = createLogger()
const libOptions: LibraryOptions = { ...baseLibOptions }
const outputs: OutputOptions[] = [{ format: 'umd' }]
Expand Down Expand Up @@ -346,7 +346,7 @@ describe('resolveLibFilename', () => {
test('module package extensions', () => {
const formatsToFilenames: FormatsToFileNames = [
['es', 'my-lib.js'],
// ['umd', 'my-lib.umd.cjs'],
['umd', 'my-lib.umd.cjs'],
['cjs', 'my-lib.cjs'],
['iife', 'my-lib.iife.js'],
]
Expand Down Expand Up @@ -496,13 +496,13 @@ describe('resolveBuildOutputs', () => {

expect(resolveBuildOutputs(undefined, libOptions, {} as Logger)).toEqual([
{ format: 'es' },
// { format: 'umd' },
{ format: 'umd' },
])
expect(
resolveBuildOutputs({ name: 'A' }, libOptions, {} as Logger),
).toEqual([
{ format: 'es', name: 'A' },
// { format: 'umd', name: 'A' },
{ format: 'umd', name: 'A' },
])
expect(
resolveBuildOutputs([{ name: 'A' }], libOptions, {} as Logger),
Expand All @@ -529,7 +529,7 @@ describe('resolveBuildOutputs', () => {
).toEqual([{ name: 'A' }])
})

test.skip('umd or iife: should not support multiple entries', () => {
test('umd or iife: should not support multiple entries', () => {
;['umd', 'iife'].forEach((format) => {
expect(() =>
resolveBuildOutputs(
Expand All @@ -546,7 +546,7 @@ describe('resolveBuildOutputs', () => {
})
})

test.skip('umd or iife: should define build.lib.name', () => {
test('umd or iife: should define build.lib.name', () => {
;['umd', 'iife'].forEach((format) => {
expect(() =>
resolveBuildOutputs(
Expand Down
12 changes: 6 additions & 6 deletions packages/vite/src/node/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ export interface LibraryOptions {
cssFileName?: string
}

export type LibraryFormats = 'es' | 'cjs' | 'iife' // | 'umd' | 'system'
export type LibraryFormats = 'es' | 'cjs' | 'iife' | 'umd' // | 'system'

export interface ModulePreloadOptions {
/**
Expand Down Expand Up @@ -773,7 +773,7 @@ async function buildEnvironment(
? `[name].[ext]`
: path.posix.join(options.assetsDir, `[name]-[hash].[ext]`),
inlineDynamicImports:
/* output.format === 'umd' || */ output.format === 'iife',
output.format === 'umd' || output.format === 'iife',
...output,
}
}
Expand Down Expand Up @@ -931,7 +931,7 @@ function resolveOutputJsExtension(
type: string = 'commonjs',
): JsExt {
if (type === 'module') {
return format === 'cjs' /* || format === 'umd' */ ? 'cjs' : 'js'
return format === 'cjs' || format === 'umd' ? 'cjs' : 'js'
} else {
return format === 'es' ? 'mjs' : 'js'
}
Expand Down Expand Up @@ -981,10 +981,10 @@ export function resolveBuildOutputs(
Object.values(libOptions.entry).length > 1
const libFormats =
libOptions.formats ||
(libHasMultipleEntries ? ['es', 'cjs'] : ['es' /* , 'umd' */])
(libHasMultipleEntries ? ['es', 'cjs'] : ['es', 'umd'])

if (!Array.isArray(outputs)) {
if (/* libFormats.includes('umd') || */ libFormats.includes('iife')) {
if (libFormats.includes('umd') || libFormats.includes('iife')) {
if (libHasMultipleEntries) {
throw new Error(
'Multiple entry points are not supported when output formats include "umd" or "iife".',
Expand Down Expand Up @@ -1012,7 +1012,7 @@ export function resolveBuildOutputs(

outputs.forEach((output) => {
if (
/* output.format === 'umd' || */ output.format === 'iife' &&
(output.format === 'umd' || output.format === 'iife') &&
!output.name
) {
throw new Error(
Expand Down
5 changes: 2 additions & 3 deletions packages/vite/src/node/packages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -272,15 +272,14 @@ export function watchPackageDataPlugin(packageCache: PackageCache): Plugin {
return {
name: 'vite:watch-package-data',
buildStart() {
// watchFile = this.addWatchFile.bind(this)
watchFile = this.addWatchFile.bind(this)
watchQueue.forEach(watchFile)
watchQueue.clear()
},
buildEnd() {
watchFile = watchFileStub
},
// TODO: use watchChange hook when implemented
handleHotUpdate({ file: id }) {
watchChange(id) {
if (id.endsWith('/package.json')) {
invalidatePackageData(packageCache, path.normalize(id))
}
Expand Down
6 changes: 3 additions & 3 deletions packages/vite/src/node/plugins/esbuild.ts
Original file line number Diff line number Diff line change
Expand Up @@ -356,9 +356,9 @@ export const buildEsbuildPlugin = (config: ResolvedConfig): Plugin => {
const contentIndex =
opts.format === 'iife'
? Math.max(esbuildCode.search(IIFE_BEGIN_RE), 0)
: // : opts.format === 'umd'
// ? esbuildCode.indexOf(`(function(`) // same for minified or not
0
: opts.format === 'umd'
? esbuildCode.indexOf(`(function(`) // same for minified or not
: 0
if (contentIndex > 0) {
const esbuildHelpers = esbuildCode.slice(0, contentIndex)
res.code = esbuildCode
Expand Down
1 change: 0 additions & 1 deletion vitest.config.e2e.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ export default defineConfig({
? [
'./playground/environment-react-ssr/**/*.spec.[tj]s', // needs investigation
'./playground/external/**/*.spec.[tj]s', // https://github.com/rolldown/rolldown/issues/2041
'./playground/lib/**/*.spec.[tj]s', // umd format
'./playground/object-hooks/**/*.spec.[tj]s', // object hook sequential
'./playground/optimize-deps/**/*.spec.[tj]s', // https://github.com/rolldown/rolldown/issues/2031
'./playground/tsconfig-json/__tests__/**/*.spec.[tj]s', // decorators is not supported by oxc
Expand Down

0 comments on commit 03ef4bc

Please sign in to comment.