Skip to content

v0.28.8 regression: Relative urls in @font-face throw exception TypeError: url.replace is not a function #659

Closed
@DawnWright

Description

@DawnWright

This bug was introduced in v0.28.8, and does not occur in v0.28.7 - it was introduced last week by #627

Repro:
Use a relative url inside a css @font-face, e.g.

@font-face {
    font-family: 'Test Font';
    src: url('../MyFont.woff2');
}

RESULT: css-loader throws exception TypeError: url.replace is not a function:

Module build failed: TypeError: url.replace is not a function
    at escape (webpack-internal:///1:11:26)
    at eval (webpack-internal:///0:7:71)
    at Object.<anonymous> (/redactedpath/node_modules/css-loader/index.js??ref--1-1!/redactedpath/node_modules/sass-loader/lib/loader.js!/redactedpath/node_modules/stripcomment-loader/index.js!/redactedpath/app/css/styles.scss:71:1)
    at __webpack_require__ (/redactedpath/node_modules/css-loader/index.js??ref--1-1!/redactedpath/node_modules/sass-loader/lib/loader.js!/redactedpath/node_modules/stripcomment-loader/index.js!/redactedpath/app/css/styles.scss:21:30)
    at /redactedpath/node_modules/css-loader/index.js??ref--1-1!/redactedpath/node_modules/sass-loader/lib/loader.js!/redactedpath/node_modules/stripcomment-loader/index.js!/redactedpath/app/css/styles.scss:64:18
    at Object.<anonymous> (/redactedpath/node_modules/css-loader/index.js??ref--1-1!/redactedpath/node_modules/sass-loader/lib/loader.js!/redactedpath/node_modules/stripcomment-loader/index.js!/redactedpath/app/css/styles.scss:67:10)
    at Module._compile (module.js:624:30)
    at Object.exec (/redactedpath/node_modules/webpack/lib/NormalModule.js:129:12)
    at /redactedpath/node_modules/extract-text-webpack-plugin/dist/loader.js:131:26
    at compile (/redactedpath/node_modules/webpack/lib/Compiler.js:300:11)```

Note, the URL is indeed a valid path, and I've confirmed that I get the appropriate ModuleNotFoundError with invalid relative paths. This bug only occurs when the relative path points to a valid file location.

Also note, the bug does not occur with an absolute path to the same file. Unfortunately, using absolute path is not a feasible workaround since the css in question is an external file we have no control over.

My understanding is that a relative url tells the css-loader to ignore the url. resolve therefore returns {} as the URL, which is passed to the new escape function and thus throws an exception calling replace:

module.exports = function escape(url) {
    // If url is already wrapped in quotes, remove them
    if (/^['"].*['"]$/.test(url)) {
        url = url.slice(1, -1);
    }
    // Should url be wrapped?
    // See https://drafts.csswg.org/css-values-3/#urls
    if (/["'() \t\n]/.test(url)) {
        return '"' + url.replace(/"/g, '\\"').replace(/\n/g, '\\n') + '"'
    }

    return url
}

We have reverted to v0.28.7 temporarily to avoid this bug.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions