Skip to content

Commit

Permalink
[build] [sourcemap] Add external sourcemap for easier production debu…
Browse files Browse the repository at this point in the history
…gging.
  • Loading branch information
swashata committed Sep 17, 2024
1 parent 9dd2062 commit 5041b9f
Show file tree
Hide file tree
Showing 12 changed files with 94 additions and 76 deletions.
15 changes: 8 additions & 7 deletions .github/workflows/release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,17 @@ jobs:
- uses: ./.github/actions/setup-node
- name: 🏗 Build
run: npm run build:lib
- name: ✏️ Rename files
run: mv lib/adapter/checkout.js lib/adapter/adapter.js && mv lib/global/checkout.js lib/global/global.js
shell: bash
- name: 📦 Package adapter.js
run: gh release upload ${{ github.event.release.tag_name }} lib/adapter/adapter.js --clobber
- name: 📦 Package checkout.adapter.js
run: |
gh release upload ${{ github.event.release.tag_name }} lib/adapter/checkout.adapter.js --clobber
gh release upload ${{ github.event.release.tag_name }} lib/adapter/checkout.adapter.js.map --clobber
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
shell: bash
- name: 📦 Package global.js
run: gh release upload ${{ github.event.release.tag_name }} lib/global/global.js --clobber
- name: 📦 Package checkout.global.js
run: |
gh release upload ${{ github.event.release.tag_name }} lib/global/checkout.global.js --clobber
gh release upload ${{ github.event.release.tag_name }} lib/global/checkout.global.js.map --clobber
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
shell: bash
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -191,3 +191,6 @@ $RECYCLE.BIN/

# Windows shortcuts
*.lnk

# Cypres screenshots and videos
cypress/screenshots
2 changes: 1 addition & 1 deletion cypress/pages/adapter/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
/>
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<title>E2E Test for the Checkout Adapter Library</title>
<script src="/lib/adapter/checkout.js" async defer></script>
<script src="/lib/adapter/checkout.adapter.js" async defer></script>
<script src="main.ts" type="module"></script>
</head>
<body>
Expand Down
2 changes: 1 addition & 1 deletion cypress/pages/global/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
/>
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<title>E2E Test for the Checkout Adapter Library</title>
<script src="/lib/global/checkout.js" async defer></script>
<script src="/lib/global/checkout.global.js" async defer></script>
<script src="main.ts" type="module"></script>
</head>
<body>
Expand Down
31 changes: 0 additions & 31 deletions src/lib/utils/vite-plugins.ts

This file was deleted.

69 changes: 69 additions & 0 deletions src/lib/utils/vite.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import { PluginOption, UserConfig } from 'vite';
import { dirname, resolve } from 'path';

export const minifyInlineCSS: PluginOption = {
name: 'fs-minify-inline-css',
transform(code, id) {
// We are only minifying TypeScript and JavaScript files.
if (!/\.(ts|js)$/.test(id)) {
return null;
}

// Regex to match CSS inside template literals starting with `/*@fs-css-minify*/`.
const cssRegex = /\/\*@fs-css-minify\*\/\s*`([^`]+)`/g;

// Check if the code contains any CSS to minify.
if (cssRegex.test(code)) {
// Replace all the CSS inside template literals with minified CSS.
code = code.replace(cssRegex, (_, css) => {
// Just remove the new lines and spaces.
css = css.replace(/\s+/g, ' ').trim();

// Return the minified CSS inside a template literal.
return `\`${css}\``;
});
}

return {
code,
map: null,
};
},
};

export function createConfig(
entry: string,
outDir: string,
sourceRootDir: string
): UserConfig {
const iifeName = outDir.charAt(0).toUpperCase() + outDir.slice(1);

return {
build: {
outDir: `lib/${outDir}`,
target: 'es2015',
lib: {
entry,
formats: ['iife'],
// @note - The build will not expose this variable because the file itself doesn't export anything, but we just need to give it a name anyway for the tooling.
name: `__FSCheckout${iifeName}Internal__`,
fileName: () => `checkout.${outDir}.js`,
},
sourcemap: true,
rollupOptions: {
output: {
sourcemapPathTransform(relativeSourcePath, sourcemapPath) {
// Make it start from the root.
return resolve(
dirname(sourcemapPath),
relativeSourcePath
)
.replace(sourceRootDir, '')
.replace('/src/', '/fs-checkout/');
},
},
},
},
plugins: [minifyInlineCSS],
};
}
2 changes: 1 addition & 1 deletion tests/lib.adapter.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// eslint-disable-next-line @typescript-eslint/no-require-imports
require('../lib/adapter/checkout.js');
require('../lib/adapter/checkout.adapter.js');

describe('lib.adapter', () => {
test('exposes Checkout', () => {
Expand Down
2 changes: 1 addition & 1 deletion tests/lib.global.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// eslint-disable-next-line @typescript-eslint/no-require-imports
require('../lib/global/checkout.js');
require('../lib/global/checkout.global.js');

describe('lib.global', () => {
test('exposes Checkout', () => {
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"useDefineForClassFields": true,
"module": "ESNext",
"lib": ["ESNext", "DOM"],
"moduleResolution": "Node",
"moduleResolution": "Bundler",
"strict": true,
"sourceMap": true,
"resolveJsonModule": true,
Expand Down
20 changes: 4 additions & 16 deletions vite.adapter.config.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,7 @@
import { defineConfig } from 'vite';
import { resolve } from 'path';
import { minifyInlineCSS } from './src/lib/utils/vite-plugins';
import { createConfig } from './src/lib/utils/vite';

export default defineConfig({
build: {
outDir: 'lib/adapter',
target: 'es2015',
lib: {
entry: resolve(__dirname, './src/adapter.ts'),
formats: ['iife'],
// @note - The build will not expose this variable because the file itself doesn't export anything, but we just need to give it a name anyway for the tooling.
name: '__FSCheckoutAdapterInternal__',
fileName: () => `checkout.js`,
},
sourcemap: false,
},
plugins: [minifyInlineCSS],
});
export default defineConfig(
createConfig(resolve(__dirname, './src/adapter.ts'), 'adapter', __dirname)
);
20 changes: 4 additions & 16 deletions vite.global.config.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,7 @@
import { defineConfig } from 'vite';
import { resolve } from 'path';
import { minifyInlineCSS } from './src/lib/utils/vite-plugins';
import { createConfig } from './src/lib/utils/vite';

export default defineConfig({
build: {
outDir: 'lib/global',
target: 'es2015',
lib: {
entry: resolve(__dirname, './src/global.ts'),
formats: ['iife'],
// @note - The build will not expose this variable because the file itself doesn't export anything, but we just need to give it a name anyway for the tooling.
name: '__FSCheckoutGlobalInternal__',
fileName: () => `checkout.js`,
},
sourcemap: false,
},
plugins: [minifyInlineCSS],
});
export default defineConfig(
createConfig(resolve(__dirname, './src/global.ts'), 'global', __dirname)
);
2 changes: 1 addition & 1 deletion vite.module.config.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { defineConfig } from 'vite';
import { resolve } from 'path';
import dts from 'vite-plugin-dts';
import { minifyInlineCSS } from './src/lib/utils/vite-plugins';
import { minifyInlineCSS } from './src/lib/utils/vite';

export default defineConfig({
build: {
Expand Down

0 comments on commit 5041b9f

Please sign in to comment.