Skip to content

Commit

Permalink
Add tests for async style-containing vue components when extracting s…
Browse files Browse the repository at this point in the history
…tyles (#2712)

* Fix docblock

* Extract vendor deps in integration test

This tests more edge cases

* Extract vue styles

This ensures extracted styles work as expected

* Add async component with styles

This used to fail but now works with webpack 5.25.1

* Fix extraction of JSX styles
  • Loading branch information
thecrypticace committed Mar 14, 2021
1 parent 3b06f0d commit 9780e7e
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/components/Preprocessor.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ class Preprocessor {
/**
* Prepare the preprocessor plugin options.
*
* @param {Object} preprocessor
* @param {Detail} preprocessor
* @param {Boolean} processUrls
*/
loaderOptions(preprocessor, processUrls) {
Expand Down
11 changes: 11 additions & 0 deletions src/components/Vue.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,17 @@ class Vue {
type: 'css/mini-extract'
}
);

this.chunks.add(
'styles-jsx',
this.styleChunkName(),
[/.jsx$/, module => module.type === 'css/mini-extract'],
{
chunks: 'all',
enforce: true,
type: 'css/mini-extract'
}
);
}

/**
Expand Down
4 changes: 4 additions & 0 deletions test/fixtures/integration/dist/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,20 @@
<html lang="en">
<head>
<link rel="stylesheet" href="/css/app.css" />
<link rel="stylesheet" href="/css/vue-styles.css" />
</head>
<body>
<p>Laravel Mix Fixture Integration Test Page</p>
<div id="app">
<div>
This is a test
<scss-module></scss-module>
<async-component></async-component>
</div>
</div>
<div id="react-app"></div>
<script src="/js/manifest.js"></script>
<script src="/js/vendor.js"></script>
<script src="/js/app.js"></script>
</body>
</html>
32 changes: 32 additions & 0 deletions test/fixtures/integration/src/js/AsyncComponent.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<style>
.async-style {
color: #ff7700;
}
</style>
<style scoped>
.async-style-scoped {
border: 1px solid #ff7700;
}
</style>

<template>
<div id="async-component" class="async-style async-style-scoped">
This will have an orange border and orange text.
</div>
</template>

<script>
import { onMounted } from 'vue';
export default {
setup() {
onMounted(() => {
const style = window.getComputedStyle(
document.querySelector('#async-component')
);
console.log(`async component style: ${style.color} ${style.borderColor}`);
});
}
};
</script>
5 changes: 3 additions & 2 deletions test/fixtures/integration/src/js/app-vue.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { createApp } from 'vue';
import { createApp, defineAsyncComponent } from 'vue';
import ScssModule from './ScssModule.vue';

async function run() {
Expand All @@ -12,7 +12,8 @@ async function run() {
export function setupVueApp() {
const app = createApp({
components: {
ScssModule
ScssModule,
AsyncComponent: defineAsyncComponent(() => import('./AsyncComponent.vue'))
},

setup() {
Expand Down
19 changes: 14 additions & 5 deletions test/integration/mix.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,10 @@ test('compiling just js', async t => {
// Build a simple mix setup
setupVueAliases(3);

mix.js('test/fixtures/integration/src/js/app.js', 'js/app.js').vue();
mix.js('test/fixtures/integration/src/js/app.js', 'js/app.js');
mix.vue({ extractStyles: 'css/vue-styles.css' });
mix.react();
mix.extract();

await webpack.compile();
await assertProducesLogs(t, ['loaded: app.js']);
Expand All @@ -44,10 +46,12 @@ test('compiling js and css together', async t => {
setupVueAliases(3);

// Build a simple mix setup
mix.js('test/fixtures/integration/src/js/app.js', 'js/app.js').vue();
mix.js('test/fixtures/integration/src/js/app.js', 'js/app.js');
mix.react();
mix.sass('test/fixtures/integration/src/css/app.scss', 'css/app.css');
mix.postCss('test/fixtures/integration/src/css/app.css', 'css/app.css');
mix.vue({ extractStyles: 'css/vue-styles.css' });
mix.extract();

await webpack.compile();
await assertProducesLogs(t, [
Expand All @@ -56,15 +60,18 @@ test('compiling js and css together', async t => {
'loaded: dynamic.js',
'run: dynamic.js',
'style: rgb(255, 119, 0)',
'style: rgb(119, 204, 51)'
'style: rgb(119, 204, 51)',
'async component style: rgb(255, 119, 0) rgb(255, 119, 0)'
]);
});

test('node browser polyfills: enabled', async t => {
setupVueAliases(3);

mix.js('test/fixtures/integration/src/js/app.js', 'js/app.js').vue();
mix.js('test/fixtures/integration/src/js/app.js', 'js/app.js');
mix.vue({ extractStyles: 'css/vue-styles.css' });
mix.react();
mix.extract();

await webpack.compile();
await assertProducesLogs(t, [
Expand All @@ -79,8 +86,10 @@ test('node browser polyfills: enabled', async t => {
test('node browser polyfills: disabled', async t => {
setupVueAliases(3);

mix.js('test/fixtures/integration/src/js/app.js', 'js/app.js').vue();
mix.js('test/fixtures/integration/src/js/app.js', 'js/app.js');
mix.vue({ extractStyles: 'css/vue-styles.css' });
mix.react();
mix.extract();
mix.options({ legacyNodePolyfills: false });

await webpack.compile();
Expand Down

0 comments on commit 9780e7e

Please sign in to comment.