-
Notifications
You must be signed in to change notification settings - Fork 916
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: compat for vue 2.7, support
<script setup>
- Loading branch information
Showing
7 changed files
with
174 additions
and
75 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
// resolve compilers to use. | ||
|
||
let cached | ||
|
||
exports.resolveCompiler = function (loaderContext) { | ||
if (cached) { | ||
return cached | ||
} | ||
|
||
const ctx = loaderContext.rootContext | ||
// check 2.7 | ||
try { | ||
const pkg = loadFromContext('vue/package.json', ctx) | ||
const [major, minor] = pkg.version.split('.') | ||
if (major === '2' && minor === '7') { | ||
return (cached = { | ||
compiler: loadFromContext('vue/compiler-sfc', ctx), | ||
templateCompiler: undefined | ||
}) | ||
} | ||
} catch (e) {} | ||
|
||
return (cached = { | ||
compiler: require('@vue/component-compiler-utils'), | ||
templateCompiler: loadTemplateCompiler(loaderContext) | ||
}) | ||
} | ||
|
||
function loadFromContext (path, ctx) { | ||
return require(require.resolve(path, { | ||
paths: [ctx] | ||
})) | ||
} | ||
|
||
function loadTemplateCompiler (loaderContext) { | ||
try { | ||
return loadFromContext('vue-template-compiler', loaderContext.rootContext) | ||
} catch (e) { | ||
if (/version mismatch/.test(e.toString())) { | ||
loaderContext.emitError(e) | ||
} else { | ||
loaderContext.emitError( | ||
new Error( | ||
`[vue-loader] vue-template-compiler must be installed as a peer dependency, ` + | ||
`or a compatible compiler implementation must be passed via options.` | ||
) | ||
) | ||
} | ||
} | ||
} |
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,49 @@ | ||
const { resolveCompiler } = require('./compiler') | ||
|
||
const clientCache = new WeakMap() | ||
const serverCache = new WeakMap() | ||
|
||
exports.resolveScript = function resolveScript ( | ||
descriptor, | ||
options, | ||
loaderContext | ||
) { | ||
if (!descriptor.script && !descriptor.scriptSetup) { | ||
return null | ||
} | ||
|
||
const { compiler } = resolveCompiler(loaderContext) | ||
if (!compiler.compileScript) { | ||
if (descriptor.scriptSetup) { | ||
loaderContext.emitError( | ||
'The version of Vue you are using does not support <script setup>. ' + | ||
'Please upgrade to 2.7 or above.' | ||
) | ||
} | ||
return descriptor.script | ||
} | ||
|
||
const isProd = | ||
loaderContext.mode === 'production' || process.env.NODE_ENV === 'production' | ||
const isServer = options.optimizeSSR || loaderContext.target === 'node' | ||
|
||
const cacheToUse = isServer ? serverCache : clientCache | ||
const cached = cacheToUse.get(descriptor) | ||
if (cached) { | ||
return cached | ||
} | ||
|
||
let resolved = null | ||
|
||
try { | ||
resolved = compiler.compileScript(descriptor, { | ||
isProd, | ||
babelParserPlugins: options.babelParserPlugins | ||
}) | ||
} catch (e) { | ||
loaderContext.emitError(e) | ||
} | ||
|
||
cacheToUse.set(descriptor, resolved) | ||
return resolved | ||
} |
Oops, something went wrong.