|
| 1 | +// Vendored from https://github.com/rollup/plugins/blob/0090e728f52828d39b071ab5c7925b9b575cd568/packages/sucrase/src/index.js and modified |
| 2 | + |
| 3 | +/* |
| 4 | +
|
| 5 | +The MIT License (MIT) |
| 6 | +
|
| 7 | +Copyright (c) 2019 RollupJS Plugin Contributors (https://github.com/rollup/plugins/graphs/contributors) |
| 8 | +
|
| 9 | +Permission is hereby granted, free of charge, to any person obtaining a copy |
| 10 | +of this software and associated documentation files (the "Software"), to deal |
| 11 | +in the Software without restriction, including without limitation the rights |
| 12 | +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 13 | +copies of the Software, and to permit persons to whom the Software is |
| 14 | +furnished to do so, subject to the following conditions: |
| 15 | +
|
| 16 | +The above copyright notice and this permission notice shall be included in |
| 17 | +all copies or substantial portions of the Software. |
| 18 | +
|
| 19 | +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 20 | +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 21 | +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 22 | +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 23 | +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 24 | +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 25 | +THE SOFTWARE. |
| 26 | +
|
| 27 | +*/ |
| 28 | + |
| 29 | +import fs from 'fs'; |
| 30 | +import path from 'path'; |
| 31 | + |
| 32 | +import { createFilter } from '@rollup/pluginutils'; |
| 33 | +import { transform } from 'sucrase'; |
| 34 | + |
| 35 | +export default function sucrase(opts = {}, sucraseOpts = {}) { |
| 36 | + const filter = createFilter(opts.include, opts.exclude); |
| 37 | + |
| 38 | + return { |
| 39 | + name: 'sucrase', |
| 40 | + |
| 41 | + // eslint-disable-next-line consistent-return |
| 42 | + resolveId(importee, importer) { |
| 43 | + if (importer && /^[./]/.test(importee)) { |
| 44 | + const resolved = path.resolve(importer ? path.dirname(importer) : process.cwd(), importee); |
| 45 | + // resolve in the same order that TypeScript resolves modules |
| 46 | + const resolvedFilenames = [ |
| 47 | + `${resolved}.ts`, |
| 48 | + `${resolved}.tsx`, |
| 49 | + `${resolved}/index.ts`, |
| 50 | + `${resolved}/index.tsx`, |
| 51 | + ]; |
| 52 | + if (resolved.endsWith('.js')) { |
| 53 | + resolvedFilenames.splice(2, 0, `${resolved.slice(0, -3)}.ts`, `${resolved.slice(0, -3)}.tsx`); |
| 54 | + } |
| 55 | + const resolvedFilename = resolvedFilenames.find(filename => fs.existsSync(filename)); |
| 56 | + |
| 57 | + if (resolvedFilename) { |
| 58 | + return resolvedFilename; |
| 59 | + } |
| 60 | + } |
| 61 | + }, |
| 62 | + |
| 63 | + transform(code, id) { |
| 64 | + if (!filter(id)) return null; |
| 65 | + const result = transform(code, { |
| 66 | + transforms: sucraseOpts.transforms, |
| 67 | + filePath: id, |
| 68 | + sourceMapOptions: { |
| 69 | + compiledFilename: id, |
| 70 | + }, |
| 71 | + ...sucraseOpts, |
| 72 | + }); |
| 73 | + return { |
| 74 | + code: result.code, |
| 75 | + map: result.sourceMap, |
| 76 | + }; |
| 77 | + }, |
| 78 | + }; |
| 79 | +} |
0 commit comments