Skip to content

Commit

Permalink
Vendor tsub
Browse files Browse the repository at this point in the history
  • Loading branch information
silesky committed Jul 12, 2024
1 parent 232708f commit 2b0e1b5
Show file tree
Hide file tree
Showing 12 changed files with 634 additions and 294 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

node_modules
dist
dist.*
package-lock.json
.DS_Store
*.log
Expand Down
5 changes: 3 additions & 2 deletions packages/browser/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@
"cjs": "yarn tsc -p tsconfig.build.json --outDir ./dist/cjs --module commonjs",
"clean": "rm -rf dist",
"lint": "yarn concurrently 'yarn:eslint .' 'yarn:tsc --noEmit'",
"test": "yarn jest"
"test": "yarn jest",
"vendor": "node scripts/vendor/run.js"
},
"size-limit": [
{
Expand All @@ -53,7 +54,6 @@
"@segment/analytics-generic-utils": "1.2.0",
"@segment/analytics.js-video-plugins": "^0.2.1",
"@segment/facade": "^3.4.9",
"@segment/tsub": "^2.0.0",
"dset": "^3.1.2",
"js-cookie": "3.0.1",
"node-fetch": "^2.6.7",
Expand All @@ -65,6 +65,7 @@
"@segment/analytics-browser-actions-braze": "^1.3.0",
"@segment/analytics.js-integration": "^3.3.3",
"@segment/analytics.js-integration-amplitude": "^3.3.3",
"@segment/tsub": "^2.0.0",
"@size-limit/preset-big-lib": "^7.0.8",
"@types/flat": "^5.0.1",
"@types/fs-extra": "^9.0.2",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,7 @@ describe('Backwards compatibility', () => {
expect(resultString).toContain(
'http://localhost:4000/dist/umd/standalone.js'
)
expect(resultString).toContain(
'http://localhost:4000/dist/umd/vendors-node_modules_segment_tsub_dist_index_js.bundle'
)

expect(resultString).toContain(
'http://localhost:4000/dist/umd/ajs-destination.bundle'
)
Expand Down
8 changes: 8 additions & 0 deletions packages/browser/scripts/vendor/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Vendor library

This script vendors the following library:
https://github.com/segmentio/tsub-js

Usage for updating tsub:
- update tsub to new version (tsub should be a _dev dependency_)
- run `yarn vendor` from package root
31 changes: 31 additions & 0 deletions packages/browser/scripts/vendor/helpers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
const fs = require('node:fs')
const path = require('node:path')

function prependToFile(filePath, content) {
const fileContent = fs.readFileSync(filePath, 'utf8')
fs.writeFileSync(filePath, content + fileContent)
}

function createTSFromJSLib(inputFilePath, outputDir, { libraryName }) {
const fileName = path.basename(inputFilePath, '.js') + '.ts'
const outputFilePath = path.join(outputDir, fileName)
const libVersion = require(`${libraryName}/package.json`).version
const tsContent = [
'// @ts-nocheck',
'// prettier-ignore',
'/* eslint-disable */',
`// ${libraryName} ${libVersion} - GENERATED DO NOT MODIFY`,
'\n',
].join('\n')
fs.renameSync(inputFilePath, outputFilePath)
prependToFile(outputFilePath, tsContent)
console.log(
`\n Built ${libraryName} ${libVersion} -> output ${outputFilePath}`
)
return { outputFilePath }
}

module.exports = {
prependToFile,
createTSFromJSLib,
}
18 changes: 18 additions & 0 deletions packages/browser/scripts/vendor/run.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/**
* This file:
* - uses webpack to build tsub.js
* - converts tsub.js to tsub.ts, appends comments, and moves it into the source directory
*/
const { execSync } = require('node:child_process')
const path = require('node:path')

const { createTSFromJSLib } = require('./helpers')
const configPath = 'scripts/vendor/webpack.config.vendor.js'

execSync(`yarn webpack --config ${configPath}`, { stdio: 'inherit' })

const tsubInputBundlePath = path.join(__dirname, 'dist.vendor', 'tsub.js')
const tsubOutputVendorDir = 'src/vendor/tsub'
createTSFromJSLib(tsubInputBundlePath, tsubOutputVendorDir, {
libraryName: '@segment/tsub',
})
17 changes: 17 additions & 0 deletions packages/browser/scripts/vendor/webpack.config.vendor.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
const path = require('node:path')

/** @type { import('webpack').Configuration } */
module.exports = {
entry: require.resolve('@segment/tsub'),
output: {
path: path.resolve(__dirname, 'dist.vendor'), // Output directory
filename: 'tsub.js',
library: {
type: 'umd',
},
},
resolve: {
extensions: ['.js'], // Resolve these extensions
},
mode: 'production', // Use production mode for minification, etc.
}
9 changes: 5 additions & 4 deletions packages/browser/src/plugins/routing-middleware/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as tsub from '@segment/tsub'
import { Matcher, Rule } from '@segment/tsub/dist/store'
// @ts-ignore
import * as tsub from '../../vendor/tsub/tsub'
import type { Matcher, Rule, Store } from '../../vendor/tsub/types'
import { DestinationMiddlewareFunction } from '../middleware'

// TODO: update tsub definition
Expand All @@ -16,10 +17,10 @@ export type RoutingRule = Rule & {
export const tsubMiddleware =
(rules: RoutingRule[]): DestinationMiddlewareFunction =>
({ payload, integration, next }): void => {
const store = new tsub.Store(rules)
const store = new tsub.Store(rules) as Store
const rulesToApply = store.getRulesByDestinationName(integration)

rulesToApply.forEach((rule) => {
rulesToApply.forEach((rule: Rule) => {
const { matchers, transformers } = rule

for (let i = 0; i < matchers.length; i++) {
Expand Down
53 changes: 53 additions & 0 deletions packages/browser/src/vendor/tsub/tsub.js.LICENSE.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/**
* @license Apache-2.0
*
* Copyright (c) 2018 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

/**
* @license Apache-2.0
*
* Copyright (c) 2021 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

/**
* @license Apache-2.0
*
* Copyright (c) 2022 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
7 changes: 7 additions & 0 deletions packages/browser/src/vendor/tsub/tsub.ts

Large diffs are not rendered by default.

19 changes: 19 additions & 0 deletions packages/browser/src/vendor/tsub/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
export interface Rule {
scope: string
target_type: string
matchers: Matcher[]
transformers: Transformer[][]
destinationName?: string
}
export interface Matcher {
type: string
ir: string
}
export interface Transformer {
type: string
config?: any
}
export interface Store {
new (rules?: Rule[]): this
getRulesByDestinationName(destinationName: string): Rule[]
}
Loading

0 comments on commit 2b0e1b5

Please sign in to comment.