|
| 1 | +/** |
| 2 | + * Based on source of @node-loader/babel |
| 3 | + * |
| 4 | + * Source: https://github.com/node-loader/node-loader-babel |
| 5 | + * |
| 6 | + * with the following license: |
| 7 | + * |
| 8 | + * MIT License |
| 9 | + * |
| 10 | + * Copyright (c) 2020 node-loaders |
| 11 | + * |
| 12 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 13 | + * of this software and associated documentation files (the "Software"), to deal |
| 14 | + * in the Software without restriction, including without limitation the rights |
| 15 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 16 | + * copies of the Software, and to permit persons to whom the Software is |
| 17 | + * furnished to do so, subject to the following conditions: |
| 18 | + * |
| 19 | + * The above copyright notice and this permission notice shall be included in all |
| 20 | + * copies or substantial portions of the Software. |
| 21 | + * |
| 22 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 23 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 24 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 25 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 26 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 27 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 28 | + * SOFTWARE. |
| 29 | + */ |
| 30 | + |
| 31 | +import process from 'process'; |
| 32 | +import path from 'path'; |
| 33 | +import {fileURLToPath} from 'url'; |
| 34 | +import {loadOptionsAsync, transformAsync} from '@babel/core'; |
| 35 | + |
| 36 | +const BABEL_FORMATS_TRANSFORMED = new Set(['module']); |
| 37 | + |
| 38 | +const BABEL_CONFIG_FILES = new Set([ |
| 39 | + '.babelrc.js', |
| 40 | + '.babelrc.mjs', |
| 41 | + 'babel.config.js', |
| 42 | + 'babel.config.mjs', |
| 43 | + '.babelrc', |
| 44 | + '.babelrc.cjs', |
| 45 | + 'babel.config.cjs', |
| 46 | +]); |
| 47 | + |
| 48 | +const anyURLToPathOrUndefined = (url) => { |
| 49 | + try { |
| 50 | + return fileURLToPath(url); |
| 51 | + } catch (error) { |
| 52 | + if (error instanceof TypeError && error.code === 'ERR_INVALID_URL_SCHEME') { |
| 53 | + return undefined; |
| 54 | + } |
| 55 | + |
| 56 | + throw error; |
| 57 | + } |
| 58 | +}; |
| 59 | + |
| 60 | +const isBabelConfigFile = (filename) => { |
| 61 | + const basename = path.basename(filename); |
| 62 | + return BABEL_CONFIG_FILES.has(basename); |
| 63 | +}; |
| 64 | + |
| 65 | +const getSourceType = (format) => { |
| 66 | + switch (format) { |
| 67 | + case 'module': |
| 68 | + return 'module'; |
| 69 | + case 'commonjs': |
| 70 | + return 'script'; |
| 71 | + default: |
| 72 | + return 'unambiguous'; |
| 73 | + } |
| 74 | +}; |
| 75 | + |
| 76 | +const prepare = async (url, context, defaultLoad) => { |
| 77 | + const original = await defaultLoad(url, context, defaultLoad); |
| 78 | + |
| 79 | + const noop = () => ({ |
| 80 | + transform: false, |
| 81 | + original, |
| 82 | + }); |
| 83 | + |
| 84 | + if ( |
| 85 | + /node_modules/.test(url) || |
| 86 | + /node:/.test(url) || |
| 87 | + !BABEL_FORMATS_TRANSFORMED.has(original.format) |
| 88 | + ) { |
| 89 | + return noop(); |
| 90 | + } |
| 91 | + |
| 92 | + const filename = anyURLToPathOrUndefined(url); |
| 93 | + |
| 94 | + // Babel config files can themselves be ES modules, |
| 95 | + // but transforming those could require more than one pass. |
| 96 | + if (isBabelConfigFile(filename)) return noop(); |
| 97 | + |
| 98 | + return { |
| 99 | + transform: true, |
| 100 | + original, |
| 101 | + options: { |
| 102 | + filename, |
| 103 | + }, |
| 104 | + }; |
| 105 | +}; |
| 106 | + |
| 107 | +const transformed = async ({format, source}, {filename}) => { |
| 108 | + const options = await loadOptionsAsync({ |
| 109 | + sourceType: getSourceType(format), |
| 110 | + root: process.cwd(), |
| 111 | + rootMode: 'root', |
| 112 | + filename, |
| 113 | + configFile: true, |
| 114 | + }); |
| 115 | + |
| 116 | + const result = await transformAsync(source, options); |
| 117 | + |
| 118 | + return { |
| 119 | + source: result.code, |
| 120 | + // TODO: look at babel config to see whether it will output ESM/CJS or other formats |
| 121 | + format, |
| 122 | + }; |
| 123 | +}; |
| 124 | + |
| 125 | +export const load = async (url, context, defaultLoad) => { |
| 126 | + const {transform, original, options} = await prepare( |
| 127 | + url, |
| 128 | + context, |
| 129 | + defaultLoad, |
| 130 | + ); |
| 131 | + return transform ? transformed(original, options) : original; |
| 132 | +}; |
0 commit comments