summaryrefslogtreecommitdiffhomepage
path: root/packages/server/build_npm.ts
blob: 57c80eb68c62cbb223c88756973f997cf0199439 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
import { build, BuildOptions, emptyDir } from 'https://deno.land/x/dnt@0.38.0/mod.ts';

const outDir = {
  publish: './npm',
  test: './npm-test',
} as const;
const lernaPackageJSON: { version: string } = JSON.parse(
  Deno.readTextFileSync('./package.json'),
);
const typesPackageJSON: { version: string } = JSON.parse(
  Deno.readTextFileSync('../typescript-types/npm/package.json'),
);

// Clear both build directories
await Promise.all([
  await emptyDir(outDir.publish),
  await emptyDir(outDir.test),
]);

/**
 * Maintain a separate build just for testing, as we need to shim crypto only
 * when test_runner.js runs to test the ESM and CJS output. The test environment
 * currently lacks `globalThis.crypto` and so shimming it is the only way to
 * get the tests to successfully execute. But we don't want the shim in the
 * build we post up to NPM so that the runtime's native Crypto can be used.
 *
 * See https://github.com/denoland/dnt/issues/181
 */
console.log('Building for testing...');
await build({
  entryPoints: getEntryPoints(),
  outDir: outDir.test,
  shims: {
    deno: {
      test: 'dev',
    },
    crypto: true,
  },
  test: true,
  // TODO: Re-enable if https://github.com/denoland/dnt/issues/331 can get resolved
  typeCheck: false,
  package: {
    name: 'for-testing-only',
    version: '0.0.0',
  },
  // Map from Deno package to NPM package for Node build
  mappings: getMappings(),
  // TypeScript tsconfig.json config
  compilerOptions: getCompilerOptions(),
});

console.log('Building for publishing...');
await build({
  entryPoints: getEntryPoints(),
  outDir: outDir.publish,
  shims: {},
  test: false,
  // TODO: Re-enable if https://github.com/denoland/dnt/issues/331 can get resolved
  typeCheck: false,
  // package.json values
  package: {
    name: '@simplewebauthn/server',
    version: lernaPackageJSON.version,
    description: 'SimpleWebAuthn for Servers',
    license: 'MIT',
    author: 'Matthew Miller <matthew@millerti.me>',
    repository: {
      type: 'git',
      url: 'https://github.com/MasterKale/SimpleWebAuthn.git',
      directory: 'packages/server',
    },
    homepage: 'https://github.com/MasterKale/SimpleWebAuthn/tree/master/packages/server#readme',
    publishConfig: {
      access: 'public',
    },
    bugs: {
      url: 'https://github.com/MasterKale/SimpleWebAuthn/issues',
    },
    keywords: [
      'typescript',
      'webauthn',
      'passkeys',
      'fido',
      'node',
    ],
    typesVersions: {
      '*': {
        '.': [
          'esm/index.d.ts',
        ],
        'helpers': [
          'esm/helpers/index.d.ts',
        ],
      },
    },
  },
  // Map from Deno package to NPM package for Node build
  mappings: getMappings(),
  // TypeScript tsconfig.json config
  compilerOptions: getCompilerOptions(),
});

// Deno.copyFileSync('LICENSE', 'npm/LICENSE');
Deno.copyFileSync('README.md', `${outDir.publish}/README.md`);

/**
 * Settings we can reuse across the two build configs
 */
function getEntryPoints(): BuildOptions['entryPoints'] {
  return [
    { name: '.', path: './src/index.ts' },
    { name: './helpers', path: './src/helpers/index.ts' },
  ];
}

function getMappings(): BuildOptions['mappings'] {
  return {
    'https://deno.land/x/b64@1.1.27/src/base64.js': {
      name: '@hexagon/base64',
      version: '^1.1.27',
    },
    'https://deno.land/x/cbor@v1.5.2/index.js': {
      name: 'cbor-x',
      version: '^1.5.2',
    },
    'https://esm.sh/v131/debug@4.3.4/denonext/debug.mjs': {
      name: 'debug',
      version: '^4.3.4',
    },
    'https://esm.sh/v131/@types/debug@4.1.8/index.d.ts': {
      name: '@types/debug',
      version: '^4.1.8',
    },
    'https://esm.sh/v131/cross-fetch@4.0.0/es2021/cross-fetch.mjs': {
      name: 'cross-fetch',
      version: '^4.0.0',
    },
    'https://esm.sh/v131/@peculiar/asn1-schema@2.3.6/denonext/asn1-schema.mjs': {
      name: '@peculiar/asn1-schema',
      version: '^2.3.6',
    },
    'https://esm.sh/v131/@peculiar/asn1-x509@2.3.6/es2021/asn1-x509.mjs': {
      name: '@peculiar/asn1-x509',
      version: '^2.3.6',
    },
    'https://esm.sh/v131/@peculiar/asn1-ecc@2.3.6/es2021/asn1-ecc.mjs': {
      name: '@peculiar/asn1-ecc',
      version: '^2.3.6',
    },
    'https://esm.sh/v131/@peculiar/asn1-rsa@2.3.6/es2021/asn1-rsa.mjs': {
      name: '@peculiar/asn1-rsa',
      version: '^2.3.6',
    },
    'https://esm.sh/v131/@peculiar/asn1-android@2.3.6/es2021/asn1-android.mjs': {
      name: '@peculiar/asn1-android',
      version: '^2.3.6',
    },
    // Mapping for '../../typescript-types/src/index.ts' in deps.ts
    '../typescript-types/src/index.ts': {
      name: '@simplewebauthn/typescript-types',
      version: `^${typesPackageJSON.version}`,
    },
  };
}

function getCompilerOptions(): BuildOptions['compilerOptions'] {
  return {
    lib: ['ES2021'],
  };
}