|
| 1 | +const swc = require("@swc/core"); |
| 2 | + |
| 3 | +function makeLoader() { |
| 4 | + return function (source, inputSourceMap) { |
| 5 | + // Make the loader async |
| 6 | + const callback = this.async(); |
| 7 | + const filename = this.resourcePath; |
| 8 | + |
| 9 | + let loaderOptions = |
| 10 | + (typeof this.getOptions === "function" |
| 11 | + ? this.getOptions() |
| 12 | + : require("loader-utils").getOptions(this)) || {}; |
| 13 | + |
| 14 | + // Standardize on 'sourceMaps' as the key passed through to Webpack, so that |
| 15 | + // users may safely use either one alongside our default use of |
| 16 | + // 'this.sourceMap' below without getting error about conflicting aliases. |
| 17 | + if ( |
| 18 | + Object.prototype.hasOwnProperty.call(loaderOptions, "sourceMap") && |
| 19 | + !Object.prototype.hasOwnProperty.call(loaderOptions, "sourceMaps") |
| 20 | + ) { |
| 21 | + loaderOptions = Object.assign({}, loaderOptions, { |
| 22 | + sourceMaps: loaderOptions.sourceMap, |
| 23 | + }); |
| 24 | + delete loaderOptions.sourceMap; |
| 25 | + } |
| 26 | + |
| 27 | + if (inputSourceMap && typeof inputSourceMap === "object") { |
| 28 | + inputSourceMap = JSON.stringify(inputSourceMap); |
| 29 | + } |
| 30 | + |
| 31 | + const programmaticOptions = Object.assign({}, loaderOptions, { |
| 32 | + filename, |
| 33 | + inputSourceMap: inputSourceMap || undefined, |
| 34 | + |
| 35 | + // Set the default sourcemap behavior based on Webpack's mapping flag, |
| 36 | + // but allow users to override if they want. |
| 37 | + sourceMaps: |
| 38 | + loaderOptions.sourceMaps === undefined |
| 39 | + ? this.sourceMap |
| 40 | + : loaderOptions.sourceMaps, |
| 41 | + |
| 42 | + // Ensure that Webpack will get a full absolute path in the sourcemap |
| 43 | + // so that it can properly map the module back to its internal cached |
| 44 | + // modules. |
| 45 | + sourceFileName: filename, |
| 46 | + }); |
| 47 | + if (!programmaticOptions.inputSourceMap) { |
| 48 | + delete programmaticOptions.inputSourceMap; |
| 49 | + } |
| 50 | + |
| 51 | + const sync = programmaticOptions.sync; |
| 52 | + const parseMap = programmaticOptions.parseMap; |
| 53 | + |
| 54 | + // Remove loader related options |
| 55 | + delete programmaticOptions.sync; |
| 56 | + delete programmaticOptions.parseMap; |
| 57 | + delete programmaticOptions.customize; |
| 58 | + delete programmaticOptions.cacheDirectory; |
| 59 | + delete programmaticOptions.cacheIdentifier; |
| 60 | + delete programmaticOptions.cacheCompression; |
| 61 | + delete programmaticOptions.metadataSubscribers; |
| 62 | + |
| 63 | + // auto detect development mode |
| 64 | + if ( |
| 65 | + this.mode && |
| 66 | + programmaticOptions.jsc && |
| 67 | + programmaticOptions.jsc.transform && |
| 68 | + programmaticOptions.jsc.transform.react && |
| 69 | + !Object.prototype.hasOwnProperty.call( |
| 70 | + programmaticOptions.jsc.transform.react, |
| 71 | + "development" |
| 72 | + ) |
| 73 | + ) { |
| 74 | + programmaticOptions.jsc.transform.react.development = |
| 75 | + this.mode === "development"; |
| 76 | + } |
| 77 | + |
| 78 | + if (programmaticOptions.sourceMaps === "inline") { |
| 79 | + // Babel has this weird behavior where if you set "inline", we |
| 80 | + // inline the sourcemap, and set 'result.map = null'. This results |
| 81 | + // in bad behavior from Babel since the maps get put into the code, |
| 82 | + // which Webpack does not expect, and because the map we return to |
| 83 | + // Webpack is null, which is also bad. To avoid that, we override the |
| 84 | + // behavior here so "inline" just behaves like 'true'. |
| 85 | + programmaticOptions.sourceMaps = true; |
| 86 | + } |
| 87 | + |
| 88 | + try { |
| 89 | + if (sync) { |
| 90 | + const output = swc.transformSync(source, programmaticOptions); |
| 91 | + callback( |
| 92 | + null, |
| 93 | + output.code, |
| 94 | + parseMap ? JSON.parse(output.map) : output.map |
| 95 | + ); |
| 96 | + } else { |
| 97 | + swc.transform(source, programmaticOptions).then( |
| 98 | + (output) => { |
| 99 | + callback( |
| 100 | + null, |
| 101 | + output.code, |
| 102 | + parseMap ? JSON.parse(output.map) : output.map |
| 103 | + ); |
| 104 | + }, |
| 105 | + (err) => { |
| 106 | + callback(err); |
| 107 | + } |
| 108 | + ); |
| 109 | + } |
| 110 | + } catch (e) { |
| 111 | + callback(e); |
| 112 | + } |
| 113 | + }; |
| 114 | +} |
| 115 | + |
| 116 | +module.exports = makeLoader(); |
| 117 | +module.exports.custom = makeLoader; |
0 commit comments