Skip to content

Commit

Permalink
Merge pull request #12 from vite-plugin/v2.2.1
Browse files Browse the repository at this point in the history
V2.2.1
  • Loading branch information
caoxiemeihao authored Jun 29, 2024
2 parents 95c387e + ddd0639 commit 22b9ed0
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 6 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 2.2.1 (2023-06-29)

- 548fedb fix: avoid Vite built-in alias

## 2.2.0 (2023-06-27)

- 64b881a feat: add `options.forceCopyIfUnbuilt`
Expand Down
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,15 @@ export default {
export interface NativeOptions {
/** @default 'node_natives' */
assetsDir?: string
/** By default native modules are automatically detected if this option is not explicitly configure by the user. */
/**
* By default native modules are automatically detected if this option is not explicitly configure by the user.
* @deprecated use `ignore` option instead
*/
natives?: string[] | ((natives: string[]) => string[])
/** Ignore the specified native module. */
ignore?: (name: string) => boolean | undefined
/** Force copy *.node files to dist/node_modules path if Webpack can't bundle native modules correctly. */
forceCopyIfUnbuilt?: true
/** Enable and configure webpack. */
webpack?: {
config?: (config: Configuration) => Configuration | undefined | Promise<Configuration | undefined>
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "vite-plugin-native",
"version": "2.2.0",
"version": "2.2.1",
"description": "Supports Node/Electron C/C++ native addons",
"main": "./dist/index.js",
"types": "./dist/index.d.ts",
Expand Down
8 changes: 4 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,10 @@ const loader1 = '@vercel/webpack-asset-relocator-loader'
const outputAssetBase = 'native_modules'
const NativeExt = '.native.cjs'
const InteropExt = '.interop.mjs'
// https://github.com/vitejs/vite/blob/v5.3.1/packages/vite/src/node/plugins/index.ts#L55
const bareImportRE = /^(?![a-zA-Z]:)[\w@](?!.*:\/\/)/
// `nativesMap` is placed in the global scope and can be effective for multiple builds.
const nativesMap = new Map<string, ResolvedNativeRecord>
// https://github.com/npm/validate-npm-package-name/blob/v5.0.1/lib/index.js#L4
const scopedPackagePattern = /^(?![a-zA-Z]:)[\w@](?!.*:\/\/)/

export default function native(options: NativeOptions): Plugin {
const assetsDir = options.assetsDir ??= 'node_natives'
Expand All @@ -75,13 +75,13 @@ export default function native(options: NativeOptions): Plugin {

const withDistAssetBase = (p: string) => (assetsDir && p) ? `${assetsDir}/${p}` : p
const alias: Alias = {
find: /(.*)/,
find: /^(?!(?:\/?@vite\/|\.))(.*)/,
// Keep `customResolver` receive original source.
// @see https://github.com/rollup/plugins/blob/alias-v5.1.0/packages/alias/src/index.ts#L92
replacement: '$1',
async customResolver(source, importer) {
if (!importer) return
if (!bareImportRE.test(source)) return
if (!scopedPackagePattern.test(source)) return

if (!nativeRecord.has(source)) {
// Dynamic deep detection.
Expand Down

0 comments on commit 22b9ed0

Please # to comment.