-
Notifications
You must be signed in to change notification settings - Fork 0
Setup Vite config for npm and Laravel packages #108
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
48f821b
chore: first setup
YvetteNikolov 5719b90
chore: add pointers to pickup
YvetteNikolov 0427338
chore: add more pointers to pick up later
YvetteNikolov 70d5fc0
chore: progress
YvetteNikolov 229068e
chore: more progress
YvetteNikolov f3bb813
docs: add design spec for optional WordPress plugins in vite-config
YvetteNikolov 6368bf5
docs: add implementation plan for optional WordPress plugins in vite-…
YvetteNikolov 83906c3
feat(vite-config): add externalizeReact and wordpressGlobals options
YvetteNikolov 3e44dec
fix(vite-config): scope filter(Boolean) to conditional WordPress plug…
YvetteNikolov bf2a5a3
docs(vite-config): add inline comments to externalizeReact and wordpr…
YvetteNikolov 7cf6f0a
chore: move package.json utilities to single file
YvetteNikolov 5b1f77d
refactor: move package helpers to separate file
YvetteNikolov f726ea7
docs: update README with options breakdown
YvetteNikolov a74d267
chore: remove superpower docs
YvetteNikolov 4d3f405
chore: remove separate packages export
YvetteNikolov 3380173
chore: copilot feedback
YvetteNikolov File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,122 @@ | ||
| /** | ||
| * External dependencies | ||
| */ | ||
| import { defineConfig } from 'vite'; | ||
| import { viteExternalsPlugin } from 'vite-plugin-externals'; | ||
| import { wordpressPlugin } from '@roots/vite-plugin'; | ||
| import dts from 'vite-plugin-dts'; | ||
| import fs from 'fs'; | ||
|
|
||
| /** | ||
| * Internal dependencies | ||
| */ | ||
| import { | ||
| getPackageJson, | ||
| validatePackageOutputFields, | ||
| } from '../utils/package-json.js'; | ||
| import { | ||
| toEntryObject, | ||
| toAbsoluteEntries, | ||
| defaultFileName, | ||
| createAssetFileNames, | ||
| } from '../utils/package-helpers.js'; | ||
|
|
||
| export const createBasePackageConfig = ( { | ||
| entryPoints, | ||
| outDir = 'dist', | ||
| externals = [], | ||
| formats = [ 'es' ], | ||
| fileName = defaultFileName, | ||
| packageJsonValidation = false, | ||
| test = {}, | ||
| manifest = false, | ||
| plugins = [], | ||
| externalizeReact = true, | ||
| wordpressGlobals = true, | ||
| } = {} ) => { | ||
| const cwd = process.cwd(); | ||
| const normalizedEntries = toEntryObject( entryPoints ); | ||
| const absoluteEntries = toAbsoluteEntries( normalizedEntries, cwd ); | ||
| const hasMissingEntry = Object.values( absoluteEntries ).some( | ||
| ( entryPath ) => ! fs.existsSync( entryPath ) | ||
| ); | ||
|
|
||
| if ( hasMissingEntry ) { | ||
| throw new Error( | ||
| '[vite-config] One or more entry points do not exist.' | ||
| ); | ||
| } | ||
|
|
||
| const resolvedFormats = | ||
| Array.isArray( formats ) && formats.length > 0 ? formats : [ 'es' ]; | ||
| const primaryEntryName = Object.keys( normalizedEntries )[ 0 ]; | ||
| const hasTsEntries = Object.values( absoluteEntries ).some( ( entry ) => | ||
| /\.tsx?$/.test( entry ) | ||
| ); | ||
| const isWatchMode = | ||
| process.env.WATCH === 'true' || process.argv.includes( '--watch' ); | ||
|
|
||
| return defineConfig( async () => { | ||
| const { packageJson } = getPackageJson( cwd ); | ||
|
|
||
| if ( packageJsonValidation ) { | ||
| validatePackageOutputFields( { | ||
| entryName: primaryEntryName, | ||
| entryNames: Object.keys( normalizedEntries ), | ||
| formats: resolvedFormats, | ||
| outDir, | ||
| packageJson, | ||
| } ); | ||
| } | ||
|
|
||
| return { | ||
| plugins: [ | ||
| /** | ||
| * Externalizes React, ReactDOM and ReactJSXRuntime & reference global versions | ||
| * provided by WordPress' wp-element (window.React, window.ReactDOM) | ||
| */ | ||
| externalizeReact && | ||
| viteExternalsPlugin( { | ||
| react: 'React', | ||
| 'react-dom': 'ReactDOM', | ||
| 'react/jsx-runtime': 'ReactJSXRuntime', | ||
| } ), | ||
| /** | ||
| * Transforms @wordpress/ dependencies to reference window.wp global | ||
| */ | ||
| wordpressGlobals && wordpressPlugin(), | ||
| ] | ||
| .filter( Boolean ) | ||
| .concat( [ hasTsEntries && dts(), ...plugins ] ), | ||
| test: { | ||
| environment: 'jsdom', | ||
| ...test, | ||
|
YvetteNikolov marked this conversation as resolved.
|
||
| }, | ||
| build: { | ||
| outDir, | ||
| lib: { | ||
| entry: absoluteEntries, | ||
| formats: resolvedFormats, | ||
| fileName, | ||
| }, | ||
| assetsInlineLimit: 0, | ||
| manifest, | ||
| target: 'esnext', | ||
| sourcemap: isWatchMode ? 'inline' : false, | ||
| minify: ! isWatchMode, | ||
| emptyOutDir: ! isWatchMode, | ||
| rollupOptions: { | ||
| external: externals, | ||
| treeshake: true, | ||
| output: { | ||
| chunkFileNames: ( chunkInfo ) => | ||
| `chunks/${ chunkInfo.name }.[hash].js`, | ||
| assetFileNames: createAssetFileNames( { | ||
| withHash: Boolean( manifest ), | ||
| } ), | ||
| }, | ||
| }, | ||
| }, | ||
| }; | ||
| } ); | ||
| }; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| import { createBasePackageConfig } from './base-package.js'; | ||
|
|
||
| export const laravelPackageConfig = ( options = {} ) => | ||
| createBasePackageConfig( { | ||
| outDir: 'public', | ||
| manifest: true, | ||
| ...options, | ||
| } ); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| import { createBasePackageConfig } from './base-package.js'; | ||
|
|
||
| export const npmPackageConfig = ( options = {} ) => | ||
| createBasePackageConfig( { | ||
| outDir: 'dist', | ||
| packageJsonValidation: true, | ||
| ...options, | ||
| } ); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,13 @@ | ||
| import { braveConfig } from './configs/brave.js'; | ||
| import { braveBlocksConfig } from './configs/brave-theme-blocks.js'; | ||
| import { createBasePackageConfig } from './configs/base-package.js'; | ||
| import { npmPackageConfig } from './configs/npm-package.js'; | ||
| import { laravelPackageConfig } from './configs/laravel-package.js'; | ||
|
|
||
| export { braveConfig, braveBlocksConfig }; | ||
| export { | ||
| braveBlocksConfig, | ||
| braveConfig, | ||
| createBasePackageConfig, | ||
| laravelPackageConfig, | ||
| npmPackageConfig, | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| import { createBasePackageConfig } from './configs/base-package.js'; | ||
| import { npmPackageConfig } from './configs/npm-package.js'; | ||
| import { laravelPackageConfig } from './configs/laravel-package.js'; | ||
|
|
||
| export { createBasePackageConfig, npmPackageConfig, laravelPackageConfig }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Het is wellicht goed om dit in de documentatie nog wat toe te lichten, voor het geval dat er wellicht ooit onverklaarbare bugs ontstaan omdat react niet gevonden kan worden