Skip to content

Commit

Permalink
Count pass instead of bool flag
Browse files Browse the repository at this point in the history
  • Loading branch information
Trinovantes committed Apr 25, 2022
1 parent 3d42d1e commit e385095
Showing 1 changed file with 32 additions and 15 deletions.
47 changes: 32 additions & 15 deletions src/webpack/QuasarUnusedPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ interface QuasarAutoImport {
}
}

enum CompilationPass {
FIND_COMPONENTS,
MODIFY_QUASAR,
NUM_PASSES,
}

const quasarAutoImport = quasarAutoImportJson as QuasarAutoImport
const componentRegex = new RegExp(quasarAutoImport.regex.components)

Expand Down Expand Up @@ -140,31 +146,40 @@ export class QuasarUnusedPlugin implements WebpackPluginInstance {

#rewriteQuasarModule(compiler: Compiler) {
const logger = compiler.getInfrastructureLogger(PLUGIN_NAME)
let isFirstPass = true

let passCount = 0
let hasModifiedQuasar = false

compiler.hooks.done.tap(PLUGIN_NAME, (stats) => {
if (stats.compilation.needAdditionalPass) {
return
}

assert(passCount === CompilationPass.NUM_PASSES)

logger.info(`Found ${this.#usedComponents.size} Quasar component(s) being used`, [...this.#usedComponents])

if (!hasModifiedQuasar) {
logger.warn(`Did not rewrite quasar. Did you import "${QUASAR_INDEX_FILE}"?`)
}
})

compiler.hooks.thisCompilation.tap(PLUGIN_NAME, (compilation) => {
// Need one complete compilation pass to be able to parse all source files for references to quasar components
compilation.hooks.needAdditionalPass.tap(PLUGIN_NAME, () => {
if (isFirstPass) {
logger.info(`Found ${this.#usedComponents.size} Quasar component(s) being used`, [...this.#usedComponents])
isFirstPass = false
return true
}

return false
passCount += 1
return passCount < CompilationPass.NUM_PASSES
})

// Disable assets for first pass since we need to first parse all assets for references to quasar components
compilation.hooks.processAssets.tap({
name: PLUGIN_NAME,
stage: Compilation.PROCESS_ASSETS_STAGE_PRE_PROCESS,
}, () => {
if (!isFirstPass) {
return
}

for (const asset of Object.keys(compilation.assets)) {
compilation.deleteAsset(asset)
if (passCount < CompilationPass.MODIFY_QUASAR) {
for (const asset of Object.keys(compilation.assets)) {
compilation.deleteAsset(asset)
}
}
})

Expand All @@ -175,10 +190,12 @@ export class QuasarUnusedPlugin implements WebpackPluginInstance {
return
}

if (isFirstPass) {
if (passCount < CompilationPass.MODIFY_QUASAR) {
return
}

hasModifiedQuasar = true

const packageJson = normalModule.resourceResolveData?.descriptionFileData as Record<string, unknown>
packageJson.sideEffects = this.#options.sideEffectsOverride

Expand Down

0 comments on commit e385095

Please sign in to comment.