Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/soft-css-sourcemaps.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@posthog/webpack-plugin": patch
---

Include emitted CSS assets with adjacent source maps when processing webpack sourcemaps.
12 changes: 12 additions & 0 deletions packages/webpack-plugin/jest.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { createDefaultPreset } from 'ts-jest'

const tsJestTransformCfg = createDefaultPreset().transform

/** @type {import('jest').Config} **/
export default {
testEnvironment: 'node',
testPathIgnorePatterns: ['/node_modules/', '/dist/'],
transform: {
...tsJestTransformCfg,
},
}
2 changes: 2 additions & 0 deletions packages/webpack-plugin/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@
"devDependencies": {
"@posthog-tooling/tsconfig-base": "workspace:*",
"@rslib/core": "catalog:",
"@types/jest": "catalog:",
"jest": "catalog:",
"ts-jest": "catalog:",
"webpack": "^5"
},
"scripts": {
Expand Down
16 changes: 13 additions & 3 deletions packages/webpack-plugin/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,24 @@ export class PosthogWebpackPlugin {
return
}

const filePaths: string[] = []
const filePaths = new Set<string>()
chunkArray.forEach((chunk) =>
chunk.files.forEach((file) => {
const chunkPath = path.resolve(outputDirectory, file)
filePaths.push(chunkPath)
filePaths.add(chunkPath)
})
)

await runSourcemapCli(config, { filePaths })
compilation.getAssets().forEach((asset) => {
if (asset.name.endsWith('.css')) {
const mapAssetName = `${asset.name}.map`
if (compilation.getAsset(mapAssetName)) {
filePaths.add(path.resolve(outputDirectory, asset.name))
filePaths.add(path.resolve(outputDirectory, mapAssetName))
}
}
})
Comment thread
marandaneto marked this conversation as resolved.

await runSourcemapCli(config, { filePaths: Array.from(filePaths) })
}
}
70 changes: 70 additions & 0 deletions packages/webpack-plugin/test/index.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import path from 'path'
import type webpack from 'webpack'
import { runSourcemapCli } from '@posthog/plugin-utils'
import { PosthogWebpackPlugin } from '../src/index'
import type { ResolvedPluginConfig } from '../src/config'

jest.mock('@posthog/plugin-utils', () => ({
runSourcemapCli: jest.fn().mockResolvedValue(undefined),
}))

const runSourcemapCliMock = runSourcemapCli as jest.MockedFunction<typeof runSourcemapCli>

const config: ResolvedPluginConfig = {
personalApiKey: 'phx_test',
projectId: '1',
host: 'https://us.i.posthog.com',
logLevel: 'info',
cliBinaryPath: 'posthog-cli',
sourcemaps: {
enabled: true,
deleteAfterUpload: true,
},
}

type TestAsset = { name: string }
type TestChunk = { files: Set<string> }

function createCompilation(outputDirectory: string, chunks: TestChunk[], assets: TestAsset[]): webpack.Compilation {
return {
outputOptions: { path: outputDirectory },
chunks: new Set(chunks),
getAssets: () => assets,
getAsset: (name: string) => assets.find((asset) => asset.name === name),
} as unknown as webpack.Compilation
}

describe('PosthogWebpackPlugin', () => {
beforeEach(() => {
runSourcemapCliMock.mockClear()
})

it('passes emitted CSS assets with adjacent source maps to the sourcemap CLI', async () => {
const outputDirectory = path.resolve('/tmp/posthog-webpack-plugin')
const plugin = new PosthogWebpackPlugin(config, true)
const compilation = createCompilation(
outputDirectory,
[
{
files: new Set(['static/chunks/app.js', 'static/chunks/app.js.map']),
},
],
[
{ name: 'static/css/app.css' },
{ name: 'static/css/app.css.map' },
{ name: 'static/css/no-map.css' },
]
)

await plugin.processSourceMaps(compilation, config)

expect(runSourcemapCliMock).toHaveBeenCalledWith(config, {
filePaths: [
path.resolve(outputDirectory, 'static/chunks/app.js'),
path.resolve(outputDirectory, 'static/chunks/app.js.map'),
path.resolve(outputDirectory, 'static/css/app.css'),
path.resolve(outputDirectory, 'static/css/app.css.map'),
],
})
})
})
3 changes: 2 additions & 1 deletion packages/webpack-plugin/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@
"declarationMap": true,
"moduleResolution": "bundler",
"module": "ES6"
}
},
"include": ["src/**/*.ts"]
}
52 changes: 50 additions & 2 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading