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 Jan 9, 2025
1 parent 9919c44 commit 0c57882
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 @@ -208,9 +208,9 @@ describe('resolveBuildOutputs', () => {
{
format: 'es',
},
// {
// format: 'umd',
// },
{
format: 'umd',
},
])
})

Expand Down Expand Up @@ -249,7 +249,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 @@ -268,7 +268,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 @@ -372,7 +372,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 @@ -522,13 +522,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 @@ -555,7 +555,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 @@ -572,7 +572,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 @@ -313,7 +313,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 @@ -759,7 +759,7 @@ async function buildEnvironment(
? `[name].[ext]`
: path.posix.join(options.assetsDir, `[name]-[hash].[ext]`),
inlineDynamicImports:
/* output.format === 'umd' || */
output.format === 'umd' ||
output.format === 'iife' ||
(isSsrTargetWebworkerEnvironment &&
(typeof input === 'string' || Object.keys(input).length === 1)),
Expand Down Expand Up @@ -920,7 +920,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 @@ -970,10 +970,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 @@ -1001,7 +1001,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 @@ -273,15 +273,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 @@ -360,9 +360,9 @@ export const buildEsbuildPlugin = (): 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 0c57882

Please sign in to comment.