|
1 | 1 | import { readFile } from 'fs/promises'; |
2 | | -import { addFallbackToVar } from '../postcss-plugins/ds-token-fallbacks.mjs'; |
| 2 | +import { fileURLToPath, pathToFileURL } from 'url'; |
| 3 | +import remapping from '@jridgewell/remapping'; |
| 4 | +import { AnyMap } from '@jridgewell/trace-mapping'; |
| 5 | +import { transformDsTokenFallbacks } from '../js-plugins/transform-ds-token-fallbacks.mjs'; |
3 | 6 |
|
4 | 7 | /** @type {Record<string, import('esbuild').Loader>} */ |
5 | 8 | const LOADER_MAP = { |
6 | 9 | '.js': 'jsx', |
7 | 10 | '.jsx': 'jsx', |
8 | | - '.ts': 'tsx', |
| 11 | + '.ts': 'ts', |
9 | 12 | '.tsx': 'tsx', |
10 | 13 | '.mjs': 'jsx', |
11 | | - '.mts': 'tsx', |
| 14 | + '.mts': 'ts', |
12 | 15 | '.cjs': 'jsx', |
13 | | - '.cts': 'tsx', |
| 16 | + '.cts': 'ts', |
14 | 17 | }; |
15 | 18 |
|
16 | 19 | /** |
@@ -40,12 +43,79 @@ const plugin = { |
40 | 43 | } |
41 | 44 |
|
42 | 45 | const ext = args.path.match( /(\.[^.]+)$/ )?.[ 1 ] || '.js'; |
| 46 | + const result = transformDsTokenFallbacks( source, args.path ); |
| 47 | + if ( ! result ) { |
| 48 | + return { |
| 49 | + contents: source, |
| 50 | + loader: LOADER_MAP[ ext ] || 'jsx', |
| 51 | + }; |
| 52 | + } |
| 53 | + let sourceMap = result.map.toUrl(); |
| 54 | + /** @type {string[]} */ |
| 55 | + const watchFiles = []; |
| 56 | + /** @type {import('esbuild').PartialMessage[]} */ |
| 57 | + const warnings = []; |
| 58 | + if ( result.sourceMappingURL ) { |
| 59 | + try { |
| 60 | + const url = new URL( |
| 61 | + result.sourceMappingURL, |
| 62 | + pathToFileURL( args.path ) |
| 63 | + ); |
| 64 | + /** @type {string | undefined} */ |
| 65 | + let inputMap; |
| 66 | + let mapPath = args.path; |
| 67 | + if ( url.protocol === 'data:' ) { |
| 68 | + const comma = url.href.indexOf( ',' ); |
| 69 | + const data = decodeURIComponent( |
| 70 | + url.href.slice( comma + 1 ) |
| 71 | + ); |
| 72 | + inputMap = url.href |
| 73 | + .slice( 0, comma ) |
| 74 | + .endsWith( ';base64' ) |
| 75 | + ? Buffer.from( data, 'base64' ).toString( |
| 76 | + 'utf8' |
| 77 | + ) |
| 78 | + : data; |
| 79 | + } else if ( url.protocol === 'file:' ) { |
| 80 | + mapPath = fileURLToPath( url ); |
| 81 | + watchFiles.push( mapPath ); |
| 82 | + inputMap = await readFile( url, 'utf8' ); |
| 83 | + } |
| 84 | + // Like esbuild, do not fetch remote source-map URLs. |
| 85 | + if ( inputMap !== undefined ) { |
| 86 | + const map = remapping( |
| 87 | + [ |
| 88 | + result.map.toString(), |
| 89 | + new AnyMap( |
| 90 | + inputMap, |
| 91 | + pathToFileURL( mapPath ).href |
| 92 | + ), |
| 93 | + ], |
| 94 | + () => null |
| 95 | + ); |
| 96 | + sourceMap = `data:application/json;base64,${ Buffer.from( map.toString() ).toString( 'base64' ) }`; |
| 97 | + } |
| 98 | + } catch ( error ) { |
| 99 | + // esbuild ignores missing maps and warns for malformed maps. |
| 100 | + if ( ! ( |
| 101 | + error && |
| 102 | + typeof error === 'object' && |
| 103 | + 'code' in error && |
| 104 | + error.code === 'ENOENT' |
| 105 | + ) ) { |
| 106 | + warnings.push( { |
| 107 | + text: `Could not load the input source map: ${ error instanceof Error ? error.message : String( error ) }`, |
| 108 | + detail: error, |
| 109 | + } ); |
| 110 | + } |
| 111 | + } |
| 112 | + } |
43 | 113 |
|
44 | 114 | return { |
45 | | - contents: addFallbackToVar( source, { |
46 | | - escapeQuotes: true, |
47 | | - } ), |
| 115 | + contents: `${ result.code }\n//# sourceMappingURL=${ sourceMap }`, |
48 | 116 | loader: LOADER_MAP[ ext ] || 'jsx', |
| 117 | + watchFiles, |
| 118 | + warnings, |
49 | 119 | }; |
50 | 120 | } |
51 | 121 | ); |
|
0 commit comments