-
Notifications
You must be signed in to change notification settings - Fork 33
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
feat: support package exports and custom paths #52
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -60,14 +60,14 @@ export default async function getPackageStats( | |
...optionsRaw, | ||
} | ||
|
||
const { name: packageName, isLocal } = parsePackageString(packageString) | ||
const { name: packageName, isLocal, normalPath, importPath } = parsePackageString(packageString) | ||
const installPath = await InstallationUtils.preparePath(packageName) | ||
|
||
if (options.debug) { | ||
console.log('Install path:', installPath) | ||
} | ||
try { | ||
await InstallationUtils.installPackage(packageString, installPath, { | ||
await InstallationUtils.installPackage(normalPath, installPath, { | ||
isLocal, | ||
client: options.client, | ||
limitConcurrency: options.limitConcurrency, | ||
|
@@ -76,10 +76,11 @@ export default async function getPackageStats( | |
}) | ||
|
||
const externals = getExternals(packageName, installPath) | ||
const [pacakgeJSONDetails, builtDetails] = await Promise.all([ | ||
const [packageJSONDetails, builtDetails] = await Promise.all([ | ||
getPackageJSONDetails(packageName, installPath), | ||
BuildUtils.buildPackageIgnoringMissingDeps({ | ||
name: packageName, | ||
importPath, | ||
installPath, | ||
externals, | ||
options: { | ||
|
@@ -91,30 +92,37 @@ export default async function getPackageStats( | |
}), | ||
]) | ||
|
||
const hasCSSAsset = builtDetails.assets.some(asset => asset.type === 'css') | ||
const mainAsset = builtDetails.assets.find( | ||
asset => | ||
asset.name === 'main' && asset.type === (hasCSSAsset ? 'css' : 'js') | ||
) | ||
const mainAssets = builtDetails.assets.filter(asset => asset.name === 'main') | ||
|
||
if (!mainAsset) { | ||
if (!mainAssets.length) { | ||
throw new UnexpectedBuildError( | ||
'Did not find a main asset in the built bundle' | ||
) | ||
} | ||
|
||
const mainSizes = mainAssets.reduce((acc, asset) => { | ||
acc.size += asset.size | ||
acc.gzip += asset.gzip | ||
if (asset.parse) { | ||
acc.parse = acc.parse || {baseParseTime: 0, scriptParseTime: 0} | ||
acc.parse.baseParseTime += asset.parse.baseParseTime || 0 | ||
acc.parse.scriptParseTime += asset.parse.scriptParseTime || 0 | ||
} | ||
return acc | ||
}, { size: 0, gzip: 0, parse: null as null | { baseParseTime: number, scriptParseTime: number } }) | ||
Comment on lines
+103
to
+112
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I maintain a component library (Vuetify) that imports its own stylesheets. The way this was before would only include the size of the CSS instead of CSS + JS. |
||
|
||
Telemetry.packageStats( | ||
packageString, | ||
true, | ||
performance.now() - startTime, | ||
options | ||
) | ||
return { | ||
...pacakgeJSONDetails, | ||
...packageJSONDetails, | ||
...builtDetails, | ||
size: mainAsset.size, | ||
gzip: mainAsset.gzip, | ||
parse: mainAsset.parse, | ||
size: mainSizes.size, | ||
gzip: mainSizes.gzip, | ||
parse: mainSizes.parse, | ||
} | ||
} catch (e) { | ||
Telemetry.packageStats( | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,6 +25,7 @@ import { | |
CreateEntryPointOptions, | ||
} from '../common.types' | ||
import Telemetry from './telemetry.utils' | ||
import { resolve } from './exports.utils' | ||
|
||
type CompilePackageArgs = { | ||
name: string | ||
|
@@ -43,6 +44,7 @@ type CompilePackageReturn = { | |
type BuildPackageArgs = { | ||
name: string | ||
installPath: string | ||
importPath: string, | ||
externals: Externals | ||
options: BuildPackageOptions | ||
} | ||
|
@@ -51,7 +53,7 @@ type WebpackStatsAsset = NonNullable<webpack.Stats.ToJsonOutput['assets']>[0] | |
|
||
const BuildUtils = { | ||
createEntryPoint( | ||
packageName: string, | ||
importPath: string, | ||
installPath: string, | ||
options: CreateEntryPointOptions | ||
) { | ||
|
@@ -65,23 +67,24 @@ const BuildUtils = { | |
if (options.esm) { | ||
if (options.customImports) { | ||
importStatement = ` | ||
import { ${options.customImports.join(', ')} } from '${packageName}'; | ||
import { ${options.customImports.join(', ')} } from '${importPath}'; | ||
console.log(${options.customImports.join(', ')}) | ||
` | ||
} else { | ||
importStatement = `import p from '${packageName}'; console.log(p)` | ||
importStatement = `import p from '${importPath}'; console.log(p)` | ||
} | ||
} else { | ||
if (options.customImports) { | ||
importStatement = ` | ||
const { ${options.customImports.join( | ||
', ' | ||
)} } = require('${packageName}'); | ||
)} } = await import(/* webpackMode: 'eager' */'${importPath}'); | ||
console.log(${options.customImports.join(', ')}) | ||
` | ||
} else { | ||
importStatement = `const p = require('${packageName}'); console.log(p)` | ||
importStatement = `const p = await import(/* webpackMode: 'eager' */'${importPath}'); console.log(p)` | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
} | ||
importStatement = `;(async () => { ${importStatement} })();` | ||
} | ||
|
||
try { | ||
|
@@ -177,6 +180,7 @@ const BuildUtils = { | |
async buildPackage({ | ||
name, | ||
installPath, | ||
importPath, | ||
externals, | ||
options, | ||
}: BuildPackageArgs) { | ||
|
@@ -187,14 +191,14 @@ const BuildUtils = { | |
return { assets: [] } | ||
} | ||
options.customImports.forEach(importt => { | ||
entry[importt] = BuildUtils.createEntryPoint(name, installPath, { | ||
entry[importt] = BuildUtils.createEntryPoint(importPath, installPath, { | ||
customImports: [importt], | ||
entryFilename: importt, | ||
esm: true, | ||
}) | ||
}) | ||
} else { | ||
entry['main'] = BuildUtils.createEntryPoint(name, installPath, { | ||
entry['main'] = BuildUtils.createEntryPoint(importPath, installPath, { | ||
esm: false, | ||
customImports: options.customImports, | ||
}) | ||
|
@@ -330,16 +334,20 @@ const BuildUtils = { | |
name, | ||
externals, | ||
installPath, | ||
importPath, | ||
options, | ||
}: BuildPackageArgs) { | ||
const buildStartTime = performance.now() | ||
let buildIteration = 1 | ||
|
||
importPath = await resolve(installPath, importPath) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We're still using webpack 4, but have webpack 5's |
||
|
||
try { | ||
const buildResult = await BuildUtils.buildPackage({ | ||
name, | ||
externals, | ||
installPath, | ||
importPath, | ||
options, | ||
}) | ||
Telemetry.buildPackage(name, true, buildStartTime, { | ||
|
@@ -367,6 +375,7 @@ const BuildUtils = { | |
const rebuiltResult = await BuildUtils.buildPackage({ | ||
name, | ||
externals: newExternals, | ||
importPath, | ||
installPath, | ||
options, | ||
}) | ||
|
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.
Not sure what's going on here, this is the path the frontend is using.
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.
This is only a dev server — the frontend uses its own routes and server. But makes sense to synchronize here.