@ditojs/build
Advanced tools
Comparing version 1.4.1 to 1.5.0
{ | ||
"name": "@ditojs/build", | ||
"version": "1.4.1", | ||
"version": "1.5.0", | ||
"type": "module", | ||
@@ -27,2 +27,3 @@ "repository": "https://github.com/ditojs/dito/tree/master/packages/build", | ||
"autoprefixer": "^10.4.4", | ||
"find-up": "^6.3.0", | ||
"mime": "^3.0.0", | ||
@@ -34,3 +35,3 @@ "mini-svg-data-uri": "^1.4.4", | ||
}, | ||
"gitHead": "0e8a22ea0cb68eec3df1b6236cf77a85f5a2c836" | ||
"gitHead": "36a57512228cc233211bf6e4038123a9e775f5c5" | ||
} |
@@ -0,2 +1,7 @@ | ||
import fs from 'fs' | ||
import path from 'path' | ||
import { builtinModules, createRequire } from 'module' | ||
import { findUpSync } from 'find-up' | ||
import { asArray, escapeRegexp } from '@ditojs/utils' | ||
const require = createRequire(import.meta.url) | ||
@@ -8,3 +13,3 @@ | ||
exclude = [] | ||
}) { | ||
} = {}) { | ||
const externals = Object.fromEntries( | ||
@@ -39,1 +44,42 @@ [...builtinModules, ...include].map(name => [name, name]) | ||
} | ||
export function createRollupImportsResolver({ cwd = process.cwd() } = {}) { | ||
// Read `package.json` from the closest package.json, so we can emulate | ||
// ESM-style imports mappings in rollup / vite. | ||
const pkg = findUpSync('package.json', { cwd }) | ||
const { imports = {} } = JSON.parse(fs.readFileSync(pkg, 'utf8')) | ||
return { | ||
// Use a custom rollup resolver to emulate ESM-style imports | ||
// mappings in vite, as read from `package.json` above: | ||
find: /^#/, | ||
replacement: '#', | ||
customResolver(id) { | ||
for (const [find, replacement] of Object.entries(imports)) { | ||
const { match, capture } = matchModuleIdentifier(id, find) | ||
if (match) { | ||
const replacementPath = path.resolve(replacement) | ||
id = capture | ||
? replacementPath.replace('*', capture) | ||
: replacementPath | ||
} | ||
} | ||
return id | ||
} | ||
} | ||
} | ||
export function matchModuleIdentifier(id, pattern) { | ||
const regexp = new RegExp(`^${escapeRegexp(pattern).replace('\\*', '(.*)')}$`) | ||
const match = id.match(regexp) | ||
return { | ||
match: !!match, | ||
capture: match?.[1] | ||
} | ||
} | ||
export function testModuleIdentifier(id, patterns) { | ||
return asArray(patterns).some( | ||
pattern => matchModuleIdentifier(id, pattern).match | ||
) | ||
} |
@@ -1,9 +0,15 @@ | ||
import { defineConfig } from 'vite' | ||
import { defineConfig as defineViteConfig } from 'vite' | ||
import { createVuePlugin } from 'vite-plugin-vue2' | ||
import { getPostCssConfig } from './postcss.js' | ||
import { getRollupExternalsFromDependencies } from './rollup.js' | ||
import { | ||
getRollupExternalsFromDependencies, | ||
createRollupImportsResolver | ||
} from './rollup.js' | ||
export function getViteConfig({ | ||
name, | ||
defineConfig = defineViteConfig, | ||
css = false, | ||
vue = false, | ||
build = true, | ||
minify = !process.argv.includes('--watch'), | ||
@@ -14,28 +20,43 @@ sourcemap = 'inline', | ||
exclude = [] | ||
} = {} | ||
} = {}, | ||
...rest | ||
} = {}) { | ||
const externals = getRollupExternalsFromDependencies({ include, exclude }) | ||
const externals = build && getRollupExternalsFromDependencies({ | ||
include, | ||
exclude | ||
}) | ||
return defineConfig({ | ||
plugins: [ | ||
createVuePlugin() | ||
], | ||
esbuild: { minify }, | ||
build: { | ||
minify, | ||
sourcemap, | ||
cssCodeSplit: false, | ||
lib: { | ||
name, | ||
format: ['es', 'umd'], | ||
entry: './src/index.js', | ||
fileName: format => `${name}.${format}.js` | ||
}, | ||
rollupOptions: { | ||
external: id => !!externals[id], | ||
output: { | ||
manualChunks: undefined, | ||
globals: externals | ||
plugins: vue | ||
? [ | ||
createVuePlugin() | ||
] | ||
: null, | ||
resolve: { | ||
alias: [ | ||
createRollupImportsResolver() | ||
] | ||
}, | ||
esbuild: build | ||
? { minify } | ||
: null, | ||
build: build | ||
? { | ||
minify, | ||
sourcemap, | ||
cssCodeSplit: false, | ||
lib: { | ||
name, | ||
format: ['es', 'umd'], | ||
entry: './src/index.js', | ||
fileName: format => `${name}.${format}.js` | ||
}, | ||
rollupOptions: { | ||
external: id => !!externals[id], | ||
output: { | ||
manualChunks: undefined, | ||
globals: externals | ||
} | ||
} | ||
} | ||
}, | ||
: null, | ||
css: css | ||
@@ -50,4 +71,5 @@ ? { | ||
} | ||
: null | ||
: null, | ||
...rest | ||
}) | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
8077
206
7
5
+ Addedfind-up@^6.3.0
+ Addedfind-up@6.3.0(transitive)
+ Addedlocate-path@7.2.0(transitive)
+ Addedp-limit@4.0.0(transitive)
+ Addedp-locate@6.0.0(transitive)
+ Addedpath-exists@5.0.0(transitive)
+ Addedyocto-queue@1.1.1(transitive)