| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -62,6 +62,7 @@ | |||
| 62 | 62 | "prepublishOnly": "pnpm run build" | |
| 63 | 63 | }, | |
| 64 | 64 | "dependencies": { | |
| 65 | + "js-tokens": "^10.0.0", | ||
| 65 | 66 | "rolldown-string": "^0.2.1", | |
| 66 | 67 | "unplugin": "^3.0.0" | |
| 67 | 68 | }, | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -45,6 +45,11 @@ export interface BaseOptions { | |||
| 45 | 45 | */ | |
| 46 | 46 | values?: ReplaceMap | ReplaceItem[] | |
| 47 | 47 | enforce?: 'pre' | 'post' | undefined | |
| 48 | + /** | ||
| 49 | + * By default, comments are ignored by the plugin. | ||
| 50 | + * Setting this option to `true` will include comments in the replacement process. | ||
| 51 | + */ | ||
| 52 | + includeComments?: boolean | ||
| 48 | 53 | } | |
| 49 | 54 | ||
| 50 | 55 | export interface Options extends BaseOptions { | |
@@ -80,6 +85,7 @@ export function resolveOptions(options: Options): OptionsResolved { | |||
| 80 | 85 | objectGuards: options.objectGuards ?? false, | |
| 81 | 86 | values: getReplacements(), | |
| 82 | 87 | enforce: 'enforce' in options ? options.enforce : 'pre', | |
| 88 | + includeComments: options.includeComments ?? false, | ||
| 83 | 89 | } | |
| 84 | 90 | ||
| 85 | 91 | function getReplacements() { | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -3,6 +3,7 @@ | |||
| 3 | 3 | * @module | |
| 4 | 4 | */ | |
| 5 | 5 | ||
| 6 | + import jsTokens from 'js-tokens' | ||
| 6 | 7 | import { withMagicString } from 'rolldown-string' | |
| 7 | 8 | import { createUnplugin, type UnpluginInstance } from 'unplugin' | |
| 8 | 9 | import { resolveOptions, type Options, type ReplaceItem } from './core/options' | |
@@ -24,6 +25,7 @@ const plugin: UnpluginInstance<Options | undefined, false> = createUnplugin< | |||
| 24 | 25 | delimiters, | |
| 25 | 26 | objectGuards, | |
| 26 | 27 | preventAssignment, | |
| 28 | + includeComments, | ||
| 27 | 29 | } = options | |
| 28 | 30 | const stringValues = options.values.filter( | |
| 29 | 31 | (value): value is ReplaceItem<string> => typeof value.find === 'string', | |
@@ -58,9 +60,25 @@ const plugin: UnpluginInstance<Options | undefined, false> = createUnplugin< | |||
| 58 | 60 | handler: withMagicString(function (s, id) { | |
| 59 | 61 | if (!values.length) return | |
| 60 | 62 | ||
| 61 | - const code = s.toString() | ||
| 62 | - let match: RegExpExecArray | null | ||
| 63 | + let code = '' | ||
| 64 | + if (includeComments) { | ||
| 65 | + code = s.toString() | ||
| 66 | + } else { | ||
| 67 | + const tokens = jsTokens(s.toString(), { jsx: /\.[jt]sx?$/.test(id) }) | ||
| 68 | + for (const token of tokens) { | ||
| 69 | + if ( | ||
| 70 | + token.type === 'MultiLineComment' || | ||
| 71 | + token.type === 'SingleLineComment' || | ||
| 72 | + token.type === 'HashbangComment' | ||
| 73 | + ) { | ||
| 74 | + code += ' '.repeat(token.value.length) | ||
| 75 | + } else { | ||
| 76 | + code += token.value | ||
| 77 | + } | ||
| 78 | + } | ||
| 79 | + } | ||
| 63 | 80 | ||
| 81 | + let match: RegExpExecArray | null | ||
| 64 | 82 | for (const { find, replacement } of values) { | |
| 65 | 83 | while ((match = find.exec(code))) { | |
| 66 | 84 | const start = match.index | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -5,8 +5,11 @@ exports[`esbuild 1`] = ` | |||
| 5 | 5 | var platform = "darwin"; | |
| 6 | 6 | console.log('hello "darwin"', platform); | |
| 7 | 7 | console.log(DEV); | |
| 8 | + function x() { | ||
| 9 | + } | ||
| 8 | 10 | export { | |
| 9 | - platform | ||
| 11 | + platform, | ||
| 12 | + x | ||
| 10 | 13 | }; | |
| 11 | 14 | " | |
| 12 | 15 | `; | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -7,7 +7,9 @@ exports[`rolldown 1`] = ` | |||
| 7 | 7 | const platform = "darwin"; | |
| 8 | 8 | console.log("hello \\"darwin\\"", platform); | |
| 9 | 9 | console.log(DEV); | |
| 10 | + /** COMMENT_FLAG */ | ||
| 11 | + function x() {} | ||
| 10 | 12 | ||
| 11 | 13 | //#endregion | |
| 12 | - export { platform };" | ||
| 14 | + export { platform, x };" | ||
| 13 | 15 | `; | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -9,7 +9,10 @@ const platform = null; | |||
| 9 | 9 | console.info('hello null', platform); | |
| 10 | 10 | console.info(dEV); | |
| 11 | 11 | ||
| 12 | - export { platform }; | ||
| 12 | + /** COMMENT_FLAG */ | ||
| 13 | + function x() {} | ||
| 14 | + | ||
| 15 | + export { platform, x }; | ||
| 13 | 16 | " | |
| 14 | 17 | `; | |
| 15 | 18 | ||
@@ -22,6 +25,9 @@ const platform = "darwin"; | |||
| 22 | 25 | console.log('hello "darwin"', platform); | |
| 23 | 26 | console.log(DEV); | |
| 24 | 27 | ||
| 25 | - export { platform }; | ||
| 28 | + /** COMMENT_FLAG */ | ||
| 29 | + function x() {} | ||
| 30 | + | ||
| 31 | + export { platform, x }; | ||
| 26 | 32 | " | |
| 27 | 33 | `; | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -4,7 +4,7 @@ exports[`webpack 1`] = ` | |||
| 4 | 4 | "/******/ (() => { // webpackBootstrap | |
| 5 | 5 | /******/ "use strict"; | |
| 6 | 6 | ||
| 7 | - // UNUSED EXPORTS: platform | ||
| 7 | + // UNUSED EXPORTS: platform, x | ||
| 8 | 8 | ||
| 9 | 9 | ;// external "node:process" | |
| 10 | 10 | const external_node_process_namespaceObject = require("node:process"); | |
@@ -16,6 +16,9 @@ const platform = "darwin" | |||
| 16 | 16 | console.log('hello "darwin"', platform) | |
| 17 | 17 | console.log(DEV) | |
| 18 | 18 | ||
| 19 | + /** COMMENT_FLAG */ | ||
| 20 | + function x() {} | ||
| 21 | + | ||
| 19 | 22 | /******/ })() | |
| 20 | 23 | ;" | |
| 21 | 24 | `; | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -4,3 +4,6 @@ export const platform = process.platform | |||
| 4 | 4 | ||
| 5 | 5 | console.log('hello process.platform', platform) | |
| 6 | 6 | console.log(DEV) | |
| 7 | + | ||
| 8 | + /** COMMENT_FLAG */ | ||
| 9 | + export function x() {} | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -10,12 +10,14 @@ test('rolldown', async () => { | |||
| 10 | 10 | plugins: [ | |
| 11 | 11 | UnpluginReplace({ | |
| 12 | 12 | 'process.platform': '"darwin"', | |
| 13 | + COMMENT_FLAG: 'null', | ||
| 13 | 14 | }), | |
| 14 | 15 | ], | |
| 15 | 16 | }) | |
| 16 | 17 | const { output } = await build.generate({ format: 'es' }) | |
| 17 | 18 | const code = output[0].code | |
| 18 | 19 | expect(code).toMatchSnapshot() | |
| 20 | + expect(code).contains('COMMENT_FLAG') | ||
| 19 | 21 | expect(code).not.contains('process.platform') | |
| 20 | 22 | expect(code).contains('"darwin"') | |
| 21 | 23 | }) | |
| Back | FazBrowse Home | New Git URL |
0 commit comments