Skip to content

Commit

Permalink
feat: auto detect style loader
Browse files Browse the repository at this point in the history
  • Loading branch information
farnabaz committed Dec 12, 2020
1 parent 3950c70 commit 2e4fd08
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
15 changes: 15 additions & 0 deletions lib/module.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,20 @@ const AMPBoilerplate = '<style amp-boilerplate>body{-webkit-animation:-amp-start
const scriptPattern = /<script\b[^<]*(?:(?!<\/script>)<[^<]*)*<\/script>/gi
const ampBodyPattern = /<amp-body[^>]*>([.\S\s]*)<\/amp-body>/

const styleLoaders = [
{
test: /\.scss$/i,
loader: '!sass-loader'
}
]

module.exports = function (moduleOptions) {
const { nuxt } = this
const options = {
cdnBase: undefined,
css: undefined,
origin: '',
cssLoader: '',
mode: 'hybrid',
tags: {},
routeAliases: 'auto',
Expand All @@ -23,6 +31,13 @@ module.exports = function (moduleOptions) {
...moduleOptions
}

if (options.css && !options.cssLoader) {
const matchedLoader = styleLoaders.find(({ test }) => options.css.match(test))
if (matchedLoader) {
options.cssLoader = matchedLoader.loader
}
}

registerPlugin.call(this, options)
registerRendererHook.call(this, options)
ensureMeta.call(this, options)
Expand Down
2 changes: 1 addition & 1 deletion templates/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export default async function (ctx, inject) {
Object.keys(result).forEach(key => inject(key, result[key]))
}
<% if (options.css) { %>if (ctx.$isAMP) {
const cssText = await import('!!raw-loader!sass-loader!<%= options.css %>').then(m => m.default || m)
const cssText = await import('!!raw-loader<%= options.cssLoader %>!<%= options.css %>').then(m => m.default || m)
ctx.app.head.style.push({ cssText, type: 'text/css', 'amp-custom': '' })
}<% } %>
}

0 comments on commit 2e4fd08

Please # to comment.