Skip to content

Commit

Permalink
feat: more options for Script Tag
Browse files Browse the repository at this point in the history
  • Loading branch information
theKashey committed Oct 28, 2019
1 parent 6e6deb5 commit 65ab5f6
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 5 deletions.
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ webpack-imported
======
📝stats-webpack-plugin and 💩webpack-flush-chunks had a baby!

> code splitting, prefetching, and resource management.
WebpackPlugin + ServerSide API + React Components(separate entrypoint)

# Server side API
## Webpack plugin
Expand Down Expand Up @@ -78,7 +81,9 @@ const tracker = createImportedTracker();// this is optional, only needed if your
`WebpackImport` has many props:
- [`preload`=false] - only preloads resources. If preload is set resources would be loaded via network, but not executed.
Never use this option for the main chunk.
- [`async`]=true] - loads scripts with `async` tag, use `deferred` in other case.
- [`anonymous`=false] - should it be loaded as anonymous
- [`async`=true] - loads scripts with `async` attribute, uses `deferred` in other case.
- [`module`=false] - loads scripts with `module` attribute
- [`critical-styles`=false] - enabled critical styles handling. No styles would be loaded or prefetched,
but system will leave extra markup to prevent `MiniCssExtractPlugin` from adding them by itself.
With this option enabled __you have to call__ `processImportedStyles` after the application starts to load the missing styles.
Expand Down
6 changes: 5 additions & 1 deletion src/react/Chunk.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ export interface WebpackImportProps {
chunks: string | string[];
prefetch?: boolean;
anonymous?: boolean;
async?: boolean;
module?: boolean;
criticalCSS?: boolean;
publicPath?: string;
}
Expand All @@ -21,6 +23,8 @@ export const WebpackImport: React.FC<WebpackImportProps> = (
prefetch,
criticalCSS,
anonymous,
async = true,
module,
publicPath = stats.config.publicPath
}) => {
const tracker = useContext(PrefetchChunkCollectorContext);
Expand All @@ -30,7 +34,7 @@ export const WebpackImport: React.FC<WebpackImportProps> = (
<>
{
scripts.load.map(asset => (
<LoadScript prefetch={prefetch} href={`${publicPath}${asset}`} anonymous={anonymous}/>
<LoadScript prefetch={prefetch} href={`${publicPath}${asset}`} async={async} module={module} anonymous={anonymous}/>
))
}

Expand Down
13 changes: 11 additions & 2 deletions src/react/Prefetch.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,21 @@ export interface KnownScript {
prefetch?: boolean;
anonymous?: boolean;
async?: boolean;
module?: boolean;
}

export const LoadScript: React.FC<KnownScript> = ({href, prefetch, anonymous, async}) => (
export const LoadScript: React.FC<KnownScript> = ({href, prefetch, anonymous, async, module}) => (
<>
<PrefetchScript href={href} anonymous={anonymous}/>
{!prefetch && <script async={!!async} defer={!async} crossOrigin={anonymous && "anonymous"} src={href}/>}
{!prefetch && (
<script
async={!!async}
defer={!async}
crossOrigin={anonymous && "anonymous"}
src={href}
type={module ? "module" : undefined}
/>
)}
</>
);

Expand Down
2 changes: 1 addition & 1 deletion src/react/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export {createImportedTracker} from "../tracker";
export {WebpackImportedProvider} from './context';
export {WebpackImport} from './Chunk';
export {PrefetchFont, PrefetchScript, PrefetchStyle} from './Prefetch';
export {PrefetchFont, PrefetchScript, PrefetchStyle, LoadStyle, LoadScript, LoadCriticalStyle} from './Prefetch';
export {processImportedStyles} from '../css/processImportedStyles'

0 comments on commit 65ab5f6

Please sign in to comment.