rsnext/packages/next/build/webpack/loaders/emit-file-loader.js
Guy Bedford 005a8abe39
feat: Webpack loader inlining (#21127)
This picks up on the inlining work in https://github.com/vercel/next.js/pull/20598 to also include webpack loader inlining optimizations.

This includes:
* The dependencies of sass-loader
* resolve-url-loader

And for added benefit:
* babel-plugin-transform-define
* babel-plugin-transform-react-remove-prop-types

style-loader and css-loader didn't inline easily. Perhaps we can come back to these ones.
2021-01-15 01:51:45 +00:00

44 lines
1.2 KiB
JavaScript

import loaderUtils from 'next/dist/compiled/loader-utils'
module.exports = function (content, sourceMap) {
this.cacheable()
const callback = this.async()
const resourcePath = this.resourcePath
const query = loaderUtils.getOptions(this)
// Allows you to do checks on the file name. For example it's used to check if there's both a .js and .jsx file.
if (query.validateFileName) {
try {
query.validateFileName(resourcePath)
} catch (err) {
callback(err)
return
}
}
const name = query.name || '[hash].[ext]'
const context = query.context || this.rootContext || this.options.context
const regExp = query.regExp
const opts = { context, content, regExp }
const interpolateName = query.interpolateName || ((inputName) => inputName)
const interpolatedName = interpolateName(
loaderUtils.interpolateName(this, name, opts),
{ name, opts }
)
const emit = (code, map) => {
this.emitFile(interpolatedName, code, map)
callback(null, code, map)
}
if (query.transform) {
const transformed = query.transform({
content,
sourceMap,
interpolatedName,
})
return emit(transformed.content, transformed.sourceMap)
}
return emit(content, sourceMap)
}