Skip to content

Commit e58dfd7

Browse files
authored
Add sourcemap support to SFCFluentPlugin (#103)
* Remove not used files * Enable and fix TS errors * Include map in ExternalFluentPlugin * Add support for sourcemaps to SFCFluentPlugin * Add automatic publish script * Don't tranform file is not needed
1 parent cdcb692 commit e58dfd7

File tree

8 files changed

+48
-21
lines changed

8 files changed

+48
-21
lines changed

.github/workflows/test.yml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,23 @@ jobs:
4949
- name: Lint
5050
run: pnpm lint
5151

52+
publish:
53+
runs-on: ubuntu-latest
54+
name: Publish test build to pkg.pr.new
55+
steps:
56+
- name: Checkout
57+
uses: actions/checkout@v4.2.2
58+
- name: Setup Node.js
59+
uses: actions/setup-node@v4.1.0
60+
- uses: pnpm/action-setup@v4.0.0
61+
with:
62+
run_install: true
63+
64+
- name: Build
65+
run: pnpm build
66+
- name: Publish
67+
run: pnpx pkg-pr-new publish
68+
5269
test-examples:
5370
runs-on: ubuntu-latest
5471
name: Test build of example projects

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,13 +83,13 @@
8383
"module": "dist/index.js",
8484
"types": "dist/index.d.ts",
8585
"files": [
86-
"*.d.ts",
8786
"dist"
8887
],
8988
"scripts": {
9089
"test": "vitest",
9190
"test:coverage": "vitest --coverage",
9291
"build": "tsup",
92+
"typecheck": "tsc --noEmit",
9393
"lint": "eslint .",
9494
"prepare": "husky",
9595
"release": "dotenv release-it"

rollup.d.ts

Lines changed: 0 additions & 1 deletion
This file was deleted.

src/plugins/external-plugin.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,19 +46,20 @@ export const unplugin = createUnplugin((options: ExternalPluginOptions) => {
4646
...options,
4747
}
4848

49+
let getFtlPath
4950
if ('getFtlPath' in options) {
50-
resolvedOptions.getFtlPath = options.getFtlPath
51+
getFtlPath = options.getFtlPath
5152
}
5253
else {
53-
resolvedOptions.getFtlPath = (locale: string, vuePath: string) => {
54+
getFtlPath = (locale: string, vuePath: string) => {
5455
return join(options.ftlDir, locale, `${relative(options.baseDir, vuePath)}.ftl`)
5556
}
5657
}
5758

5859
const getTranslationsForFile = async (id: string) => {
5960
const dependencies: Dependency[] = []
6061
for (const locale of options.locales) {
61-
const ftlPath = normalizePath(resolvedOptions.getFtlPath(locale, id))
62+
const ftlPath = normalizePath(getFtlPath(locale, id))
6263
const ftlExists = await fileExists(ftlPath)
6364
let relativeFtlPath = normalizePath(relative(dirname(id), ftlPath))
6465
if (!relativeFtlPath.startsWith('.'))
@@ -125,7 +126,7 @@ export const unplugin = createUnplugin((options: ExternalPluginOptions) => {
125126

126127
return {
127128
code: magic.toString(),
128-
map: { mappings: '' },
129+
map: magic.generateMap(),
129130
}
130131
}
131132

src/plugins/sfc-plugin.ts

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import type { VitePlugin } from 'unplugin'
22
import type { SFCPluginOptions } from '../types'
33

4+
import MagicString from 'magic-string'
45
import { createUnplugin } from 'unplugin'
56
import { isCustomBlock, parseVueRequest } from '../loader-query'
67
import { getSyntaxErrors } from './ftl/parse'
@@ -23,41 +24,50 @@ export const unplugin = createUnplugin((options: SFCPluginOptions, meta) => {
2324
const { query } = parseVueRequest(id)
2425

2526
if (isCustomBlock(query, resolvedOptions)) {
26-
const data = source
27-
// vue-loader pads SFC file sections with newlines - trim those
28-
.replace(/^(\n|\r\n)+|(\n|\r\n)+$/g, '')
29-
// normalise newlines
30-
.replace(/\r\n/g, '\n')
27+
const originalSource = source
28+
29+
const magic = new MagicString(source, { filename: id })
30+
31+
source = source.replace(/\r\n/g, '\n').trim()
3132

3233
if (query.locale == null)
3334
this.error('Custom block does not have locale attribute')
3435

3536
// I have no idea why webpack processes this file multiple times
3637
if (source.includes('FluentResource') || source.includes('unplugin-fluent-vue-sfc'))
37-
return source
38+
return undefined
3839

3940
if (resolvedOptions.checkSyntax) {
40-
const errorsText = getSyntaxErrors(data)
41+
const errorsText = getSyntaxErrors(source)
4142
if (errorsText)
4243
this.error(errorsText)
4344
}
4445

45-
return `
46+
if (originalSource.length > 0)
47+
magic.update(0, originalSource.length, JSON.stringify(source))
48+
else
49+
magic.append('""')
50+
51+
magic.prepend(`
4652
import { FluentResource } from '@fluent/bundle'
4753
4854
export default function (Component) {
4955
const target = Component.options || Component
5056
target.fluent = target.fluent || {}
51-
target.fluent['${query.locale}'] = new FluentResource(${JSON.stringify(data)})
52-
}
53-
`
57+
target.fluent['${query.locale}'] = new FluentResource(`)
58+
magic.append(')\n}\n')
59+
60+
return {
61+
code: magic.toString(),
62+
map: magic.generateMap(),
63+
}
5464
}
5565

5666
return undefined
5767
},
5868
}
5969
})
6070

61-
export const vitePlugin: (options?: SFCPluginOptions) => VitePlugin = unplugin.vite
71+
export const vitePlugin = unplugin.vite as (options?: SFCPluginOptions) => VitePlugin
6272
export const rollupPlugin = unplugin.rollup
6373
export const webpackPlugin = unplugin.webpack

tsconfig.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@
44
"baseUrl": ".",
55
"moduleResolution": "node",
66
"types": ["node"],
7+
"strict": true,
78
"sourceMap": true,
8-
"esModuleInterop": true
9+
"esModuleInterop": true,
10+
"skipLibCheck": true
911
},
1012
"exclude": [
1113
"node_modules"

vite.d.ts

Lines changed: 0 additions & 1 deletion
This file was deleted.

webpack.d.ts

Lines changed: 0 additions & 1 deletion
This file was deleted.

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy