Skip to content

Commit aeac495

Browse files
Merge pull request #27 from NxtLvLSoftware/dev-to-dist
Merge dev changes to dist
2 parents 1d367b6 + 31c3420 commit aeac495

22 files changed

Lines changed: 487 additions & 115 deletions

README.md

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -263,10 +263,12 @@ $ touch src/Plugin.ts
263263
```
264264
Copy the following and change the namespace to the name of your package:
265265
```typescript
266+
import type Alpine from 'alpinejs';
267+
266268
import {
267269
AlpineComponents,
268270
makeAlpineConstructor,
269-
type Globals
271+
Globals
270272
} from '@nxtlvlsoftware/alpine-typescript';
271273

272274
import {
@@ -292,21 +294,27 @@ export namespace MyComponents {
292294

293295
export function bootstrap(
294296
options: Partial<Options> = defaultOptions,
295-
alpinejs: Globals.Alpine = window.Alpine
297+
alpinejs: typeof Alpine = window.Alpine
296298
): void {
297299
const opts: Options = {
298300
...defaultOptions,
299301
...options
300302
};
301303

302-
// make typescript happy
303-
let alpine = <Globals.AlpineWithComponents>alpinejs;
304-
305304
document.addEventListener('alpine:init', () => {
306305
// Register any alpine stores your components rely on here.
307306
});
308307

309308
document.addEventListener('alpine-components:init', () => {
309+
// make typescript happy
310+
let alpine = Globals.castToAlpineWithComponents(alpinejs);
311+
if (alpine === null) {
312+
if (opts.logErrors) {
313+
console.error('Alpine object does not have Components properties injected. Did the Components package boot properly?');
314+
}
315+
return;
316+
}
317+
310318
// Basic registration with support for consumers to provide their own
311319
// name for use with x-data.
312320
alpine.Components.register(MyComponent, opts.myComponentName);
@@ -315,7 +323,7 @@ export namespace MyComponents {
315323

316324
// Allow booting alpine and the components package.
317325
if (opts.bootstrapComponents) {
318-
AlpineComponents.bootstrap(opts, alpine);
326+
AlpineComponents.bootstrap(opts, alpinejs);
319327
}
320328
}
321329

examples/package/package.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,15 @@
3030
"src/**/*.js"
3131
],
3232
"scripts": {
33-
"build": "tsc --inlineSourceMap --removeComments -p ./",
34-
"build-ci": "tsc --pretty false --inlineSourceMap --removeComments --listEmittedFiles -p ./",
33+
"build": "tsc --inlineSourceMap -p ./",
34+
"build-ci": "tsc --pretty false --inlineSourceMap --listEmittedFiles -p ./",
3535
"dev": "tsc --watch -p ./"
3636
},
3737
"dependencies": {
38-
"@nxtlvlsoftware/alpine-typescript": "file:../../",
39-
"@types/alpinejs": "^3.7.2"
38+
"@nxtlvlsoftware/alpine-typescript": "file:../../"
4039
},
4140
"devDependencies": {
42-
"typescript": "^5.1.6"
41+
"@types/alpinejs": "^3.0.0",
42+
"typescript": "^5.2.2"
4343
}
4444
}

examples/package/src/Plugin.ts

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1+
import type Alpine from 'alpinejs';
2+
13
import {
24
AlpineComponents,
35
makeAlpineConstructor,
4-
type Globals
6+
Globals
57
} from '@nxtlvlsoftware/alpine-typescript';
68

79
import {
@@ -35,23 +37,29 @@ export namespace MyComponents {
3537

3638
export function bootstrap (
3739
options: Partial<Options> = defaultOptions,
38-
alpinejs: Globals.Alpine = window.Alpine
40+
alpinejs: typeof Alpine = window.Alpine
3941
): void {
4042
const opts: Options = {
4143
...defaultOptions,
4244
...options
4345
};
4446

45-
// make typescript happy
46-
let alpine = <Globals.AlpineWithComponents>alpinejs;
47-
4847
document.addEventListener('alpine:init', () => {
4948
// Register any alpine stores your components rely on here.
5049
// You can pull them and any other dependencies in as default params
5150
// for a custom component constructor when alpine-components:init is fired.
5251
});
5352

5453
document.addEventListener('alpine-components:init', () => {
54+
// make typescript happy
55+
let alpine = Globals.castToAlpineWithComponents(alpinejs);
56+
if (alpine === null) {
57+
if (opts.logErrors) {
58+
console.error('Alpine object does not have Components properties injected. Did the Components package boot properly?');
59+
}
60+
return;
61+
}
62+
5563
// Basic registration by falling back to the prototype name when left undefined.
5664
// Can be used in your HTML with: x-data="ToggleComponent([true|false])"
5765
alpine.Components.register(AlertComponent);
@@ -90,7 +98,7 @@ export namespace MyComponents {
9098
// Allow booting alpine and the components package with a single call to our
9199
// bootstrap function. This makes the client script a bit simpler.
92100
if (opts.bootstrapComponents) {
93-
AlpineComponents.bootstrap(opts, alpine);
101+
AlpineComponents.bootstrap(opts, alpinejs);
94102
}
95103
}
96104

examples/package/tsconfig.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
"strict": true,
1111
"declaration": true,
1212
"noEmit": false,
13+
"removeComments": false,
1314
"lib": [
1415
"es2017",
1516
"dom"

examples/project/package-lock.json

Lines changed: 36 additions & 38 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/project/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
"alpinejs": "^3.13.0"
2626
},
2727
"devDependencies": {
28-
"@types/alpinejs": "^3.7.2",
28+
"@types/alpinejs": "^3.13.0",
2929
"copy-webpack-plugin": "^11.0.0",
3030
"css-loader": "^6.8.1",
3131
"cssnano": "^6.0.1",
@@ -39,7 +39,7 @@
3939
"tailwindcss": "^3.3.3",
4040
"terser-webpack-plugin": "^5.3.9",
4141
"ts-loader": "^9.4.4",
42-
"typescript": "^5.1.6",
42+
"typescript": "^5.2.2",
4343
"warnings-to-errors-webpack-plugin": "^2.3.0",
4444
"webpack": "^5.88.2",
4545
"webpack-cli": "^5.1.4",

examples/project/webpack.config.js

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import CopyWebpackPlugin from 'copy-webpack-plugin';
88
import TerserPlugin from 'terser-webpack-plugin';
99
import WarningsToErrorsPlugin from 'warnings-to-errors-webpack-plugin';
1010

11+
import webpack from 'webpack';
12+
1113
const isProduction = process.env.NODE_ENV === 'production';
1214

1315
const __filename = fileURLToPath(import.meta.url);
@@ -19,19 +21,24 @@ const outPath = path.resolve(rootPath, 'dist');
1921

2022
const Mode = isProduction ? 'production' : 'development';
2123
const productionPlugins = [
22-
new WarningsToErrorsPlugin()
24+
new WarningsToErrorsPlugin(),
25+
new webpack.optimize.LimitChunkCountPlugin({
26+
maxChunks: 1
27+
})
2328
];
2429
const developmentPlugins = [
2530
//
2631
];
2732

2833
/** @type {Partial<import('webpack').Configuration>} */
2934
let Config = {
30-
entry: './src/index.ts',
35+
entry: {
36+
index: './src/index.ts'
37+
},
3138
mode: Mode,
3239
output: {
3340
path: outPath,
34-
filename: 'index.js',
41+
filename: '[name].js',
3542
clean: true,
3643
strictModuleErrorHandling: true,
3744
strictModuleExceptionHandling: true

index.d.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
1+
/**
2+
* Export functions and types.
3+
*/
14
export { type KnownClassConstructor, type KnownConstructor, type AlpineComponentConstructor, type AlpineDataContext, type AlpineData, AlpineComponent } from './src/Component';
25
export { type ComponentList, ComponentStore, transformToAlpineData, makeAlpineConstructor } from './src/Store';
36
export * as Globals from './src/Global';
47
export { AlpineComponents, componentsPlugin } from './src/Plugin';
8+
/**
9+
* Alpine plugin as default export.
10+
*/
511
import { componentsPlugin } from './src/Plugin';
612
export default componentsPlugin;

index.js

Lines changed: 7 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)