Skip to content

Commit 91e72ba

Browse files
authored
Fix npm install error because of node-gyp (#1107)
1 parent a37d5fc commit 91e72ba

File tree

13 files changed

+640
-294
lines changed

13 files changed

+640
-294
lines changed

.changeset/lovely-tables-shake.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@segment/analytics-next': minor
3+
---
4+
5+
Vendor tsub.js 2.0.0

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
node_modules
1717
dist
18+
dist.*
1819
package-lock.json
1920
.DS_Store
2021
*.log

packages/browser/package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@
3939
"cjs": "yarn tsc -p tsconfig.build.json --outDir ./dist/cjs --module commonjs",
4040
"clean": "rm -rf dist",
4141
"lint": "yarn concurrently 'yarn:eslint .' 'yarn:tsc --noEmit'",
42-
"test": "yarn jest"
42+
"test": "yarn jest",
43+
"vendor": "node scripts/vendor/run.js"
4344
},
4445
"size-limit": [
4546
{
@@ -53,7 +54,6 @@
5354
"@segment/analytics-generic-utils": "1.2.0",
5455
"@segment/analytics.js-video-plugins": "^0.2.1",
5556
"@segment/facade": "^3.4.9",
56-
"@segment/tsub": "^2.0.0",
5757
"dset": "^3.1.2",
5858
"js-cookie": "3.0.1",
5959
"node-fetch": "^2.6.7",
@@ -65,6 +65,7 @@
6565
"@segment/analytics-browser-actions-braze": "^1.3.0",
6666
"@segment/analytics.js-integration": "^3.3.3",
6767
"@segment/analytics.js-integration-amplitude": "^3.3.3",
68+
"@segment/tsub": "^2.0.0",
6869
"@size-limit/preset-big-lib": "^7.0.8",
6970
"@types/flat": "^5.0.1",
7071
"@types/fs-extra": "^9.0.2",

packages/browser/qa/__tests__/backwards-compatibility.test.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,9 +92,7 @@ describe('Backwards compatibility', () => {
9292
expect(resultString).toContain(
9393
'http://localhost:4000/dist/umd/standalone.js'
9494
)
95-
expect(resultString).toContain(
96-
'http://localhost:4000/dist/umd/vendors-node_modules_segment_tsub_dist_index_js.bundle'
97-
)
95+
9896
expect(resultString).toContain(
9997
'http://localhost:4000/dist/umd/ajs-destination.bundle'
10098
)
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Vendor library
2+
3+
This script vendors the following library:
4+
https://github.com/segmentio/tsub-js
5+
6+
Usage for updating tsub:
7+
- update tsub to new version (tsub should be a _dev dependency_)
8+
- run `yarn vendor` from package root
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
const fs = require('node:fs')
2+
const path = require('node:path')
3+
4+
function prependToFile(filePath, content) {
5+
const fileContent = fs.readFileSync(filePath, 'utf8')
6+
fs.writeFileSync(filePath, content + fileContent)
7+
}
8+
9+
function createTSFromJSLib(inputFilePath, outputDir, { libraryName }) {
10+
const fileName = path.basename(inputFilePath, '.js') + '.ts'
11+
const outputFilePath = path.join(outputDir, fileName)
12+
const libVersion = require(`${libraryName}/package.json`).version
13+
const tsContent = [
14+
'// @ts-nocheck',
15+
'// prettier-ignore',
16+
'/* eslint-disable */',
17+
`// ${libraryName} ${libVersion} - GENERATED DO NOT MODIFY`,
18+
'\n',
19+
].join('\n')
20+
fs.renameSync(inputFilePath, outputFilePath)
21+
prependToFile(outputFilePath, tsContent)
22+
console.log(
23+
`\n Built ${libraryName} ${libVersion} -> output ${outputFilePath}`
24+
)
25+
return { outputFilePath }
26+
}
27+
28+
module.exports = {
29+
prependToFile,
30+
createTSFromJSLib,
31+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/**
2+
* This file:
3+
* - uses webpack to build tsub.js
4+
* - converts tsub.js to tsub.ts, appends comments, and moves it into the source directory
5+
*/
6+
const { execSync } = require('node:child_process')
7+
const path = require('node:path')
8+
const { createTSFromJSLib } = require('./helpers')
9+
10+
// build tsub.js with webpack
11+
const configPath = path.join(__dirname, 'webpack.config.vendor.js')
12+
execSync(`yarn webpack --config ${configPath}`, { stdio: 'inherit' })
13+
14+
// create tsub.ts artifact and move to source directory
15+
const tsubInputBundlePath = path.join(__dirname, 'dist.vendor', 'tsub.js')
16+
const tsubOutputVendorDir = 'src/vendor/tsub'
17+
createTSFromJSLib(tsubInputBundlePath, tsubOutputVendorDir, {
18+
libraryName: '@segment/tsub',
19+
})
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
const path = require('node:path')
2+
3+
/** @type { import('webpack').Configuration } */
4+
module.exports = {
5+
entry: require.resolve('@segment/tsub'),
6+
output: {
7+
path: path.resolve(__dirname, 'dist.vendor'), // Output directory
8+
filename: 'tsub.js',
9+
library: {
10+
type: 'umd',
11+
},
12+
},
13+
resolve: {
14+
extensions: ['.js'], // Resolve these extensions
15+
},
16+
mode: 'production', // Use production mode for minification, etc.
17+
}

packages/browser/src/plugins/routing-middleware/index.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
import * as tsub from '@segment/tsub'
2-
import { Matcher, Rule } from '@segment/tsub/dist/store'
1+
// @ts-ignore
2+
import * as tsub from '../../vendor/tsub/tsub'
3+
import type { Matcher, Rule, Store } from '../../vendor/tsub/types'
34
import { DestinationMiddlewareFunction } from '../middleware'
45

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

22-
rulesToApply.forEach((rule) => {
23+
rulesToApply.forEach((rule: Rule) => {
2324
const { matchers, transformers } = rule
2425

2526
for (let i = 0; i < matchers.length; i++) {
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2018 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
/**
20+
* @license Apache-2.0
21+
*
22+
* Copyright (c) 2021 The Stdlib Authors.
23+
*
24+
* Licensed under the Apache License, Version 2.0 (the "License");
25+
* you may not use this file except in compliance with the License.
26+
* You may obtain a copy of the License at
27+
*
28+
* http://www.apache.org/licenses/LICENSE-2.0
29+
*
30+
* Unless required by applicable law or agreed to in writing, software
31+
* distributed under the License is distributed on an "AS IS" BASIS,
32+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
33+
* See the License for the specific language governing permissions and
34+
* limitations under the License.
35+
*/
36+
37+
/**
38+
* @license Apache-2.0
39+
*
40+
* Copyright (c) 2022 The Stdlib Authors.
41+
*
42+
* Licensed under the Apache License, Version 2.0 (the "License");
43+
* you may not use this file except in compliance with the License.
44+
* You may obtain a copy of the License at
45+
*
46+
* http://www.apache.org/licenses/LICENSE-2.0
47+
*
48+
* Unless required by applicable law or agreed to in writing, software
49+
* distributed under the License is distributed on an "AS IS" BASIS,
50+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
51+
* See the License for the specific language governing permissions and
52+
* limitations under the License.
53+
*/

0 commit comments

Comments
 (0)