esbuild-css-modules-plugin
Advanced tools
Comparing version 3.0.0-dev.1 to 3.0.0-dev.2
@@ -52,2 +52,4 @@ import { | ||
log('initialize build context with options:', options); | ||
/** @param {string} to */ | ||
@@ -88,3 +90,3 @@ const relative = (to) => { | ||
} | ||
let root = globalThis.document.querySelector("${containerSelector}"); | ||
let root = globalThis.document.querySelector(${JSON.stringify(containerSelector)}); | ||
if (root && root.shadowRoot) { | ||
@@ -143,24 +145,30 @@ root = root.shadowRoot; | ||
patchedBuild.onLoad({ filter: /.*/, namespace: pluginCssNamespace }, ({ pluginData }) => { | ||
const result = pluginData.builtCssResult; | ||
if (!result) { | ||
return; | ||
patchedBuild.onLoad( | ||
{ filter: /.*/, namespace: pluginCssNamespace }, | ||
({ path, pluginData, namespace }) => { | ||
const result = pluginData.builtCssResult; | ||
if (!result) { | ||
return; | ||
} | ||
const { code, map } = result; | ||
const codeWithSourcemap = | ||
(code ? code.toString('utf8') : '') + | ||
(map | ||
? `\n/*# sourceMappingURL=data:application/json;base64,${map.toString('base64')} */` | ||
: ''); | ||
log(`on load ${namespace}:${path} in ${relative(pluginData.resolveDir)}`); | ||
return { | ||
contents: codeWithSourcemap, | ||
loader: patchedBuild.initialOptions.loader?.['.css'] || 'css', | ||
resolveDir: pluginData.resolveDir | ||
}; | ||
} | ||
const { code, map } = result; | ||
const codeWithSourcemap = | ||
(code ? code.toString('utf8') : '') + | ||
(map | ||
? `\n/*# sourceMappingURL=data:application/json;base64,${map.toString('base64')} */` | ||
: ''); | ||
); | ||
return { | ||
contents: codeWithSourcemap, | ||
loader: patchedBuild.initialOptions.loader?.['.css'] || 'css', | ||
resolveDir: pluginData.resolveDir | ||
}; | ||
}); | ||
patchedBuild.onResolve( | ||
{ filter: modulesCssRegExp, namespace: pluginJsNamespace }, | ||
({ path, pluginData, resolveDir }) => { | ||
({ path, pluginData, resolveDir, namespace }) => { | ||
log(`on resolve ${namespace}:${path} in ${relative(resolveDir)}`); | ||
return { | ||
@@ -179,3 +187,3 @@ path, | ||
{ filter: new RegExp(`\\.${virtualJsExt}$`), namespace: pluginJsNamespace }, | ||
async ({ pluginData }) => { | ||
async ({ pluginData, path, namespace }) => { | ||
const { resolveDir, originPath } = pluginData; | ||
@@ -185,2 +193,4 @@ const fullpath = _resolve(resolveDir, originPath); | ||
log(`on load ${namespace}:${path} in ${relative(resolveDir)}`); | ||
const classPrefix = basename(originPath, extname(originPath)) | ||
@@ -197,4 +207,7 @@ .replace(/[^a-zA-Z0-9]/g, '-') | ||
const pathForLightningCss = _relative(process.cwd(), fullpath); | ||
log(`build css modules for ${pathForLightningCss}`); | ||
const result = buildFn({ | ||
filename: relativeFilePath, | ||
filename: pathForLightningCss, | ||
// @ts-ignore | ||
@@ -220,3 +233,2 @@ code: shouldBundle ? undefined : await readFile(fullpath), | ||
/** @type {string[]} */ | ||
// const jsLines = isEsbuildBundleMode ? [`import "${pluginNamespace}:${relativeFilePath}";`] : []; | ||
const jsLines = isEsbuildBundleMode ? [`import "${originPath}";`] : []; | ||
@@ -358,2 +370,5 @@ | ||
({ path, resolveDir }) => { | ||
log( | ||
`resolve ${path} in ${relative(resolveDir)} to ${path}.${virtualJsExt} with namespace ${pluginJsNamespace}` | ||
); | ||
return { | ||
@@ -433,13 +448,19 @@ path: `${path}.${virtualJsExt}`, | ||
patchedBuild.esbuild.build({ | ||
absWorkingDir: buildRoot, | ||
define: defines, | ||
outExtension: patchedBuild.initialOptions.outExtension, | ||
allowOverwrite: true, | ||
entryPoints, | ||
write: true, | ||
bundle: false, | ||
sourcemap: patchedBuild.initialOptions.sourcemap, | ||
outdir: buildRoot | ||
}); | ||
log('build inject js'); | ||
patchedBuild.esbuild | ||
.build({ | ||
absWorkingDir: buildRoot, | ||
define: defines, | ||
outExtension: patchedBuild.initialOptions.outExtension, | ||
allowOverwrite: true, | ||
entryPoints, | ||
write: true, | ||
bundle: false, | ||
target: patchedBuild.initialOptions.target || 'esnext', | ||
format: patchedBuild.initialOptions.format || 'esm', | ||
sourcemap: patchedBuild.initialOptions.sourcemap, | ||
outdir: buildRoot | ||
}) | ||
.finally(() => log('build end')); | ||
} else if (!isEsbuildBundleMode) { | ||
@@ -451,2 +472,3 @@ /** @type {string[]} */ | ||
}); | ||
log(`modify \`*.css\` to \`*.js\` in output js files`); | ||
await Promise.all( | ||
@@ -463,2 +485,3 @@ builtJsFiles.map((js) => { | ||
); | ||
log('build end'); | ||
} | ||
@@ -465,0 +488,0 @@ }); |
{ | ||
"name": "esbuild-css-modules-plugin", | ||
"version": "3.0.0-dev.1", | ||
"version": "3.0.0-dev.2", | ||
"description": "A esbuild plugin to bundle css modules into js(x)/ts(x).", | ||
@@ -5,0 +5,0 @@ "main": "./index.js", |
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
30829
731