|
| 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 urlModule from 'url'; |
| 34 | +import babel from '@babel/core'; |
| 35 | + |
| 36 | +const {loadOptionsAsync, transformAsync} = babel; |
| 37 | + |
| 38 | +const isBabelConfigFile = (filename) => { |
| 39 | + const basename = path.basename(filename); |
| 40 | + return ( |
| 41 | + basename === '.babelrc.js' || |
| 42 | + basename === '.babelrc.mjs' || |
| 43 | + basename === 'babel.config.js' || |
| 44 | + basename === 'babel.config.mjs' || |
| 45 | + basename === '.babelrc' || |
| 46 | + basename === '.babelrc.cjs' || |
| 47 | + basename === 'babel.config.cjs' |
| 48 | + ); |
| 49 | +}; |
| 50 | + |
| 51 | +const getFormatWithCertitude = (filename) => { |
| 52 | + return /\.cjs$/.test(filename) |
| 53 | + ? 'commonjs' |
| 54 | + : /\.mjs$/.test(filename) |
| 55 | + ? 'module' |
| 56 | + : undefined; |
| 57 | +}; |
| 58 | + |
| 59 | +const getFormat = (filename) => { |
| 60 | + const packageType = 'module'; // TODO query package.json |
| 61 | + const defaultFormat = 'json'; // TODO is this correct as default? |
| 62 | + |
| 63 | + return ( |
| 64 | + getFormatWithCertitude(filename) ?? |
| 65 | + (/\.js$/.test(filename) ? packageType : defaultFormat) |
| 66 | + ); |
| 67 | +}; |
| 68 | + |
| 69 | +const getSourceType = (filename) => { |
| 70 | + // TODO replace with getFormat once getFormat queries package.json |
| 71 | + const format = getFormatWithCertitude(filename); |
| 72 | + |
| 73 | + switch (format) { |
| 74 | + case 'module': |
| 75 | + return 'module'; |
| 76 | + case 'commonjs': |
| 77 | + return 'script'; |
| 78 | + default: |
| 79 | + return 'unambiguous'; |
| 80 | + } |
| 81 | +}; |
| 82 | + |
| 83 | +const skip = (url) => { |
| 84 | + return /node_modules/.test(url) || /node:/.test(url); |
| 85 | +}; |
| 86 | + |
| 87 | +export const load = async (url, context, defaultLoad) => { |
| 88 | + if (skip(url)) return defaultLoad(url, context, defaultLoad); |
| 89 | + |
| 90 | + const {source} = await defaultLoad(url, context, defaultLoad); |
| 91 | + |
| 92 | + const filename = urlModule.fileURLToPath(url); |
| 93 | + // Babel config files can themselves be ES modules, |
| 94 | + // but we cannot transform those since doing so would cause an infinite loop. |
| 95 | + if (isBabelConfigFile(filename)) { |
| 96 | + return { |
| 97 | + source, |
| 98 | + format: getFormat(filename), |
| 99 | + }; |
| 100 | + } |
| 101 | + |
| 102 | + const options = await loadOptionsAsync({ |
| 103 | + sourceType: getSourceType(filename), |
| 104 | + root: process.cwd(), |
| 105 | + rootMode: 'root', |
| 106 | + filename, |
| 107 | + configFile: true, |
| 108 | + }); |
| 109 | + const transformed = await transformAsync(source, options); |
| 110 | + |
| 111 | + return { |
| 112 | + source: transformed.code, |
| 113 | + // TODO: look at babel config to see whether it will output ESM/CJS or other formats |
| 114 | + format: getFormat(filename), |
| 115 | + }; |
| 116 | +}; |
0 commit comments