diff --git a/lib/module.js b/lib/module.js index a9e3800..1427df9 100644 --- a/lib/module.js +++ b/lib/module.js @@ -1,4 +1,4 @@ -const { resolve } = require('path') +const { resolve, join } = require('path') const { prepareFonts } = require('./utils/fontFace') const { addWebWorker } = require('./worker') @@ -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 @@ -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) { @@ -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'), @@ -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) } } diff --git a/package.json b/package.json index a42ed91..86f2c76 100755 --- a/package.json +++ b/package.json @@ -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\"", @@ -66,4 +66,4 @@ "publishConfig": { "access": "public" } -} +} \ No newline at end of file