Skip to content

Commit

Permalink
fix(imports): fix css import for require and absolute source
Browse files Browse the repository at this point in the history
- fix base path by font css
  • Loading branch information
Thorn Walli committed Jan 29, 2021
1 parent 52951bf commit 2ed40c9
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 13 deletions.
23 changes: 13 additions & 10 deletions lib/module.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { resolve } = require('path')
const { resolve, join } = require('path')
const { prepareFonts } = require('./utils/fontFace')
const { addWebWorker } = require('./worker')

Expand Down Expand Up @@ -44,7 +44,7 @@ module.exports = async function (moduleOptions) {
* CSS `@import` Font-Path Resolver
*/
importPathResolve: (file) => {
return file.replace(/^@\/(.*)$/, '~$1')
return `~${file}`
},
/**
* JS `require` Font-Path Resolver
Expand All @@ -55,7 +55,7 @@ module.exports = async function (moduleOptions) {
...moduleOptions
}

addTemplates(this, await prepareFonts(options, options.importPathResolve), options)
addTemplates(this, await prepareFonts(options, file => filepathNormalizer(this.nuxt, file, filepath => options.importPathResolve(filepath))), options)
addPlugins(this, await prepareFonts(options, options.requirePathResolve, false), options)

if (options.useWorker) {
Expand Down Expand Up @@ -97,7 +97,7 @@ function addPlugins (moduleScope, fonts, options) {
}
})
const json = preparedFonts.reduce((result, { source }) => {
return result.replace(`"${source.path}"`, prepareFilePath(moduleScope, source.path))
return result.replace(`"${source.path}"`, filepathNormalizer(moduleScope, source.path, filepath => `require('${filepath}')`, '"'))
}, JSON.stringify(preparedFonts))
moduleScope.addPlugin({
src: resolve(__dirname, 'plugin.js'),
Expand All @@ -116,15 +116,18 @@ function addPlugins (moduleScope, fonts, options) {
})
}

function prepareFilePath (moduleScope, path) {
if (path.startsWith('/')) {
module.exports.meta = require('../package.json')

function filepathNormalizer (nuxt, filepath, resolve = f => f, quotes = '') {
const addQuotes = value => `${quotes}${value}${quotes}`
if (filepath.startsWith('/')) {
// local filepath with router base (static dir)
return `"${(moduleScope.options.router.base + path).replace(/^\/\//, '')}"`
} else if (path.startsWith('http') || path.startsWith('//')) {
return addQuotes(join(nuxt.options.router.base + filepath).replace(/^\/\//, '/'))
} else if (/^(ht|f)tp(s?):\/\//.test(filepath) || filepath.startsWith('//')) {
// url filepath
return `"${path}"`
return addQuotes(filepath)
} else {
return `require('${path}')`
return resolve(filepath)
}
}

Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@
"main": "lib/module.js",
"scripts": {
"dev": "nuxt example",
"generate": "nuxt generate --config-file example/nuxt.config.js",
"generate": "nuxt generate --config-file example/nuxt.config.js --target static",
"build": "nuxt build --config-file example/nuxt.config.js",
"start": "nuxt start --config-file example/nuxt.config.js",
"start": "nuxt start --config-file example/nuxt.config.js --target static",
"lint": "yarn lint:js && yarn lint:css",
"lint:js": "eslint --ext .js,.vue example lib test",
"lint:css": "stylelint \"example/**/*.vue\" \"example/**/*.css\", \"lib/**/*.vue\" \"lib/**/*.css\"",
Expand Down Expand Up @@ -66,4 +66,4 @@
"publishConfig": {
"access": "public"
}
}
}

0 comments on commit 2ed40c9

Please sign in to comment.