-
Notifications
You must be signed in to change notification settings - Fork 400
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: upgrade lwc rollup to support compat (#646)
* fix: comapt mode in rollup * fix: util refactor for better testing * chore: upgrade best * perf: change back to prod
- Loading branch information
Showing
6 changed files
with
119 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
const path = require('path'); | ||
|
||
function getModuleQualifiedName(file) { | ||
const registry = { | ||
entry: file, | ||
moduleSpecifier: null, | ||
moduleName: null, | ||
moduleNamespace: null | ||
}; | ||
|
||
const rootParts = path.dirname(file).split(path.sep); | ||
|
||
registry.moduleName = rootParts.pop(); | ||
registry.moduleNamespace = rootParts.pop(); | ||
|
||
return registry; | ||
} | ||
|
||
function normalizeResult(result) { | ||
return { code: result.code || result, map: result.map || { mappings: "" } }; | ||
} | ||
|
||
function getLwcEnginePath(mode) { | ||
const path = require.resolve('lwc-engine'); | ||
const moduleTypeFolder = 'modules'; | ||
const target = mode.includes('compat') ? 'es5' : 'es2017'; | ||
|
||
return path | ||
.replace('commonjs', moduleTypeFolder) | ||
.replace('es2017', target); | ||
} | ||
|
||
function resolveRollupCompat({ mode, compat }) { | ||
if (mode === 'compat' || mode === 'prod_compat') { | ||
try { | ||
// We will compose compat plugin on top of this one (delegated) | ||
return require("rollup-plugin-compat").default(compat); | ||
} catch (e) { | ||
throw new Error( | ||
`Unable to compile resources for mode ${mode}` + | ||
`In order to use "compat" mode, you must include 'rollup-plugin-compat' as part of your dependencies` | ||
); | ||
} | ||
} | ||
// If we are not in compat, rollup becomes a noop | ||
// This just simplifies the logic bellow | ||
return { | ||
resolveId() { return undefined; }, | ||
load() { return undefined; }, | ||
knownCompatModule() { return false; }, | ||
transform(src) { return src; }, | ||
transformBundle(src) { return src; } | ||
}; | ||
} | ||
|
||
module.exports = { | ||
getModuleQualifiedName, | ||
normalizeResult, | ||
getLwcEnginePath, | ||
resolveRollupCompat | ||
}; |